Index: trunk/extensions/StringFunctions/StringFunctions_body.php |
— | — | @@ -0,0 +1,360 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class ExtStringFunctions { |
| 5 | + |
| 6 | + /** |
| 7 | + * Returns part of the perl regexp pattern that matches a marker. |
| 8 | + * Unfortunatelly, we are still backward-supporting old versions. |
| 9 | + */ |
| 10 | + static protected function mwMarkerRE( &$parser ) { |
| 11 | + if ( defined( 'Parser::MARKER_SUFFIX' ) ) { |
| 12 | + $suffix = preg_quote( Parser::MARKER_SUFFIX, '/' ); |
| 13 | + } elseif ( isset( $parser->mMarkerSuffix ) ) { |
| 14 | + $suffix = preg_quote( $parser->mMarkerSuffix, '/' ); |
| 15 | + } elseif ( defined( 'MW_PARSER_VERSION' ) && strcmp( MW_PARSER_VERSION, '1.6.1' ) > 0 ) { |
| 16 | + $suffix = "QINU\x07"; |
| 17 | + } else { |
| 18 | + $suffix = 'QINU'; |
| 19 | + } |
| 20 | + |
| 21 | + return preg_quote( $parser->mUniqPrefix, '/' ) . '.*?' . $suffix; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * {{#len:value}} |
| 26 | + * |
| 27 | + * Main idea: Count multibytes. Find markers. Substract. |
| 28 | + */ |
| 29 | + static function runLen( &$parser, $inStr = '' ) { |
| 30 | + $len = mb_strlen( (string)$inStr ); |
| 31 | + |
| 32 | + $count = preg_match_all ( |
| 33 | + '/' . self::mwMarkerRE( $parser ) . '/', |
| 34 | + (string) $inStr, $matches |
| 35 | + ); |
| 36 | + |
| 37 | + foreach ( $matches[0] as $match ) { |
| 38 | + $len -= strlen( $match ) - 1; |
| 39 | + } |
| 40 | + |
| 41 | + return $len; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Splits the string into its component parts using preg_match_all(). |
| 46 | + * $chars is set to the resulting array of multibyte characters. |
| 47 | + * Returns count($chars). |
| 48 | + */ |
| 49 | + static protected function mwSplit( &$parser, $str, &$chars ) { |
| 50 | + # Get marker prefix & suffix |
| 51 | + $prefix = preg_quote( $parser->mUniqPrefix, '/' ); |
| 52 | + if ( defined( 'Parser::MARKER_SUFFIX' ) ) { |
| 53 | + $suffix = preg_quote( Parser::MARKER_SUFFIX, '/' ); |
| 54 | + } elseif ( isset( $parser->mMarkerSuffix ) ) { |
| 55 | + $suffix = preg_quote( $parser->mMarkerSuffix, '/' ); |
| 56 | + } elseif ( defined( 'MW_PARSER_VERSION' ) && strcmp( MW_PARSER_VERSION, '1.6.1' ) > 0 ) { |
| 57 | + $suffix = "QINU\x07"; |
| 58 | + } else { |
| 59 | + $suffix = 'QINU'; |
| 60 | + } |
| 61 | + |
| 62 | + # Treat strip markers as single multibyte characters |
| 63 | + $count = preg_match_all( '/' . $prefix . '.*?' . $suffix . '|./su', $str, $arr ); |
| 64 | + $chars = $arr[0]; |
| 65 | + return $count; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {{#pos:value|key|offset}} |
| 70 | + * Note: If the needle is an empty string, single space is used instead. |
| 71 | + * Note: If the needle is not found, empty string is returned. |
| 72 | + * Note: The needle is limited to specific length. |
| 73 | + */ |
| 74 | + static function runPos( &$parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) { |
| 75 | + global $wgStringFunctionsLimitSearch; |
| 76 | + |
| 77 | + if ( $inNeedle === '' ) { |
| 78 | + # empty needle |
| 79 | + $needle = array( ' ' ); |
| 80 | + $nSize = 1; |
| 81 | + } else { |
| 82 | + # convert needle |
| 83 | + $nSize = self::mwSplit( $parser, $inNeedle, $needle ); |
| 84 | + |
| 85 | + if ( $nSize > $wgStringFunctionsLimitSearch ) { |
| 86 | + $nSize = $wgStringFunctionsLimitSearch; |
| 87 | + $needle = array_slice( $needle, 0, $nSize ); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + # convert string |
| 92 | + $size = self::mwSplit( $parser, $inStr, $chars ) - $nSize; |
| 93 | + $inOffset = max( intval( $inOffset ), 0 ); |
| 94 | + |
| 95 | + # find needle |
| 96 | + for ( $i = $inOffset; $i <= $size; $i++ ) { |
| 97 | + if ( $chars[$i] !== $needle[0] ) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + for ( $j = 1; ; $j++ ) { |
| 101 | + if ( $j >= $nSize ) { |
| 102 | + return $i; |
| 103 | + } |
| 104 | + if ( $chars[$i + $j] !== $needle[$j] ) { |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + # return empty string upon not found |
| 111 | + return ''; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * {{#rpos:value|key}} |
| 116 | + * Note: If the needle is an empty string, single space is used instead. |
| 117 | + * Note: If the needle is not found, -1 is returned. |
| 118 | + * Note: The needle is limited to specific length. |
| 119 | + */ |
| 120 | + static function runRPos( &$parser, $inStr = '', $inNeedle = '' ) { |
| 121 | + global $wgStringFunctionsLimitSearch; |
| 122 | + |
| 123 | + if ( $inNeedle === '' ) { |
| 124 | + # empty needle |
| 125 | + $needle = array( ' ' ); |
| 126 | + $nSize = 1; |
| 127 | + } else { |
| 128 | + # convert needle |
| 129 | + $nSize = self::mwSplit( $parser, $inNeedle, $needle ); |
| 130 | + |
| 131 | + if ( $nSize > $wgStringFunctionsLimitSearch ) { |
| 132 | + $nSize = $wgStringFunctionsLimitSearch; |
| 133 | + $needle = array_slice ( $needle, 0, $nSize ); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + # convert string |
| 138 | + $size = self::mwSplit( $parser, $inStr, $chars ) - $nSize; |
| 139 | + |
| 140 | + # find needle |
| 141 | + for ( $i = $size; $i >= 0; $i-- ) { |
| 142 | + if ( $chars[$i] !== $needle[0] ) { |
| 143 | + continue; |
| 144 | + } |
| 145 | + for ( $j = 1; ; $j++ ) { |
| 146 | + if ( $j >= $nSize ) { |
| 147 | + return $i; |
| 148 | + } |
| 149 | + if ( $chars[$i + $j] !== $needle[$j] ) { |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + # return -1 upon not found |
| 156 | + return '-1'; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * {{#sub:value|start|length}} |
| 161 | + * Note: If length is zero, the rest of the input is returned. |
| 162 | + */ |
| 163 | + static function runSub( &$parser, $inStr = '', $inStart = 0, $inLength = 0 ) { |
| 164 | + # convert string |
| 165 | + self::mwSplit( $parser, $inStr, $chars ); |
| 166 | + |
| 167 | + # zero length |
| 168 | + if ( intval( $inLength ) == 0 ) { |
| 169 | + return join( '', array_slice( $chars, intval( $inStart ) ) ); |
| 170 | + } |
| 171 | + |
| 172 | + # non-zero length |
| 173 | + return join( '', array_slice( $chars, intval( $inStart ), intval( $inLength ) ) ); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * {{#pad:value|length|with|direction}} |
| 178 | + * Note: Length of the resulting string is limited. |
| 179 | + */ |
| 180 | + static function runPad( &$parser, $inStr = '', $inLen = 0, $inWith = '', $inDirection = '' ) { |
| 181 | + global $wgStringFunctionsLimitPad; |
| 182 | + |
| 183 | + # direction |
| 184 | + switch ( strtolower( $inDirection ) ) { |
| 185 | + case 'center': |
| 186 | + $direction = STR_PAD_BOTH; |
| 187 | + break; |
| 188 | + case 'right': |
| 189 | + $direction = STR_PAD_RIGHT; |
| 190 | + break; |
| 191 | + case 'left': |
| 192 | + default: |
| 193 | + $direction = STR_PAD_LEFT; |
| 194 | + break; |
| 195 | + } |
| 196 | + |
| 197 | + # prevent markers in padding |
| 198 | + $a = explode( $parser->mUniqPrefix, $inWith, 2 ); |
| 199 | + if ( $a[0] === '' ) { |
| 200 | + $inWith = ' '; |
| 201 | + } else { |
| 202 | + $inWith = $a[0]; |
| 203 | + } |
| 204 | + |
| 205 | + # limit pad length |
| 206 | + $inLen = intval( $inLen ); |
| 207 | + if ( $wgStringFunctionsLimitPad > 0 ) { |
| 208 | + $inLen = min( $inLen, $wgStringFunctionsLimitPad ); |
| 209 | + } |
| 210 | + |
| 211 | + # adjust for multibyte strings |
| 212 | + $inLen += strlen( $inStr ) - self::mwSplit( $parser, $inStr, $a ); |
| 213 | + |
| 214 | + # pad |
| 215 | + return str_pad( $inStr, $inLen, $inWith, $direction ); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * {{#replace:value|from|to}} |
| 220 | + * Note: If the needle is an empty string, single space is used instead. |
| 221 | + * Note: The needle is limited to specific length. |
| 222 | + * Note: The product is limited to specific length. |
| 223 | + */ |
| 224 | + static function runReplace( &$parser, $inStr = '', $inReplaceFrom = '', $inReplaceTo = '' ) { |
| 225 | + global $wgStringFunctionsLimitSearch, $wgStringFunctionsLimitReplace; |
| 226 | + |
| 227 | + if ( $inReplaceFrom === '' ) { |
| 228 | + # empty needle |
| 229 | + $needle = array( ' ' ); |
| 230 | + $nSize = 1; |
| 231 | + } else { |
| 232 | + # convert needle |
| 233 | + $nSize = self::mwSplit( $parser, $inReplaceFrom, $needle ); |
| 234 | + if ( $nSize > $wgStringFunctionsLimitSearch ) { |
| 235 | + $nSize = $wgStringFunctionsLimitSearch; |
| 236 | + $needle = array_slice( $needle, 0, $nSize ); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + # convert product |
| 241 | + $pSize = self::mwSplit( $parser, $inReplaceTo, $product ); |
| 242 | + if ( $pSize > $wgStringFunctionsLimitReplace ) { |
| 243 | + $pSize = $wgStringFunctionsLimitReplace; |
| 244 | + $product = array_slice( $product, 0, $pSize ); |
| 245 | + } |
| 246 | + |
| 247 | + # remove markers in product |
| 248 | + for( $i = 0; $i < $pSize; $i++ ) { |
| 249 | + if( strlen( $product[$i] ) > 6 ) { |
| 250 | + $product[$i] = ' '; |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + # convert string |
| 255 | + $size = self::mwSplit( $parser, $inStr, $chars ) - $nSize; |
| 256 | + |
| 257 | + # replace |
| 258 | + for ( $i = 0; $i <= $size; $i++ ) { |
| 259 | + if ( $chars[$i] !== $needle[0] ) { |
| 260 | + continue; |
| 261 | + } |
| 262 | + for ( $j = 1; ; $j++ ) { |
| 263 | + if ( $j >= $nSize ) { |
| 264 | + array_splice( $chars, $i, $j, $product ); |
| 265 | + $size += ( $pSize - $nSize ); |
| 266 | + $i += ( $pSize - 1 ); |
| 267 | + break; |
| 268 | + } |
| 269 | + if ( $chars[$i + $j] !== $needle[$j] ) { |
| 270 | + break; |
| 271 | + } |
| 272 | + } |
| 273 | + } |
| 274 | + return join( '', $chars ); |
| 275 | + } |
| 276 | + |
| 277 | + /** |
| 278 | + * {{#explode:value|delimiter|position}} |
| 279 | + * Note: Negative position can be used to specify tokens from the end. |
| 280 | + * Note: If the divider is an empty string, single space is used instead. |
| 281 | + * Note: The divider is limited to specific length. |
| 282 | + * Note: Empty string is returned, if there is not enough exploded chunks. |
| 283 | + */ |
| 284 | + static function runExplode( &$parser, $inStr = '', $inDiv = '', $inPos = 0 ) { |
| 285 | + global $wgStringFunctionsLimitSearch; |
| 286 | + |
| 287 | + if ( $inDiv === '' ) { |
| 288 | + # empty divider |
| 289 | + $div = array( ' ' ); |
| 290 | + $dSize = 1; |
| 291 | + } else { |
| 292 | + # convert divider |
| 293 | + $dSize = self::mwSplit( $parser, $inDiv, $div ); |
| 294 | + if ( $dSize > $wgStringFunctionsLimitSearch ) { |
| 295 | + $dSize = $wgStringFunctionsLimitSearch; |
| 296 | + $div = array_slice ( $div, 0, $dSize ); |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + # convert string |
| 301 | + $size = self::mwSplit( $parser, $inStr, $chars ) - $dSize; |
| 302 | + |
| 303 | + # explode |
| 304 | + $inPos = intval( $inPos ); |
| 305 | + $tokens = array(); |
| 306 | + $start = 0; |
| 307 | + for ( $i = 0; $i <= $size; $i++ ) { |
| 308 | + if ( $chars[$i] !== $div[0] ) { |
| 309 | + continue; |
| 310 | + } |
| 311 | + for ( $j = 1; ; $j++ ) { |
| 312 | + if ( $j >= $dSize ) { |
| 313 | + if ( $inPos > 0 ) { |
| 314 | + $inPos--; |
| 315 | + } else { |
| 316 | + $tokens[] = join( '', array_slice( $chars, $start, ( $i - $start ) ) ); |
| 317 | + if ( $inPos == 0 ) { |
| 318 | + return $tokens[0]; |
| 319 | + } |
| 320 | + } |
| 321 | + $start = $i + $j; |
| 322 | + $i = $start - 1; |
| 323 | + break; |
| 324 | + } |
| 325 | + if ( $chars[$i + $j] !== $div[$j] ) { |
| 326 | + break; |
| 327 | + } |
| 328 | + } |
| 329 | + } |
| 330 | + $tokens[] = join( '', array_slice( $chars, $start ) ); |
| 331 | + |
| 332 | + # negative $inPos |
| 333 | + if ( $inPos < 0 ) { |
| 334 | + $inPos += count( $tokens ); |
| 335 | + } |
| 336 | + |
| 337 | + # out of range |
| 338 | + if ( !isset( $tokens[$inPos] ) ) { |
| 339 | + return ''; |
| 340 | + } |
| 341 | + |
| 342 | + # in range |
| 343 | + return $tokens[$inPos]; |
| 344 | + } |
| 345 | + |
| 346 | + /** |
| 347 | + * {{#urlencode:value}} |
| 348 | + */ |
| 349 | + static function runUrlEncode( &$parser, $inStr = '' ) { |
| 350 | + # encode |
| 351 | + return urlencode( $inStr ); |
| 352 | + } |
| 353 | + |
| 354 | + /** |
| 355 | + * {{#urldecode:value}} |
| 356 | + */ |
| 357 | + static function runUrlDecode( &$parser, $inStr = '' ) { |
| 358 | + # decode |
| 359 | + return urldecode( $inStr ); |
| 360 | + } |
| 361 | +} |
Property changes on: trunk/extensions/StringFunctions/StringFunctions_body.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 362 | + native |
Index: trunk/extensions/StringFunctions/StringFunctions.php |
— | — | @@ -111,6 +111,7 @@ |
112 | 112 | |
113 | 113 | $dir = dirname( __FILE__ ) . '/'; |
114 | 114 | $wgExtensionMessagesFiles['StringFunctions'] = $dir . 'StringFunctions.i18n.php'; |
| 115 | +$wgAutoloadClasses['ExtStringFunctions'] = $dir . 'StringFunctions_body.php'; |
115 | 116 | |
116 | 117 | $wgHooks['ParserFirstCallInit'][] = 'wfStringFunctions'; |
117 | 118 | |
— | — | @@ -150,361 +151,3 @@ |
151 | 152 | return true; |
152 | 153 | } |
153 | 154 | |
154 | | -class ExtStringFunctions { |
155 | | - |
156 | | - /** |
157 | | - * Returns part of the perl regexp pattern that matches a marker. |
158 | | - * Unfortunatelly, we are still backward-supporting old versions. |
159 | | - */ |
160 | | - static protected function mwMarkerRE( &$parser ) { |
161 | | - if ( defined( 'Parser::MARKER_SUFFIX' ) ) { |
162 | | - $suffix = preg_quote( Parser::MARKER_SUFFIX, '/' ); |
163 | | - } elseif ( isset( $parser->mMarkerSuffix ) ) { |
164 | | - $suffix = preg_quote( $parser->mMarkerSuffix, '/' ); |
165 | | - } elseif ( defined( 'MW_PARSER_VERSION' ) && strcmp( MW_PARSER_VERSION, '1.6.1' ) > 0 ) { |
166 | | - $suffix = "QINU\x07"; |
167 | | - } else { |
168 | | - $suffix = 'QINU'; |
169 | | - } |
170 | | - |
171 | | - return preg_quote( $parser->mUniqPrefix, '/' ) . '.*?' . $suffix; |
172 | | - } |
173 | | - |
174 | | - /** |
175 | | - * {{#len:value}} |
176 | | - * |
177 | | - * Main idea: Count multibytes. Find markers. Substract. |
178 | | - */ |
179 | | - static function runLen( &$parser, $inStr = '' ) { |
180 | | - $len = mb_strlen( (string)$inStr ); |
181 | | - |
182 | | - $count = preg_match_all ( |
183 | | - '/' . self::mwMarkerRE( $parser ) . '/', |
184 | | - (string) $inStr, $matches |
185 | | - ); |
186 | | - |
187 | | - foreach ( $matches[0] as $match ) { |
188 | | - $len -= strlen( $match ) - 1; |
189 | | - } |
190 | | - |
191 | | - return $len; |
192 | | - } |
193 | | - |
194 | | - /** |
195 | | - * Splits the string into its component parts using preg_match_all(). |
196 | | - * $chars is set to the resulting array of multibyte characters. |
197 | | - * Returns count($chars). |
198 | | - */ |
199 | | - static protected function mwSplit( &$parser, $str, &$chars ) { |
200 | | - # Get marker prefix & suffix |
201 | | - $prefix = preg_quote( $parser->mUniqPrefix, '/' ); |
202 | | - if ( defined( 'Parser::MARKER_SUFFIX' ) ) { |
203 | | - $suffix = preg_quote( Parser::MARKER_SUFFIX, '/' ); |
204 | | - } elseif ( isset( $parser->mMarkerSuffix ) ) { |
205 | | - $suffix = preg_quote( $parser->mMarkerSuffix, '/' ); |
206 | | - } elseif ( defined( 'MW_PARSER_VERSION' ) && strcmp( MW_PARSER_VERSION, '1.6.1' ) > 0 ) { |
207 | | - $suffix = "QINU\x07"; |
208 | | - } else { |
209 | | - $suffix = 'QINU'; |
210 | | - } |
211 | | - |
212 | | - # Treat strip markers as single multibyte characters |
213 | | - $count = preg_match_all( '/' . $prefix . '.*?' . $suffix . '|./su', $str, $arr ); |
214 | | - $chars = $arr[0]; |
215 | | - return $count; |
216 | | - } |
217 | | - |
218 | | - /** |
219 | | - * {{#pos:value|key|offset}} |
220 | | - * Note: If the needle is an empty string, single space is used instead. |
221 | | - * Note: If the needle is not found, empty string is returned. |
222 | | - * Note: The needle is limited to specific length. |
223 | | - */ |
224 | | - static function runPos( &$parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) { |
225 | | - global $wgStringFunctionsLimitSearch; |
226 | | - |
227 | | - if ( $inNeedle === '' ) { |
228 | | - # empty needle |
229 | | - $needle = array( ' ' ); |
230 | | - $nSize = 1; |
231 | | - } else { |
232 | | - # convert needle |
233 | | - $nSize = self::mwSplit( $parser, $inNeedle, $needle ); |
234 | | - |
235 | | - if ( $nSize > $wgStringFunctionsLimitSearch ) { |
236 | | - $nSize = $wgStringFunctionsLimitSearch; |
237 | | - $needle = array_slice( $needle, 0, $nSize ); |
238 | | - } |
239 | | - } |
240 | | - |
241 | | - # convert string |
242 | | - $size = self::mwSplit( $parser, $inStr, $chars ) - $nSize; |
243 | | - $inOffset = max( intval( $inOffset ), 0 ); |
244 | | - |
245 | | - # find needle |
246 | | - for ( $i = $inOffset; $i <= $size; $i++ ) { |
247 | | - if ( $chars[$i] !== $needle[0] ) { |
248 | | - continue; |
249 | | - } |
250 | | - for ( $j = 1; ; $j++ ) { |
251 | | - if ( $j >= $nSize ) { |
252 | | - return $i; |
253 | | - } |
254 | | - if ( $chars[$i + $j] !== $needle[$j] ) { |
255 | | - break; |
256 | | - } |
257 | | - } |
258 | | - } |
259 | | - |
260 | | - # return empty string upon not found |
261 | | - return ''; |
262 | | - } |
263 | | - |
264 | | - /** |
265 | | - * {{#rpos:value|key}} |
266 | | - * Note: If the needle is an empty string, single space is used instead. |
267 | | - * Note: If the needle is not found, -1 is returned. |
268 | | - * Note: The needle is limited to specific length. |
269 | | - */ |
270 | | - static function runRPos( &$parser, $inStr = '', $inNeedle = '' ) { |
271 | | - global $wgStringFunctionsLimitSearch; |
272 | | - |
273 | | - if ( $inNeedle === '' ) { |
274 | | - # empty needle |
275 | | - $needle = array( ' ' ); |
276 | | - $nSize = 1; |
277 | | - } else { |
278 | | - # convert needle |
279 | | - $nSize = self::mwSplit( $parser, $inNeedle, $needle ); |
280 | | - |
281 | | - if ( $nSize > $wgStringFunctionsLimitSearch ) { |
282 | | - $nSize = $wgStringFunctionsLimitSearch; |
283 | | - $needle = array_slice ( $needle, 0, $nSize ); |
284 | | - } |
285 | | - } |
286 | | - |
287 | | - # convert string |
288 | | - $size = self::mwSplit( $parser, $inStr, $chars ) - $nSize; |
289 | | - |
290 | | - # find needle |
291 | | - for ( $i = $size; $i >= 0; $i-- ) { |
292 | | - if ( $chars[$i] !== $needle[0] ) { |
293 | | - continue; |
294 | | - } |
295 | | - for ( $j = 1; ; $j++ ) { |
296 | | - if ( $j >= $nSize ) { |
297 | | - return $i; |
298 | | - } |
299 | | - if ( $chars[$i + $j] !== $needle[$j] ) { |
300 | | - break; |
301 | | - } |
302 | | - } |
303 | | - } |
304 | | - |
305 | | - # return -1 upon not found |
306 | | - return '-1'; |
307 | | - } |
308 | | - |
309 | | - /** |
310 | | - * {{#sub:value|start|length}} |
311 | | - * Note: If length is zero, the rest of the input is returned. |
312 | | - */ |
313 | | - static function runSub( &$parser, $inStr = '', $inStart = 0, $inLength = 0 ) { |
314 | | - # convert string |
315 | | - self::mwSplit( $parser, $inStr, $chars ); |
316 | | - |
317 | | - # zero length |
318 | | - if ( intval( $inLength ) == 0 ) { |
319 | | - return join( '', array_slice( $chars, intval( $inStart ) ) ); |
320 | | - } |
321 | | - |
322 | | - # non-zero length |
323 | | - return join( '', array_slice( $chars, intval( $inStart ), intval( $inLength ) ) ); |
324 | | - } |
325 | | - |
326 | | - /** |
327 | | - * {{#pad:value|length|with|direction}} |
328 | | - * Note: Length of the resulting string is limited. |
329 | | - */ |
330 | | - static function runPad( &$parser, $inStr = '', $inLen = 0, $inWith = '', $inDirection = '' ) { |
331 | | - global $wgStringFunctionsLimitPad; |
332 | | - |
333 | | - # direction |
334 | | - switch ( strtolower( $inDirection ) ) { |
335 | | - case 'center': |
336 | | - $direction = STR_PAD_BOTH; |
337 | | - break; |
338 | | - case 'right': |
339 | | - $direction = STR_PAD_RIGHT; |
340 | | - break; |
341 | | - case 'left': |
342 | | - default: |
343 | | - $direction = STR_PAD_LEFT; |
344 | | - break; |
345 | | - } |
346 | | - |
347 | | - # prevent markers in padding |
348 | | - $a = explode( $parser->mUniqPrefix, $inWith, 2 ); |
349 | | - if ( $a[0] === '' ) { |
350 | | - $inWith = ' '; |
351 | | - } else { |
352 | | - $inWith = $a[0]; |
353 | | - } |
354 | | - |
355 | | - # limit pad length |
356 | | - $inLen = intval( $inLen ); |
357 | | - if ( $wgStringFunctionsLimitPad > 0 ) { |
358 | | - $inLen = min( $inLen, $wgStringFunctionsLimitPad ); |
359 | | - } |
360 | | - |
361 | | - # adjust for multibyte strings |
362 | | - $inLen += strlen( $inStr ) - self::mwSplit( $parser, $inStr, $a ); |
363 | | - |
364 | | - # pad |
365 | | - return str_pad( $inStr, $inLen, $inWith, $direction ); |
366 | | - } |
367 | | - |
368 | | - /** |
369 | | - * {{#replace:value|from|to}} |
370 | | - * Note: If the needle is an empty string, single space is used instead. |
371 | | - * Note: The needle is limited to specific length. |
372 | | - * Note: The product is limited to specific length. |
373 | | - */ |
374 | | - static function runReplace( &$parser, $inStr = '', $inReplaceFrom = '', $inReplaceTo = '' ) { |
375 | | - global $wgStringFunctionsLimitSearch, $wgStringFunctionsLimitReplace; |
376 | | - |
377 | | - if ( $inReplaceFrom === '' ) { |
378 | | - # empty needle |
379 | | - $needle = array( ' ' ); |
380 | | - $nSize = 1; |
381 | | - } else { |
382 | | - # convert needle |
383 | | - $nSize = self::mwSplit( $parser, $inReplaceFrom, $needle ); |
384 | | - if ( $nSize > $wgStringFunctionsLimitSearch ) { |
385 | | - $nSize = $wgStringFunctionsLimitSearch; |
386 | | - $needle = array_slice( $needle, 0, $nSize ); |
387 | | - } |
388 | | - } |
389 | | - |
390 | | - # convert product |
391 | | - $pSize = self::mwSplit( $parser, $inReplaceTo, $product ); |
392 | | - if ( $pSize > $wgStringFunctionsLimitReplace ) { |
393 | | - $pSize = $wgStringFunctionsLimitReplace; |
394 | | - $product = array_slice( $product, 0, $pSize ); |
395 | | - } |
396 | | - |
397 | | - # remove markers in product |
398 | | - for( $i = 0; $i < $pSize; $i++ ) { |
399 | | - if( strlen( $product[$i] ) > 6 ) { |
400 | | - $product[$i] = ' '; |
401 | | - } |
402 | | - } |
403 | | - |
404 | | - # convert string |
405 | | - $size = self::mwSplit( $parser, $inStr, $chars ) - $nSize; |
406 | | - |
407 | | - # replace |
408 | | - for ( $i = 0; $i <= $size; $i++ ) { |
409 | | - if ( $chars[$i] !== $needle[0] ) { |
410 | | - continue; |
411 | | - } |
412 | | - for ( $j = 1; ; $j++ ) { |
413 | | - if ( $j >= $nSize ) { |
414 | | - array_splice( $chars, $i, $j, $product ); |
415 | | - $size += ( $pSize - $nSize ); |
416 | | - $i += ( $pSize - 1 ); |
417 | | - break; |
418 | | - } |
419 | | - if ( $chars[$i + $j] !== $needle[$j] ) { |
420 | | - break; |
421 | | - } |
422 | | - } |
423 | | - } |
424 | | - return join( '', $chars ); |
425 | | - } |
426 | | - |
427 | | - /** |
428 | | - * {{#explode:value|delimiter|position}} |
429 | | - * Note: Negative position can be used to specify tokens from the end. |
430 | | - * Note: If the divider is an empty string, single space is used instead. |
431 | | - * Note: The divider is limited to specific length. |
432 | | - * Note: Empty string is returned, if there is not enough exploded chunks. |
433 | | - */ |
434 | | - static function runExplode( &$parser, $inStr = '', $inDiv = '', $inPos = 0 ) { |
435 | | - global $wgStringFunctionsLimitSearch; |
436 | | - |
437 | | - if ( $inDiv === '' ) { |
438 | | - # empty divider |
439 | | - $div = array( ' ' ); |
440 | | - $dSize = 1; |
441 | | - } else { |
442 | | - # convert divider |
443 | | - $dSize = self::mwSplit( $parser, $inDiv, $div ); |
444 | | - if ( $dSize > $wgStringFunctionsLimitSearch ) { |
445 | | - $dSize = $wgStringFunctionsLimitSearch; |
446 | | - $div = array_slice ( $div, 0, $dSize ); |
447 | | - } |
448 | | - } |
449 | | - |
450 | | - # convert string |
451 | | - $size = self::mwSplit( $parser, $inStr, $chars ) - $dSize; |
452 | | - |
453 | | - # explode |
454 | | - $inPos = intval( $inPos ); |
455 | | - $tokens = array(); |
456 | | - $start = 0; |
457 | | - for ( $i = 0; $i <= $size; $i++ ) { |
458 | | - if ( $chars[$i] !== $div[0] ) { |
459 | | - continue; |
460 | | - } |
461 | | - for ( $j = 1; ; $j++ ) { |
462 | | - if ( $j >= $dSize ) { |
463 | | - if ( $inPos > 0 ) { |
464 | | - $inPos--; |
465 | | - } else { |
466 | | - $tokens[] = join( '', array_slice( $chars, $start, ( $i - $start ) ) ); |
467 | | - if ( $inPos == 0 ) { |
468 | | - return $tokens[0]; |
469 | | - } |
470 | | - } |
471 | | - $start = $i + $j; |
472 | | - $i = $start - 1; |
473 | | - break; |
474 | | - } |
475 | | - if ( $chars[$i + $j] !== $div[$j] ) { |
476 | | - break; |
477 | | - } |
478 | | - } |
479 | | - } |
480 | | - $tokens[] = join( '', array_slice( $chars, $start ) ); |
481 | | - |
482 | | - # negative $inPos |
483 | | - if ( $inPos < 0 ) { |
484 | | - $inPos += count( $tokens ); |
485 | | - } |
486 | | - |
487 | | - # out of range |
488 | | - if ( !isset( $tokens[$inPos] ) ) { |
489 | | - return ''; |
490 | | - } |
491 | | - |
492 | | - # in range |
493 | | - return $tokens[$inPos]; |
494 | | - } |
495 | | - |
496 | | - /** |
497 | | - * {{#urlencode:value}} |
498 | | - */ |
499 | | - static function runUrlEncode( &$parser, $inStr = '' ) { |
500 | | - # encode |
501 | | - return urlencode( $inStr ); |
502 | | - } |
503 | | - |
504 | | - /** |
505 | | - * {{#urldecode:value}} |
506 | | - */ |
507 | | - static function runUrlDecode( &$parser, $inStr = '' ) { |
508 | | - # decode |
509 | | - return urldecode( $inStr ); |
510 | | - } |
511 | | -} |