More return type documentation
[lhc/web/wiklou.git] / includes / MagicWord.php
1 <?php
2 /**
3 * File for magic words
4 *
5 * See docs/magicword.txt
6 *
7 * @file
8 * @ingroup Parser
9 */
10
11 /**
12 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
13 * Usage:
14 * if (MagicWord::get( 'redirect' )->match( $text ) )
15 *
16 * Possible future improvements:
17 * * Simultaneous searching for a number of magic words
18 * * MagicWord::$mObjects in shared memory
19 *
20 * Please avoid reading the data out of one of these objects and then writing
21 * special case code. If possible, add another match()-like function here.
22 *
23 * To add magic words in an extension, use the LanguageGetMagic hook. For
24 * magic words which are also Parser variables, add a MagicWordwgVariableIDs
25 * hook. Use string keys.
26 *
27 * @ingroup Parser
28 */
29 class MagicWord {
30 /**#@+
31 * @private
32 */
33 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
34 var $mRegexStart, $mBaseRegex, $mVariableRegex;
35 var $mModified, $mFound;
36
37 static public $mVariableIDsInitialised = false;
38 static public $mVariableIDs = array(
39 'currentmonth',
40 'currentmonth1',
41 'currentmonthname',
42 'currentmonthnamegen',
43 'currentmonthabbrev',
44 'currentday',
45 'currentday2',
46 'currentdayname',
47 'currentyear',
48 'currenttime',
49 'currenthour',
50 'localmonth',
51 'localmonth1',
52 'localmonthname',
53 'localmonthnamegen',
54 'localmonthabbrev',
55 'localday',
56 'localday2',
57 'localdayname',
58 'localyear',
59 'localtime',
60 'localhour',
61 'numberofarticles',
62 'numberoffiles',
63 'numberofedits',
64 'articlepath',
65 'sitename',
66 'server',
67 'servername',
68 'scriptpath',
69 'stylepath',
70 'pagename',
71 'pagenamee',
72 'fullpagename',
73 'fullpagenamee',
74 'namespace',
75 'namespacee',
76 'currentweek',
77 'currentdow',
78 'localweek',
79 'localdow',
80 'revisionid',
81 'revisionday',
82 'revisionday2',
83 'revisionmonth',
84 'revisionmonth1',
85 'revisionyear',
86 'revisiontimestamp',
87 'revisionuser',
88 'subpagename',
89 'subpagenamee',
90 'talkspace',
91 'talkspacee',
92 'subjectspace',
93 'subjectspacee',
94 'talkpagename',
95 'talkpagenamee',
96 'subjectpagename',
97 'subjectpagenamee',
98 'numberofusers',
99 'numberofactiveusers',
100 'numberofpages',
101 'currentversion',
102 'basepagename',
103 'basepagenamee',
104 'currenttimestamp',
105 'localtimestamp',
106 'directionmark',
107 'contentlanguage',
108 'numberofadmins',
109 'numberofviews',
110 );
111
112 /* Array of caching hints for ParserCache */
113 static public $mCacheTTLs = array (
114 'currentmonth' => 86400,
115 'currentmonth1' => 86400,
116 'currentmonthname' => 86400,
117 'currentmonthnamegen' => 86400,
118 'currentmonthabbrev' => 86400,
119 'currentday' => 3600,
120 'currentday2' => 3600,
121 'currentdayname' => 3600,
122 'currentyear' => 86400,
123 'currenttime' => 3600,
124 'currenthour' => 3600,
125 'localmonth' => 86400,
126 'localmonth1' => 86400,
127 'localmonthname' => 86400,
128 'localmonthnamegen' => 86400,
129 'localmonthabbrev' => 86400,
130 'localday' => 3600,
131 'localday2' => 3600,
132 'localdayname' => 3600,
133 'localyear' => 86400,
134 'localtime' => 3600,
135 'localhour' => 3600,
136 'numberofarticles' => 3600,
137 'numberoffiles' => 3600,
138 'numberofedits' => 3600,
139 'currentweek' => 3600,
140 'currentdow' => 3600,
141 'localweek' => 3600,
142 'localdow' => 3600,
143 'numberofusers' => 3600,
144 'numberofactiveusers' => 3600,
145 'numberofpages' => 3600,
146 'currentversion' => 86400,
147 'currenttimestamp' => 3600,
148 'localtimestamp' => 3600,
149 'pagesinnamespace' => 3600,
150 'numberofadmins' => 3600,
151 'numberofviews' => 3600,
152 'numberingroup' => 3600,
153 );
154
155 static public $mDoubleUnderscoreIDs = array(
156 'notoc',
157 'nogallery',
158 'forcetoc',
159 'toc',
160 'noeditsection',
161 'newsectionlink',
162 'nonewsectionlink',
163 'hiddencat',
164 'index',
165 'noindex',
166 'staticredirect',
167 'notitleconvert',
168 'nocontentconvert',
169 );
170
171 static public $mSubstIDs = array(
172 'subst',
173 'safesubst',
174 );
175
176 static public $mObjects = array();
177 static public $mDoubleUnderscoreArray = null;
178
179 /**#@-*/
180
181 function __construct($id = 0, $syn = '', $cs = false) {
182 $this->mId = $id;
183 $this->mSynonyms = (array)$syn;
184 $this->mCaseSensitive = $cs;
185 $this->mRegex = '';
186 $this->mRegexStart = '';
187 $this->mVariableRegex = '';
188 $this->mVariableStartToEndRegex = '';
189 $this->mModified = false;
190 }
191
192 /**
193 * Factory: creates an object representing an ID
194 * @static
195 * @return MagicWord
196 */
197 static function &get( $id ) {
198 wfProfileIn( __METHOD__ );
199 if ( !isset( self::$mObjects[$id] ) ) {
200 $mw = new MagicWord();
201 $mw->load( $id );
202 self::$mObjects[$id] = $mw;
203 }
204 wfProfileOut( __METHOD__ );
205 return self::$mObjects[$id];
206 }
207
208 /**
209 * Get an array of parser variable IDs
210 */
211 static function getVariableIDs() {
212 if ( !self::$mVariableIDsInitialised ) {
213 # Deprecated constant definition hook, available for extensions that need it
214 $magicWords = array();
215 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
216 foreach ( $magicWords as $word ) {
217 define( $word, $word );
218 }
219
220 # Get variable IDs
221 wfRunHooks( 'MagicWordwgVariableIDs', array( &self::$mVariableIDs ) );
222 self::$mVariableIDsInitialised = true;
223 }
224 return self::$mVariableIDs;
225 }
226
227 /**
228 * Get an array of parser substitution modifier IDs
229 */
230 static function getSubstIDs() {
231 return self::$mSubstIDs;
232 }
233
234 /* Allow external reads of TTL array */
235 static function getCacheTTL($id) {
236 if (array_key_exists($id,self::$mCacheTTLs)) {
237 return self::$mCacheTTLs[$id];
238 } else {
239 return -1;
240 }
241 }
242
243 /** Get a MagicWordArray of double-underscore entities */
244 static function getDoubleUnderscoreArray() {
245 if ( is_null( self::$mDoubleUnderscoreArray ) ) {
246 self::$mDoubleUnderscoreArray = new MagicWordArray( self::$mDoubleUnderscoreIDs );
247 }
248 return self::$mDoubleUnderscoreArray;
249 }
250
251 /**
252 * Clear the self::$mObjects variable
253 * For use in parser tests
254 */
255 public static function clearCache() {
256 self::$mObjects = array();
257 }
258
259 # Initialises this object with an ID
260 function load( $id ) {
261 global $wgContLang;
262 $this->mId = $id;
263 $wgContLang->getMagic( $this );
264 if ( !$this->mSynonyms ) {
265 $this->mSynonyms = array( 'dkjsagfjsgashfajsh' );
266 #throw new MWException( "Error: invalid magic word '$id'" );
267 wfDebugLog( 'exception', "Error: invalid magic word '$id'\n" );
268 }
269 }
270
271 /**
272 * Preliminary initialisation
273 * @private
274 */
275 function initRegex() {
276 #$variableClass = Title::legalChars();
277 # This was used for matching "$1" variables, but different uses of the feature will have
278 # different restrictions, which should be checked *after* the MagicWord has been matched,
279 # not here. - IMSoP
280
281 $escSyn = array();
282 foreach ( $this->mSynonyms as $synonym )
283 // In case a magic word contains /, like that's going to happen;)
284 $escSyn[] = preg_quote( $synonym, '/' );
285 $this->mBaseRegex = implode( '|', $escSyn );
286
287 $case = $this->mCaseSensitive ? '' : 'iu';
288 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
289 $this->mRegexStart = "/^(?:{$this->mBaseRegex})/{$case}";
290 $this->mVariableRegex = str_replace( "\\$1", "(.*?)", $this->mRegex );
291 $this->mVariableStartToEndRegex = str_replace( "\\$1", "(.*?)",
292 "/^(?:{$this->mBaseRegex})$/{$case}" );
293 }
294
295 /**
296 * Gets a regex representing matching the word
297 */
298 function getRegex() {
299 if ($this->mRegex == '' ) {
300 $this->initRegex();
301 }
302 return $this->mRegex;
303 }
304
305 /**
306 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
307 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
308 * the complete expression
309 */
310 function getRegexCase() {
311 if ( $this->mRegex === '' )
312 $this->initRegex();
313
314 return $this->mCaseSensitive ? '' : 'iu';
315 }
316
317 /**
318 * Gets a regex matching the word, if it is at the string start
319 */
320 function getRegexStart() {
321 if ($this->mRegex == '' ) {
322 $this->initRegex();
323 }
324 return $this->mRegexStart;
325 }
326
327 /**
328 * regex without the slashes and what not
329 */
330 function getBaseRegex() {
331 if ($this->mRegex == '') {
332 $this->initRegex();
333 }
334 return $this->mBaseRegex;
335 }
336
337 /**
338 * Returns true if the text contains the word
339 * @return bool
340 */
341 function match( $text ) {
342 return (bool)preg_match( $this->getRegex(), $text );
343 }
344
345 /**
346 * Returns true if the text starts with the word
347 * @return bool
348 */
349 function matchStart( $text ) {
350 return (bool)preg_match( $this->getRegexStart(), $text );
351 }
352
353 /**
354 * Returns NULL if there's no match, the value of $1 otherwise
355 * The return code is the matched string, if there's no variable
356 * part in the regex and the matched variable part ($1) if there
357 * is one.
358 */
359 function matchVariableStartToEnd( $text ) {
360 $matches = array();
361 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
362 if ( $matchcount == 0 ) {
363 return null;
364 } else {
365 # multiple matched parts (variable match); some will be empty because of
366 # synonyms. The variable will be the second non-empty one so remove any
367 # blank elements and re-sort the indices.
368 # See also bug 6526
369
370 $matches = array_values(array_filter($matches));
371
372 if ( count($matches) == 1 ) { return $matches[0]; }
373 else { return $matches[1]; }
374 }
375 }
376
377
378 /**
379 * Returns true if the text matches the word, and alters the
380 * input string, removing all instances of the word
381 */
382 function matchAndRemove( &$text ) {
383 $this->mFound = false;
384 $text = preg_replace_callback( $this->getRegex(), array( &$this, 'pregRemoveAndRecord' ), $text );
385 return $this->mFound;
386 }
387
388 function matchStartAndRemove( &$text ) {
389 $this->mFound = false;
390 $text = preg_replace_callback( $this->getRegexStart(), array( &$this, 'pregRemoveAndRecord' ), $text );
391 return $this->mFound;
392 }
393
394 /**
395 * Used in matchAndRemove()
396 * @private
397 **/
398 function pregRemoveAndRecord( ) {
399 $this->mFound = true;
400 return '';
401 }
402
403 /**
404 * Replaces the word with something else
405 */
406 function replace( $replacement, $subject, $limit=-1 ) {
407 $res = preg_replace( $this->getRegex(), StringUtils::escapeRegexReplacement( $replacement ), $subject, $limit );
408 $this->mModified = !($res === $subject);
409 return $res;
410 }
411
412 /**
413 * Variable handling: {{SUBST:xxx}} style words
414 * Calls back a function to determine what to replace xxx with
415 * Input word must contain $1
416 */
417 function substituteCallback( $text, $callback ) {
418 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
419 $this->mModified = !($res === $text);
420 return $res;
421 }
422
423 /**
424 * Matches the word, where $1 is a wildcard
425 */
426 function getVariableRegex() {
427 if ( $this->mVariableRegex == '' ) {
428 $this->initRegex();
429 }
430 return $this->mVariableRegex;
431 }
432
433 /**
434 * Matches the entire string, where $1 is a wildcard
435 */
436 function getVariableStartToEndRegex() {
437 if ( $this->mVariableStartToEndRegex == '' ) {
438 $this->initRegex();
439 }
440 return $this->mVariableStartToEndRegex;
441 }
442
443 /**
444 * Accesses the synonym list directly
445 */
446 function getSynonym( $i ) {
447 return $this->mSynonyms[$i];
448 }
449
450 function getSynonyms() {
451 return $this->mSynonyms;
452 }
453
454 /**
455 * Returns true if the last call to replace() or substituteCallback()
456 * returned a modified text, otherwise false.
457 */
458 function getWasModified(){
459 return $this->mModified;
460 }
461
462 /**
463 * $magicarr is an associative array of (magic word ID => replacement)
464 * This method uses the php feature to do several replacements at the same time,
465 * thereby gaining some efficiency. The result is placed in the out variable
466 * $result. The return value is true if something was replaced.
467 * @static
468 **/
469 function replaceMultiple( $magicarr, $subject, &$result ){
470 $search = array();
471 $replace = array();
472 foreach( $magicarr as $id => $replacement ){
473 $mw = MagicWord::get( $id );
474 $search[] = $mw->getRegex();
475 $replace[] = $replacement;
476 }
477
478 $result = preg_replace( $search, $replace, $subject );
479 return !($result === $subject);
480 }
481
482 /**
483 * Adds all the synonyms of this MagicWord to an array, to allow quick
484 * lookup in a list of magic words
485 */
486 function addToArray( &$array, $value ) {
487 global $wgContLang;
488 foreach ( $this->mSynonyms as $syn ) {
489 $array[$wgContLang->lc($syn)] = $value;
490 }
491 }
492
493 function isCaseSensitive() {
494 return $this->mCaseSensitive;
495 }
496
497 function getId() {
498 return $this->mId;
499 }
500 }
501
502 /**
503 * Class for handling an array of magic words
504 * @ingroup Parser
505 */
506 class MagicWordArray {
507 var $names = array();
508 var $hash;
509 var $baseRegex, $regex;
510 var $matches;
511
512 function __construct( $names = array() ) {
513 $this->names = $names;
514 }
515
516 /**
517 * Add a magic word by name
518 */
519 public function add( $name ) {
520 $this->names[] = $name;
521 $this->hash = $this->baseRegex = $this->regex = null;
522 }
523
524 /**
525 * Add a number of magic words by name
526 */
527 public function addArray( $names ) {
528 $this->names = array_merge( $this->names, array_values( $names ) );
529 $this->hash = $this->baseRegex = $this->regex = null;
530 }
531
532 /**
533 * Get a 2-d hashtable for this array
534 */
535 function getHash() {
536 if ( is_null( $this->hash ) ) {
537 global $wgContLang;
538 $this->hash = array( 0 => array(), 1 => array() );
539 foreach ( $this->names as $name ) {
540 $magic = MagicWord::get( $name );
541 $case = intval( $magic->isCaseSensitive() );
542 foreach ( $magic->getSynonyms() as $syn ) {
543 if ( !$case ) {
544 $syn = $wgContLang->lc( $syn );
545 }
546 $this->hash[$case][$syn] = $name;
547 }
548 }
549 }
550 return $this->hash;
551 }
552
553 /**
554 * Get the base regex
555 */
556 function getBaseRegex() {
557 if ( is_null( $this->baseRegex ) ) {
558 $this->baseRegex = array( 0 => '', 1 => '' );
559 foreach ( $this->names as $name ) {
560 $magic = MagicWord::get( $name );
561 $case = intval( $magic->isCaseSensitive() );
562 foreach ( $magic->getSynonyms() as $i => $syn ) {
563 $group = "(?P<{$i}_{$name}>" . preg_quote( $syn, '/' ) . ')';
564 if ( $this->baseRegex[$case] === '' ) {
565 $this->baseRegex[$case] = $group;
566 } else {
567 $this->baseRegex[$case] .= '|' . $group;
568 }
569 }
570 }
571 }
572 return $this->baseRegex;
573 }
574
575 /**
576 * Get an unanchored regex that does not match parameters
577 */
578 function getRegex() {
579 if ( is_null( $this->regex ) ) {
580 $base = $this->getBaseRegex();
581 $this->regex = array( '', '' );
582 if ( $this->baseRegex[0] !== '' ) {
583 $this->regex[0] = "/{$base[0]}/iuS";
584 }
585 if ( $this->baseRegex[1] !== '' ) {
586 $this->regex[1] = "/{$base[1]}/S";
587 }
588 }
589 return $this->regex;
590 }
591
592 /**
593 * Get a regex for matching variables with parameters
594 */
595 function getVariableRegex() {
596 return str_replace( "\\$1", "(.*?)", $this->getRegex() );
597 }
598
599 /**
600 * Get a regex anchored to the start of the string that does not match parameters
601 */
602 function getRegexStart() {
603 $base = $this->getBaseRegex();
604 $newRegex = array( '', '' );
605 if ( $base[0] !== '' ) {
606 $newRegex[0] = "/^(?:{$base[0]})/iuS";
607 }
608 if ( $base[1] !== '' ) {
609 $newRegex[1] = "/^(?:{$base[1]})/S";
610 }
611 return $newRegex;
612 }
613
614 /**
615 * Get an anchored regex for matching variables with parameters
616 */
617 function getVariableStartToEndRegex() {
618 $base = $this->getBaseRegex();
619 $newRegex = array( '', '' );
620 if ( $base[0] !== '' ) {
621 $newRegex[0] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[0]})$/iuS" );
622 }
623 if ( $base[1] !== '' ) {
624 $newRegex[1] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[1]})$/S" );
625 }
626 return $newRegex;
627 }
628
629 /**
630 * Parse a match array from preg_match
631 * Returns array(magic word ID, parameter value)
632 * If there is no parameter value, that element will be false.
633 */
634 function parseMatch( $m ) {
635 reset( $m );
636 while ( list( $key, $value ) = each( $m ) ) {
637 if ( $key === 0 || $value === '' ) {
638 continue;
639 }
640 $parts = explode( '_', $key, 2 );
641 if ( count( $parts ) != 2 ) {
642 // This shouldn't happen
643 // continue;
644 throw new MWException( __METHOD__ . ': bad parameter name' );
645 }
646 list( /* $synIndex */, $magicName ) = $parts;
647 $paramValue = next( $m );
648 return array( $magicName, $paramValue );
649 }
650 // This shouldn't happen either
651 throw new MWException( __METHOD__.': parameter not found' );
652 }
653
654 /**
655 * Match some text, with parameter capture
656 * Returns an array with the magic word name in the first element and the
657 * parameter in the second element.
658 * Both elements are false if there was no match.
659 */
660 public function matchVariableStartToEnd( $text ) {
661 $regexes = $this->getVariableStartToEndRegex();
662 foreach ( $regexes as $regex ) {
663 if ( $regex !== '' ) {
664 $m = false;
665 if ( preg_match( $regex, $text, $m ) ) {
666 return $this->parseMatch( $m );
667 }
668 }
669 }
670 return array( false, false );
671 }
672
673 /**
674 * Match some text, without parameter capture
675 * Returns the magic word name, or false if there was no capture
676 */
677 public function matchStartToEnd( $text ) {
678 $hash = $this->getHash();
679 if ( isset( $hash[1][$text] ) ) {
680 return $hash[1][$text];
681 }
682 global $wgContLang;
683 $lc = $wgContLang->lc( $text );
684 if ( isset( $hash[0][$lc] ) ) {
685 return $hash[0][$lc];
686 }
687 return false;
688 }
689
690 /**
691 * Returns an associative array, ID => param value, for all items that match
692 * Removes the matched items from the input string (passed by reference)
693 */
694 public function matchAndRemove( &$text ) {
695 $found = array();
696 $regexes = $this->getRegex();
697 foreach ( $regexes as $regex ) {
698 if ( $regex === '' ) {
699 continue;
700 }
701 preg_match_all( $regex, $text, $matches, PREG_SET_ORDER );
702 foreach ( $matches as $m ) {
703 list( $name, $param ) = $this->parseMatch( $m );
704 $found[$name] = $param;
705 }
706 $text = preg_replace( $regex, '', $text );
707 }
708 return $found;
709 }
710
711 /**
712 * Return the ID of the magic word at the start of $text, and remove
713 * the prefix from $text.
714 * Return false if no match found and $text is not modified.
715 * Does not match parameters.
716 */
717 public function matchStartAndRemove( &$text ) {
718 $regexes = $this->getRegexStart();
719 foreach ( $regexes as $regex ) {
720 if ( $regex === '' ) {
721 continue;
722 }
723 if ( preg_match( $regex, $text, $m ) ) {
724 list( $id, ) = $this->parseMatch( $m );
725 if ( strlen( $m[0] ) >= strlen( $text ) ) {
726 $text = '';
727 } else {
728 $text = substr( $text, strlen( $m[0] ) );
729 }
730 return $id;
731 }
732 }
733 return false;
734 }
735 }