4 * See docs/magicword.txt
11 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
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.
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(
41 'currentmonthnamegen',
95 'numberofactiveusers',
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,
128 'localdayname' => 3600,
129 'localyear' => 86400,
132 'numberofarticles' => 3600,
133 'numberoffiles' => 3600,
134 'numberofedits' => 3600,
135 'currentweek' => 3600,
136 'currentdow' => 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,
151 static public $mDoubleUnderscoreIDs = array(
166 static public $mObjects = array();
167 static public $mDoubleUnderscoreArray = null;
171 function __construct($id = 0, $syn = '', $cs = false) {
173 $this->mSynonyms
= (array)$syn;
174 $this->mCaseSensitive
= $cs;
176 $this->mRegexStart
= '';
177 $this->mVariableRegex
= '';
178 $this->mVariableStartToEndRegex
= '';
179 $this->mModified
= false;
183 * Factory: creates an object representing an ID
186 static function &get( $id ) {
187 wfProfileIn( __METHOD__
);
188 if ( !isset( self
::$mObjects[$id] ) ) {
189 $mw = new MagicWord();
191 self
::$mObjects[$id] = $mw;
193 wfProfileOut( __METHOD__
);
194 return self
::$mObjects[$id];
198 * Get an array of parser variable IDs
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 );
210 wfRunHooks( 'MagicWordwgVariableIDs', array( &self
::$mVariableIDs ) );
211 self
::$mVariableIDsInitialised = true;
213 return self
::$mVariableIDs;
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];
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 );
230 return self
::$mDoubleUnderscoreArray;
234 * Clear the self::$mObjects variable
235 * For use in parser tests
237 public static function clearCache() {
238 self
::$mObjects = array();
241 # Initialises this object with an ID
242 function load( $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" );
254 * Preliminary initialisation
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,
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 );
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}" );
278 * Gets a regex representing matching the word
280 function getRegex() {
281 if ($this->mRegex
== '' ) {
284 return $this->mRegex
;
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
292 function getRegexCase() {
293 if ( $this->mRegex
=== '' )
296 return $this->mCaseSensitive ?
'' : 'iu';
300 * Gets a regex matching the word, if it is at the string start
302 function getRegexStart() {
303 if ($this->mRegex
== '' ) {
306 return $this->mRegexStart
;
310 * regex without the slashes and what not
312 function getBaseRegex() {
313 if ($this->mRegex
== '') {
316 return $this->mBaseRegex
;
320 * Returns true if the text contains the word
323 function match( $text ) {
324 return preg_match( $this->getRegex(), $text );
328 * Returns true if the text starts with the word
331 function matchStart( $text ) {
332 return preg_match( $this->getRegexStart(), $text );
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
341 function matchVariableStartToEnd( $text ) {
343 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
344 if ( $matchcount == 0 ) {
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.
352 $matches = array_values(array_filter($matches));
354 if ( count($matches) == 1 ) { return $matches[0]; }
355 else { return $matches[1]; }
361 * Returns true if the text matches the word, and alters the
362 * input string, removing all instances of the word
364 function matchAndRemove( &$text ) {
365 $this->mFound
= false;
366 $text = preg_replace_callback( $this->getRegex(), array( &$this, 'pregRemoveAndRecord' ), $text );
367 return $this->mFound
;
370 function matchStartAndRemove( &$text ) {
371 $this->mFound
= false;
372 $text = preg_replace_callback( $this->getRegexStart(), array( &$this, 'pregRemoveAndRecord' ), $text );
373 return $this->mFound
;
377 * Used in matchAndRemove()
380 function pregRemoveAndRecord( ) {
381 $this->mFound
= true;
386 * Replaces the word with something else
388 function replace( $replacement, $subject, $limit=-1 ) {
389 $res = preg_replace( $this->getRegex(), StringUtils
::escapeRegexReplacement( $replacement ), $subject, $limit );
390 $this->mModified
= !($res === $subject);
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
399 function substituteCallback( $text, $callback ) {
400 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
401 $this->mModified
= !($res === $text);
406 * Matches the word, where $1 is a wildcard
408 function getVariableRegex() {
409 if ( $this->mVariableRegex
== '' ) {
412 return $this->mVariableRegex
;
416 * Matches the entire string, where $1 is a wildcard
418 function getVariableStartToEndRegex() {
419 if ( $this->mVariableStartToEndRegex
== '' ) {
422 return $this->mVariableStartToEndRegex
;
426 * Accesses the synonym list directly
428 function getSynonym( $i ) {
429 return $this->mSynonyms
[$i];
432 function getSynonyms() {
433 return $this->mSynonyms
;
437 * Returns true if the last call to replace() or substituteCallback()
438 * returned a modified text, otherwise false.
440 function getWasModified(){
441 return $this->mModified
;
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.
451 function replaceMultiple( $magicarr, $subject, &$result ){
454 foreach( $magicarr as $id => $replacement ){
455 $mw = MagicWord
::get( $id );
456 $search[] = $mw->getRegex();
457 $replace[] = $replacement;
460 $result = preg_replace( $search, $replace, $subject );
461 return !($result === $subject);
465 * Adds all the synonyms of this MagicWord to an array, to allow quick
466 * lookup in a list of magic words
468 function addToArray( &$array, $value ) {
470 foreach ( $this->mSynonyms
as $syn ) {
471 $array[$wgContLang->lc($syn)] = $value;
475 function isCaseSensitive() {
476 return $this->mCaseSensitive
;
485 * Class for handling an array of magic words
488 class MagicWordArray
{
489 var $names = array();
491 var $baseRegex, $regex;
494 function __construct( $names = array() ) {
495 $this->names
= $names;
499 * Add a magic word by name
501 public function add( $name ) {
503 $this->names
[] = $name;
504 $this->hash
= $this->baseRegex
= $this->regex
= null;
508 * Add a number of magic words by name
510 public function addArray( $names ) {
511 $this->names
= array_merge( $this->names
, array_values( $names ) );
512 $this->hash
= $this->baseRegex
= $this->regex
= null;
516 * Get a 2-d hashtable for this array
519 if ( is_null( $this->hash
) ) {
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 ) {
527 $syn = $wgContLang->lc( $syn );
529 $this->hash
[$case][$syn] = $name;
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;
550 $this->baseRegex
[$case] .= '|' . $group;
555 return $this->baseRegex
;
559 * Get an unanchored regex
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";
568 if ( $this->baseRegex
[1] !== '' ) {
569 $this->regex
[1] = "/{$base[1]}/S";
576 * Get a regex for matching variables
578 function getVariableRegex() {
579 return str_replace( "\\$1", "(.*?)", $this->getRegex() );
583 * Get an anchored regex for matching variables
585 function getVariableStartToEndRegex() {
586 $base = $this->getBaseRegex();
587 $newRegex = array( '', '' );
588 if ( $base[0] !== '' ) {
589 $newRegex[0] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[0]})$/iuS" );
591 if ( $base[1] !== '' ) {
592 $newRegex[1] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[1]})$/S" );
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.
602 function parseMatch( $m ) {
604 while ( list( $key, $value ) = each( $m ) ) {
605 if ( $key === 0 ||
$value === '' ) {
608 $parts = explode( '_', $key, 2 );
609 if ( count( $parts ) != 2 ) {
610 // This shouldn't happen
612 throw new MWException( __METHOD__
. ': bad parameter name' );
614 list( /* $synIndex */, $magicName ) = $parts;
615 $paramValue = next( $m );
616 return array( $magicName, $paramValue );
618 // This shouldn't happen either
619 throw new MWException( __METHOD__
.': parameter not found' );
620 return array( false, false );
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.
629 public function matchVariableStartToEnd( $text ) {
631 $regexes = $this->getVariableStartToEndRegex();
632 foreach ( $regexes as $regex ) {
633 if ( $regex !== '' ) {
635 if ( preg_match( $regex, $text, $m ) ) {
636 return $this->parseMatch( $m );
640 return array( false, false );
644 * Match some text, without parameter capture
645 * Returns the magic word name, or false if there was no capture
647 public function matchStartToEnd( $text ) {
648 $hash = $this->getHash();
649 if ( isset( $hash[1][$text] ) ) {
650 return $hash[1][$text];
653 $lc = $wgContLang->lc( $text );
654 if ( isset( $hash[0][$lc] ) ) {
655 return $hash[0][$lc];
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)
664 public function matchAndRemove( &$text ) {
666 $regexes = $this->getRegex();
667 foreach ( $regexes as $regex ) {
668 if ( $regex === '' ) {
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;
676 $text = preg_replace( $regex, '', $text );