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