r103565 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103564‎ | r103565 | r103566 >
Date:03:09, 18 November 2011
Author:danwe
Status:deferred
Tags:
Comment:
'#arraysearch' now only expands yes/no parameters when necessary similar to '#if'. Regex check improved. Generally more tolerance against missing parameter values.
Modified paths:
  • /trunk/extensions/ArrayExtension/ArrayExtension.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ArrayExtension/ArrayExtension.php
@@ -212,7 +212,7 @@
213213 $temp_result_value = $parser->preprocessToDom( $temp_result_value, $frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0 );
214214 $temp_result_value = trim( $frame->expand( $temp_result_value ) );
215215 }
216 - $rendered_values[] = $temp_result_value ;
 216+ $rendered_values[] = $temp_result_value;
217217 }
218218
219219 $output = implode( $delimiter, $rendered_values );
@@ -266,7 +266,7 @@
267267 return $this->get_array_value( $ary_option, "default" );
268268 }
269269
270 - $ret = $this->validate_array_index( $index, $this->mArrayExtension[$arrayid] );
 270+ $ret = $this->validate_array_index( $arrayid, $index );
271271 if ( $ret !== true ) {
272272 return $this->get_array_value( $ary_option, "default" );
273273 }
@@ -306,31 +306,49 @@
307307 * See: http://www.php.net/manual/en/function.array-search.php
308308 * note it is extended to support regular expression match and index
309309 */
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+ }
314331
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 ];
318336
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+ }
331348 }
332 - }
 349+ }
333350
334 - // no match:
 351+ // no match! (Expand only when needed!)
 352+ $no = isset( $args[4] ) ? trim( $frame->expand( $args[4] ) ) : '-1';
335353 return $no;
336354 }
337355
@@ -492,7 +510,7 @@
493511 * this merge differs from array_merge of php because it merges values.
494512 */
495513 function arraymerge( &$parser, $arrayid_new, $arrayid1, $arrayid2 = '' ) {
496 - if ( !isset( $arrayid_new ) )
 514+ if ( !isset( $arrayid_new ) || !isset( $arrayid1 ) )
497515 return '';
498516
499517 $ret = $this->validate_array_by_arrayid( $arrayid1 );
@@ -527,7 +545,7 @@
528546 * see: http://www.php.net/manual/en/function.array-slice.php
529547 */
530548 function arrayslice( &$parser, $arrayid_new , $arrayid , $offset, $length = '' ) {
531 - if ( !isset( $arrayid_new ) )
 549+ if ( ! isset( $arrayid_new ) || ! isset( $arrayid ) || ! isset( $offset ) )
532550 return '';
533551
534552 $ret = $this->validate_array_by_arrayid( $arrayid );
@@ -562,11 +580,11 @@
563581
564582 * similar to arraymerge, this union works on values.
565583 */
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 ) ) {
568586 return '';
 587+ }
569588
570 -
571589 $ret = $this->validate_array_by_arrayid( $arrayid1 );
572590 if ( $ret !== true ) {
573591 return '';
@@ -592,9 +610,10 @@
593611 * {{#arrayintersect:arrayid_new|arrayid1|arrayid2}}
594612 * See: http://www.php.net/manual/en/function.array-intersect.php
595613 */
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 ) ) {
598616 return '';
 617+ }
599618
600619 $ret = $this->validate_array_by_arrayid( $arrayid1 );
601620 if ( $ret !== true ) {
@@ -623,9 +642,10 @@
624643 * set operation, {white} = {red, white} - {red}
625644 * see: http://www.php.net/manual/en/function.array-diff.php
626645 */
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 ) ) {
629648 return '';
 649+ }
630650
631651 $ret = $this->validate_array_by_arrayid( $arrayid1 );
632652 if ( $ret !== true ) {
@@ -657,19 +677,22 @@
658678 }
659679
660680 // 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 ) )
663684 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 ) )
666692 return false;
667693
668 - if ( !isset( $array ) || !is_array( $array ) )
 694+ if ( ! array_key_exists( $index, $array ) )
669695 return false;
670696
671 - if ( !array_key_exists( $index, $array ) ) /*($index<0 || $index>=count($array))*/
672 - return false;
673 -
674697 return true;
675698 }
676699
@@ -815,8 +838,14 @@
816839 * @param string $pattern regular expression including delimiters and optional flags
817840 * @return boolean
818841 */
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;
821850 }
822851 }
823852
@@ -834,7 +863,7 @@
835864
836865 $parser->setFunctionHook( 'arraysize', array( &$wgArrayExtension, 'arraysize' ) );
837866 $parser->setFunctionHook( 'arrayindex', array( &$wgArrayExtension, 'arrayindex' ) );
838 - $parser->setFunctionHook( 'arraysearch', array( &$wgArrayExtension, 'arraysearch' ) );
 867+ $parser->setFunctionHook( 'arraysearch', array( &$wgArrayExtension, 'arraysearch' ), SFH_OBJECT_ARGS );
839868
840869 $parser->setFunctionHook( 'arraysort', array( &$wgArrayExtension, 'arraysort' ) );
841870 $parser->setFunctionHook( 'arrayunique', array( &$wgArrayExtension, 'arrayunique' ) );

Status & tagging log