Index: trunk/extensions/ArrayExtension/ArrayExtension.php |
— | — | @@ -212,7 +212,7 @@ |
213 | 213 | $temp_result_value = $parser->preprocessToDom( $temp_result_value, $frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0 ); |
214 | 214 | $temp_result_value = trim( $frame->expand( $temp_result_value ) ); |
215 | 215 | } |
216 | | - $rendered_values[] = $temp_result_value ; |
| 216 | + $rendered_values[] = $temp_result_value; |
217 | 217 | } |
218 | 218 | |
219 | 219 | $output = implode( $delimiter, $rendered_values ); |
— | — | @@ -266,7 +266,7 @@ |
267 | 267 | return $this->get_array_value( $ary_option, "default" ); |
268 | 268 | } |
269 | 269 | |
270 | | - $ret = $this->validate_array_index( $index, $this->mArrayExtension[$arrayid] ); |
| 270 | + $ret = $this->validate_array_index( $arrayid, $index ); |
271 | 271 | if ( $ret !== true ) { |
272 | 272 | return $this->get_array_value( $ary_option, "default" ); |
273 | 273 | } |
— | — | @@ -306,31 +306,49 @@ |
307 | 307 | * See: http://www.php.net/manual/en/function.array-search.php |
308 | 308 | * note it is extended to support regular expression match and index |
309 | 309 | */ |
310 | | - function arraysearch( &$parser, $arrayid, $needle = '/^\s*$/', $index = 0, $yes = null, $no = '-1' ) { |
311 | | - $ret = $this->validate_array_by_arrayid( $arrayid ); |
312 | | - if ( $ret !== true ) |
313 | | - return $no; |
| 310 | + //function arraysearch( &$parser, $arrayid, $needle = '/^\s*$/', $index = 0, $yes = null, $no = '-1' ) { |
| 311 | + function arraysearch( Parser &$parser, PPFrame $frame, $args ) { |
| 312 | + |
| 313 | + $arrayId = trim( $frame->expand( $args[0] ) ); |
| 314 | + $startIndex = isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : 0; |
| 315 | + |
| 316 | + if( $this->validate_array_by_arrayid( $arrayId ) |
| 317 | + && $this->validate_array_index( $arrayId, $startIndex ) |
| 318 | + ) { |
| 319 | + // validate/build search regex: |
| 320 | + if( isset( $args[1] ) ) { |
| 321 | + |
| 322 | + $needle = trim( $frame->expand( $args[1] ) ); |
| 323 | + |
| 324 | + if ( ! $this->isValidRegEx( $needle ) ) { |
| 325 | + $needle = '/^\s*' . preg_quote( trim( $needle ), '/' ) . '\s*$/'; |
| 326 | + } |
| 327 | + } |
| 328 | + else { |
| 329 | + $needle = '/^\s*$/'; |
| 330 | + } |
314 | 331 | |
315 | | - $ret = $this->validate_array_index( $index, $this->mArrayExtension[$arrayid] ); |
316 | | - if ( !$ret ) |
317 | | - return $no; |
| 332 | + // 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 ]; |
318 | 336 | |
319 | | - if ( ! $this->isValidRegEx( $needle ) ) |
320 | | - $needle = '/^\s*' . preg_quote( $needle, '/' ) . '\s*$/'; |
321 | | - |
322 | | - // search for a match inside the array: |
323 | | - for ( $i = $index; $i < count( $this->mArrayExtension[$arrayid] ); $i++ ) { |
324 | | - $value = $this->mArrayExtension[$arrayid][$i]; |
325 | | - |
326 | | - if ( preg_match( $needle, $value ) ) { |
327 | | - if ( isset( $yes ) ) |
328 | | - return $yes; |
329 | | - else |
330 | | - return $i; |
| 337 | + if ( preg_match( $needle, $value ) ) { |
| 338 | + // found! |
| 339 | + if ( isset( $args[3] ) ) { |
| 340 | + // Expand only when needed! |
| 341 | + return trim( $frame->expand( $args[3] ) ); |
| 342 | + } |
| 343 | + else { |
| 344 | + // return index of first found item |
| 345 | + return $i; |
| 346 | + } |
| 347 | + } |
331 | 348 | } |
332 | | - } |
| 349 | + } |
333 | 350 | |
334 | | - // no match: |
| 351 | + // no match! (Expand only when needed!) |
| 352 | + $no = isset( $args[4] ) ? trim( $frame->expand( $args[4] ) ) : '-1'; |
335 | 353 | return $no; |
336 | 354 | } |
337 | 355 | |
— | — | @@ -492,7 +510,7 @@ |
493 | 511 | * this merge differs from array_merge of php because it merges values. |
494 | 512 | */ |
495 | 513 | function arraymerge( &$parser, $arrayid_new, $arrayid1, $arrayid2 = '' ) { |
496 | | - if ( !isset( $arrayid_new ) ) |
| 514 | + if ( !isset( $arrayid_new ) || !isset( $arrayid1 ) ) |
497 | 515 | return ''; |
498 | 516 | |
499 | 517 | $ret = $this->validate_array_by_arrayid( $arrayid1 ); |
— | — | @@ -527,7 +545,7 @@ |
528 | 546 | * see: http://www.php.net/manual/en/function.array-slice.php |
529 | 547 | */ |
530 | 548 | function arrayslice( &$parser, $arrayid_new , $arrayid , $offset, $length = '' ) { |
531 | | - if ( !isset( $arrayid_new ) ) |
| 549 | + if ( ! isset( $arrayid_new ) || ! isset( $arrayid ) || ! isset( $offset ) ) |
532 | 550 | return ''; |
533 | 551 | |
534 | 552 | $ret = $this->validate_array_by_arrayid( $arrayid ); |
— | — | @@ -562,11 +580,11 @@ |
563 | 581 | |
564 | 582 | * similar to arraymerge, this union works on values. |
565 | 583 | */ |
566 | | - function arrayunion( &$parser, $arrayid_new , $arrayid1 , $arrayid2 ) { |
567 | | - if ( !isset( $arrayid_new ) ) |
| 584 | + function arrayunion( &$parser, $arrayid_new , $arrayid1 = null , $arrayid2 = null ) { |
| 585 | + if ( ! isset( $arrayid_new ) || ! isset( $arrayid1 ) || ! isset( $arrayid2 ) ) { |
568 | 586 | return ''; |
| 587 | + } |
569 | 588 | |
570 | | - |
571 | 589 | $ret = $this->validate_array_by_arrayid( $arrayid1 ); |
572 | 590 | if ( $ret !== true ) { |
573 | 591 | return ''; |
— | — | @@ -592,9 +610,10 @@ |
593 | 611 | * {{#arrayintersect:arrayid_new|arrayid1|arrayid2}} |
594 | 612 | * See: http://www.php.net/manual/en/function.array-intersect.php |
595 | 613 | */ |
596 | | - function arrayintersect( &$parser, $arrayid_new , $arrayid1 , $arrayid2 ) { |
597 | | - if ( !isset( $arrayid_new ) ) |
| 614 | + function arrayintersect( &$parser, $arrayid_new , $arrayid1 = null , $arrayid2 = null ) { |
| 615 | + if ( ! isset( $arrayid_new ) || ! isset( $arrayid1 ) || ! isset( $arrayid2 ) ) { |
598 | 616 | return ''; |
| 617 | + } |
599 | 618 | |
600 | 619 | $ret = $this->validate_array_by_arrayid( $arrayid1 ); |
601 | 620 | if ( $ret !== true ) { |
— | — | @@ -623,9 +642,10 @@ |
624 | 643 | * set operation, {white} = {red, white} - {red} |
625 | 644 | * see: http://www.php.net/manual/en/function.array-diff.php |
626 | 645 | */ |
627 | | - function arraydiff( &$parser, $arrayid_new , $arrayid1 , $arrayid2 ) { |
628 | | - if ( !isset( $arrayid_new ) ) |
| 646 | + function arraydiff( &$parser, $arrayid_new , $arrayid1 = null , $arrayid2 = null ) { |
| 647 | + if ( ! isset( $arrayid_new ) || ! isset( $arrayid1 ) || ! isset( $arrayid2 ) ) { |
629 | 648 | return ''; |
| 649 | + } |
630 | 650 | |
631 | 651 | $ret = $this->validate_array_by_arrayid( $arrayid1 ); |
632 | 652 | if ( $ret !== true ) { |
— | — | @@ -657,19 +677,22 @@ |
658 | 678 | } |
659 | 679 | |
660 | 680 | // private functions for validating the index of an array |
661 | | - function validate_array_index( $index, $array ) { |
662 | | - if ( !isset( $index ) ) |
| 681 | + function validate_array_index( $arrayId, $index ) { |
| 682 | + |
| 683 | + if ( ! is_numeric( $index ) ) |
663 | 684 | return false; |
664 | | - |
665 | | - if ( !is_numeric( $index ) ) |
| 685 | + |
| 686 | + if( ! array_key_exists( $arrayId, $this->mArrayExtension ) ) |
| 687 | + return false; |
| 688 | + |
| 689 | + $array = $this->mArrayExtension[ $arrayId ]; |
| 690 | + |
| 691 | + if ( ! isset( $array ) ) |
666 | 692 | return false; |
667 | 693 | |
668 | | - if ( !isset( $array ) || !is_array( $array ) ) |
| 694 | + if ( ! array_key_exists( $index, $array ) ) |
669 | 695 | return false; |
670 | 696 | |
671 | | - if ( !array_key_exists( $index, $array ) ) /*($index<0 || $index>=count($array))*/ |
672 | | - return false; |
673 | | - |
674 | 697 | return true; |
675 | 698 | } |
676 | 699 | |
— | — | @@ -815,8 +838,14 @@ |
816 | 839 | * @param string $pattern regular expression including delimiters and optional flags |
817 | 840 | * @return boolean |
818 | 841 | */ |
819 | | - function isValidRegEx( $pattern ) { |
820 | | - return (bool)preg_match( '/^([\\/\\|%]).*\\1[imsSuUx]*$/', $pattern ); |
| 842 | + function isValidRegEx( $pattern ) { |
| 843 | + if( ! preg_match( '/^([\\/\\|%]).*\\1[imsSuUx]*$/', $pattern ) ) { |
| 844 | + return false; |
| 845 | + } |
| 846 | + wfSuppressWarnings(); // instead of using the evil @ operator! |
| 847 | + $isValid = false !== preg_match( $pattern, ' ' ); // preg_match returns false on error |
| 848 | + wfRestoreWarnings(); |
| 849 | + return $isValid; |
821 | 850 | } |
822 | 851 | } |
823 | 852 | |
— | — | @@ -834,7 +863,7 @@ |
835 | 864 | |
836 | 865 | $parser->setFunctionHook( 'arraysize', array( &$wgArrayExtension, 'arraysize' ) ); |
837 | 866 | $parser->setFunctionHook( 'arrayindex', array( &$wgArrayExtension, 'arrayindex' ) ); |
838 | | - $parser->setFunctionHook( 'arraysearch', array( &$wgArrayExtension, 'arraysearch' ) ); |
| 867 | + $parser->setFunctionHook( 'arraysearch', array( &$wgArrayExtension, 'arraysearch' ), SFH_OBJECT_ARGS ); |
839 | 868 | |
840 | 869 | $parser->setFunctionHook( 'arraysort', array( &$wgArrayExtension, 'arraysort' ) ); |
841 | 870 | $parser->setFunctionHook( 'arrayunique', array( &$wgArrayExtension, 'arrayunique' ) ); |