r103567 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103566‎ | r103567 | r103568 >
Date:03:32, 18 November 2011
Author:danwe
Status:deferred
Tags:
Comment:
negative indexes for '#arraysearch' and '#arrayindex' implemented.
Modified paths:
  • /trunk/extensions/ArrayExtension/ArrayExtension.php (modified) (history)
  • /trunk/extensions/ArrayExtension/RELEASE-NOTES (modified) (history)

Diff [purge]

Index: trunk/extensions/ArrayExtension/ArrayExtension.php
@@ -46,7 +46,7 @@
4747 'path' => __FILE__,
4848 'name' => 'ArrayExtension',
4949 'url' => 'http://www.mediawiki.org/wiki/Extension:ArrayExtension',
50 - 'author' => array ( 'Li Ding', 'Jie Bao', 'Daniel Werner' ),
 50+ 'author' => array ( 'Li Ding', 'Jie Bao', '[http://www.mediawiki.org/wiki/User:Danwe Daniel Werner]' ),
5151 'descriptionmsg' => 'arrayext-desc',
5252 'version' => ArrayExtension::VERSION
5353 );
@@ -265,13 +265,12 @@
266266 if ( $ret !== true ) {
267267 return $this->get_array_value( $ary_option, "default" );
268268 }
269 -
270 - $ret = $this->validate_array_index( $arrayid, $index );
271 - if ( $ret !== true ) {
 269+
 270+ if ( ! $this->validate_array_index( $arrayid, $index, false ) ) {
272271 return $this->get_array_value( $ary_option, "default" );
273272 }
274273
275 - return $this->mArrayExtension[$arrayid][$index];
 274+ return $this->mArrayExtension[ $arrayid ][ $index ];
276275 }
277276
278277 /**
@@ -310,11 +309,13 @@
311310 function arraysearch( Parser &$parser, PPFrame $frame, $args ) {
312311
313312 $arrayId = trim( $frame->expand( $args[0] ) );
314 - $startIndex = isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : 0;
 313+ $index = isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : 0;
315314
316315 if( $this->validate_array_by_arrayid( $arrayId )
317 - && $this->validate_array_index( $arrayId, $startIndex )
 316+ && $this->validate_array_index( $arrayId, $index, true )
318317 ) {
 318+ $array = $this->mArrayExtension[ $arrayId ];
 319+
319320 // validate/build search regex:
320321 if( isset( $args[1] ) ) {
321322
@@ -329,9 +330,9 @@
330331 }
331332
332333 // search for a match inside the array:
333 - $total = count( $this->mArrayExtension[ $arrayId ] );
334 - for ( $i = $startIndex; $i < $total; $i++ ) {
335 - $value = $this->mArrayExtension[ $arrayId ][ $i ];
 334+ $total = count( $array );
 335+ for ( $i = $index; $i < $total; $i++ ) {
 336+ $value = $array[ $i ];
336337
337338 if ( preg_match( $needle, $value ) ) {
338339 // found!
@@ -677,7 +678,7 @@
678679 }
679680
680681 // private functions for validating the index of an array
681 - function validate_array_index( $arrayId, $index ) {
 682+ function validate_array_index( $arrayId, &$index, $negativeBelowZeroReset = false ) {
682683
683684 if ( ! is_numeric( $index ) )
684685 return false;
@@ -687,6 +688,14 @@
688689
689690 $array = $this->mArrayExtension[ $arrayId ];
690691
 692+ // calculate start index for negative start indexes:
 693+ if ( $index < 0 ) {
 694+ $index = count( $array ) + $index;
 695+ if ( $index < 0 && $negativeBelowZeroReset ) {
 696+ $index = 0;
 697+ }
 698+ }
 699+
691700 if ( ! isset( $array ) )
692701 return false;
693702
Index: trunk/extensions/ArrayExtension/RELEASE-NOTES
@@ -4,6 +4,8 @@
55 - arrayprint will handle <includeonly>/<noinclude> correct in case its used in a template.
66 - Internationalization in several languages added.
77 - moved into mediawiki.org svn repository.
 8+ - '#arraysearch' will only update expand 'yes' or 'no' if given, but never both.
 9+ - negative indexes for '#arraysearch' and '#arrayindex' possible.
810 @ToDo before release:
911 - As in Variables 2.0, one store per Parser should be used.
1012 - ''#arraysearch'' should not expand both, 'yes' and 'no', just the one actual case.

Status & tagging log