Jamesmontalvo3 has submitted this change and it was merged.

Change subject: Added capability to assign levels for counters
......................................................................


Added capability to assign levels for counters

Added unit tests for new levels.

The following wikitext:
{{#counter:list-with-levels|level prefix=:}} Start lev 1
{{#counter:}} Stay lev 1
{{#counter:|level=2}} Jump to lev 2
{{#counter:}} Stay lev 2
{{#counter:|level=1}} Drop to lev 1
{{#counter:|level=2}} back to lev 2
{{#counter:|level=3}} jump to lev 3

Results in:
:1
:2
::1
::2
:3
::1
:::1

This allows you to have hierarchical lists. Furthermore,
if you add "format=outline" to the first #counter you can
get output like:

:1
:2
::2.1
::2.2
:3
::3.1
:::3.1.1

Change-Id: If8f16fa961b0ec730d7dc38c6b885f7ec290bd47
---
M NumerAlpha.class.php
M NumerAlpha.php
M i18n/en.json
M tests/phpunit/NumerAlphaTest.php
4 files changed, 276 insertions(+), 80 deletions(-)

Approvals:
  Jamesmontalvo3: Verified; Looks good to me, approved



diff --git a/NumerAlpha.class.php b/NumerAlpha.class.php
old mode 100644
new mode 100755
index e994297..d38a8c9
--- a/NumerAlpha.class.php
+++ b/NumerAlpha.class.php
@@ -68,7 +68,9 @@
                }
 
                if ( ! isset( self::$lists[ $name ] ) ) {
-                       self::$lists[ $name ] = array();
+                       self::$lists[ $name ] = array(
+                               'levels' => array(),
+                       );
                }
 
                foreach( $rawArgs as $rawArg ) {
@@ -87,6 +89,9 @@
                                                break;
                                        case wfMessage( 
'ext-numeralpha-list-set-label' )->text():
                                                $newCountValue = intVal( $value 
);
+                                               if ( $newCountValue < 1 ) {
+                                                       $newCountValue = 1;
+                                               }
                                                break;
                                        case wfMessage( 
'ext-numeralpha-list-pad-length' )->text():
                                                self::$lists[ $name ][ 
'padlength' ] = intVal( $value );
@@ -100,22 +105,77 @@
                                        case wfMessage( 
'ext-numeralpha-list-suffix' )->text():
                                                self::$lists[ $name ][ 'suffix' 
] = $value;
                                                break;
+                                       case wfMessage( 
'ext-numeralpha-list-level-label' )->text():
+                                               self::$lists[ $name ][ 'level' 
] = intVal($value);
+                                               break;
+                                       case wfMessage( 
'ext-numeralpha-list-level-prefix-label' )->text():
+                                               self::$lists[ $name ][ 'level 
prefix' ] = $value;
+                                               break;
+                                       case wfMessage( 
'ext-numeralpha-list-format-label' )->text():
+                                               if ( $value === wfMessage( 
'ext-numeralpha-list-format-outline' )->text() ) {
+                                                       self::$lists[ $name ][ 
'format' ] = 'outline';
+                                               }
+                                               else {
+                                                       self::$lists[ $name ][ 
'format' ] = 'standard';
+                                               }
+                                               break;
                                }
 
                        }
 
                }
 
-               // either set count value or increment existing value
-               if ( isset( $newCountValue ) ) {
-                       self::$lists[ $name ][ 'count' ] = $newCountValue;
+               // if level not specifed, use the previous level if there is one
+               if ( isset( self::$lists[ $name ][ 'level' ] ) ) {
+                       $farts = true;
                }
-               else if ( isset( self::$lists[ $name ][ 'count' ] ) ) {
-                       self::$lists[ $name ][ 'count' ]++;
+               elseif ( isset( self::$lists[ $name ][ 'prev-level' ] ) ) {
+                       self::$lists[ $name ][ 'level' ] = self::$lists[ $name 
][ 'prev-level' ];
                }
+               // otherwise set level to 1
                else {
-                       self::$lists[ $name ][ 'count' ] = 1;
+                       self::$lists[ $name ][ 'level' ] = 1;
                }
+               self::$lists[ $name ][ 'prev-level' ] = self::$lists[ $name ][ 
'level' ];
+
+               // if user inputs level 1, equates to array index zero
+               $levelIndex = self::$lists[ $name ][ 'level' ] - 1;
+
+               // if level has been used before (and hasn't been reset due to 
lower
+               // level being used) then increase the count on this level
+               if ( isset( self::$lists[ $name ][ 'levels' ][ $levelIndex ] ) 
) {
+                       self::$lists[ $name ][ 'levels' ][ $levelIndex ]++;
+               }
+
+               // @todo FIXME this shoudln't be necessary
+               else {
+                       self::$lists[ $name ][ 'levels' ][ $levelIndex ] = 1;
+               }
+
+               // Make sure all levels are set. Fill in blanks with 1.
+               // Is this excessive?
+               for ( $i = 0; $i <= $levelIndex; $i++ ) {
+                       if ( ! isset( self::$lists[ $name ][ 'levels' ][ $i ] ) 
) {
+                               self::$lists[ $name ][ 'levels' ][ $i ] = 1;
+                       }
+               }
+
+               // if indices exist beyond the desired level, cut the array of 
levels
+               // down just to the zeroth index through the desired index
+               if ( isset( self::$lists[ $name ][ 'levels' ][ $levelIndex + 1 
] ) ) {
+                       self::$lists[ $name ][ 'levels' ] = array_slice(
+                               self::$lists[ $name ][ 'levels' ],
+                               0,
+                               self::$lists[ $name ][ 'level' ] // AKA 
$levelIndex + 1
+                       );
+               }
+
+               // despite all the level-working above, if user has specified a 
new
+               // count value, override:
+               if ( isset( $newCountValue ) ) {
+                       self::$lists[ $name ][ 'levels' ][ $levelIndex ] = 
$newCountValue;
+               }
+
 
                // set default list type if not already set
                if ( ! isset( self::$lists[ $name ][ 'type' ] ) ) {
@@ -142,6 +202,29 @@
                }
                if ( ! isset( self::$lists[ $name ][ 'suffix' ] ) ) {
                        self::$lists[ $name ][ 'suffix' ] = '';
+               }
+
+               // level prefix = string applied before counter, x times per 
level
+               if ( ! isset( self::$lists[ $name ][ 'level prefix' ] ) ) {
+                       self::$lists[ $name ][ 'level prefix' ] = '';
+               }
+               self::$lists[ $name ][ 'full level prefix' ] = str_repeat(
+                       self::$lists[ $name ][ 'level prefix' ],
+                       self::$lists[ $name ][ 'level' ]
+               );
+
+
+               // This if-statement determines if we're using standard or 
outline format
+               // self::$lists[ $name ][ 'count' ] is the display for the 
counter.
+               // The name sucks, but is here for now due to historical naming.
+               if ( ! isset( self::$lists[ $name ][ 'format' ] ) || 
self::$lists[ $name ][ 'format' ] == 'standard' ) {
+                       self::$lists[ $name ][ 'count' ] = self::$lists[ $name 
][ 'levels' ][ $levelIndex ];
+               }
+               else {
+                       self::$lists[ $name ][ 'count' ] = implode(
+                               wfMessage( 
'ext-numeralpha-list-format-outline-glue' ),
+                               self::$lists[ $name ][ 'levels' ]
+                       );
                }
 
                return self::$lists[ $name ];
@@ -180,7 +263,7 @@
                        STR_PAD_LEFT // this may require internationalization...
                );
 
-               return htmlspecialchars( $list[ 'prefix' ] . $counterValue . 
$list[ 'suffix' ] );
+               return htmlspecialchars( $list['full level prefix'] . $list[ 
'prefix' ] . $counterValue . $list[ 'suffix' ] );
        }
 
 
@@ -195,7 +278,7 @@
                        $num = $num / 26;
                }
 
-               return htmlspecialchars( $list[ 'prefix' ] . $alpha . $list[ 
'suffix' ] );
+               return htmlspecialchars( $list['full level prefix'] . $list[ 
'prefix' ] . $alpha . $list[ 'suffix' ] );
 
        }
 
@@ -221,11 +304,11 @@
                );
                foreach ($equival as $roma => $val) {
                        $concordances = intval($num / $val);
-                        $result .= str_repeat($roma, $concordances);
-                                       $num = $num % $val;
+                       $result .= str_repeat($roma, $concordances);
+                       $num = $num % $val;
                }
 
-               return htmlspecialchars( $list[ 'prefix' ] . $result . $list[ 
'suffix' ] );
+               return htmlspecialchars( $list['full level prefix'] . $list[ 
'prefix' ] . $result . $list[ 'suffix' ] );
 
        }
 
diff --git a/NumerAlpha.php b/NumerAlpha.php
old mode 100644
new mode 100755
index 4bc92fd..b80ee42
--- a/NumerAlpha.php
+++ b/NumerAlpha.php
@@ -18,8 +18,8 @@
 // Tell everybody who we are
 $GLOBALS['wgExtensionCredits']['parserhook'][] = array(
        'name' => 'NumerAlpha',
-       'version' => '0.6.0',
-       'author' => array( 'Thierry G. Veilleux', 'James Montalvo' ),
+       'version' => '0.7.0',
+       'author' => array( 'Thierry G. Veilleux', 
'[https://www.mediawiki.org/wiki/User:Jamesmontalvo3 James Montalvo]', 
'Emanspeaks' ),
        'descriptionmsg' => 'numeralpha-desc',
        'url' => 'https://www.mediawiki.org/wiki/Extension:NumerAlpha'
 );
diff --git a/i18n/en.json b/i18n/en.json
old mode 100644
new mode 100755
index 960091b..32240c3
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -13,5 +13,11 @@
        "ext-numeralpha-list-pad-length": "pad length",
        "ext-numeralpha-list-pad-char": "pad character",
        "ext-numeralpha-list-prefix": "prefix",
-       "ext-numeralpha-list-suffix": "suffix"
+       "ext-numeralpha-list-suffix": "suffix",
+       "ext-numeralpha-list-level-label": "level",
+       "ext-numeralpha-list-level-prefix-label": "level prefix",
+       "ext-numeralpha-list-format-label": "format",
+       "ext-numeralpha-list-format-standard": "standard",
+       "ext-numeralpha-list-format-outline": "outline",
+       "ext-numeralpha-list-format-outline-glue": "."
 }
diff --git a/tests/phpunit/NumerAlphaTest.php b/tests/phpunit/NumerAlphaTest.php
old mode 100644
new mode 100755
index 6f7eb53..a36b555
--- a/tests/phpunit/NumerAlphaTest.php
+++ b/tests/phpunit/NumerAlphaTest.php
@@ -5,79 +5,186 @@
  */
 class NumerAlphaTest extends MediaWikiTestCase {
 
-    protected function setUp() {
-        parent::setUp();
-    }
+       protected function setUp() {
+               parent::setUp();
+       }
 
-    protected function tearDown() {
-        parent::tearDown();
-    }
+       protected function tearDown() {
+               parent::tearDown();
+       }
 
-    public function testNumeralValues () {
+       public function testNumeralValues () {
 
-        $parser = new Parser();
-        $frame = new PPFrame_DOM( new Preprocessor_DOM( $parser ) );
+               $parser = new Parser();
+               $frame = new PPFrame_DOM( new Preprocessor_DOM( $parser ) );
 
-        $this->assertEquals(
-            '1',
-            NumerAlpha::renderCounter( $parser, $frame, array( ' First list ' 
) ),
-            'First item of "First list" should equal 1'
-        );
-        $this->assertEquals(
-            '2',
-            NumerAlpha::renderCounter( $parser, $frame, array() ),
-            'Implied "First list" second item should equal 2'
-        );
-        $this->assertEquals(
-            '1',
-            NumerAlpha::renderCounter( $parser, $frame, array( ' Second list ' 
) ),
-            'First it of "Second list" should equal 1'
-        );
-        $this->assertEquals(
-            '2',
-            NumerAlpha::renderCounter( $parser, $frame, array() ),
-            'Implied "Second list" second item should equal 2'
-        );
-        $this->assertEquals(
-            '3',
-            NumerAlpha::renderCounter( $parser, $frame, array( '    First list 
' ) ),
-            '"First list" third item should equal 3'
-        );
+               $this->assertEquals(
+                       '1',
+                       NumerAlpha::renderCounter( $parser, $frame, array( ' 
First list ' ) ),
+                       'First item of "First list" should equal 1'
+               );
+               $this->assertEquals(
+                       '2',
+                       NumerAlpha::renderCounter( $parser, $frame, array() ),
+                       'Implied "First list" second item should equal 2'
+               );
+               $this->assertEquals(
+                       '1',
+                       NumerAlpha::renderCounter( $parser, $frame, array( ' 
Second list ' ) ),
+                       'First it of "Second list" should equal 1'
+               );
+               $this->assertEquals(
+                       '2',
+                       NumerAlpha::renderCounter( $parser, $frame, array() ),
+                       'Implied "Second list" second item should equal 2'
+               );
+               $this->assertEquals(
+                       '3',
+                       NumerAlpha::renderCounter( $parser, $frame, array( '    
First list ' ) ),
+                       '"First list" third item should equal 3'
+               );
 
 
-        $thirdListArgs = array(
-            'Third list',
-            ' pad length = 2 ',
-            ' pad character = x ',
-        );
-        $this->assertEquals(
-            'x1',
-            NumerAlpha::renderCounter( $parser, $frame, $thirdListArgs ),
-            '"Third list" first item should equal x1'
-        );
-        $this->assertEquals(
-            'x2',
-            NumerAlpha::renderCounter( $parser, $frame, array() ),
-            '"Third list" second item should equal x2'
-        );
-        $this->assertEquals(
-            'xxxx3',
-            NumerAlpha::renderCounter( $parser, $frame, array( '   ', ' pad 
length = 5 ' ) ),
-            '"Third list" third item should equal xxxx3'
-        );
-        $this->assertEquals(
-            '00004',
-            NumerAlpha::renderCounter( $parser, $frame, array( '   ', ' pad 
character = 0 ' ) ),
-            '"Third list" fourth item should equal 00004'
-        );
+               $thirdListArgs = array(
+                       'Third list',
+                       ' pad length = 2 ',
+                       ' pad character = x ',
+               );
+               $this->assertEquals(
+                       'x1',
+                       NumerAlpha::renderCounter( $parser, $frame, 
$thirdListArgs ),
+                       '"Third list" first item should equal x1'
+               );
+               $this->assertEquals(
+                       'x2',
+                       NumerAlpha::renderCounter( $parser, $frame, array() ),
+                       '"Third list" second item should equal x2'
+               );
+               $this->assertEquals(
+                       'xxxx3',
+                       NumerAlpha::renderCounter( $parser, $frame, array( '   
', ' pad length = 5 ' ) ),
+                       '"Third list" third item should equal xxxx3'
+               );
+               $this->assertEquals(
+                       '00004',
+                       NumerAlpha::renderCounter( $parser, $frame, array( '   
', ' pad character = 0 ' ) ),
+                       '"Third list" fourth item should equal 00004'
+               );
 
-        $this->assertEquals(
-            '(4)',
-            NumerAlpha::renderCounter( $parser, $frame, array( '  First list 
', ' prefix = ( ', 'suffix = )' ) ),
-            '"Third list" third item should equal (4)'
-        );
+               $this->assertEquals(
+                       '(4)',
+                       NumerAlpha::renderCounter( $parser, $frame, array( '  
First list ', ' prefix = ( ', 'suffix = )' ) ),
+                       '"Third list" third item should equal (4)'
+               );
 
+               /** list with standard format showing levels
+                       {{#counter:list-with-levels|level prefix=:}} Start lev 1
+                       {{#counter:}} Stay lev 1
+                       {{#counter:|level=2}} Jump to lev 2
+                       {{#counter:}} Stay lev 2
+                       {{#counter:|level=1}} Drop to lev 1
+                       {{#counter:|level=2}} back to lev 2
+                       {{#counter:|level=3}} jump to lev 3
 
-    }
+                       Results in:
+                       :1
+                       :2
+                       ::1
+                       ::2
+                       :3
+                       ::1
+                       :::1
+
+               */
+               $this->assertEquals(
+                       ':1',
+                       NumerAlpha::renderCounter( $parser, $frame, array( 
'list-with-levels', ' level prefix = : ' ) ),
+                       '"list-with-levels" first item should equal 1 at level 
1'
+               );
+               $this->assertEquals(
+                       ':2',
+                       NumerAlpha::renderCounter( $parser, $frame, array() ),
+                       '"list-with-levels" second item should equal 2, 
remembering last counter and maintains level'
+               );
+               $this->assertEquals(
+                       '::1',
+                       NumerAlpha::renderCounter( $parser, $frame, 
array('','level=2') ),
+                       '"list-with-levels" first item at level 2 should equal 
1'
+               );
+               $this->assertEquals(
+                       '::2',
+                       NumerAlpha::renderCounter( $parser, $frame, array() ),
+                       '"list-with-levels" second item at level 2 should equal 
2, remembering last counter and maintains level'
+               );
+               $this->assertEquals(
+                       ':3',
+                       NumerAlpha::renderCounter( $parser, $frame, array('', ' 
level =1') ),
+                       '"list-with-levels" third item at level 1 should equal 
3, remembers level'
+               );
+               $this->assertEquals(
+                       '::1',
+                       NumerAlpha::renderCounter( $parser, $frame, array('', ' 
level  = 2 ') ),
+                       '"list-with-levels" should equal 1, previous higher 
level increment resets lower level counters'
+               );
+               $this->assertEquals(
+                       ':::1',
+                       NumerAlpha::renderCounter( $parser, $frame, array(' 
','level=3') ),
+                       '"list-with-levels" should equal 1, increasing level 
resets counter'
+               );
+
+               /** outline-format list with levels and non-standard level 
prefix
+                       {{#counter:outline-list-with-levels|level 
prefix=_|format=outline}} Start lev 1
+                       {{#counter:}} Stay lev 1
+                       {{#counter:|level=2}} Jump to lev 2
+                       {{#counter:}} Stay lev 2
+                       {{#counter:|level=1}} Drop to lev 1
+                       {{#counter:|level=2}} back to lev 2
+                       {{#counter:|level=3}} jump to lev 3
+
+                       Results in:
+                       _1
+                       _2
+                       __2.1
+                       __2.2
+                       _3
+                       __3.1
+                       ___3.1.1
+               */
+               $this->assertEquals(
+                       '_1',
+                       NumerAlpha::renderCounter( $parser, $frame, array( 
'outline-list-with-levels', ' level prefix = _ ' ) ),
+                       '"outline-list-with-levels" first item should equal 1 
at level 1'
+               );
+               $this->assertEquals(
+                       '_2',
+                       NumerAlpha::renderCounter( $parser, $frame, array() ),
+                       '"outline-list-with-levels" second item should equal 2, 
remembering last counter and maintains level'
+               );
+               $this->assertEquals(
+                       '__1',
+                       NumerAlpha::renderCounter( $parser, $frame, 
array('','level=2') ),
+                       '"outline-list-with-levels" first item at level 2 
should equal 1'
+               );
+               $this->assertEquals(
+                       '__2',
+                       NumerAlpha::renderCounter( $parser, $frame, array() ),
+                       '"outline-list-with-levels" second item at level 2 
should equal 2, remembering last counter and maintains level'
+               );
+               $this->assertEquals(
+                       '_3',
+                       NumerAlpha::renderCounter( $parser, $frame, array('', ' 
level =1') ),
+                       '"outline-list-with-levels" third item at level 1 
should equal 3, remembers level'
+               );
+               $this->assertEquals(
+                       '__1',
+                       NumerAlpha::renderCounter( $parser, $frame, array('', 
'level=2') ),
+                       '"outline-list-with-levels" should equal 1, previous 
higher level increment resets lower level counters'
+               );
+               $this->assertEquals(
+                       '___1',
+                       NumerAlpha::renderCounter( $parser, $frame, array(' 
','level=3') ),
+                       '"outline-list-with-levels" should equal 1, increasing 
level resets counter'
+               );
+       }
 
 }
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/228419
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: If8f16fa961b0ec730d7dc38c6b885f7ec290bd47
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/NumerAlpha
Gerrit-Branch: master
Gerrit-Owner: Emanspeaks <emanspe...@gmail.com>
Gerrit-Reviewer: Emanspeaks <emanspe...@gmail.com>
Gerrit-Reviewer: Jaideraf <jaide...@gmail.com>
Gerrit-Reviewer: Jamesmontalvo3 <jamesmontal...@gmail.com>
Gerrit-Reviewer: Siebrand <siebr...@kitano.nl>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to