Localisation updates for core messages from translatewiki.net (2009-02-20 19:30 UTC)
[mediawiki.git] / includes / MagicWord.php
blobfcd6b0343d6bf794122773899ed22923b1415aac
1 <?php
2 /**
3 * File for magic words
4 * See docs/magicword.txt
6 * @file
7 * @ingroup Parser
8 */
10 /**
11 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
12 * Usage:
13 * if (MagicWord::get( 'redirect' )->match( $text ) )
15 * Possible future improvements:
16 * * Simultaneous searching for a number of magic words
17 * * MagicWord::$mObjects in shared memory
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.
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.
26 * @ingroup Parser
28 class MagicWord {
29 /**#@+
30 * @private
32 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
33 var $mRegexStart, $mBaseRegex, $mVariableRegex;
34 var $mModified, $mFound;
36 static public $mVariableIDsInitialised = false;
37 static public $mVariableIDs = array(
38 'currentmonth',
39 'currentmonthname',
40 'currentmonthnamegen',
41 'currentmonthabbrev',
42 'currentday',
43 'currentday2',
44 'currentdayname',
45 'currentyear',
46 'currenttime',
47 'currenthour',
48 'localmonth',
49 'localmonthname',
50 'localmonthnamegen',
51 'localmonthabbrev',
52 'localday',
53 'localday2',
54 'localdayname',
55 'localyear',
56 'localtime',
57 'localhour',
58 'numberofarticles',
59 'numberoffiles',
60 'numberofedits',
61 'sitename',
62 'server',
63 'servername',
64 'scriptpath',
65 'pagename',
66 'pagenamee',
67 'fullpagename',
68 'fullpagenamee',
69 'namespace',
70 'namespacee',
71 'currentweek',
72 'currentdow',
73 'localweek',
74 'localdow',
75 'revisionid',
76 'revisionday',
77 'revisionday2',
78 'revisionmonth',
79 'revisionyear',
80 'revisiontimestamp',
81 'subpagename',
82 'subpagenamee',
83 'displaytitle',
84 'talkspace',
85 'talkspacee',
86 'subjectspace',
87 'subjectspacee',
88 'talkpagename',
89 'talkpagenamee',
90 'subjectpagename',
91 'subjectpagenamee',
92 'numberofusers',
93 'numberofactiveusers',
94 'newsectionlink',
95 'nonewsectionlink',
96 'numberofpages',
97 'currentversion',
98 'basepagename',
99 'basepagenamee',
100 'urlencode',
101 'currenttimestamp',
102 'localtimestamp',
103 'directionmark',
104 'language',
105 'contentlanguage',
106 'pagesinnamespace',
107 'numberofadmins',
108 'numberofviews',
109 'defaultsort',
110 'pagesincategory',
111 'index',
112 'noindex',
113 'numberingroup',
116 /* Array of caching hints for ParserCache */
117 static public $mCacheTTLs = array (
118 'currentmonth' => 86400,
119 'currentmonthname' => 86400,
120 'currentmonthnamegen' => 86400,
121 'currentmonthabbrev' => 86400,
122 'currentday' => 3600,
123 'currentday2' => 3600,
124 'currentdayname' => 3600,
125 'currentyear' => 86400,
126 'currenttime' => 3600,
127 'currenthour' => 3600,
128 'localmonth' => 86400,
129 'localmonthname' => 86400,
130 'localmonthnamegen' => 86400,
131 'localmonthabbrev' => 86400,
132 'localday' => 3600,
133 'localday2' => 3600,
134 'localdayname' => 3600,
135 'localyear' => 86400,
136 'localtime' => 3600,
137 'localhour' => 3600,
138 'numberofarticles' => 3600,
139 'numberoffiles' => 3600,
140 'numberofedits' => 3600,
141 'currentweek' => 3600,
142 'currentdow' => 3600,
143 'localweek' => 3600,
144 'localdow' => 3600,
145 'numberofusers' => 3600,
146 'numberofactiveusers' => 3600,
147 'numberofpages' => 3600,
148 'currentversion' => 86400,
149 'currenttimestamp' => 3600,
150 'localtimestamp' => 3600,
151 'pagesinnamespace' => 3600,
152 'numberofadmins' => 3600,
153 'numberofviews' => 3600,
154 'numberingroup' => 3600,
157 static public $mDoubleUnderscoreIDs = array(
158 'notoc',
159 'nogallery',
160 'forcetoc',
161 'toc',
162 'noeditsection',
163 'newsectionlink',
164 'nonewsectionlink',
165 'hiddencat',
166 'index',
167 'noindex',
168 'staticredirect',
172 static public $mObjects = array();
173 static public $mDoubleUnderscoreArray = null;
175 /**#@-*/
177 function __construct($id = 0, $syn = '', $cs = false) {
178 $this->mId = $id;
179 $this->mSynonyms = (array)$syn;
180 $this->mCaseSensitive = $cs;
181 $this->mRegex = '';
182 $this->mRegexStart = '';
183 $this->mVariableRegex = '';
184 $this->mVariableStartToEndRegex = '';
185 $this->mModified = false;
189 * Factory: creates an object representing an ID
190 * @static
192 static function &get( $id ) {
193 wfProfileIn( __METHOD__ );
194 if (!array_key_exists( $id, self::$mObjects ) ) {
195 $mw = new MagicWord();
196 $mw->load( $id );
197 self::$mObjects[$id] = $mw;
199 wfProfileOut( __METHOD__ );
200 return self::$mObjects[$id];
204 * Get an array of parser variable IDs
206 static function getVariableIDs() {
207 if ( !self::$mVariableIDsInitialised ) {
208 # Deprecated constant definition hook, available for extensions that need it
209 $magicWords = array();
210 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
211 foreach ( $magicWords as $word ) {
212 define( $word, $word );
215 # Get variable IDs
216 wfRunHooks( 'MagicWordwgVariableIDs', array( &self::$mVariableIDs ) );
217 self::$mVariableIDsInitialised = true;
219 return self::$mVariableIDs;
222 /* Allow external reads of TTL array */
223 static function getCacheTTL($id) {
224 if (array_key_exists($id,self::$mCacheTTLs)) {
225 return self::$mCacheTTLs[$id];
226 } else {
227 return -1;
231 /** Get a MagicWordArray of double-underscore entities */
232 static function getDoubleUnderscoreArray() {
233 if ( is_null( self::$mDoubleUnderscoreArray ) ) {
234 self::$mDoubleUnderscoreArray = new MagicWordArray( self::$mDoubleUnderscoreIDs );
236 return self::$mDoubleUnderscoreArray;
239 # Initialises this object with an ID
240 function load( $id ) {
241 global $wgContLang;
242 $this->mId = $id;
243 $wgContLang->getMagic( $this );
244 if ( !$this->mSynonyms ) {
245 $this->mSynonyms = array( 'dkjsagfjsgashfajsh' );
246 #throw new MWException( "Error: invalid magic word '$id'" );
247 wfDebugLog( 'exception', "Error: invalid magic word '$id'\n" );
252 * Preliminary initialisation
253 * @private
255 function initRegex() {
256 #$variableClass = Title::legalChars();
257 # This was used for matching "$1" variables, but different uses of the feature will have
258 # different restrictions, which should be checked *after* the MagicWord has been matched,
259 # not here. - IMSoP
261 $escSyn = array();
262 foreach ( $this->mSynonyms as $synonym )
263 // In case a magic word contains /, like that's going to happen;)
264 $escSyn[] = preg_quote( $synonym, '/' );
265 $this->mBaseRegex = implode( '|', $escSyn );
267 $case = $this->mCaseSensitive ? '' : 'iu';
268 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
269 $this->mRegexStart = "/^(?:{$this->mBaseRegex})/{$case}";
270 $this->mVariableRegex = str_replace( "\\$1", "(.*?)", $this->mRegex );
271 $this->mVariableStartToEndRegex = str_replace( "\\$1", "(.*?)",
272 "/^(?:{$this->mBaseRegex})$/{$case}" );
276 * Gets a regex representing matching the word
278 function getRegex() {
279 if ($this->mRegex == '' ) {
280 $this->initRegex();
282 return $this->mRegex;
286 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
287 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
288 * the complete expression
290 function getRegexCase() {
291 if ( $this->mRegex === '' )
292 $this->initRegex();
294 return $this->mCaseSensitive ? '' : 'iu';
298 * Gets a regex matching the word, if it is at the string start
300 function getRegexStart() {
301 if ($this->mRegex == '' ) {
302 $this->initRegex();
304 return $this->mRegexStart;
308 * regex without the slashes and what not
310 function getBaseRegex() {
311 if ($this->mRegex == '') {
312 $this->initRegex();
314 return $this->mBaseRegex;
318 * Returns true if the text contains the word
319 * @return bool
321 function match( $text ) {
322 return preg_match( $this->getRegex(), $text );
326 * Returns true if the text starts with the word
327 * @return bool
329 function matchStart( $text ) {
330 return preg_match( $this->getRegexStart(), $text );
334 * Returns NULL if there's no match, the value of $1 otherwise
335 * The return code is the matched string, if there's no variable
336 * part in the regex and the matched variable part ($1) if there
337 * is one.
339 function matchVariableStartToEnd( $text ) {
340 $matches = array();
341 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
342 if ( $matchcount == 0 ) {
343 return NULL;
344 } else {
345 # multiple matched parts (variable match); some will be empty because of
346 # synonyms. The variable will be the second non-empty one so remove any
347 # blank elements and re-sort the indices.
348 # See also bug 6526
350 $matches = array_values(array_filter($matches));
352 if ( count($matches) == 1 ) { return $matches[0]; }
353 else { return $matches[1]; }
359 * Returns true if the text matches the word, and alters the
360 * input string, removing all instances of the word
362 function matchAndRemove( &$text ) {
363 $this->mFound = false;
364 $text = preg_replace_callback( $this->getRegex(), array( &$this, 'pregRemoveAndRecord' ), $text );
365 return $this->mFound;
368 function matchStartAndRemove( &$text ) {
369 $this->mFound = false;
370 $text = preg_replace_callback( $this->getRegexStart(), array( &$this, 'pregRemoveAndRecord' ), $text );
371 return $this->mFound;
375 * Used in matchAndRemove()
376 * @private
378 function pregRemoveAndRecord( ) {
379 $this->mFound = true;
380 return '';
384 * Replaces the word with something else
386 function replace( $replacement, $subject, $limit=-1 ) {
387 $res = preg_replace( $this->getRegex(), StringUtils::escapeRegexReplacement( $replacement ), $subject, $limit );
388 $this->mModified = !($res === $subject);
389 return $res;
393 * Variable handling: {{SUBST:xxx}} style words
394 * Calls back a function to determine what to replace xxx with
395 * Input word must contain $1
397 function substituteCallback( $text, $callback ) {
398 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
399 $this->mModified = !($res === $text);
400 return $res;
404 * Matches the word, where $1 is a wildcard
406 function getVariableRegex() {
407 if ( $this->mVariableRegex == '' ) {
408 $this->initRegex();
410 return $this->mVariableRegex;
414 * Matches the entire string, where $1 is a wildcard
416 function getVariableStartToEndRegex() {
417 if ( $this->mVariableStartToEndRegex == '' ) {
418 $this->initRegex();
420 return $this->mVariableStartToEndRegex;
424 * Accesses the synonym list directly
426 function getSynonym( $i ) {
427 return $this->mSynonyms[$i];
430 function getSynonyms() {
431 return $this->mSynonyms;
435 * Returns true if the last call to replace() or substituteCallback()
436 * returned a modified text, otherwise false.
438 function getWasModified(){
439 return $this->mModified;
443 * $magicarr is an associative array of (magic word ID => replacement)
444 * This method uses the php feature to do several replacements at the same time,
445 * thereby gaining some efficiency. The result is placed in the out variable
446 * $result. The return value is true if something was replaced.
447 * @static
449 function replaceMultiple( $magicarr, $subject, &$result ){
450 $search = array();
451 $replace = array();
452 foreach( $magicarr as $id => $replacement ){
453 $mw = MagicWord::get( $id );
454 $search[] = $mw->getRegex();
455 $replace[] = $replacement;
458 $result = preg_replace( $search, $replace, $subject );
459 return !($result === $subject);
463 * Adds all the synonyms of this MagicWord to an array, to allow quick
464 * lookup in a list of magic words
466 function addToArray( &$array, $value ) {
467 global $wgContLang;
468 foreach ( $this->mSynonyms as $syn ) {
469 $array[$wgContLang->lc($syn)] = $value;
473 function isCaseSensitive() {
474 return $this->mCaseSensitive;
477 function getId() {
478 return $this->mId;
483 * Class for handling an array of magic words
484 * @ingroup Parser
486 class MagicWordArray {
487 var $names = array();
488 var $hash;
489 var $baseRegex, $regex;
490 var $matches;
492 function __construct( $names = array() ) {
493 $this->names = $names;
497 * Add a magic word by name
499 public function add( $name ) {
500 global $wgContLang;
501 $this->names[] = $name;
502 $this->hash = $this->baseRegex = $this->regex = null;
506 * Add a number of magic words by name
508 public function addArray( $names ) {
509 $this->names = array_merge( $this->names, array_values( $names ) );
510 $this->hash = $this->baseRegex = $this->regex = null;
514 * Get a 2-d hashtable for this array
516 function getHash() {
517 if ( is_null( $this->hash ) ) {
518 global $wgContLang;
519 $this->hash = array( 0 => array(), 1 => array() );
520 foreach ( $this->names as $name ) {
521 $magic = MagicWord::get( $name );
522 $case = intval( $magic->isCaseSensitive() );
523 foreach ( $magic->getSynonyms() as $syn ) {
524 if ( !$case ) {
525 $syn = $wgContLang->lc( $syn );
527 $this->hash[$case][$syn] = $name;
531 return $this->hash;
535 * Get the base regex
537 function getBaseRegex() {
538 if ( is_null( $this->baseRegex ) ) {
539 $this->baseRegex = array( 0 => '', 1 => '' );
540 foreach ( $this->names as $name ) {
541 $magic = MagicWord::get( $name );
542 $case = intval( $magic->isCaseSensitive() );
543 foreach ( $magic->getSynonyms() as $i => $syn ) {
544 $group = "(?P<{$i}_{$name}>" . preg_quote( $syn, '/' ) . ')';
545 if ( $this->baseRegex[$case] === '' ) {
546 $this->baseRegex[$case] = $group;
547 } else {
548 $this->baseRegex[$case] .= '|' . $group;
553 return $this->baseRegex;
557 * Get an unanchored regex
559 function getRegex() {
560 if ( is_null( $this->regex ) ) {
561 $base = $this->getBaseRegex();
562 $this->regex = array( '', '' );
563 if ( $this->baseRegex[0] !== '' ) {
564 $this->regex[0] = "/{$base[0]}/iuS";
566 if ( $this->baseRegex[1] !== '' ) {
567 $this->regex[1] = "/{$base[1]}/S";
570 return $this->regex;
574 * Get a regex for matching variables
576 function getVariableRegex() {
577 return str_replace( "\\$1", "(.*?)", $this->getRegex() );
581 * Get an anchored regex for matching variables
583 function getVariableStartToEndRegex() {
584 $base = $this->getBaseRegex();
585 $newRegex = array( '', '' );
586 if ( $base[0] !== '' ) {
587 $newRegex[0] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[0]})$/iuS" );
589 if ( $base[1] !== '' ) {
590 $newRegex[1] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[1]})$/S" );
592 return $newRegex;
596 * Parse a match array from preg_match
597 * Returns array(magic word ID, parameter value)
598 * If there is no parameter value, that element will be false.
600 function parseMatch( $m ) {
601 reset( $m );
602 while ( list( $key, $value ) = each( $m ) ) {
603 if ( $key === 0 || $value === '' ) {
604 continue;
606 $parts = explode( '_', $key, 2 );
607 if ( count( $parts ) != 2 ) {
608 // This shouldn't happen
609 // continue;
610 throw new MWException( __METHOD__ . ': bad parameter name' );
612 list( /* $synIndex */, $magicName ) = $parts;
613 $paramValue = next( $m );
614 return array( $magicName, $paramValue );
616 // This shouldn't happen either
617 throw new MWException( __METHOD__.': parameter not found' );
618 return array( false, false );
622 * Match some text, with parameter capture
623 * Returns an array with the magic word name in the first element and the
624 * parameter in the second element.
625 * Both elements are false if there was no match.
627 public function matchVariableStartToEnd( $text ) {
628 global $wgContLang;
629 $regexes = $this->getVariableStartToEndRegex();
630 foreach ( $regexes as $regex ) {
631 if ( $regex !== '' ) {
632 $m = false;
633 if ( preg_match( $regex, $text, $m ) ) {
634 return $this->parseMatch( $m );
638 return array( false, false );
642 * Match some text, without parameter capture
643 * Returns the magic word name, or false if there was no capture
645 public function matchStartToEnd( $text ) {
646 $hash = $this->getHash();
647 if ( isset( $hash[1][$text] ) ) {
648 return $hash[1][$text];
650 global $wgContLang;
651 $lc = $wgContLang->lc( $text );
652 if ( isset( $hash[0][$lc] ) ) {
653 return $hash[0][$lc];
655 return false;
659 * Returns an associative array, ID => param value, for all items that match
660 * Removes the matched items from the input string (passed by reference)
662 public function matchAndRemove( &$text ) {
663 $found = array();
664 $regexes = $this->getRegex();
665 foreach ( $regexes as $regex ) {
666 if ( $regex === '' ) {
667 continue;
669 preg_match_all( $regex, $text, $matches, PREG_SET_ORDER );
670 foreach ( $matches as $m ) {
671 list( $name, $param ) = $this->parseMatch( $m );
672 $found[$name] = $param;
674 $text = preg_replace( $regex, '', $text );
676 return $found;