Index: trunk/extensions/RegexFun/RELEASE-NOTES |
— | — | @@ -7,7 +7,12 @@ |
8 | 8 | - '#regex' with 'e' flag now first escapes matches before putting them into the back-references |
9 | 9 | and expanding them. Therefore a new configuration variable '$egRegexFunExpansionEscapeTemplates' |
10 | 10 | 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. |
11 | 15 | |
| 16 | + |
12 | 17 | * December 5, 2011 -- Version 1.0.2 |
13 | 18 | - Limit won't exceed early when 'e' flag with many backrefs in replacement is used extensivelly. |
14 | 19 | - 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 @@ |
65 | 65 | */ |
66 | 66 | public static function init( Parser &$parser ) { |
67 | 67 | self::initFunction( $parser, 'regex', SFH_OBJECT_ARGS ); |
68 | | - self::initFunction( $parser, 'regex_var' ); |
| 68 | + self::initFunction( $parser, 'regex_var', SFH_OBJECT_ARGS ); |
69 | 69 | self::initFunction( $parser, 'regexquote' ); |
70 | 70 | self::initFunction( $parser, 'regexall' ); |
71 | 71 | |
— | — | @@ -220,7 +220,7 @@ |
221 | 221 | |
222 | 222 | protected static function msgLimitExceeded() { |
223 | 223 | 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>'; |
225 | 225 | return array( $msg, 'noparse' => true, 'isHTML' => false ); // 'isHTML' must be false for #iferror! |
226 | 226 | } |
227 | 227 | |
— | — | @@ -228,17 +228,17 @@ |
229 | 229 | * Helper function. Validates regex and takes care of security risks in pattern which is why |
230 | 230 | * the pattern is taken by reference! |
231 | 231 | */ |
232 | | - protected static function validateRegexCall( Parser &$parser, $subject, &$pattern, &$specialFlags, $resetLastRegex = false ) { |
| 232 | + protected static function validateRegexCall( PPFrame &$frame, $subject, &$pattern, &$specialFlags, $resetLastRegex = false ) { |
233 | 233 | if( $resetLastRegex ) { |
234 | 234 | //reset last matches for the case anything goes wrong |
235 | | - self::setLastMatches( $parser , null ); |
| 235 | + self::setLastMatches( $frame , null ); |
236 | 236 | } |
237 | 237 | if( ! self::validateRegex( $pattern, $specialFlags ) ) { |
238 | 238 | return false; |
239 | 239 | } |
240 | 240 | if( $resetLastRegex ) { |
241 | 241 | // store infos for this regex for '#regex_var' |
242 | | - self::initLastRegex( $parser, $pattern, $subject ); |
| 242 | + self::initLastRegex( $frame, $pattern, $subject ); |
243 | 243 | } |
244 | 244 | return true; |
245 | 245 | } |
— | — | @@ -273,34 +273,34 @@ |
274 | 274 | // search mode: |
275 | 275 | |
276 | 276 | // 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 ); |
278 | 278 | if( ! $continue ) { |
279 | 279 | return self::msgInvalidRegex( $pattern ); |
280 | 280 | } |
281 | 281 | |
282 | | - $lastMatches = self::getLastMatches( $parser ); |
| 282 | + $lastMatches = self::getLastMatches( $frame ); |
283 | 283 | $output = ( preg_match( $pattern, $subject, $lastMatches ) ? $lastMatches[0] : '' ); |
284 | | - self::setLastMatches( $parser, $lastMatches ); |
| 284 | + self::setLastMatches( $frame, $lastMatches ); |
285 | 285 | } |
286 | 286 | else { |
287 | 287 | // replace mode: |
288 | 288 | $limit = (int)$limit; |
289 | 289 | |
290 | 290 | // 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 ); |
292 | 292 | |
293 | 293 | // do the regex plus all handling of special flags and validation |
294 | 294 | $output = self::doPregReplace( $pattern, $replacement, $subject, $limit, $parser, $frame ); |
295 | 295 | |
296 | 296 | if( $output === false ) { |
297 | 297 | // invalid regex, don't store any infor for '#regex_var' |
298 | | - self::setLastMatches( $parser , null ); |
| 298 | + self::setLastMatches( $frame , null ); |
299 | 299 | return self::msgInvalidRegex( $pattern ); |
300 | 300 | } |
301 | 301 | |
302 | 302 | // 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 ); |
305 | 305 | } |
306 | 306 | |
307 | 307 | return $output; |
— | — | @@ -459,9 +459,8 @@ |
460 | 460 | } |
461 | 461 | self::increaseRegexCount( $parser ); |
462 | 462 | |
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 ) ) { |
466 | 465 | return self::msgInvalidRegex( $pattern );; |
467 | 466 | } |
468 | 467 | |
— | — | @@ -497,9 +496,12 @@ |
498 | 497 | * @param $index Integer index of the last match which should be returnd or a string containing $n as indexes to be replaced |
499 | 498 | * @param $defaultVal Integer default value which will be returned when the result with the given index doesn't exist or is a void string |
500 | 499 | */ |
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 | + |
502 | 504 | // get matches from last #regex |
503 | | - $lastMatches = self::getLastMatches( $parser ); |
| 505 | + $lastMatches = self::getLastMatches( $frame ); |
504 | 506 | |
505 | 507 | if( $lastMatches === null ) { // last regex was invalid or none executed yet |
506 | 508 | return $defaultVal; |
— | — | @@ -677,18 +679,20 @@ |
678 | 680 | ** |
679 | 681 | ***/ |
680 | 682 | |
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 ); |
685 | 687 | } |
686 | 688 | |
687 | 689 | public static function onParserClearState( &$parser ) { |
688 | 690 | //cleanup to avoid conflicts with job queue or Special:Import |
| 691 | + /* |
689 | 692 | $parser->mExtRegexFun = array(); |
690 | 693 | self::setLastMatches( $parser, null ); |
691 | 694 | self::setLastPattern( $parser, '' ); |
692 | | - self::setLastSubject( $parser, '' ); |
| 695 | + self::setLastSubject( $parser, '' ); |
| 696 | + */ |
693 | 697 | $parser->mExtRegexFun['counter'] = 0; |
694 | 698 | |
695 | 699 | return true; |
— | — | @@ -700,7 +704,7 @@ |
701 | 705 | * |
702 | 706 | * @return boolean |
703 | 707 | */ |
704 | | - public static function limitExceeded( Parser &$parser ) { |
| 708 | + public static function limitExceeded( Parser &$parser ) { |
705 | 709 | global $egRegexFunMaxRegexPerParse; |
706 | 710 | return ( |
707 | 711 | $egRegexFunMaxRegexPerParse !== -1 |
— | — | @@ -716,56 +720,56 @@ |
717 | 721 | } |
718 | 722 | |
719 | 723 | private static function increaseRegexCount( Parser &$parser ) { |
720 | | - $parser->mExtRegexFun['counter']++; |
| 724 | + $parser->mExtRegexFun['counter']++; |
721 | 725 | } |
722 | 726 | |
723 | 727 | /** |
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. |
725 | 729 | * |
726 | | - * @param Parser $parser |
| 730 | + * @param PPFrame $frame |
727 | 731 | * @return array|null |
728 | 732 | */ |
729 | | - public static function getLastMatches( Parser &$parser ) { |
| 733 | + public static function getLastMatches( PPFrame $frame ) { |
730 | 734 | |
731 | | - if( isset( $parser->mExtRegexFun['lastMatches'] ) ) { |
| 735 | + if( isset( $frame->mExtRegexFun['lastMatches'] ) ) { |
732 | 736 | |
733 | 737 | // 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 ) { |
735 | 739 | // first, validate pattern to remove special flags! |
736 | | - $pattern = self::getLastPattern( $parser ); |
| 740 | + $pattern = self::getLastPattern( $frame ); |
737 | 741 | self::validateRegex( $pattern ); |
738 | 742 | preg_match( |
739 | 743 | $pattern, |
740 | | - self::getLastSubject( $parser ), |
741 | | - $parser->mExtRegexFun['lastMatches'] |
| 744 | + self::getLastSubject( $frame ), |
| 745 | + $frame->mExtRegexFun['lastMatches'] |
742 | 746 | ); |
743 | 747 | } |
744 | | - return $parser->mExtRegexFun['lastMatches']; |
| 748 | + return $frame->mExtRegexFun['lastMatches']; |
745 | 749 | } |
746 | 750 | return null; |
747 | 751 | } |
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; |
750 | 754 | } |
751 | 755 | |
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']; |
755 | 759 | } |
756 | 760 | return ''; |
757 | 761 | } |
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; |
760 | 764 | } |
761 | 765 | |
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']; |
765 | 769 | } |
766 | 770 | return ''; |
767 | 771 | } |
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; |
770 | 774 | } |
771 | 775 | |
772 | 776 | } |
\ No newline at end of file |
Index: trunk/extensions/RegexFun/RegexFun_Settings.php |
— | — | @@ -60,4 +60,3 @@ |
61 | 61 | '{{' => '{{((}}', |
62 | 62 | '}}' => '{{))}}' |
63 | 63 | ); |
64 | | -$egRegexFunExpansionEscapeTemplates = null; |
\ No newline at end of file |