Index: trunk/extensions/StringFunctions/StringFunctions.i18n.php |
— | — | @@ -0,0 +1,17 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Internationalisation for extension StringFunctions |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
| 8 | + */ |
| 9 | + |
| 10 | +$messages = array(); |
| 11 | + |
| 12 | +/** English |
| 13 | + * @author Ross McClure |
| 14 | + * @author Juraj Simlovic |
| 15 | + */ |
| 16 | +$messages['en'] = array( |
| 17 | + 'stringfunctions-desc' => 'Enhances the parser with string functions', |
| 18 | +); |
Property changes on: trunk/extensions/StringFunctions/StringFunctions.i18n.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 19 | + native |
Added: svn:keywords |
2 | 20 | + Id |
Index: trunk/extensions/StringFunctions/StringFunctions.php |
— | — | @@ -97,375 +97,374 @@ |
98 | 98 | */ |
99 | 99 | |
100 | 100 | $wgExtensionCredits['parserhook'][] = array( |
101 | | -'name' => 'StringFunctions', |
102 | | -'version' => '2.0.1', // Aug 27, 2008 |
103 | | -'description' => 'Enhances parser with string functions', |
104 | | -'author' => array('Ross McClure', 'Juraj Simlovic'), |
105 | | -'url' => 'http://www.mediawiki.org/wiki/Extension:StringFunctions', |
| 101 | + 'name' => 'StringFunctions', |
| 102 | + 'version' => '2.0.2', // Oct 27, 2008 |
| 103 | + 'description' => 'Enhances parser with string functions', |
| 104 | + 'descriptionmsg' => 'stringfunctions-desc', |
| 105 | + 'author' => array('Ross McClure', 'Juraj Simlovic'), |
| 106 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:StringFunctions', |
106 | 107 | ); |
107 | 108 | |
| 109 | +$dir = dirname( __FILE__ ) . '/'; |
| 110 | +$wgExtensionMessagesFiles['StringFunctions'] = $dir . 'StringFunctions.i18n.php'; |
| 111 | + |
108 | 112 | $wgExtensionFunctions[] = 'wfStringFunctions'; |
109 | 113 | |
110 | 114 | $wgHooks['LanguageGetMagic'][] = 'wfStringFunctionsLanguageGetMagic'; |
111 | 115 | |
112 | 116 | function wfStringFunctions ( ) { |
113 | | - global $wgParser, $wgExtStringFunctions; |
114 | | - global $wgStringFunctionsLimitSearch; |
115 | | - global $wgStringFunctionsLimitReplace; |
116 | | - global $wgStringFunctionsLimitPad; |
| 117 | + global $wgParser, $wgExtStringFunctions; |
| 118 | + global $wgStringFunctionsLimitSearch; |
| 119 | + global $wgStringFunctionsLimitReplace; |
| 120 | + global $wgStringFunctionsLimitPad; |
117 | 121 | |
118 | | - $wgExtStringFunctions = new ExtStringFunctions ( ); |
119 | | - $wgStringFunctionsLimitSearch = 30; |
120 | | - $wgStringFunctionsLimitReplace = 30; |
121 | | - $wgStringFunctionsLimitPad = 100; |
| 122 | + $wgExtStringFunctions = new ExtStringFunctions ( ); |
| 123 | + $wgStringFunctionsLimitSearch = 30; |
| 124 | + $wgStringFunctionsLimitReplace = 30; |
| 125 | + $wgStringFunctionsLimitPad = 100; |
122 | 126 | |
123 | | - $wgParser->setFunctionHook('len', array(&$wgExtStringFunctions,'runLen' )); |
124 | | - $wgParser->setFunctionHook('pos', array(&$wgExtStringFunctions,'runPos' )); |
125 | | - $wgParser->setFunctionHook('rpos', array(&$wgExtStringFunctions,'runRPos' )); |
126 | | - $wgParser->setFunctionHook('sub', array(&$wgExtStringFunctions,'runSub' )); |
127 | | - $wgParser->setFunctionHook('pad', array(&$wgExtStringFunctions,'runPad' )); |
128 | | - $wgParser->setFunctionHook('replace', array(&$wgExtStringFunctions,'runReplace' )); |
129 | | - $wgParser->setFunctionHook('explode', array(&$wgExtStringFunctions,'runExplode' )); |
130 | | - $wgParser->setFunctionHook('urlencode',array(&$wgExtStringFunctions,'runUrlEncode')); |
131 | | - $wgParser->setFunctionHook('urldecode',array(&$wgExtStringFunctions,'runUrlDecode')); |
| 127 | + $wgParser->setFunctionHook('len', array(&$wgExtStringFunctions,'runLen' )); |
| 128 | + $wgParser->setFunctionHook('pos', array(&$wgExtStringFunctions,'runPos' )); |
| 129 | + $wgParser->setFunctionHook('rpos', array(&$wgExtStringFunctions,'runRPos' )); |
| 130 | + $wgParser->setFunctionHook('sub', array(&$wgExtStringFunctions,'runSub' )); |
| 131 | + $wgParser->setFunctionHook('pad', array(&$wgExtStringFunctions,'runPad' )); |
| 132 | + $wgParser->setFunctionHook('replace', array(&$wgExtStringFunctions,'runReplace' )); |
| 133 | + $wgParser->setFunctionHook('explode', array(&$wgExtStringFunctions,'runExplode' )); |
| 134 | + $wgParser->setFunctionHook('urlencode',array(&$wgExtStringFunctions,'runUrlEncode')); |
| 135 | + $wgParser->setFunctionHook('urldecode',array(&$wgExtStringFunctions,'runUrlDecode')); |
132 | 136 | } |
133 | 137 | |
134 | 138 | function wfStringFunctionsLanguageGetMagic( &$magicWords, $langCode = "en" ) { |
135 | | - switch ( $langCode ) { |
136 | | - default: |
137 | | - $magicWords['len'] = array ( 0, 'len' ); |
138 | | - $magicWords['pos'] = array ( 0, 'pos' ); |
139 | | - $magicWords['rpos'] = array ( 0, 'rpos' ); |
140 | | - $magicWords['sub'] = array ( 0, 'sub' ); |
141 | | - $magicWords['pad'] = array ( 0, 'pad' ); |
142 | | - $magicWords['replace'] = array ( 0, 'replace' ); |
143 | | - $magicWords['explode'] = array ( 0, 'explode' ); |
144 | | - $magicWords['urlencode'] = array ( 0, 'urlencode' ); |
145 | | - $magicWords['urldecode'] = array ( 0, 'urldecode' ); |
146 | | - } |
147 | | - return true; |
| 139 | + switch ( $langCode ) { |
| 140 | + default: |
| 141 | + $magicWords['len'] = array ( 0, 'len' ); |
| 142 | + $magicWords['pos'] = array ( 0, 'pos' ); |
| 143 | + $magicWords['rpos'] = array ( 0, 'rpos' ); |
| 144 | + $magicWords['sub'] = array ( 0, 'sub' ); |
| 145 | + $magicWords['pad'] = array ( 0, 'pad' ); |
| 146 | + $magicWords['replace'] = array ( 0, 'replace' ); |
| 147 | + $magicWords['explode'] = array ( 0, 'explode' ); |
| 148 | + $magicWords['urlencode'] = array ( 0, 'urlencode' ); |
| 149 | + $magicWords['urldecode'] = array ( 0, 'urldecode' ); |
| 150 | + } |
| 151 | + return true; |
148 | 152 | } |
149 | 153 | |
150 | 154 | class ExtStringFunctions { |
151 | 155 | |
152 | | - /** |
153 | | - * Returns part of the perl regexp pattern that matches a marker. |
154 | | - * Unfortunatelly, we are still backward-supporting old versions. |
155 | | - */ |
156 | | - function mwMarkerRE ( &$parser ) |
157 | | - { |
158 | | - if( isset($parser->mMarkerSuffix) ) |
159 | | - $suffix = preg_quote( $parser->mMarkerSuffix, '/' ); |
160 | | - else if ( strcmp( MW_PARSER_VERSION, '1.6.1' ) > 0 ) |
161 | | - $suffix = "QINU\x07"; |
162 | | - else $suffix = 'QINU'; |
| 156 | + /** |
| 157 | + * Returns part of the perl regexp pattern that matches a marker. |
| 158 | + * Unfortunatelly, we are still backward-supporting old versions. |
| 159 | + */ |
| 160 | + function mwMarkerRE ( &$parser ) |
| 161 | + { |
| 162 | + if( isset($parser->mMarkerSuffix) ) |
| 163 | + $suffix = preg_quote( $parser->mMarkerSuffix, '/' ); |
| 164 | + else if ( strcmp( MW_PARSER_VERSION, '1.6.1' ) > 0 ) |
| 165 | + $suffix = "QINU\x07"; |
| 166 | + else $suffix = 'QINU'; |
163 | 167 | |
164 | | - return preg_quote( $parser->mUniqPrefix, '/' ) . '.*?' . $suffix; |
165 | | - } |
| 168 | + return preg_quote( $parser->mUniqPrefix, '/' ) . '.*?' . $suffix; |
| 169 | + } |
166 | 170 | |
167 | | - /** |
168 | | - * {{#len:value}} |
169 | | - * |
170 | | - * Main idea: Count multibytes. Find markers. Substract. |
171 | | - */ |
172 | | - function runLen ( &$parser, $inStr = '' ) { |
| 171 | + /** |
| 172 | + * {{#len:value}} |
| 173 | + * |
| 174 | + * Main idea: Count multibytes. Find markers. Substract. |
| 175 | + */ |
| 176 | + function runLen ( &$parser, $inStr = '' ) { |
173 | 177 | |
174 | | - $len = mb_strlen ( (string)$inStr ); |
| 178 | + $len = mb_strlen ( (string)$inStr ); |
175 | 179 | |
176 | | - $count = preg_match_all ( |
177 | | - '/' . $this->mwMarkerRE($parser) . '/', |
178 | | - (string) $inStr, $matches |
179 | | - ); |
| 180 | + $count = preg_match_all ( |
| 181 | + '/' . $this->mwMarkerRE($parser) . '/', |
| 182 | + (string) $inStr, $matches |
| 183 | + ); |
180 | 184 | |
181 | | - foreach ($matches[0] as $match) |
182 | | - $len -= strlen ($match) - 1; |
| 185 | + foreach ($matches[0] as $match) |
| 186 | + $len -= strlen ($match) - 1; |
183 | 187 | |
184 | | - return $len; |
185 | | - } |
| 188 | + return $len; |
| 189 | + } |
186 | 190 | |
| 191 | + /** |
| 192 | + * Splits the string into its component parts using preg_match_all(). |
| 193 | + * $chars is set to the resulting array of multibyte characters. |
| 194 | + * Returns count($chars). |
| 195 | + */ |
| 196 | + function mwSplit ( &$parser, $str, &$chars ) { |
| 197 | + # Get marker prefix & suffix |
| 198 | + $prefix = preg_quote( $parser->mUniqPrefix, '/' ); |
| 199 | + if( isset($parser->mMarkerSuffix) ) |
| 200 | + $suffix = preg_quote( $parser->mMarkerSuffix, '/' ); |
| 201 | + else if ( strcmp( MW_PARSER_VERSION, '1.6.1' ) > 0 ) |
| 202 | + $suffix = "QINU\x07"; |
| 203 | + else $suffix = 'QINU'; |
187 | 204 | |
| 205 | + # Treat strip markers as single multibyte characters |
| 206 | + $count = preg_match_all('/' . $prefix . '.*?' . $suffix . '|./su', $str, $arr); |
| 207 | + $chars = $arr[0]; |
| 208 | + return $count; |
| 209 | + } |
188 | 210 | |
| 211 | + /** |
| 212 | + * {{#pos:value|key|offset}} |
| 213 | + * Note: If the needle is an empty string, single space is used instead. |
| 214 | + * Note: If the needle is not found, empty string is returned. |
| 215 | + * Note: The needle is limited to specific length. |
| 216 | + */ |
| 217 | + function runPos ( &$parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) { |
| 218 | + global $wgStringFunctionsLimitSearch; |
189 | 219 | |
| 220 | + if ( $inNeedle === '' ) { |
| 221 | + # empty needle |
| 222 | + $needle = array(' '); |
| 223 | + $nSize = 1; |
| 224 | + } else { |
| 225 | + # convert needle |
| 226 | + $nSize = $this->mwSplit ( $parser, $inNeedle, $needle ); |
190 | 227 | |
| 228 | + if ( $nSize > $wgStringFunctionsLimitSearch ) { |
| 229 | + $nSize = $wgStringFunctionsLimitSearch; |
| 230 | + $needle = array_slice ( $needle, 0, $nSize ); |
| 231 | + } |
| 232 | + } |
191 | 233 | |
192 | | - /** |
193 | | - * Splits the string into its component parts using preg_match_all(). |
194 | | - * $chars is set to the resulting array of multibyte characters. |
195 | | - * Returns count($chars). |
196 | | - */ |
197 | | - function mwSplit ( &$parser, $str, &$chars ) { |
198 | | - # Get marker prefix & suffix |
199 | | - $prefix = preg_quote( $parser->mUniqPrefix, '/' ); |
200 | | - if( isset($parser->mMarkerSuffix) ) |
201 | | - $suffix = preg_quote( $parser->mMarkerSuffix, '/' ); |
202 | | - else if ( strcmp( MW_PARSER_VERSION, '1.6.1' ) > 0 ) |
203 | | - $suffix = "QINU\x07"; |
204 | | - else $suffix = 'QINU'; |
| 234 | + # convert string |
| 235 | + $size = $this->mwSplit( $parser, $inStr, $chars ) - $nSize; |
| 236 | + $inOffset = max ( intval($inOffset), 0 ); |
205 | 237 | |
206 | | - # Treat strip markers as single multibyte characters |
207 | | - $count = preg_match_all('/' . $prefix . '.*?' . $suffix . '|./su', $str, $arr); |
208 | | - $chars = $arr[0]; |
209 | | - return $count; |
210 | | - } |
| 238 | + # find needle |
| 239 | + for ( $i = $inOffset; $i <= $size; $i++ ) { |
| 240 | + if ( $chars[$i] !== $needle[0] ) continue; |
| 241 | + for ( $j = 1; ; $j++ ) { |
| 242 | + if ( $j >= $nSize ) return $i; |
| 243 | + if ( $chars[$i + $j] !== $needle[$j] ) break; |
| 244 | + } |
| 245 | + } |
211 | 246 | |
212 | | - /** |
213 | | - * {{#pos:value|key|offset}} |
214 | | - * Note: If the needle is an empty string, single space is used instead. |
215 | | - * Note: If the needle is not found, empty string is returned. |
216 | | - * Note: The needle is limited to specific length. |
217 | | - */ |
218 | | - function runPos ( &$parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) { |
219 | | - global $wgStringFunctionsLimitSearch; |
| 247 | + # return empty string upon not found |
| 248 | + return ''; |
| 249 | + } |
220 | 250 | |
221 | | - if ( $inNeedle === '' ) { |
222 | | - # empty needle |
223 | | - $needle = array(' '); |
224 | | - $nSize = 1; |
225 | | - } else { |
226 | | - # convert needle |
227 | | - $nSize = $this->mwSplit ( $parser, $inNeedle, $needle ); |
| 251 | + /** |
| 252 | + * {{#rpos:value|key}} |
| 253 | + * Note: If the needle is an empty string, single space is used instead. |
| 254 | + * Note: If the needle is not found, -1 is returned. |
| 255 | + * Note: The needle is limited to specific length. |
| 256 | + */ |
| 257 | + function runRPos ( &$parser, $inStr = '', $inNeedle = '' ) { |
| 258 | + global $wgStringFunctionsLimitSearch; |
228 | 259 | |
229 | | - if ( $nSize > $wgStringFunctionsLimitSearch ) { |
230 | | - $nSize = $wgStringFunctionsLimitSearch; |
231 | | - $needle = array_slice ( $needle, 0, $nSize ); |
232 | | - } |
233 | | - } |
| 260 | + if ( $inNeedle === '' ) { |
| 261 | + # empty needle |
| 262 | + $needle = array(' '); |
| 263 | + $nSize = 1; |
| 264 | + } else { |
| 265 | + # convert needle |
| 266 | + $nSize = $this->mwSplit ( $parser, $inNeedle, $needle ); |
234 | 267 | |
235 | | - # convert string |
236 | | - $size = $this->mwSplit( $parser, $inStr, $chars ) - $nSize; |
237 | | - $inOffset = max ( intval($inOffset), 0 ); |
| 268 | + if ( $nSize > $wgStringFunctionsLimitSearch ) { |
| 269 | + $nSize = $wgStringFunctionsLimitSearch; |
| 270 | + $needle = array_slice ( $needle, 0, $nSize ); |
| 271 | + } |
| 272 | + } |
238 | 273 | |
239 | | - # find needle |
240 | | - for ( $i = $inOffset; $i <= $size; $i++ ) { |
241 | | - if ( $chars[$i] !== $needle[0] ) continue; |
242 | | - for ( $j = 1; ; $j++ ) { |
243 | | - if ( $j >= $nSize ) return $i; |
244 | | - if ( $chars[$i + $j] !== $needle[$j] ) break; |
245 | | - } |
246 | | - } |
| 274 | + # convert string |
| 275 | + $size = $this->mwSplit( $parser, $inStr, $chars ) - $nSize; |
247 | 276 | |
248 | | - # return empty string upon not found |
249 | | - return ''; |
250 | | - } |
| 277 | + # find needle |
| 278 | + for ( $i = $size; $i >= 0; $i-- ) { |
| 279 | + if ( $chars[$i] !== $needle[0] ) continue; |
| 280 | + for ( $j = 1; ; $j++ ) { |
| 281 | + if ( $j >= $nSize ) return $i; |
| 282 | + if ( $chars[$i + $j] !== $needle[$j] ) break; |
| 283 | + } |
| 284 | + } |
251 | 285 | |
252 | | - /** |
253 | | - * {{#rpos:value|key}} |
254 | | - * Note: If the needle is an empty string, single space is used instead. |
255 | | - * Note: If the needle is not found, -1 is returned. |
256 | | - * Note: The needle is limited to specific length. |
257 | | - */ |
258 | | - function runRPos ( &$parser, $inStr = '', $inNeedle = '' ) { |
259 | | - global $wgStringFunctionsLimitSearch; |
| 286 | + # return -1 upon not found |
| 287 | + return "-1"; |
| 288 | + } |
260 | 289 | |
261 | | - if ( $inNeedle === '' ) { |
262 | | - # empty needle |
263 | | - $needle = array(' '); |
264 | | - $nSize = 1; |
265 | | - } else { |
266 | | - # convert needle |
267 | | - $nSize = $this->mwSplit ( $parser, $inNeedle, $needle ); |
| 290 | + /** |
| 291 | + * {{#sub:value|start|length}} |
| 292 | + * Note: If length is zero, the rest of the input is returned. |
| 293 | + */ |
| 294 | + function runSub ( &$parser, $inStr = '', $inStart = 0, $inLength = 0 ) { |
| 295 | + # convert string |
| 296 | + $this->mwSplit( $parser, $inStr, $chars ); |
268 | 297 | |
269 | | - if ( $nSize > $wgStringFunctionsLimitSearch ) { |
270 | | - $nSize = $wgStringFunctionsLimitSearch; |
271 | | - $needle = array_slice ( $needle, 0, $nSize ); |
272 | | - } |
273 | | - } |
| 298 | + # zero length |
| 299 | + if ( intval($inLength) == 0 ) |
| 300 | + return join('', array_slice( $chars, intval($inStart) )); |
274 | 301 | |
275 | | - # convert string |
276 | | - $size = $this->mwSplit( $parser, $inStr, $chars ) - $nSize; |
| 302 | + # non-zero length |
| 303 | + return join('', array_slice( $chars, intval($inStart), intval($inLength) )); |
| 304 | + } |
277 | 305 | |
278 | | - # find needle |
279 | | - for ( $i = $size; $i >= 0; $i-- ) { |
280 | | - if ( $chars[$i] !== $needle[0] ) continue; |
281 | | - for ( $j = 1; ; $j++ ) { |
282 | | - if ( $j >= $nSize ) return $i; |
283 | | - if ( $chars[$i + $j] !== $needle[$j] ) break; |
284 | | - } |
285 | | - } |
| 306 | + /** |
| 307 | + * {{#pad:value|length|with|direction}} |
| 308 | + * Note: Length of the resulting string is limited. |
| 309 | + */ |
| 310 | + function runPad( &$parser, $inStr = '', $inLen = 0, $inWith = '', $inDirection = '' ) { |
| 311 | + global $wgStringFunctionsLimitPad; |
286 | 312 | |
287 | | - # return -1 upon not found |
288 | | - return "-1"; |
289 | | - } |
| 313 | + # direction |
| 314 | + switch ( strtolower ( $inDirection ) ) { |
| 315 | + case 'center': |
| 316 | + $direction = STR_PAD_BOTH; |
| 317 | + break; |
| 318 | + case 'right': |
| 319 | + $direction = STR_PAD_RIGHT; |
| 320 | + break; |
| 321 | + case 'left': |
| 322 | + default: |
| 323 | + $direction = STR_PAD_LEFT; |
| 324 | + break; |
| 325 | + } |
290 | 326 | |
291 | | - /** |
292 | | - * {{#sub:value|start|length}} |
293 | | - * Note: If length is zero, the rest of the input is returned. |
294 | | - */ |
295 | | - function runSub ( &$parser, $inStr = '', $inStart = 0, $inLength = 0 ) { |
296 | | - # convert string |
297 | | - $this->mwSplit( $parser, $inStr, $chars ); |
| 327 | + # prevent markers in padding |
| 328 | + $a = explode ( $parser->mUniqPrefix, $inWith, 2 ); |
| 329 | + if ( $a[0] === '' ) |
| 330 | + $inWith = ' '; |
| 331 | + else $inWith = $a[0]; |
298 | 332 | |
299 | | - # zero length |
300 | | - if ( intval($inLength) == 0 ) |
301 | | - return join('', array_slice( $chars, intval($inStart) )); |
| 333 | + # limit pad length |
| 334 | + $inLen = intval ( $inLen ); |
| 335 | + if ($wgStringFunctionsLimitPad > 0) |
| 336 | + $inLen = min ( $inLen, $wgStringFunctionsLimitPad ); |
302 | 337 | |
303 | | - # non-zero length |
304 | | - return join('', array_slice( $chars, intval($inStart), intval($inLength) )); |
305 | | - } |
| 338 | + # adjust for multibyte strings |
| 339 | + $inLen += strlen( $inStr ) - $this->mwSplit( $parser, $inStr, $a ); |
306 | 340 | |
307 | | - /** |
308 | | - * {{#pad:value|length|with|direction}} |
309 | | - * Note: Length of the resulting string is limited. |
310 | | - */ |
311 | | - function runPad( &$parser, $inStr = '', $inLen = 0, $inWith = '', $inDirection = '' ) { |
312 | | - global $wgStringFunctionsLimitPad; |
| 341 | + # pad |
| 342 | + return str_pad ( $inStr, $inLen, $inWith, $direction ); |
| 343 | + } |
313 | 344 | |
314 | | - # direction |
315 | | - switch ( strtolower ( $inDirection ) ) { |
316 | | - case 'center': |
317 | | - $direction = STR_PAD_BOTH; |
318 | | - break; |
319 | | - case 'right': |
320 | | - $direction = STR_PAD_RIGHT; |
321 | | - break; |
322 | | - case 'left': |
323 | | - default: |
324 | | - $direction = STR_PAD_LEFT; |
325 | | - break; |
326 | | - } |
| 345 | + /** |
| 346 | + * {{#replace:value|from|to}} |
| 347 | + * Note: If the needle is an empty string, single space is used instead. |
| 348 | + * Note: The needle is limited to specific length. |
| 349 | + * Note: The product is limited to specific length. |
| 350 | + */ |
| 351 | + function runReplace( &$parser, $inStr = '', $inReplaceFrom = '', $inReplaceTo = '' ) { |
| 352 | + global $wgStringFunctionsLimitSearch, $wgStringFunctionsLimitReplace; |
327 | 353 | |
328 | | - # prevent markers in padding |
329 | | - $a = explode ( $parser->mUniqPrefix, $inWith, 2 ); |
330 | | - if ( $a[0] === '' ) |
331 | | - $inWith = ' '; |
332 | | - else $inWith = $a[0]; |
| 354 | + if ( $inReplaceFrom === '' ) { |
| 355 | + # empty needle |
| 356 | + $needle = array(' '); |
| 357 | + $nSize = 1; |
| 358 | + } else { |
| 359 | + # convert needle |
| 360 | + $nSize = $this->mwSplit ( $parser, $inReplaceFrom, $needle ); |
| 361 | + if ( $nSize > $wgStringFunctionsLimitSearch ) { |
| 362 | + $nSize = $wgStringFunctionsLimitSearch; |
| 363 | + $needle = array_slice ( $needle, 0, $nSize ); |
| 364 | + } |
| 365 | + } |
333 | 366 | |
334 | | - # limit pad length |
335 | | - $inLen = intval ( $inLen ); |
336 | | - if ($wgStringFunctionsLimitPad > 0) |
337 | | - $inLen = min ( $inLen, $wgStringFunctionsLimitPad ); |
| 367 | + # convert product |
| 368 | + $pSize = $this->mwSplit ( $parser, $inReplaceTo, $product ); |
| 369 | + if ( $pSize > $wgStringFunctionsLimitReplace ) { |
| 370 | + $pSize = $wgStringFunctionsLimitReplace; |
| 371 | + $product = array_slice ( $product, 0, $pSize ); |
| 372 | + } |
338 | 373 | |
339 | | - # adjust for multibyte strings |
340 | | - $inLen += strlen( $inStr ) - $this->mwSplit( $parser, $inStr, $a ); |
| 374 | + # remove markers in product |
| 375 | + for( $i = 0; $i < $pSize; $i++ ) { |
| 376 | + if( strlen( $product[$i] ) > 6 ) $product[$i] = ' '; |
| 377 | + } |
341 | 378 | |
342 | | - # pad |
343 | | - return str_pad ( $inStr, $inLen, $inWith, $direction ); |
344 | | - } |
| 379 | + # convert string |
| 380 | + $size = $this->mwSplit ( $parser, $inStr, $chars ) - $nSize; |
345 | 381 | |
346 | | - /** |
347 | | - * {{#replace:value|from|to}} |
348 | | - * Note: If the needle is an empty string, single space is used instead. |
349 | | - * Note: The needle is limited to specific length. |
350 | | - * Note: The product is limited to specific length. |
351 | | - */ |
352 | | - function runReplace( &$parser, $inStr = '', $inReplaceFrom = '', $inReplaceTo = '' ) { |
353 | | - global $wgStringFunctionsLimitSearch, $wgStringFunctionsLimitReplace; |
| 382 | + # replace |
| 383 | + for ( $i = 0; $i <= $size; $i++ ) { |
| 384 | + if ( $chars[$i] !== $needle[0] ) continue; |
| 385 | + for ( $j = 1; ; $j++ ) { |
| 386 | + if ( $j >= $nSize ) { |
| 387 | + array_splice ( $chars, $i, $j, $product ); |
| 388 | + $size += ( $pSize - $nSize ); |
| 389 | + $i += ( $pSize - 1 ); |
| 390 | + break; |
| 391 | + } |
| 392 | + if ( $chars[$i + $j] !== $needle[$j] ) break; |
| 393 | + } |
| 394 | + } |
| 395 | + return join('', $chars); |
| 396 | + } |
354 | 397 | |
355 | | - if ( $inReplaceFrom === '' ) { |
356 | | - # empty needle |
357 | | - $needle = array(' '); |
358 | | - $nSize = 1; |
359 | | - } else { |
360 | | - # convert needle |
361 | | - $nSize = $this->mwSplit ( $parser, $inReplaceFrom, $needle ); |
362 | | - if ( $nSize > $wgStringFunctionsLimitSearch ) { |
363 | | - $nSize = $wgStringFunctionsLimitSearch; |
364 | | - $needle = array_slice ( $needle, 0, $nSize ); |
365 | | - } |
366 | | - } |
| 398 | + /** |
| 399 | + * {{#explode:value|delimiter|position}} |
| 400 | + * Note: Negative position can be used to specify tokens from the end. |
| 401 | + * Note: If the divider is an empty string, single space is used instead. |
| 402 | + * Note: The divider is limited to specific length. |
| 403 | + * Note: Empty string is returned, if there is not enough exploded chunks. |
| 404 | + */ |
| 405 | + function runExplode ( &$parser, $inStr = '', $inDiv = '', $inPos = 0 ) { |
| 406 | + global $wgStringFunctionsLimitSearch; |
367 | 407 | |
368 | | - # convert product |
369 | | - $pSize = $this->mwSplit ( $parser, $inReplaceTo, $product ); |
370 | | - if ( $pSize > $wgStringFunctionsLimitReplace ) { |
371 | | - $pSize = $wgStringFunctionsLimitReplace; |
372 | | - $product = array_slice ( $product, 0, $pSize ); |
373 | | - } |
| 408 | + if ( $inDiv === '' ) { |
| 409 | + # empty divider |
| 410 | + $div = array(' '); |
| 411 | + $dSize = 1; |
| 412 | + } else { |
| 413 | + # convert divider |
| 414 | + $dSize = $this->mwSplit ( $parser, $inDiv, $div ); |
| 415 | + if ( $dSize > $wgStringFunctionsLimitSearch ) { |
| 416 | + $dSize = $wgStringFunctionsLimitSearch; |
| 417 | + $div = array_slice ( $div, 0, $dSize ); |
| 418 | + } |
| 419 | + } |
374 | 420 | |
375 | | - # remove markers in product |
376 | | - for( $i = 0; $i < $pSize; $i++ ) { |
377 | | - if( strlen( $product[$i] ) > 6 ) $product[$i] = ' '; |
378 | | - } |
| 421 | + # convert string |
| 422 | + $size = $this->mwSplit ( $parser, $inStr, $chars ) - $dSize; |
379 | 423 | |
380 | | - # convert string |
381 | | - $size = $this->mwSplit ( $parser, $inStr, $chars ) - $nSize; |
| 424 | + # explode |
| 425 | + $inPos = intval ( $inPos ); |
| 426 | + $tokens = array(); |
| 427 | + $start = 0; |
| 428 | + for ( $i = 0; $i <= $size; $i++ ) { |
| 429 | + if ( $chars[$i] !== $div[0] ) continue; |
| 430 | + for ( $j = 1; ; $j++ ) { |
| 431 | + if ( $j >= $dSize ) { |
| 432 | + if ( $inPos > 0 ) $inPos--; |
| 433 | + else { |
| 434 | + $tokens[] = join('', array_slice($chars, $start, ($i - $start))); |
| 435 | + if ( $inPos == 0 ) return $tokens[0]; |
| 436 | + } |
| 437 | + $start = $i + $j; |
| 438 | + $i = $start - 1; |
| 439 | + break; |
| 440 | + } |
| 441 | + if ( $chars[$i + $j] !== $div[$j] ) break; |
| 442 | + } |
| 443 | + } |
| 444 | + $tokens[] = join('', array_slice( $chars, $start )); |
382 | 445 | |
383 | | - # replace |
384 | | - for ( $i = 0; $i <= $size; $i++ ) { |
385 | | - if ( $chars[$i] !== $needle[0] ) continue; |
386 | | - for ( $j = 1; ; $j++ ) { |
387 | | - if ( $j >= $nSize ) { |
388 | | - array_splice ( $chars, $i, $j, $product ); |
389 | | - $size += ( $pSize - $nSize ); |
390 | | - $i += ( $pSize - 1 ); |
391 | | - break; |
392 | | - } |
393 | | - if ( $chars[$i + $j] !== $needle[$j] ) break; |
394 | | - } |
395 | | - } |
396 | | - return join('', $chars); |
397 | | - } |
| 446 | + # negative $inPos |
| 447 | + if ( $inPos < 0 ) $inPos += count ( $tokens ); |
398 | 448 | |
399 | | - /** |
400 | | - * {{#explode:value|delimiter|position}} |
401 | | - * Note: Negative position can be used to specify tokens from the end. |
402 | | - * Note: If the divider is an empty string, single space is used instead. |
403 | | - * Note: The divider is limited to specific length. |
404 | | - * Note: Empty string is returned, if there is not enough exploded chunks. |
405 | | - */ |
406 | | - function runExplode ( &$parser, $inStr = '', $inDiv = '', $inPos = 0 ) { |
407 | | - global $wgStringFunctionsLimitSearch; |
| 449 | + # out of range |
| 450 | + if ( !isset ( $tokens[$inPos] ) ) return ""; |
408 | 451 | |
409 | | - if ( $inDiv === '' ) { |
410 | | - # empty divider |
411 | | - $div = array(' '); |
412 | | - $dSize = 1; |
413 | | - } else { |
414 | | - # convert divider |
415 | | - $dSize = $this->mwSplit ( $parser, $inDiv, $div ); |
416 | | - if ( $dSize > $wgStringFunctionsLimitSearch ) { |
417 | | - $dSize = $wgStringFunctionsLimitSearch; |
418 | | - $div = array_slice ( $div, 0, $dSize ); |
419 | | - } |
420 | | - } |
| 452 | + # in range |
| 453 | + return $tokens[$inPos]; |
| 454 | + } |
421 | 455 | |
422 | | - # convert string |
423 | | - $size = $this->mwSplit ( $parser, $inStr, $chars ) - $dSize; |
| 456 | + /** |
| 457 | + * {{#urlencode:value}} |
| 458 | + */ |
| 459 | + function runUrlEncode ( &$parser, $inStr = '' ) { |
| 460 | + # encode |
| 461 | + return urlencode ( $inStr ); |
| 462 | + } |
424 | 463 | |
425 | | - # explode |
426 | | - $inPos = intval ( $inPos ); |
427 | | - $tokens = array(); |
428 | | - $start = 0; |
429 | | - for ( $i = 0; $i <= $size; $i++ ) { |
430 | | - if ( $chars[$i] !== $div[0] ) continue; |
431 | | - for ( $j = 1; ; $j++ ) { |
432 | | - if ( $j >= $dSize ) { |
433 | | - if ( $inPos > 0 ) $inPos--; |
434 | | - else { |
435 | | - $tokens[] = join('', array_slice($chars, $start, ($i - $start))); |
436 | | - if ( $inPos == 0 ) return $tokens[0]; |
437 | | - } |
438 | | - $start = $i + $j; |
439 | | - $i = $start - 1; |
440 | | - break; |
441 | | - } |
442 | | - if ( $chars[$i + $j] !== $div[$j] ) break; |
443 | | - } |
444 | | - } |
445 | | - $tokens[] = join('', array_slice( $chars, $start )); |
446 | | - |
447 | | - # negative $inPos |
448 | | - if ( $inPos < 0 ) $inPos += count ( $tokens ); |
449 | | - |
450 | | - # out of range |
451 | | - if ( !isset ( $tokens[$inPos] ) ) return ""; |
452 | | - |
453 | | - # in range |
454 | | - return $tokens[$inPos]; |
455 | | - } |
456 | | - |
457 | | - /** |
458 | | - * {{#urlencode:value}} |
459 | | - */ |
460 | | - function runUrlEncode ( &$parser, $inStr = '' ) { |
461 | | - # encode |
462 | | - return urlencode ( $inStr ); |
463 | | - } |
464 | | - |
465 | | - /** |
466 | | - * {{#urldecode:value}} |
467 | | - */ |
468 | | - function runUrlDecode ( &$parser, $inStr = '' ) { |
469 | | - # decode |
470 | | - return urldecode ( $inStr ); |
471 | | - } |
| 464 | + /** |
| 465 | + * {{#urldecode:value}} |
| 466 | + */ |
| 467 | + function runUrlDecode ( &$parser, $inStr = '' ) { |
| 468 | + # decode |
| 469 | + return urldecode ( $inStr ); |
| 470 | + } |
472 | 471 | } |
\ No newline at end of file |
Index: trunk/extensions/Translate/groups/mediawiki-defines.txt |
— | — | @@ -669,6 +669,8 @@ |
670 | 670 | |
671 | 671 | Stale Pages |
672 | 672 | |
| 673 | +String Functions |
| 674 | + |
673 | 675 | Syntax Highlight GeSHi |
674 | 676 | file = SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.i18n.php |
675 | 677 | descmsg = syntaxhighlight-desc |