r84624 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r84623‎ | r84624 | r84625 >
Date:19:05, 23 March 2011
Author:ialex
Status:deferred
Tags:
Comment:
* Made methods static so it's not needed to instanciate the class
* Use ParserFirstCallInit to register parser functions
* Added ParserClearState hook to reset the usage counter
Modified paths:
  • /trunk/extensions/RegexFunctions/RegexFunctions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/RegexFunctions/RegexFunctions.php
@@ -9,9 +9,6 @@
1010 die( 1 );
1111 }
1212
13 -//credits and hooks
14 -$wgExtensionFunctions[] = 'wfRegexFunctions';
15 -
1613 $wgExtensionCredits['parserhook'][] = array(
1714 'path' => __FILE__,
1815 'name' => 'RegexFunctions',
@@ -25,6 +22,9 @@
2623 $wgExtensionMessagesFiles['RegexFunctions'] = $dir . 'RegexFunctions.i18n.php';
2724 $wgExtensionMessagesFiles['RegexFunctionsMagic'] = $dir . 'RegexFunctions.i18n.magic.php';
2825
 26+$wgHooks['ParserFirstCallInit'][] = 'ExtRegexFunctions::onParserFirstCallInit';
 27+$wgHooks['ParserClearState'][] = 'ExtRegexFunctions::onParserClearState';
 28+
2929 //default globals
3030 //how many functions are allowed in a single page? Keep this at least above 3 for usability
3131 $wgRegexFunctionsPerPage = 10;
@@ -41,28 +41,31 @@
4242 //array of functions to disable, aka these functions cannot be used :)
4343 $wgRegexFunctionsDisable = array();
4444
45 -function wfRegexFunctions() {
46 - global $wgParser, $wgExtRegexFunctions;
 45+class ExtRegexFunctions {
 46+ private static $num = 0;
 47+ private static $modifiers = array('i', 'm', 's', 'x', 'A', 'D', 'S', 'U', 'X', 'J', 'u', 'e');
 48+ private static $options = array('i', 'm', 's', 'x', 'U', 'X', 'J');
4749
48 - $wgExtRegexFunctions = new ExtRegexFunctions();
49 - $wgParser->setFunctionHook( 'rmatch', array(&$wgExtRegexFunctions, 'rmatch') );
50 - $wgParser->setFunctionHook( 'rsplit', array(&$wgExtRegexFunctions, 'rsplit') );
51 - $wgParser->setFunctionHook( 'rreplace', array(&$wgExtRegexFunctions, 'rreplace') );
52 -}
 50+ public static function onParserFirstCallInit( $parser ) {
 51+ $parser->setFunctionHook( 'rmatch', array( __CLASS__, 'rmatch' ) );
 52+ $parser->setFunctionHook( 'rsplit', array( __CLASS__, 'rsplit' ) );
 53+ $parser->setFunctionHook( 'rreplace', array( __CLASS__, 'rreplace' ) );
 54+ return true;
 55+ }
5356
54 -class ExtRegexFunctions {
55 - var $num = 0;
56 - var $modifiers = array('i', 'm', 's', 'x', 'A', 'D', 'S', 'U', 'X', 'J', 'u', 'e');
57 - var $options = array('i', 'm', 's', 'x', 'U', 'X', 'J');
58 -
59 - function rmatch ( &$parser, $string = '', $pattern = '', $return = '', $notfound = '', $offset = 0 ) {
 57+ public static function onParserClearState( $parser ) {
 58+ self::$num = 0;
 59+ return true;
 60+ }
 61+
 62+ public static function rmatch ( &$parser, $string = '', $pattern = '', $return = '', $notfound = '', $offset = 0 ) {
6063 global $wgRegexFunctionsPerPage, $wgRegexFunctionsAllowModifiers, $wgRegexFunctionsDisable;
6164 if(in_array('rmatch', $wgRegexFunctionsDisable))
6265 return;
63 - $this->num++;
64 - if($this->num > $wgRegexFunctionsPerPage)
 66+ self::$num++;
 67+ if( self::$num > $wgRegexFunctionsPerPage)
6568 return;
66 - $pattern = $this->sanitize($pattern, $wgRegexFunctionsAllowModifiers, false);
 69+ $pattern = self::sanitize($pattern, $wgRegexFunctionsAllowModifiers, false);
6770 $num = preg_match( $pattern, $string, $matches, PREG_OFFSET_CAPTURE, (int) $offset );
6871 if($num === false)
6972 return;
@@ -78,14 +81,14 @@
7982 return $return;
8083 }
8184
82 - function rsplit ( &$parser, $string = '', $pattern = '', $piece = 0 ) {
 85+ public static function rsplit ( &$parser, $string = '', $pattern = '', $piece = 0 ) {
8386 global $wgRegexFunctionsPerPage, $wgRegexFunctionsAllowModifiers, $wgRegexFunctionsLimit, $wgRegexFunctionsDisable;
8487 if(in_array('rsplit', $wgRegexFunctionsDisable))
8588 return;
86 - $this->num++;
87 - if($this->num > $wgRegexFunctionsPerPage)
 89+ self::$num++;
 90+ if(self::$num > $wgRegexFunctionsPerPage)
8891 return;
89 - $pattern = $this->sanitize($pattern, $wgRegexFunctionsAllowModifiers, false);
 92+ $pattern = self::sanitize($pattern, $wgRegexFunctionsAllowModifiers, false);
9093 $res = preg_split( $pattern, $string, $wgRegexFunctionsLimit );
9194 $p = (int) $piece;
9295 //allow negative pieces to work from the end of the array
@@ -99,26 +102,26 @@
100103 return $res[$p];
101104 }
102105
103 - function rreplace ( &$parser, $string = '', $pattern = '', &$replace = '' ) {
 106+ public static function rreplace ( &$parser, $string = '', $pattern = '', &$replace = '' ) {
104107 global $wgRegexFunctionsPerPage, $wgRegexFunctionsAllowModifiers, $wgRegexFunctionsAllowE, $wgRegexFunctionsLimit, $wgRegexFunctionsDisable;
105108 if(in_array('rreplace', $wgRegexFunctionsDisable))
106109 return;
107 - $this->num++;
108 - if($this->num > $wgRegexFunctionsPerPage)
 110+ self::$num++;
 111+ if(self::$num > $wgRegexFunctionsPerPage)
109112 return;
110 - $pattern = $this->sanitize($pattern, $wgRegexFunctionsAllowModifiers, $wgRegexFunctionsAllowE);
 113+ $pattern = self::sanitize($pattern, $wgRegexFunctionsAllowModifiers, $wgRegexFunctionsAllowE);
111114 $res = preg_replace($pattern, $replace, $string, $wgRegexFunctionsLimit);
112115 return $res;
113116 }
114117
115118 //santizes a regex pattern
116 - function sanitize($pattern, $m = false, $e = false) {
 119+ private static function sanitize($pattern, $m = false, $e = false) {
117120 if(preg_match('/^\/(.*)([^\\\\])\/(.*?)$/', $pattern, $matches)) {
118 - $pat = preg_replace('/([^\\\\])?\(\?(.*\:)?(.*)\)/Ue', '\'$1(?\' . $this->cleanupInternal(\'$2\') . \'$3)\'', $matches[1] . $matches[2]);
 121+ $pat = preg_replace('/([^\\\\])?\(\?(.*\:)?(.*)\)/Ue', '\'$1(?\' . self::cleanupInternal(\'$2\') . \'$3)\'', $matches[1] . $matches[2]);
119122 $ret = '/' . $pat . '/';
120123 if($m) {
121124 $mod = '';
122 - foreach($this->modifiers as $val) {
 125+ foreach(self::$modifiers as $val) {
123126 if(strpos($matches[3], $val) !== false)
124127 $mod .= $val;
125128 }
@@ -127,7 +130,7 @@
128131 $ret .= $mod;
129132 }
130133 } else {
131 - $pat = preg_replace('/([^\\\\])?\(\?(.*\:)?(.*)\)/Ue', '\'$1(?\' . $this->cleanupInternal(\'$2\') . \'$3)\'', $pattern);
 134+ $pat = preg_replace('/([^\\\\])?\(\?(.*\:)?(.*)\)/Ue', '\'$1(?\' . self::cleanupInternal(\'$2\') . \'$3)\'', $pattern);
132135 $pat = preg_replace('!([^\\\\])/!', '$1\\/', $pat);
133136 $ret = '/' . $pat . '/';
134137 }
@@ -135,12 +138,12 @@
136139 }
137140
138141 //cleans up internal options, making sure they are valid
139 - function cleanupInternal($str) {
 142+ private static function cleanupInternal($str) {
140143 global $wgRegexFunctionsAllowOptions;
141144 $ret = '';
142145 if(!$wgRegexFunctionsAllowOptions)
143146 return '';
144 - foreach($this->options as $opt) {
 147+ foreach(self::$options as $opt) {
145148 if(strpos($str, $opt) !== false)
146149 $ret .= $opt;
147150 }

Status & tagging log