Index: trunk/extensions/ArrayExtension/ArrayExtension.php |
— | — | @@ -46,7 +46,7 @@ |
47 | 47 | 'path' => __FILE__, |
48 | 48 | 'name' => 'ArrayExtension', |
49 | 49 | '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]' ), |
51 | 51 | 'descriptionmsg' => 'arrayext-desc', |
52 | 52 | 'version' => ArrayExtension::VERSION |
53 | 53 | ); |
— | — | @@ -265,13 +265,12 @@ |
266 | 266 | if ( $ret !== true ) { |
267 | 267 | return $this->get_array_value( $ary_option, "default" ); |
268 | 268 | } |
269 | | - |
270 | | - $ret = $this->validate_array_index( $arrayid, $index ); |
271 | | - if ( $ret !== true ) { |
| 269 | + |
| 270 | + if ( ! $this->validate_array_index( $arrayid, $index, false ) ) { |
272 | 271 | return $this->get_array_value( $ary_option, "default" ); |
273 | 272 | } |
274 | 273 | |
275 | | - return $this->mArrayExtension[$arrayid][$index]; |
| 274 | + return $this->mArrayExtension[ $arrayid ][ $index ]; |
276 | 275 | } |
277 | 276 | |
278 | 277 | /** |
— | — | @@ -310,11 +309,13 @@ |
311 | 310 | function arraysearch( Parser &$parser, PPFrame $frame, $args ) { |
312 | 311 | |
313 | 312 | $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; |
315 | 314 | |
316 | 315 | if( $this->validate_array_by_arrayid( $arrayId ) |
317 | | - && $this->validate_array_index( $arrayId, $startIndex ) |
| 316 | + && $this->validate_array_index( $arrayId, $index, true ) |
318 | 317 | ) { |
| 318 | + $array = $this->mArrayExtension[ $arrayId ]; |
| 319 | + |
319 | 320 | // validate/build search regex: |
320 | 321 | if( isset( $args[1] ) ) { |
321 | 322 | |
— | — | @@ -329,9 +330,9 @@ |
330 | 331 | } |
331 | 332 | |
332 | 333 | // 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 ]; |
336 | 337 | |
337 | 338 | if ( preg_match( $needle, $value ) ) { |
338 | 339 | // found! |
— | — | @@ -677,7 +678,7 @@ |
678 | 679 | } |
679 | 680 | |
680 | 681 | // 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 ) { |
682 | 683 | |
683 | 684 | if ( ! is_numeric( $index ) ) |
684 | 685 | return false; |
— | — | @@ -687,6 +688,14 @@ |
688 | 689 | |
689 | 690 | $array = $this->mArrayExtension[ $arrayId ]; |
690 | 691 | |
| 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 | + |
691 | 700 | if ( ! isset( $array ) ) |
692 | 701 | return false; |
693 | 702 | |
Index: trunk/extensions/ArrayExtension/RELEASE-NOTES |
— | — | @@ -4,6 +4,8 @@ |
5 | 5 | - arrayprint will handle <includeonly>/<noinclude> correct in case its used in a template. |
6 | 6 | - Internationalization in several languages added. |
7 | 7 | - 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. |
8 | 10 | @ToDo before release: |
9 | 11 | - As in Variables 2.0, one store per Parser should be used. |
10 | 12 | - ''#arraysearch'' should not expand both, 'yes' and 'no', just the one actual case. |