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