r104076 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r104075‎ | r104076 | r104077 >
Date:19:52, 23 November 2011
Author:danwe
Status:deferred
Tags:
Comment:
If 'Regex Fun' extension is installed, '#arraysearcharray' can use Regex Funs 'e' flag feature for transforming the result and at the same time parsing them after each back-reference inclusion.
Modified paths:
  • /trunk/extensions/Arrays/Arrays.php (modified) (history)
  • /trunk/extensions/Arrays/RELEASE-NOTES (modified) (history)

Diff [purge]

Index: trunk/extensions/Arrays/RELEASE-NOTES
@@ -15,6 +15,8 @@
1616 + '#arraymerge', '#arrayunion', '#arraydiff' and '#arrayintersect' with only one array for
1717 the operation will make a copy of that array instead of creating no array at all.
1818 + See 1.4 alpha for previous changes
 19+ - If 'Regex Fun' extension is installed, '#arraysearcharray' can use Regex Funs 'e' flag feature
 20+ for transforming the result and at the same time parsing them after each back-reference inclusion.
1921
2022
2123 * November 20, 2001 -- Version 1.4 alpha (r103716)
Index: trunk/extensions/Arrays/Arrays.php
@@ -125,7 +125,6 @@
126126 */
127127 public static function getDir() {
128128 static $dir = null;
129 -
130129 if( $dir === null ) {
131130 $dir = dirname( __FILE__ );
132131 }
@@ -287,6 +286,7 @@
288287 }
289288
290289 $rendered_values = array();
 290+
291291 foreach( $array as $val ) {
292292 // replace place holder with current value:
293293 $rawResult = str_replace( $search, $val, $subject );
@@ -503,8 +503,9 @@
504504 }
505505
506506 $newArr = array();
507 -
508 - if( ! self::isValidRegEx( $needle ) ) {
 507+
 508+ $regexFunSupport = self::hasRegexFunSupport();
 509+ if( ! self::isValidRegEx( $needle, $regexFunSupport ) ) {
509510 $needle = '/^\s*(' . preg_quote( $needle, '/' ) . ')\s*$/';
510511 }
511512
@@ -515,8 +516,24 @@
516517 $value = $array[ $i ];
517518
518519 if( preg_match( $needle, $value ) ) {
 520+ // Found something!
519521 if( $transform !== '' ) {
520 - $value = preg_replace( $needle, $transform, $value );
 522+ // Transform the found string. Can we use 'Regex Fun' ?
 523+ if( $regexFunSupport ) {
 524+ // do the transformation with Regex Fun to support 'e' flag:
 525+ $value = ExtRegexFun::doPregReplace(
 526+ $needle,
 527+ $transform,
 528+ $value,
 529+ -1,
 530+ $parser,
 531+ array( ExtRegexFun::FLAG_REPLACEMENT_PARSE )
 532+ );
 533+ }
 534+ else {
 535+ // regular preg_replace:
 536+ $value = preg_replace( $needle, $transform, $value );
 537+ }
521538 }
522539 $newArr[] = trim( $value );
523540
@@ -598,6 +615,7 @@
599616
600617 // reset all hash tables if no specific tables are given:
601618 if( ! isset( $args[0] ) || ( $args[0] === '' && count( $args ) == 1 ) ) {
 619+ // reset ALL arrays!
602620 $store->mArrays = array();
603621 }
604622 else {
@@ -672,7 +690,8 @@
673691 break;
674692
675693 case 'reverse':
676 - $store->mArrays[ $arrayId ] = array_reverse( $store->mArrays[ $arrayId ] );
 694+ $reverse = array_reverse( $store->getArray( $arrayId ) );
 695+ $store->setArray( $arrayId, $reverse );
677696 break;
678697 } ;
679698 }
@@ -1123,10 +1142,17 @@
11241143 * Arrays parser functions or not.
11251144 *
11261145 * @param string $pattern regular expression including delimiters and optional flags
 1146+ * @param bool $forRegexFun whether the regular expression is inteded to be used with 'Regex Fun'
 1147+ * if supported by the wikis infrastructure. In case 'Regex Fun' is not available,
 1148+ * the default validation will be used.
11271149 *
11281150 * @return boolean
11291151 */
1130 - static function isValidRegEx( $pattern ) {
 1152+ static function isValidRegEx( $pattern, $forRegexFun = false ) {
 1153+ if( $forRegexFun && self::hasRegexFunSupport() ) {
 1154+ return ExtRegexFun::validateRegex( $pattern );
 1155+ }
 1156+
11311157 if( ! preg_match( '/^([\\/\\|%]).*\\1[imsSuUx]*$/', $pattern ) ) {
11321158 return false;
11331159 }
@@ -1135,4 +1161,19 @@
11361162 wfRestoreWarnings();
11371163 return $isValid;
11381164 }
 1165+
 1166+ /**
 1167+ * Whether 'Regex Fun' extension is available in this wiki to take over preg_replace handling
 1168+ * for '#arraysearcharray' function.
 1169+ */
 1170+ static function hasRegexFunSupport() {
 1171+ static $support = null;
 1172+ if( $support === null ) {
 1173+ $support = (
 1174+ defined( 'ExtRegexFun::VERSION' )
 1175+ && version_compare( ExtRegexFun::VERSION, '1.0.1', '>=' )
 1176+ );
 1177+ }
 1178+ return $support;
 1179+ }
11391180 }

Status & tagging log