r105775 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105774‎ | r105775 | r105776 >
Date:23:40, 10 December 2011
Author:danwe
Status:deferred
Tags:
Comment:
'#regex_var' now operates in a frame-scoped context.
Modified paths:
  • /trunk/extensions/RegexFun/RELEASE-NOTES (modified) (history)
  • /trunk/extensions/RegexFun/RegexFun.php (modified) (history)
  • /trunk/extensions/RegexFun/RegexFun_Settings.php (modified) (history)

Diff [purge]

Index: trunk/extensions/RegexFun/RELEASE-NOTES
@@ -7,7 +7,12 @@
88 - '#regex' with 'e' flag now first escapes matches before putting them into the back-references
99 and expanding them. Therefore a new configuration variable '$egRegexFunExpansionEscapeTemplates'
1010 has been introduced which serves as a workaround for MW bug #32829.
 11+ - '#regex_var' stored variables are now working in a frame-based scope. '#regex' used on a site or
 12+ template followed by a template call using '#regex' as well with '#regex_var after that template
 13+ call will now access the variable from the '#regex' within the same page/template.
 14+ - Changes in ExtRegexFun::doPregReplace(). Other extensions using this might have to adjust usage.
1115
 16+
1217 * December 5, 2011 -- Version 1.0.2
1318 - Limit won't exceed early when 'e' flag with many backrefs in replacement is used extensivelly.
1419 - It's possible to use the 'Regex Fun' regex system with advanced flags within other extensions.
Index: trunk/extensions/RegexFun/RegexFun.php
@@ -64,7 +64,7 @@
6565 */
6666 public static function init( Parser &$parser ) {
6767 self::initFunction( $parser, 'regex', SFH_OBJECT_ARGS );
68 - self::initFunction( $parser, 'regex_var' );
 68+ self::initFunction( $parser, 'regex_var', SFH_OBJECT_ARGS );
6969 self::initFunction( $parser, 'regexquote' );
7070 self::initFunction( $parser, 'regexall' );
7171
@@ -220,7 +220,7 @@
221221
222222 protected static function msgLimitExceeded() {
223223 global $egRegexFunMaxRegexPerParse, $wgContLang;
224 - $msg = '<span class="error">' . wfMsgForContent( 'regexfun-limit-exceed', $wgContLang->formatNum( $$egRegexFunMaxRegexPerParse ) ) . '</span>';
 224+ $msg = '<span class="error">' . wfMsgForContent( 'regexfun-limit-exceed', $wgContLang->formatNum( $egRegexFunMaxRegexPerParse ) ) . '</span>';
225225 return array( $msg, 'noparse' => true, 'isHTML' => false ); // 'isHTML' must be false for #iferror!
226226 }
227227
@@ -228,17 +228,17 @@
229229 * Helper function. Validates regex and takes care of security risks in pattern which is why
230230 * the pattern is taken by reference!
231231 */
232 - protected static function validateRegexCall( Parser &$parser, $subject, &$pattern, &$specialFlags, $resetLastRegex = false ) {
 232+ protected static function validateRegexCall( PPFrame &$frame, $subject, &$pattern, &$specialFlags, $resetLastRegex = false ) {
233233 if( $resetLastRegex ) {
234234 //reset last matches for the case anything goes wrong
235 - self::setLastMatches( $parser , null );
 235+ self::setLastMatches( $frame , null );
236236 }
237237 if( ! self::validateRegex( $pattern, $specialFlags ) ) {
238238 return false;
239239 }
240240 if( $resetLastRegex ) {
241241 // store infos for this regex for '#regex_var'
242 - self::initLastRegex( $parser, $pattern, $subject );
 242+ self::initLastRegex( $frame, $pattern, $subject );
243243 }
244244 return true;
245245 }
@@ -273,34 +273,34 @@
274274 // search mode:
275275
276276 // validate, initialise and check for wrong input:
277 - $continue = self::validateRegexCall( $parser, $subject, $pattern, $specialFlags, true );
 277+ $continue = self::validateRegexCall( $frame, $subject, $pattern, $specialFlags, true );
278278 if( ! $continue ) {
279279 return self::msgInvalidRegex( $pattern );
280280 }
281281
282 - $lastMatches = self::getLastMatches( $parser );
 282+ $lastMatches = self::getLastMatches( $frame );
283283 $output = ( preg_match( $pattern, $subject, $lastMatches ) ? $lastMatches[0] : '' );
284 - self::setLastMatches( $parser, $lastMatches );
 284+ self::setLastMatches( $frame, $lastMatches );
285285 }
286286 else {
287287 // replace mode:
288288 $limit = (int)$limit;
289289
290290 // set last matches to 'false' and get them on demand instead since preg_replace won't communicate them
291 - self::setLastMatches( $parser, false );
 291+ self::setLastMatches( $frame, false );
292292
293293 // do the regex plus all handling of special flags and validation
294294 $output = self::doPregReplace( $pattern, $replacement, $subject, $limit, $parser, $frame );
295295
296296 if( $output === false ) {
297297 // invalid regex, don't store any infor for '#regex_var'
298 - self::setLastMatches( $parser , null );
 298+ self::setLastMatches( $frame , null );
299299 return self::msgInvalidRegex( $pattern );
300300 }
301301
302302 // set these infos only if valid, pattern still contains special flags though
303 - self::setLastPattern( $parser, $pattern );
304 - self::setLastSubject( $parser, $subject );
 303+ self::setLastPattern( $frame, $pattern );
 304+ self::setLastSubject( $frame, $subject );
305305 }
306306
307307 return $output;
@@ -459,9 +459,8 @@
460460 }
461461 self::increaseRegexCount( $parser );
462462
463 - // validate and check for wrong input:
464 - $continue = self::validateRegexCall( $parser, $subject, $pattern, $specialFlags, false );
465 - if( ! $continue ) {
 463+ // validate and check for wrong input (no effect on #regex_var):
 464+ if( ! self::validateRegex( $pattern, $specialFlags ) ) {
466465 return self::msgInvalidRegex( $pattern );;
467466 }
468467
@@ -497,9 +496,12 @@
498497 * @param $index Integer index of the last match which should be returnd or a string containing $n as indexes to be replaced
499498 * @param $defaultVal Integer default value which will be returned when the result with the given index doesn't exist or is a void string
500499 */
501 - public static function pf_regex_var( &$parser, $index = 0, $defaultVal = '' ) {
 500+ public static function pfObj_regex_var( Parser &$parser, PPFrame $frame, array $args ) {
 501+ $index = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : 0;
 502+ $defaultVal = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : '';
 503+
502504 // get matches from last #regex
503 - $lastMatches = self::getLastMatches( $parser );
 505+ $lastMatches = self::getLastMatches( $frame );
504506
505507 if( $lastMatches === null ) { // last regex was invalid or none executed yet
506508 return $defaultVal;
@@ -677,18 +679,20 @@
678680 **
679681 ***/
680682
681 - protected static function initLastRegex( Parser &$parser, $pattern, $subject ) {
682 - self::setLastMatches( $parser, array() );
683 - self::setLastPattern( $parser, $pattern );
684 - self::setLastSubject( $parser, $subject );
 683+ protected static function initLastRegex( PPFrame $frame, $pattern, $subject ) {
 684+ self::setLastMatches( $frame, array() );
 685+ self::setLastPattern( $frame, $pattern );
 686+ self::setLastSubject( $frame, $subject );
685687 }
686688
687689 public static function onParserClearState( &$parser ) {
688690 //cleanup to avoid conflicts with job queue or Special:Import
 691+ /*
689692 $parser->mExtRegexFun = array();
690693 self::setLastMatches( $parser, null );
691694 self::setLastPattern( $parser, '' );
692 - self::setLastSubject( $parser, '' );
 695+ self::setLastSubject( $parser, '' );
 696+ */
693697 $parser->mExtRegexFun['counter'] = 0;
694698
695699 return true;
@@ -700,7 +704,7 @@
701705 *
702706 * @return boolean
703707 */
704 - public static function limitExceeded( Parser &$parser ) {
 708+ public static function limitExceeded( Parser &$parser ) {
705709 global $egRegexFunMaxRegexPerParse;
706710 return (
707711 $egRegexFunMaxRegexPerParse !== -1
@@ -716,56 +720,56 @@
717721 }
718722
719723 private static function increaseRegexCount( Parser &$parser ) {
720 - $parser->mExtRegexFun['counter']++;
 724+ $parser->mExtRegexFun['counter']++;
721725 }
722726
723727 /**
724 - * Returns the last regex matches done by #regex in the context of the same parser object.
 728+ * Returns the last regex matches done by #regex in the context of the same frame.
725729 *
726 - * @param Parser $parser
 730+ * @param PPFrame $frame
727731 * @return array|null
728732 */
729 - public static function getLastMatches( Parser &$parser ) {
 733+ public static function getLastMatches( PPFrame $frame ) {
730734
731 - if( isset( $parser->mExtRegexFun['lastMatches'] ) ) {
 735+ if( isset( $frame->mExtRegexFun['lastMatches'] ) ) {
732736
733737 // last matches are set to false in case last regex was in replace mode! Get them on demand:
734 - if( $parser->mExtRegexFun['lastMatches'] === false ) {
 738+ if( $frame->mExtRegexFun['lastMatches'] === false ) {
735739 // first, validate pattern to remove special flags!
736 - $pattern = self::getLastPattern( $parser );
 740+ $pattern = self::getLastPattern( $frame );
737741 self::validateRegex( $pattern );
738742 preg_match(
739743 $pattern,
740 - self::getLastSubject( $parser ),
741 - $parser->mExtRegexFun['lastMatches']
 744+ self::getLastSubject( $frame ),
 745+ $frame->mExtRegexFun['lastMatches']
742746 );
743747 }
744 - return $parser->mExtRegexFun['lastMatches'];
 748+ return $frame->mExtRegexFun['lastMatches'];
745749 }
746750 return null;
747751 }
748 - protected static function setLastMatches( Parser &$parser, $value ) {
749 - $parser->mExtRegexFun['lastMatches'] = $value;
 752+ protected static function setLastMatches( PPFrame $frame, $value ) {
 753+ $frame->mExtRegexFun['lastMatches'] = $value;
750754 }
751755
752 - public static function getLastPattern( Parser &$parser ) {
753 - if( isset( $parser->mExtRegexFun['lastPattern'] ) ) {
754 - return $parser->mExtRegexFun['lastPattern'];
 756+ public static function getLastPattern( PPFrame $frame ) {
 757+ if( isset( $frame->mExtRegexFun['lastPattern'] ) ) {
 758+ return $frame->mExtRegexFun['lastPattern'];
755759 }
756760 return '';
757761 }
758 - protected static function setLastPattern( Parser &$parser, $value ) {
759 - $parser->mExtRegexFun['lastPattern'] = $value;
 762+ protected static function setLastPattern( PPFrame $frame, $value ) {
 763+ $frame->mExtRegexFun['lastPattern'] = $value;
760764 }
761765
762 - public static function getLastSubject( Parser &$parser ) {
763 - if( isset( $parser->mExtRegexFun['lastSubject'] ) ) {
764 - return $parser->mExtRegexFun['lastSubject'];
 766+ public static function getLastSubject( PPFrame $frame ) {
 767+ if( isset( $frame->mExtRegexFun['lastSubject'] ) ) {
 768+ return $frame->mExtRegexFun['lastSubject'];
765769 }
766770 return '';
767771 }
768 - protected static function setLastSubject( Parser &$parser, $value ) {
769 - $parser->mExtRegexFun['lastSubject'] = $value;
 772+ protected static function setLastSubject( PPFrame $frame, $value ) {
 773+ $frame->mExtRegexFun['lastSubject'] = $value;
770774 }
771775
772776 }
\ No newline at end of file
Index: trunk/extensions/RegexFun/RegexFun_Settings.php
@@ -60,4 +60,3 @@
6161 '{{' => '{{((}}',
6262 '}}' => '{{))}}'
6363 );
64 -$egRegexFunExpansionEscapeTemplates = null;
\ No newline at end of file

Status & tagging log