Index: trunk/extensions/ParserFunctions/ParserFunctions_body.php |
— | — | @@ -1,22 +1,38 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | class ExtParserFunctions { |
5 | | - var $mExprParser; |
6 | | - var $mTimeCache = array(); |
7 | | - var $mTimeChars = 0; |
8 | | - var $mMaxTimeChars = 6000; # ~10 seconds |
| 5 | + static $mExprParser; |
| 6 | + static $mConvertParser; |
| 7 | + static $mTimeCache = array(); |
| 8 | + static $mTimeChars = 0; |
| 9 | + static $mMaxTimeChars = 6000; # ~10 seconds |
9 | 10 | |
10 | | - function clearState( $parser ) { |
11 | | - $this->mTimeChars = 0; |
| 11 | + public static function clearState( $parser ) { |
| 12 | + self::$mTimeChars = 0; |
12 | 13 | $parser->pf_ifexist_breakdown = array(); |
13 | 14 | $parser->pf_markerRegex = null; |
14 | 15 | return true; |
15 | 16 | } |
16 | 17 | |
17 | 18 | /** |
| 19 | + * Register ParserClearState hook. |
| 20 | + * We defer this until needed to avoid the loading of the code of this file |
| 21 | + * when no parser function is actually called. |
| 22 | + */ |
| 23 | + public static function registerClearHook() { |
| 24 | + static $done = false; |
| 25 | + if( !$done ) { |
| 26 | + global $wgHooks; |
| 27 | + $wgHooks['ParserClearState'][] = __CLASS__ . '::clearState'; |
| 28 | + $done = true; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /** |
18 | 33 | * Get the marker regex. Cached. |
19 | 34 | */ |
20 | | - function getMarkerRegex( $parser ) { |
| 35 | + public static function getMarkerRegex( $parser ) { |
| 36 | + self::registerClearHook(); |
21 | 37 | if ( isset( $parser->pf_markerRegex ) ) { |
22 | 38 | return $parser->pf_markerRegex; |
23 | 39 | } |
— | — | @@ -43,31 +59,31 @@ |
44 | 60 | } |
45 | 61 | |
46 | 62 | // Removes unique markers from passed parameters, used by string functions. |
47 | | - private function killMarkers ( $parser, $text ) { |
48 | | - return preg_replace( $this->getMarkerRegex( $parser ), '' , $text ); |
| 63 | + private static function killMarkers ( $parser, $text ) { |
| 64 | + return preg_replace( self::getMarkerRegex( $parser ), '' , $text ); |
49 | 65 | } |
50 | 66 | |
51 | 67 | /** |
52 | 68 | * @return ExprParser |
53 | 69 | */ |
54 | | - function &getExprParser() { |
55 | | - if ( !isset( $this->mExprParser ) ) { |
56 | | - $this->mExprParser = new ExprParser; |
| 70 | + public static function &getExprParser() { |
| 71 | + if ( !isset( self::$mExprParser ) ) { |
| 72 | + self::$mExprParser = new ExprParser; |
57 | 73 | } |
58 | | - return $this->mExprParser; |
| 74 | + return self::$mExprParser; |
59 | 75 | } |
60 | 76 | |
61 | | - function expr( $parser, $expr = '' ) { |
| 77 | + public static function expr( $parser, $expr = '' ) { |
62 | 78 | try { |
63 | | - return $this->getExprParser()->doExpression( $expr ); |
| 79 | + return self::getExprParser()->doExpression( $expr ); |
64 | 80 | } catch ( ExprError $e ) { |
65 | 81 | return $e->getMessage(); |
66 | 82 | } |
67 | 83 | } |
68 | 84 | |
69 | | - function ifexpr( $parser, $expr = '', $then = '', $else = '' ) { |
| 85 | + public static function ifexpr( $parser, $expr = '', $then = '', $else = '' ) { |
70 | 86 | try { |
71 | | - $ret = $this->getExprParser()->doExpression( $expr ); |
| 87 | + $ret = self::getExprParser()->doExpression( $expr ); |
72 | 88 | if ( is_numeric( $ret ) ) { |
73 | 89 | $ret = floatval( $ret ); |
74 | 90 | } |
— | — | @@ -81,18 +97,18 @@ |
82 | 98 | } |
83 | 99 | } |
84 | 100 | |
85 | | - function ifexprObj( $parser, $frame, $args ) { |
| 101 | + public static function ifexprObj( $parser, $frame, $args ) { |
86 | 102 | $expr = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
87 | 103 | $then = isset( $args[1] ) ? $args[1] : ''; |
88 | 104 | $else = isset( $args[2] ) ? $args[2] : ''; |
89 | | - $result = $this->ifexpr( $parser, $expr, $then, $else ); |
| 105 | + $result = self::ifexpr( $parser, $expr, $then, $else ); |
90 | 106 | if ( is_object( $result ) ) { |
91 | 107 | $result = trim( $frame->expand( $result ) ); |
92 | 108 | } |
93 | 109 | return $result; |
94 | 110 | } |
95 | 111 | |
96 | | - function ifHook( $parser, $test = '', $then = '', $else = '' ) { |
| 112 | + public static function ifHook( $parser, $test = '', $then = '', $else = '' ) { |
97 | 113 | if ( $test !== '' ) { |
98 | 114 | return $then; |
99 | 115 | } else { |
— | — | @@ -100,7 +116,7 @@ |
101 | 117 | } |
102 | 118 | } |
103 | 119 | |
104 | | - function ifObj( $parser, $frame, $args ) { |
| 120 | + public static function ifObj( $parser, $frame, $args ) { |
105 | 121 | $test = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
106 | 122 | if ( $test !== '' ) { |
107 | 123 | return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; |
— | — | @@ -109,7 +125,7 @@ |
110 | 126 | } |
111 | 127 | } |
112 | 128 | |
113 | | - function ifeq( $parser, $left = '', $right = '', $then = '', $else = '' ) { |
| 129 | + public static function ifeq( $parser, $left = '', $right = '', $then = '', $else = '' ) { |
114 | 130 | if ( $left == $right ) { |
115 | 131 | return $then; |
116 | 132 | } else { |
— | — | @@ -117,7 +133,7 @@ |
118 | 134 | } |
119 | 135 | } |
120 | 136 | |
121 | | - function ifeqObj( $parser, $frame, $args ) { |
| 137 | + public static function ifeqObj( $parser, $frame, $args ) { |
122 | 138 | $left = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
123 | 139 | $right = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; |
124 | 140 | if ( $left == $right ) { |
— | — | @@ -127,7 +143,7 @@ |
128 | 144 | } |
129 | 145 | } |
130 | 146 | |
131 | | - function iferror( $parser, $test = '', $then = '', $else = false ) { |
| 147 | + public static function iferror( $parser, $test = '', $then = '', $else = false ) { |
132 | 148 | if ( preg_match( '/<(?:strong|span|p|div)\s(?:[^\s>]*\s+)*?class="(?:[^"\s>]*\s+)*?error(?:\s[^">]*)?"/', $test ) ) { |
133 | 149 | return $then; |
134 | 150 | } elseif ( $else === false ) { |
— | — | @@ -137,11 +153,11 @@ |
138 | 154 | } |
139 | 155 | } |
140 | 156 | |
141 | | - function iferrorObj( $parser, $frame, $args ) { |
| 157 | + public static function iferrorObj( $parser, $frame, $args ) { |
142 | 158 | $test = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
143 | 159 | $then = isset( $args[1] ) ? $args[1] : false; |
144 | 160 | $else = isset( $args[2] ) ? $args[2] : false; |
145 | | - $result = $this->iferror( $parser, $test, $then, $else ); |
| 161 | + $result = self::iferror( $parser, $test, $then, $else ); |
146 | 162 | if ( $result === false ) { |
147 | 163 | return ''; |
148 | 164 | } else { |
— | — | @@ -149,7 +165,7 @@ |
150 | 166 | } |
151 | 167 | } |
152 | 168 | |
153 | | - function switchHook( $parser /*,...*/ ) { |
| 169 | + public static function switchHook( $parser /*,...*/ ) { |
154 | 170 | $args = func_get_args(); |
155 | 171 | array_shift( $args ); |
156 | 172 | $primary = trim( array_shift( $args ) ); |
— | — | @@ -188,7 +204,7 @@ |
189 | 205 | } |
190 | 206 | } |
191 | 207 | |
192 | | - function switchObj( $parser, $frame, $args ) { |
| 208 | + public static function switchObj( $parser, $frame, $args ) { |
193 | 209 | if ( count( $args ) == 0 ) { |
194 | 210 | return ''; |
195 | 211 | } |
— | — | @@ -248,7 +264,7 @@ |
249 | 265 | * Following subpage link syntax instead of standard path syntax, an |
250 | 266 | * initial slash is treated as a relative path, and vice versa. |
251 | 267 | */ |
252 | | - public function rel2abs( $parser , $to = '' , $from = '' ) { |
| 268 | + public static function rel2abs( $parser , $to = '' , $from = '' ) { |
253 | 269 | |
254 | 270 | $from = trim( $from ); |
255 | 271 | if ( $from == '' ) { |
— | — | @@ -302,9 +318,10 @@ |
303 | 319 | return implode( '/' , $newExploded ); |
304 | 320 | } |
305 | 321 | |
306 | | - function incrementIfexistCount( $parser, $frame ) { |
| 322 | + public static function incrementIfexistCount( $parser, $frame ) { |
307 | 323 | // Don't let this be called more than a certain number of times. It tends to make the database explode. |
308 | 324 | global $wgExpensiveParserFunctionLimit; |
| 325 | + self::registerClearHook(); |
309 | 326 | $parser->mExpensiveFunctionCount++; |
310 | 327 | if ( $frame ) { |
311 | 328 | $pdbk = $frame->getPDBK( 1 ); |
— | — | @@ -316,11 +333,11 @@ |
317 | 334 | return $parser->mExpensiveFunctionCount <= $wgExpensiveParserFunctionLimit; |
318 | 335 | } |
319 | 336 | |
320 | | - function ifexist( $parser, $title = '', $then = '', $else = '' ) { |
321 | | - return $this->ifexistCommon( $parser, false, $title, $then, $else ); |
| 337 | + public static function ifexist( $parser, $title = '', $then = '', $else = '' ) { |
| 338 | + return self::ifexistCommon( $parser, false, $title, $then, $else ); |
322 | 339 | } |
323 | 340 | |
324 | | - function ifexistCommon( $parser, $frame, $titletext = '', $then = '', $else = '' ) { |
| 341 | + public static function ifexistCommon( $parser, $frame, $titletext = '', $then = '', $else = '' ) { |
325 | 342 | global $wgContLang; |
326 | 343 | $title = Title::newFromText( $titletext ); |
327 | 344 | $wgContLang->findVariantLink( $titletext, $title, true ); |
— | — | @@ -329,7 +346,7 @@ |
330 | 347 | /* If namespace is specified as NS_MEDIA, then we want to |
331 | 348 | * check the physical file, not the "description" page. |
332 | 349 | */ |
333 | | - if ( !$this->incrementIfexistCount( $parser, $frame ) ) { |
| 350 | + if ( !self::incrementIfexistCount( $parser, $frame ) ) { |
334 | 351 | return $else; |
335 | 352 | } |
336 | 353 | $file = wfFindFile( $title ); |
— | — | @@ -354,7 +371,7 @@ |
355 | 372 | } else { |
356 | 373 | $pdbk = $title->getPrefixedDBkey(); |
357 | 374 | $lc = LinkCache::singleton(); |
358 | | - if ( !$this->incrementIfexistCount( $parser, $frame ) ) { |
| 375 | + if ( !self::incrementIfexistCount( $parser, $frame ) ) { |
359 | 376 | return $else; |
360 | 377 | } |
361 | 378 | if ( 0 != ( $id = $lc->getGoodLinkID( $pdbk ) ) ) { |
— | — | @@ -374,12 +391,12 @@ |
375 | 392 | return $else; |
376 | 393 | } |
377 | 394 | |
378 | | - function ifexistObj( $parser, $frame, $args ) { |
| 395 | + public static function ifexistObj( $parser, $frame, $args ) { |
379 | 396 | $title = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
380 | 397 | $then = isset( $args[1] ) ? $args[1] : null; |
381 | 398 | $else = isset( $args[2] ) ? $args[2] : null; |
382 | 399 | |
383 | | - $result = $this->ifexistCommon( $parser, $frame, $title, $then, $else ); |
| 400 | + $result = self::ifexistCommon( $parser, $frame, $title, $then, $else ); |
384 | 401 | if ( $result === null ) { |
385 | 402 | return ''; |
386 | 403 | } else { |
— | — | @@ -387,10 +404,11 @@ |
388 | 405 | } |
389 | 406 | } |
390 | 407 | |
391 | | - function time( $parser, $format = '', $date = '', $local = false ) { |
| 408 | + public static function time( $parser, $format = '', $date = '', $local = false ) { |
392 | 409 | global $wgContLang, $wgLocaltimezone; |
393 | | - if ( isset( $this->mTimeCache[$format][$date][$local] ) ) { |
394 | | - return $this->mTimeCache[$format][$date][$local]; |
| 410 | + self::registerClearHook(); |
| 411 | + if ( isset( self::$mTimeCache[$format][$date][$local] ) ) { |
| 412 | + return self::$mTimeCache[$format][$date][$local]; |
395 | 413 | } |
396 | 414 | |
397 | 415 | # compute the timestamp string $ts |
— | — | @@ -462,19 +480,19 @@ |
463 | 481 | if ( $invalidTime ) { |
464 | 482 | $result = '<strong class="error">' . wfMsgForContent( 'pfunc_time_error' ) . '</strong>'; |
465 | 483 | } else { |
466 | | - $this->mTimeChars += strlen( $format ); |
467 | | - if ( $this->mTimeChars > $this->mMaxTimeChars ) { |
| 484 | + self::$mTimeChars += strlen( $format ); |
| 485 | + if ( self::$mTimeChars > self::$mMaxTimeChars ) { |
468 | 486 | return '<strong class="error">' . wfMsgForContent( 'pfunc_time_too_long' ) . '</strong>'; |
469 | 487 | } else { |
470 | 488 | $result = $wgContLang->sprintfDate( $format, $ts ); |
471 | 489 | } |
472 | 490 | } |
473 | | - $this->mTimeCache[$format][$date][$local] = $result; |
| 491 | + self::$mTimeCache[$format][$date][$local] = $result; |
474 | 492 | return $result; |
475 | 493 | } |
476 | 494 | |
477 | | - function localTime( $parser, $format = '', $date = '' ) { |
478 | | - return $this->time( $parser, $format, $date, true ); |
| 495 | + public static function localTime( $parser, $format = '', $date = '' ) { |
| 496 | + return self::time( $parser, $format, $date, true ); |
479 | 497 | } |
480 | 498 | |
481 | 499 | /** |
— | — | @@ -487,7 +505,7 @@ |
488 | 506 | * @param int $offset Offset starting at 1 |
489 | 507 | * @return string |
490 | 508 | */ |
491 | | - public function titleparts( $parser, $title = '', $parts = 0, $offset = 0 ) { |
| 509 | + public static function titleparts( $parser, $title = '', $parts = 0, $offset = 0 ) { |
492 | 510 | $parts = intval( $parts ); |
493 | 511 | $offset = intval( $offset ); |
494 | 512 | $ntitle = Title::newFromText( $title ); |
— | — | @@ -514,17 +532,17 @@ |
515 | 533 | * Get a ConvertParser object |
516 | 534 | * @return ConvertParser |
517 | 535 | */ |
518 | | - protected function &getConvertParser() { |
519 | | - if ( !isset( $this->mConvertParser ) ) { |
520 | | - $this->mConvertParser = new ConvertParser; |
| 536 | + protected static function &getConvertParser() { |
| 537 | + if ( !isset( self::$mConvertParser ) ) { |
| 538 | + self::$mConvertParser = new ConvertParser; |
521 | 539 | } |
522 | | - return $this->mConvertParser; |
| 540 | + return self::$mConvertParser; |
523 | 541 | } |
524 | 542 | |
525 | | - public function convert( /*...*/ ) { |
| 543 | + public static function convert( /*...*/ ) { |
526 | 544 | try { |
527 | 545 | $args = func_get_args(); |
528 | | - return $this->getConvertParser()->execute( $args ); |
| 546 | + return self::getConvertParser()->execute( $args ); |
529 | 547 | } catch ( ConvertError $e ) { |
530 | 548 | return $e->getMessage(); |
531 | 549 | } |
— | — | @@ -537,7 +555,7 @@ |
538 | 556 | } |
539 | 557 | |
540 | 558 | // Generates error message. Called when string is too long. |
541 | | - private function tooLongError() { |
| 559 | + private static function tooLongError() { |
542 | 560 | global $wgPFStringLengthLimit, $wgContLang; |
543 | 561 | return '<strong class="error">' . |
544 | 562 | wfMsgExt( 'pfunc_string_too_long', |
— | — | @@ -551,10 +569,10 @@ |
552 | 570 | * |
553 | 571 | * Reports number of characters in string. |
554 | 572 | */ |
555 | | - function runLen ( $parser, $inStr = '' ) { |
| 573 | + public static function runLen ( $parser, $inStr = '' ) { |
556 | 574 | wfProfileIn( __METHOD__ ); |
557 | 575 | |
558 | | - $inStr = $this->killMarkers( $parser, (string)$inStr ); |
| 576 | + $inStr = self::killMarkers( $parser, (string)$inStr ); |
559 | 577 | $len = mb_strlen( $inStr ); |
560 | 578 | |
561 | 579 | wfProfileOut( __METHOD__ ); |
— | — | @@ -569,16 +587,16 @@ |
570 | 588 | * Note: If the needle is an empty string, single space is used instead. |
571 | 589 | * Note: If the needle is not found, empty string is returned. |
572 | 590 | */ |
573 | | - function runPos ( $parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) { |
| 591 | + public static function runPos ( $parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) { |
574 | 592 | wfProfileIn( __METHOD__ ); |
575 | 593 | |
576 | | - $inStr = $this->killMarkers( $parser, (string)$inStr ); |
577 | | - $inNeedle = $this->killMarkers( $parser, (string)$inNeedle ); |
| 594 | + $inStr = self::killMarkers( $parser, (string)$inStr ); |
| 595 | + $inNeedle = self::killMarkers( $parser, (string)$inNeedle ); |
578 | 596 | |
579 | | - if ( !$this->checkLength( $inStr ) || |
580 | | - !$this->checkLength( $inNeedle ) ) { |
| 597 | + if ( !self::checkLength( $inStr ) || |
| 598 | + !self::checkLength( $inNeedle ) ) { |
581 | 599 | wfProfileOut( __METHOD__ ); |
582 | | - return $this->tooLongError(); |
| 600 | + return self::tooLongError(); |
583 | 601 | } |
584 | 602 | |
585 | 603 | if ( $inNeedle == '' ) { $inNeedle = ' '; } |
— | — | @@ -598,16 +616,16 @@ |
599 | 617 | * Note: If the needle is an empty string, single space is used instead. |
600 | 618 | * Note: If the needle is not found, -1 is returned. |
601 | 619 | */ |
602 | | - function runRPos ( $parser, $inStr = '', $inNeedle = '' ) { |
| 620 | + public static function runRPos ( $parser, $inStr = '', $inNeedle = '' ) { |
603 | 621 | wfProfileIn( __METHOD__ ); |
604 | 622 | |
605 | | - $inStr = $this->killMarkers( $parser, (string)$inStr ); |
606 | | - $inNeedle = $this->killMarkers( $parser, (string)$inNeedle ); |
| 623 | + $inStr = self::killMarkers( $parser, (string)$inStr ); |
| 624 | + $inNeedle = self::killMarkers( $parser, (string)$inNeedle ); |
607 | 625 | |
608 | | - if ( !$this->checkLength( $inStr ) || |
609 | | - !$this->checkLength( $inNeedle ) ) { |
| 626 | + if ( !self::checkLength( $inStr ) || |
| 627 | + !self::checkLength( $inNeedle ) ) { |
610 | 628 | wfProfileOut( __METHOD__ ); |
611 | | - return $this->tooLongError(); |
| 629 | + return self::tooLongError(); |
612 | 630 | } |
613 | 631 | |
614 | 632 | if ( $inNeedle == '' ) { $inNeedle = ' '; } |
— | — | @@ -631,14 +649,14 @@ |
632 | 650 | * Note: A negative value for "length" returns a string reduced in |
633 | 651 | * length by that amount. |
634 | 652 | */ |
635 | | - function runSub ( $parser, $inStr = '', $inStart = 0, $inLength = 0 ) { |
| 653 | + public static function runSub ( $parser, $inStr = '', $inStart = 0, $inLength = 0 ) { |
636 | 654 | wfProfileIn( __METHOD__ ); |
637 | 655 | |
638 | | - $inStr = $this->killMarkers( $parser, (string)$inStr ); |
| 656 | + $inStr = self::killMarkers( $parser, (string)$inStr ); |
639 | 657 | |
640 | | - if ( !$this->checkLength( $inStr ) ) { |
| 658 | + if ( !self::checkLength( $inStr ) ) { |
641 | 659 | wfProfileOut( __METHOD__ ); |
642 | | - return $this->tooLongError(); |
| 660 | + return self::tooLongError(); |
643 | 661 | } |
644 | 662 | |
645 | 663 | if ( intval( $inLength ) == 0 ) { |
— | — | @@ -658,16 +676,16 @@ |
659 | 677 | * |
660 | 678 | * Note: If "substr" is empty, a single space is used. |
661 | 679 | */ |
662 | | - function runCount ( $parser, $inStr = '', $inSubStr = '' ) { |
| 680 | + public static function runCount ( $parser, $inStr = '', $inSubStr = '' ) { |
663 | 681 | wfProfileIn( __METHOD__ ); |
664 | 682 | |
665 | | - $inStr = $this->killMarkers( $parser, (string)$inStr ); |
666 | | - $inSubStr = $this->killMarkers( $parser, (string)$inSubStr ); |
| 683 | + $inStr = self::killMarkers( $parser, (string)$inStr ); |
| 684 | + $inSubStr = self::killMarkers( $parser, (string)$inSubStr ); |
667 | 685 | |
668 | | - if ( !$this->checkLength( $inStr ) || |
669 | | - !$this->checkLength( $inSubStr ) ) { |
| 686 | + if ( !self::checkLength( $inStr ) || |
| 687 | + !self::checkLength( $inSubStr ) ) { |
670 | 688 | wfProfileOut( __METHOD__ ); |
671 | | - return $this->tooLongError(); |
| 689 | + return self::tooLongError(); |
672 | 690 | } |
673 | 691 | |
674 | 692 | if ( $inSubStr == '' ) { $inSubStr = ' '; } |
— | — | @@ -687,20 +705,20 @@ |
688 | 706 | * Note: Armored against replacements that would generate huge strings. |
689 | 707 | * Note: If "from" is an empty string, single space is used instead. |
690 | 708 | */ |
691 | | - function runReplace( $parser, $inStr = '', |
| 709 | + public static function runReplace( $parser, $inStr = '', |
692 | 710 | $inReplaceFrom = '', $inReplaceTo = '', $inLimit = -1 ) { |
693 | 711 | global $wgPFStringLengthLimit; |
694 | 712 | wfProfileIn( __METHOD__ ); |
695 | 713 | |
696 | | - $inStr = $this->killMarkers( $parser, (string)$inStr ); |
697 | | - $inReplaceFrom = $this->killMarkers( $parser, (string)$inReplaceFrom ); |
698 | | - $inReplaceTo = $this->killMarkers( $parser, (string)$inReplaceTo ); |
| 714 | + $inStr = self::killMarkers( $parser, (string)$inStr ); |
| 715 | + $inReplaceFrom = self::killMarkers( $parser, (string)$inReplaceFrom ); |
| 716 | + $inReplaceTo = self::killMarkers( $parser, (string)$inReplaceTo ); |
699 | 717 | |
700 | | - if ( !$this->checkLength( $inStr ) || |
701 | | - !$this->checkLength( $inReplaceFrom ) || |
702 | | - !$this->checkLength( $inReplaceTo ) ) { |
| 718 | + if ( !self::checkLength( $inStr ) || |
| 719 | + !self::checkLength( $inReplaceFrom ) || |
| 720 | + !self::checkLength( $inReplaceTo ) ) { |
703 | 721 | wfProfileOut( __METHOD__ ); |
704 | | - return $this->tooLongError(); |
| 722 | + return self::tooLongError(); |
705 | 723 | } |
706 | 724 | |
707 | 725 | if ( $inReplaceFrom == '' ) { $inReplaceFrom = ' '; } |
— | — | @@ -725,9 +743,9 @@ |
726 | 744 | $result = preg_replace( '/' . $inReplaceFrom . '/u', |
727 | 745 | $inReplaceTo, $inStr, $limit ); |
728 | 746 | |
729 | | - if ( !$this->checkLength( $result ) ) { |
| 747 | + if ( !self::checkLength( $result ) ) { |
730 | 748 | wfProfileOut( __METHOD__ ); |
731 | | - return $this->tooLongError(); |
| 749 | + return self::tooLongError(); |
732 | 750 | } |
733 | 751 | |
734 | 752 | wfProfileOut( __METHOD__ ); |
— | — | @@ -745,18 +763,18 @@ |
746 | 764 | * Note: If the divider is an empty string, single space is used instead. |
747 | 765 | * Note: Empty string is returned if there are not enough exploded chunks. |
748 | 766 | */ |
749 | | - function runExplode ( $parser, $inStr = '', $inDiv = '', $inPos = 0, $inLim = null ) { |
| 767 | + public static function runExplode ( $parser, $inStr = '', $inDiv = '', $inPos = 0, $inLim = null ) { |
750 | 768 | wfProfileIn( __METHOD__ ); |
751 | 769 | |
752 | | - $inStr = $this->killMarkers( $parser, (string)$inStr ); |
753 | | - $inDiv = $this->killMarkers( $parser, (string)$inDiv ); |
| 770 | + $inStr = self::killMarkers( $parser, (string)$inStr ); |
| 771 | + $inDiv = self::killMarkers( $parser, (string)$inDiv ); |
754 | 772 | |
755 | 773 | if ( $inDiv == '' ) { $inDiv = ' '; } |
756 | 774 | |
757 | | - if ( !$this->checkLength( $inStr ) || |
758 | | - !$this->checkLength( $inDiv ) ) { |
| 775 | + if ( !self::checkLength( $inStr ) || |
| 776 | + !self::checkLength( $inDiv ) ) { |
759 | 777 | wfProfileOut( __METHOD__ ); |
760 | | - return $this->tooLongError(); |
| 778 | + return self::tooLongError(); |
761 | 779 | } |
762 | 780 | |
763 | 781 | $inDiv = preg_quote( $inDiv, '/' ); |
— | — | @@ -780,13 +798,13 @@ |
781 | 799 | * |
782 | 800 | * Decodes URL-encoded (like%20that) strings. |
783 | 801 | */ |
784 | | - function runUrlDecode( $parser, $inStr = '' ) { |
| 802 | + public static function runUrlDecode( $parser, $inStr = '' ) { |
785 | 803 | wfProfileIn( __METHOD__ ); |
786 | 804 | |
787 | | - $inStr = $this->killMarkers( $parser, (string)$inStr ); |
788 | | - if ( !$this->checkLength( $inStr ) ) { |
| 805 | + $inStr = self::killMarkers( $parser, (string)$inStr ); |
| 806 | + if ( !self::checkLength( $inStr ) ) { |
789 | 807 | wfProfileOut( __METHOD__ ); |
790 | | - return $this->tooLongError(); |
| 808 | + return self::tooLongError(); |
791 | 809 | } |
792 | 810 | |
793 | 811 | $result = urldecode( $inStr ); |
Index: trunk/extensions/ParserFunctions/ParserFunctions.php |
— | — | @@ -30,7 +30,6 @@ |
31 | 31 | $wgPFEnableStringFunctions = false; |
32 | 32 | |
33 | 33 | /** REGISTRATION */ |
34 | | -$wgExtensionFunctions[] = 'wfSetupParserFunctions'; |
35 | 34 | $wgExtensionCredits['parserhook'][] = array( |
36 | 35 | 'path' => __FILE__, |
37 | 36 | 'name' => 'ParserFunctions', |
— | — | @@ -51,79 +50,46 @@ |
52 | 51 | $wgParserTestFiles[] = dirname( __FILE__ ) . "/stringFunctionTests.txt"; |
53 | 52 | $wgParserTestFiles[] = dirname( __FILE__ ) . "/convertTests.txt"; |
54 | 53 | |
55 | | -function wfSetupParserFunctions() { |
56 | | - global $wgPFHookStub, $wgHooks; |
| 54 | +$wgHooks['ParserFirstCallInit'][] = 'wfRegisterParserFunctions'; |
57 | 55 | |
58 | | - $wgPFHookStub = new ParserFunctions_HookStub; |
| 56 | +function wfRegisterParserFunctions( $parser ) { |
| 57 | + global $wgPFEnableStringFunctions; |
59 | 58 | |
60 | | - $wgHooks['ParserFirstCallInit'][] = array( &$wgPFHookStub, 'registerParser' ); |
61 | | - |
62 | | - $wgHooks['ParserClearState'][] = array( &$wgPFHookStub, 'clearState' ); |
63 | | -} |
64 | | - |
65 | | -/** |
66 | | - * Stub class to defer loading of the bulk of the code until a parser function is |
67 | | - * actually used. |
68 | | - */ |
69 | | -class ParserFunctions_HookStub { |
70 | | - var $realObj; |
71 | | - |
72 | | - function registerParser( $parser ) { |
73 | | - global $wgPFEnableStringFunctions; |
74 | | - |
75 | | - if ( defined( get_class( $parser ) . '::SFH_OBJECT_ARGS' ) ) { |
76 | | - // These functions accept DOM-style arguments |
77 | | - $parser->setFunctionHook( 'if', array( &$this, 'ifObj' ), SFH_OBJECT_ARGS ); |
78 | | - $parser->setFunctionHook( 'ifeq', array( &$this, 'ifeqObj' ), SFH_OBJECT_ARGS ); |
79 | | - $parser->setFunctionHook( 'switch', array( &$this, 'switchObj' ), SFH_OBJECT_ARGS ); |
80 | | - $parser->setFunctionHook( 'ifexist', array( &$this, 'ifexistObj' ), SFH_OBJECT_ARGS ); |
81 | | - $parser->setFunctionHook( 'ifexpr', array( &$this, 'ifexprObj' ), SFH_OBJECT_ARGS ); |
82 | | - $parser->setFunctionHook( 'iferror', array( &$this, 'iferrorObj' ), SFH_OBJECT_ARGS ); |
83 | | - } else { |
84 | | - $parser->setFunctionHook( 'if', array( &$this, 'ifHook' ) ); |
85 | | - $parser->setFunctionHook( 'ifeq', array( &$this, 'ifeq' ) ); |
86 | | - $parser->setFunctionHook( 'switch', array( &$this, 'switchHook' ) ); |
87 | | - $parser->setFunctionHook( 'ifexist', array( &$this, 'ifexist' ) ); |
88 | | - $parser->setFunctionHook( 'ifexpr', array( &$this, 'ifexpr' ) ); |
89 | | - $parser->setFunctionHook( 'iferror', array( &$this, 'iferror' ) ); |
90 | | - } |
91 | | - |
92 | | - $parser->setFunctionHook( 'expr', array( &$this, 'expr' ) ); |
93 | | - $parser->setFunctionHook( 'time', array( &$this, 'time' ) ); |
94 | | - $parser->setFunctionHook( 'timel', array( &$this, 'localTime' ) ); |
95 | | - $parser->setFunctionHook( 'rel2abs', array( &$this, 'rel2abs' ) ); |
96 | | - $parser->setFunctionHook( 'titleparts', array( &$this, 'titleparts' ) ); |
97 | | - $parser->setFunctionHook( 'convert', array( &$this, 'convert' ) ); |
98 | | - |
99 | | - // String Functions |
100 | | - if ( $wgPFEnableStringFunctions ) { |
101 | | - $parser->setFunctionHook( 'len', array( &$this, 'runLen' ) ); |
102 | | - $parser->setFunctionHook( 'pos', array( &$this, 'runPos' ) ); |
103 | | - $parser->setFunctionHook( 'rpos', array( &$this, 'runRPos' ) ); |
104 | | - $parser->setFunctionHook( 'sub', array( &$this, 'runSub' ) ); |
105 | | - $parser->setFunctionHook( 'count', array( &$this, 'runCount' ) ); |
106 | | - $parser->setFunctionHook( 'replace', array( &$this, 'runReplace' ) ); |
107 | | - $parser->setFunctionHook( 'explode', array( &$this, 'runExplode' ) ); |
108 | | - $parser->setFunctionHook( 'urldecode', array( &$this, 'runUrlDecode' ) ); |
109 | | - } |
110 | | - |
111 | | - return true; |
| 59 | + if ( defined( get_class( $parser ) . '::SFH_OBJECT_ARGS' ) ) { |
| 60 | + // These functions accept DOM-style arguments |
| 61 | + $parser->setFunctionHook( 'if', 'ExtParserFunctions::ifObj', SFH_OBJECT_ARGS ); |
| 62 | + $parser->setFunctionHook( 'ifeq', 'ExtParserFunctions::ifeqObj', SFH_OBJECT_ARGS ); |
| 63 | + $parser->setFunctionHook( 'switch', 'ExtParserFunctions::switchObj', SFH_OBJECT_ARGS ); |
| 64 | + $parser->setFunctionHook( 'ifexist', 'ExtParserFunctions::ifexistObj', SFH_OBJECT_ARGS ); |
| 65 | + $parser->setFunctionHook( 'ifexpr', 'ExtParserFunctions::ifexprObj', SFH_OBJECT_ARGS ); |
| 66 | + $parser->setFunctionHook( 'iferror', 'ExtParserFunctions::iferrorObj', SFH_OBJECT_ARGS ); |
| 67 | + } else { |
| 68 | + $parser->setFunctionHook( 'if', 'ExtParserFunctions::ifHook' ); |
| 69 | + $parser->setFunctionHook( 'ifeq', 'ExtParserFunctions::ifeq' ); |
| 70 | + $parser->setFunctionHook( 'switch', 'ExtParserFunctions::switchHook' ); |
| 71 | + $parser->setFunctionHook( 'ifexist', 'ExtParserFunctions::ifexist' ); |
| 72 | + $parser->setFunctionHook( 'ifexpr', 'ExtParserFunctions::ifexpr' ); |
| 73 | + $parser->setFunctionHook( 'iferror', 'ExtParserFunctions::iferror' ); |
112 | 74 | } |
113 | 75 | |
114 | | - /** Defer ParserClearState */ |
115 | | - function clearState( $parser ) { |
116 | | - if ( !is_null( $this->realObj ) ) { |
117 | | - $this->realObj->clearState( $parser ); |
118 | | - } |
119 | | - return true; |
120 | | - } |
| 76 | + $parser->setFunctionHook( 'expr', 'ExtParserFunctions::expr' ); |
| 77 | + $parser->setFunctionHook( 'time', 'ExtParserFunctions::time' ); |
| 78 | + $parser->setFunctionHook( 'timel', 'ExtParserFunctions::localTime' ); |
| 79 | + $parser->setFunctionHook( 'rel2abs', 'ExtParserFunctions::rel2abs' ); |
| 80 | + $parser->setFunctionHook( 'titleparts', 'ExtParserFunctions::titleparts' ); |
| 81 | + $parser->setFunctionHook( 'convert', 'ExtParserFunctions::convert' ); |
121 | 82 | |
122 | | - /** Pass through function call */ |
123 | | - function __call( $name, $args ) { |
124 | | - if ( is_null( $this->realObj ) ) { |
125 | | - $this->realObj = new ExtParserFunctions; |
126 | | - $this->realObj->clearState( $args[0] ); |
127 | | - } |
128 | | - return call_user_func_array( array( $this->realObj, $name ), $args ); |
| 83 | + // String Functions |
| 84 | + if ( $wgPFEnableStringFunctions ) { |
| 85 | + $parser->setFunctionHook( 'len', 'ExtParserFunctions::runLen' ); |
| 86 | + $parser->setFunctionHook( 'pos', 'ExtParserFunctions::runPos' ); |
| 87 | + $parser->setFunctionHook( 'rpos', 'ExtParserFunctions::runRPos' ); |
| 88 | + $parser->setFunctionHook( 'sub', 'ExtParserFunctions::runSub' ); |
| 89 | + $parser->setFunctionHook( 'count', 'ExtParserFunctions::runCount' ); |
| 90 | + $parser->setFunctionHook( 'replace', 'ExtParserFunctions::runReplace' ); |
| 91 | + $parser->setFunctionHook( 'explode', 'ExtParserFunctions::runExplode' ); |
| 92 | + $parser->setFunctionHook( 'urldecode', 'ExtParserFunctions::runUrlDecode' ); |
129 | 93 | } |
| 94 | + |
| 95 | + return true; |
130 | 96 | } |