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(
40 'currentmonthnamegen',
94 'numberofactiveusers',
117 /* Array of caching hints for ParserCache */
118 static public $mCacheTTLs = array (
119 'currentmonth' => 86400,
120 'currentmonthname' => 86400,
121 'currentmonthnamegen' => 86400,
122 'currentmonthabbrev' => 86400,
123 'currentday' => 3600,
124 'currentday2' => 3600,
125 'currentdayname' => 3600,
126 'currentyear' => 86400,
127 'currenttime' => 3600,
128 'currenthour' => 3600,
129 'localmonth' => 86400,
130 'localmonthname' => 86400,
131 'localmonthnamegen' => 86400,
132 'localmonthabbrev' => 86400,
135 'localdayname' => 3600,
136 'localyear' => 86400,
139 'numberofarticles' => 3600,
140 'numberoffiles' => 3600,
141 'numberofedits' => 3600,
142 'currentweek' => 3600,
143 'currentdow' => 3600,
146 'numberofusers' => 3600,
147 'numberofactiveusers' => 3600,
148 'numberofpages' => 3600,
149 'currentversion' => 86400,
150 'currenttimestamp' => 3600,
151 'localtimestamp' => 3600,
152 'pagesinnamespace' => 3600,
153 'numberofadmins' => 3600,
154 'numberofviews' => 3600,
155 'numberingroup' => 3600,
158 static public $mDoubleUnderscoreIDs = array(
173 static public $mObjects = array();
174 static public $mDoubleUnderscoreArray = null;
178 function __construct($id = 0, $syn = '', $cs = false) {
180 $this->mSynonyms
= (array)$syn;
181 $this->mCaseSensitive
= $cs;
183 $this->mRegexStart
= '';
184 $this->mVariableRegex
= '';
185 $this->mVariableStartToEndRegex
= '';
186 $this->mModified
= false;
190 * Factory: creates an object representing an ID
193 static function &get( $id ) {
194 wfProfileIn( __METHOD__
);
195 if (!array_key_exists( $id, self
::$mObjects ) ) {
196 $mw = new MagicWord();
198 self
::$mObjects[$id] = $mw;
200 wfProfileOut( __METHOD__
);
201 return self
::$mObjects[$id];
205 * Get an array of parser variable IDs
207 static function getVariableIDs() {
208 if ( !self
::$mVariableIDsInitialised ) {
209 # Deprecated constant definition hook, available for extensions that need it
210 $magicWords = array();
211 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
212 foreach ( $magicWords as $word ) {
213 define( $word, $word );
217 wfRunHooks( 'MagicWordwgVariableIDs', array( &self
::$mVariableIDs ) );
218 self
::$mVariableIDsInitialised = true;
220 return self
::$mVariableIDs;
223 /* Allow external reads of TTL array */
224 static function getCacheTTL($id) {
225 if (array_key_exists($id,self
::$mCacheTTLs)) {
226 return self
::$mCacheTTLs[$id];
232 /** Get a MagicWordArray of double-underscore entities */
233 static function getDoubleUnderscoreArray() {
234 if ( is_null( self
::$mDoubleUnderscoreArray ) ) {
235 self
::$mDoubleUnderscoreArray = new MagicWordArray( self
::$mDoubleUnderscoreIDs );
237 return self
::$mDoubleUnderscoreArray;
240 # Initialises this object with an ID
241 function load( $id ) {
244 $wgContLang->getMagic( $this );
245 if ( !$this->mSynonyms
) {
246 $this->mSynonyms
= array( 'dkjsagfjsgashfajsh' );
247 #throw new MWException( "Error: invalid magic word '$id'" );
248 wfDebugLog( 'exception', "Error: invalid magic word '$id'\n" );
253 * Preliminary initialisation
256 function initRegex() {
257 #$variableClass = Title::legalChars();
258 # This was used for matching "$1" variables, but different uses of the feature will have
259 # different restrictions, which should be checked *after* the MagicWord has been matched,
263 foreach ( $this->mSynonyms
as $synonym )
264 // In case a magic word contains /, like that's going to happen;)
265 $escSyn[] = preg_quote( $synonym, '/' );
266 $this->mBaseRegex
= implode( '|', $escSyn );
268 $case = $this->mCaseSensitive ?
'' : 'iu';
269 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
270 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
271 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
272 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
273 "/^(?:{$this->mBaseRegex})$/{$case}" );
277 * Gets a regex representing matching the word
279 function getRegex() {
280 if ($this->mRegex
== '' ) {
283 return $this->mRegex
;
287 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
288 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
289 * the complete expression
291 function getRegexCase() {
292 if ( $this->mRegex
=== '' )
295 return $this->mCaseSensitive ?
'' : 'iu';
299 * Gets a regex matching the word, if it is at the string start
301 function getRegexStart() {
302 if ($this->mRegex
== '' ) {
305 return $this->mRegexStart
;
309 * regex without the slashes and what not
311 function getBaseRegex() {
312 if ($this->mRegex
== '') {
315 return $this->mBaseRegex
;
319 * Returns true if the text contains the word
322 function match( $text ) {
323 return preg_match( $this->getRegex(), $text );
327 * Returns true if the text starts with the word
330 function matchStart( $text ) {
331 return preg_match( $this->getRegexStart(), $text );
335 * Returns NULL if there's no match, the value of $1 otherwise
336 * The return code is the matched string, if there's no variable
337 * part in the regex and the matched variable part ($1) if there
340 function matchVariableStartToEnd( $text ) {
342 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
343 if ( $matchcount == 0 ) {
346 # multiple matched parts (variable match); some will be empty because of
347 # synonyms. The variable will be the second non-empty one so remove any
348 # blank elements and re-sort the indices.
351 $matches = array_values(array_filter($matches));
353 if ( count($matches) == 1 ) { return $matches[0]; }
354 else { return $matches[1]; }
360 * Returns true if the text matches the word, and alters the
361 * input string, removing all instances of the word
363 function matchAndRemove( &$text ) {
364 $this->mFound
= false;
365 $text = preg_replace_callback( $this->getRegex(), array( &$this, 'pregRemoveAndRecord' ), $text );
366 return $this->mFound
;
369 function matchStartAndRemove( &$text ) {
370 $this->mFound
= false;
371 $text = preg_replace_callback( $this->getRegexStart(), array( &$this, 'pregRemoveAndRecord' ), $text );
372 return $this->mFound
;
376 * Used in matchAndRemove()
379 function pregRemoveAndRecord( ) {
380 $this->mFound
= true;
385 * Replaces the word with something else
387 function replace( $replacement, $subject, $limit=-1 ) {
388 $res = preg_replace( $this->getRegex(), StringUtils
::escapeRegexReplacement( $replacement ), $subject, $limit );
389 $this->mModified
= !($res === $subject);
394 * Variable handling: {{SUBST:xxx}} style words
395 * Calls back a function to determine what to replace xxx with
396 * Input word must contain $1
398 function substituteCallback( $text, $callback ) {
399 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
400 $this->mModified
= !($res === $text);
405 * Matches the word, where $1 is a wildcard
407 function getVariableRegex() {
408 if ( $this->mVariableRegex
== '' ) {
411 return $this->mVariableRegex
;
415 * Matches the entire string, where $1 is a wildcard
417 function getVariableStartToEndRegex() {
418 if ( $this->mVariableStartToEndRegex
== '' ) {
421 return $this->mVariableStartToEndRegex
;
425 * Accesses the synonym list directly
427 function getSynonym( $i ) {
428 return $this->mSynonyms
[$i];
431 function getSynonyms() {
432 return $this->mSynonyms
;
436 * Returns true if the last call to replace() or substituteCallback()
437 * returned a modified text, otherwise false.
439 function getWasModified(){
440 return $this->mModified
;
444 * $magicarr is an associative array of (magic word ID => replacement)
445 * This method uses the php feature to do several replacements at the same time,
446 * thereby gaining some efficiency. The result is placed in the out variable
447 * $result. The return value is true if something was replaced.
450 function replaceMultiple( $magicarr, $subject, &$result ){
453 foreach( $magicarr as $id => $replacement ){
454 $mw = MagicWord
::get( $id );
455 $search[] = $mw->getRegex();
456 $replace[] = $replacement;
459 $result = preg_replace( $search, $replace, $subject );
460 return !($result === $subject);
464 * Adds all the synonyms of this MagicWord to an array, to allow quick
465 * lookup in a list of magic words
467 function addToArray( &$array, $value ) {
469 foreach ( $this->mSynonyms
as $syn ) {
470 $array[$wgContLang->lc($syn)] = $value;
474 function isCaseSensitive() {
475 return $this->mCaseSensitive
;
484 * Class for handling an array of magic words
487 class MagicWordArray
{
488 var $names = array();
490 var $baseRegex, $regex;
493 function __construct( $names = array() ) {
494 $this->names
= $names;
498 * Add a magic word by name
500 public function add( $name ) {
502 $this->names
[] = $name;
503 $this->hash
= $this->baseRegex
= $this->regex
= null;
507 * Add a number of magic words by name
509 public function addArray( $names ) {
510 $this->names
= array_merge( $this->names
, array_values( $names ) );
511 $this->hash
= $this->baseRegex
= $this->regex
= null;
515 * Get a 2-d hashtable for this array
518 if ( is_null( $this->hash
) ) {
520 $this->hash
= array( 0 => array(), 1 => array() );
521 foreach ( $this->names
as $name ) {
522 $magic = MagicWord
::get( $name );
523 $case = intval( $magic->isCaseSensitive() );
524 foreach ( $magic->getSynonyms() as $syn ) {
526 $syn = $wgContLang->lc( $syn );
528 $this->hash
[$case][$syn] = $name;
538 function getBaseRegex() {
539 if ( is_null( $this->baseRegex
) ) {
540 $this->baseRegex
= array( 0 => '', 1 => '' );
541 foreach ( $this->names
as $name ) {
542 $magic = MagicWord
::get( $name );
543 $case = intval( $magic->isCaseSensitive() );
544 foreach ( $magic->getSynonyms() as $i => $syn ) {
545 $group = "(?P<{$i}_{$name}>" . preg_quote( $syn, '/' ) . ')';
546 if ( $this->baseRegex
[$case] === '' ) {
547 $this->baseRegex
[$case] = $group;
549 $this->baseRegex
[$case] .= '|' . $group;
554 return $this->baseRegex
;
558 * Get an unanchored regex
560 function getRegex() {
561 if ( is_null( $this->regex
) ) {
562 $base = $this->getBaseRegex();
563 $this->regex
= array( '', '' );
564 if ( $this->baseRegex
[0] !== '' ) {
565 $this->regex
[0] = "/{$base[0]}/iuS";
567 if ( $this->baseRegex
[1] !== '' ) {
568 $this->regex
[1] = "/{$base[1]}/S";
575 * Get a regex for matching variables
577 function getVariableRegex() {
578 return str_replace( "\\$1", "(.*?)", $this->getRegex() );
582 * Get an anchored regex for matching variables
584 function getVariableStartToEndRegex() {
585 $base = $this->getBaseRegex();
586 $newRegex = array( '', '' );
587 if ( $base[0] !== '' ) {
588 $newRegex[0] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[0]})$/iuS" );
590 if ( $base[1] !== '' ) {
591 $newRegex[1] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[1]})$/S" );
597 * Parse a match array from preg_match
598 * Returns array(magic word ID, parameter value)
599 * If there is no parameter value, that element will be false.
601 function parseMatch( $m ) {
603 while ( list( $key, $value ) = each( $m ) ) {
604 if ( $key === 0 ||
$value === '' ) {
607 $parts = explode( '_', $key, 2 );
608 if ( count( $parts ) != 2 ) {
609 // This shouldn't happen
611 throw new MWException( __METHOD__
. ': bad parameter name' );
613 list( /* $synIndex */, $magicName ) = $parts;
614 $paramValue = next( $m );
615 return array( $magicName, $paramValue );
617 // This shouldn't happen either
618 throw new MWException( __METHOD__
.': parameter not found' );
619 return array( false, false );
623 * Match some text, with parameter capture
624 * Returns an array with the magic word name in the first element and the
625 * parameter in the second element.
626 * Both elements are false if there was no match.
628 public function matchVariableStartToEnd( $text ) {
630 $regexes = $this->getVariableStartToEndRegex();
631 foreach ( $regexes as $regex ) {
632 if ( $regex !== '' ) {
634 if ( preg_match( $regex, $text, $m ) ) {
635 return $this->parseMatch( $m );
639 return array( false, false );
643 * Match some text, without parameter capture
644 * Returns the magic word name, or false if there was no capture
646 public function matchStartToEnd( $text ) {
647 $hash = $this->getHash();
648 if ( isset( $hash[1][$text] ) ) {
649 return $hash[1][$text];
652 $lc = $wgContLang->lc( $text );
653 if ( isset( $hash[0][$lc] ) ) {
654 return $hash[0][$lc];
660 * Returns an associative array, ID => param value, for all items that match
661 * Removes the matched items from the input string (passed by reference)
663 public function matchAndRemove( &$text ) {
665 $regexes = $this->getRegex();
666 foreach ( $regexes as $regex ) {
667 if ( $regex === '' ) {
670 preg_match_all( $regex, $text, $matches, PREG_SET_ORDER
);
671 foreach ( $matches as $m ) {
672 list( $name, $param ) = $this->parseMatch( $m );
673 $found[$name] = $param;
675 $text = preg_replace( $regex, '', $text );