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