r104134 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r104133‎ | r104134 | r104135 >
Date:03:55, 24 November 2011
Author:danwe
Status:deferred
Tags:
Comment:
doPregReplace() won't take $pattern by value anymore + internal cache re-designed since there was a bug regarding '#regex_var'
Modified paths:
  • /trunk/extensions/RegexFun/RegexFun.php (modified) (history)

Diff [purge]

Index: trunk/extensions/RegexFun/RegexFun.php
@@ -289,7 +289,7 @@
290290 return self::msgInvalidRegex( $pattern );
291291 }
292292
293 - // set these infos after pattern validation/correction
 293+ // set these infos only if valid, pattern still contains special flags though
294294 self::setLastPattern( $parser, $pattern );
295295 self::setLastSubject( $parser, $subject );
296296 }
@@ -308,7 +308,7 @@
309309 * @param array $allowedSpecialFlags all special flags that should be handled, by default 'e' and 'r'.
310310 */
311311 public static function doPregReplace(
312 - &$pattern,
 312+ $pattern, // not by value in here!
313313 $replacement,
314314 $subject,
315315 $limit = -1,
@@ -319,31 +319,37 @@
320320 )
321321 ) {
322322 static $lastPattern = null;
323 - static $lastFlags = null;
 323+ static $activePattern = null;
324324 static $specialFlags = null;
325325
326326 /*
327327 * cache validated pattern and use it as long as nothing has changed, this makes things
328328 * faster in case we do a lot of stuff with the same regex.
329329 */
330 - if( $lastPattern === null || $lastPattern !== $pattern
331 - || $lastFlags !== implode( ',', $allowedSpecialFlags )
332 - ) {
 330+ if( $lastPattern === null || $lastPattern !== $pattern ) {
 331+ // remember pattern without validation
 332+ $lastPattern = $pattern;
 333+
333334 // if allowed special flags change, we have to validate again^^
334335 $lastFlags = implode( ',', $allowedSpecialFlags );
335336
336337 // validate regex and get special flags 'e' and 'r' if given:
337338 if( ! self::validateRegex( $pattern, $specialFlags ) ) {
338339 // invalid regex!
 340+ $lastPattern = null;
339341 return false;
340342 }
 343+ // set validated pattern as active one
 344+ $activePattern = $pattern;
341345
342346 // filter unwanted special flags:
343 - $allowedSpecialFlags = array_flip( $allowedSpecialFlags );
 347+ $allowedSpecialFlags = array_flip( $allowedSpecialFlags );
344348 $specialFlags = array_intersect_key( $specialFlags, $allowedSpecialFlags );
345 -
346 - $lastPattern = $pattern;
347349 }
 350+ else {
 351+ // set last validated pattern without flags 'e' and 'r'
 352+ $pattern = $activePattern;
 353+ }
348354
349355
350356 // FLAG 'e' (parse replace after match) handling:
@@ -658,9 +664,12 @@
659665 if( isset( $parser->mExtRegexFun['lastMatches'] ) ) {
660666
661667 // last matches are set to false in case last regex was in replace mode! Get them on demand:
662 - if( $parser->mExtRegexFun['lastMatches'] === false ) {
 668+ if( $parser->mExtRegexFun['lastMatches'] === false ) {
 669+ // first, validate pattern to remove special flags!
 670+ $pattern = self::getLastPattern( $parser );
 671+ self::validateRegex( $pattern );
663672 preg_match(
664 - self::getLastPattern( $parser ),
 673+ $pattern,
665674 self::getLastSubject( $parser ),
666675 $parser->mExtRegexFun['lastMatches']
667676 );

Status & tagging log