Index: trunk/extensions/RegexFunctions/RegexFunctions.php |
— | — | @@ -9,9 +9,6 @@ |
10 | 10 | die( 1 ); |
11 | 11 | } |
12 | 12 | |
13 | | -//credits and hooks |
14 | | -$wgExtensionFunctions[] = 'wfRegexFunctions'; |
15 | | - |
16 | 13 | $wgExtensionCredits['parserhook'][] = array( |
17 | 14 | 'path' => __FILE__, |
18 | 15 | 'name' => 'RegexFunctions', |
— | — | @@ -25,6 +22,9 @@ |
26 | 23 | $wgExtensionMessagesFiles['RegexFunctions'] = $dir . 'RegexFunctions.i18n.php'; |
27 | 24 | $wgExtensionMessagesFiles['RegexFunctionsMagic'] = $dir . 'RegexFunctions.i18n.magic.php'; |
28 | 25 | |
| 26 | +$wgHooks['ParserFirstCallInit'][] = 'ExtRegexFunctions::onParserFirstCallInit'; |
| 27 | +$wgHooks['ParserClearState'][] = 'ExtRegexFunctions::onParserClearState'; |
| 28 | + |
29 | 29 | //default globals |
30 | 30 | //how many functions are allowed in a single page? Keep this at least above 3 for usability |
31 | 31 | $wgRegexFunctionsPerPage = 10; |
— | — | @@ -41,28 +41,31 @@ |
42 | 42 | //array of functions to disable, aka these functions cannot be used :) |
43 | 43 | $wgRegexFunctionsDisable = array(); |
44 | 44 | |
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'); |
47 | 49 | |
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 | + } |
53 | 56 | |
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 ) { |
60 | 63 | global $wgRegexFunctionsPerPage, $wgRegexFunctionsAllowModifiers, $wgRegexFunctionsDisable; |
61 | 64 | if(in_array('rmatch', $wgRegexFunctionsDisable)) |
62 | 65 | return; |
63 | | - $this->num++; |
64 | | - if($this->num > $wgRegexFunctionsPerPage) |
| 66 | + self::$num++; |
| 67 | + if( self::$num > $wgRegexFunctionsPerPage) |
65 | 68 | return; |
66 | | - $pattern = $this->sanitize($pattern, $wgRegexFunctionsAllowModifiers, false); |
| 69 | + $pattern = self::sanitize($pattern, $wgRegexFunctionsAllowModifiers, false); |
67 | 70 | $num = preg_match( $pattern, $string, $matches, PREG_OFFSET_CAPTURE, (int) $offset ); |
68 | 71 | if($num === false) |
69 | 72 | return; |
— | — | @@ -78,14 +81,14 @@ |
79 | 82 | return $return; |
80 | 83 | } |
81 | 84 | |
82 | | - function rsplit ( &$parser, $string = '', $pattern = '', $piece = 0 ) { |
| 85 | + public static function rsplit ( &$parser, $string = '', $pattern = '', $piece = 0 ) { |
83 | 86 | global $wgRegexFunctionsPerPage, $wgRegexFunctionsAllowModifiers, $wgRegexFunctionsLimit, $wgRegexFunctionsDisable; |
84 | 87 | if(in_array('rsplit', $wgRegexFunctionsDisable)) |
85 | 88 | return; |
86 | | - $this->num++; |
87 | | - if($this->num > $wgRegexFunctionsPerPage) |
| 89 | + self::$num++; |
| 90 | + if(self::$num > $wgRegexFunctionsPerPage) |
88 | 91 | return; |
89 | | - $pattern = $this->sanitize($pattern, $wgRegexFunctionsAllowModifiers, false); |
| 92 | + $pattern = self::sanitize($pattern, $wgRegexFunctionsAllowModifiers, false); |
90 | 93 | $res = preg_split( $pattern, $string, $wgRegexFunctionsLimit ); |
91 | 94 | $p = (int) $piece; |
92 | 95 | //allow negative pieces to work from the end of the array |
— | — | @@ -99,26 +102,26 @@ |
100 | 103 | return $res[$p]; |
101 | 104 | } |
102 | 105 | |
103 | | - function rreplace ( &$parser, $string = '', $pattern = '', &$replace = '' ) { |
| 106 | + public static function rreplace ( &$parser, $string = '', $pattern = '', &$replace = '' ) { |
104 | 107 | global $wgRegexFunctionsPerPage, $wgRegexFunctionsAllowModifiers, $wgRegexFunctionsAllowE, $wgRegexFunctionsLimit, $wgRegexFunctionsDisable; |
105 | 108 | if(in_array('rreplace', $wgRegexFunctionsDisable)) |
106 | 109 | return; |
107 | | - $this->num++; |
108 | | - if($this->num > $wgRegexFunctionsPerPage) |
| 110 | + self::$num++; |
| 111 | + if(self::$num > $wgRegexFunctionsPerPage) |
109 | 112 | return; |
110 | | - $pattern = $this->sanitize($pattern, $wgRegexFunctionsAllowModifiers, $wgRegexFunctionsAllowE); |
| 113 | + $pattern = self::sanitize($pattern, $wgRegexFunctionsAllowModifiers, $wgRegexFunctionsAllowE); |
111 | 114 | $res = preg_replace($pattern, $replace, $string, $wgRegexFunctionsLimit); |
112 | 115 | return $res; |
113 | 116 | } |
114 | 117 | |
115 | 118 | //santizes a regex pattern |
116 | | - function sanitize($pattern, $m = false, $e = false) { |
| 119 | + private static function sanitize($pattern, $m = false, $e = false) { |
117 | 120 | 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]); |
119 | 122 | $ret = '/' . $pat . '/'; |
120 | 123 | if($m) { |
121 | 124 | $mod = ''; |
122 | | - foreach($this->modifiers as $val) { |
| 125 | + foreach(self::$modifiers as $val) { |
123 | 126 | if(strpos($matches[3], $val) !== false) |
124 | 127 | $mod .= $val; |
125 | 128 | } |
— | — | @@ -127,7 +130,7 @@ |
128 | 131 | $ret .= $mod; |
129 | 132 | } |
130 | 133 | } else { |
131 | | - $pat = preg_replace('/([^\\\\])?\(\?(.*\:)?(.*)\)/Ue', '\'$1(?\' . $this->cleanupInternal(\'$2\') . \'$3)\'', $pattern); |
| 134 | + $pat = preg_replace('/([^\\\\])?\(\?(.*\:)?(.*)\)/Ue', '\'$1(?\' . self::cleanupInternal(\'$2\') . \'$3)\'', $pattern); |
132 | 135 | $pat = preg_replace('!([^\\\\])/!', '$1\\/', $pat); |
133 | 136 | $ret = '/' . $pat . '/'; |
134 | 137 | } |
— | — | @@ -135,12 +138,12 @@ |
136 | 139 | } |
137 | 140 | |
138 | 141 | //cleans up internal options, making sure they are valid |
139 | | - function cleanupInternal($str) { |
| 142 | + private static function cleanupInternal($str) { |
140 | 143 | global $wgRegexFunctionsAllowOptions; |
141 | 144 | $ret = ''; |
142 | 145 | if(!$wgRegexFunctionsAllowOptions) |
143 | 146 | return ''; |
144 | | - foreach($this->options as $opt) { |
| 147 | + foreach(self::$options as $opt) { |
145 | 148 | if(strpos($str, $opt) !== false) |
146 | 149 | $ret .= $opt; |
147 | 150 | } |