3 * File for magic words.
5 * See docs/magicword.txt.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
27 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
31 * if (MagicWord::get( 'redirect' )->match( $text ) ) {
36 * Possible future improvements:
37 * * Simultaneous searching for a number of magic words
38 * * MagicWord::$mObjects in shared memory
40 * Please avoid reading the data out of one of these objects and then writing
41 * special case code. If possible, add another match()-like function here.
43 * To add magic words in an extension, use $magicWords in a file listed in
44 * $wgExtensionMessagesFiles[].
48 * $magicWords = array();
50 * $magicWords['en'] = array(
51 * 'magicwordkey' => array( 0, 'case_insensitive_magic_word' ),
52 * 'magicwordkey2' => array( 1, 'CASE_sensitive_magic_word2' ),
56 * For magic words which are also Parser variables, add a MagicWordwgVariableIDs
57 * hook. Use string keys.
65 var $mId, $mSynonyms, $mCaseSensitive;
67 var $mRegexStart = '';
69 var $mVariableRegex = '';
70 var $mVariableStartToEndRegex = '';
71 var $mModified = false;
74 static public $mVariableIDsInitialised = false;
75 static public $mVariableIDs = array(
79 'currentmonthnamegen',
137 'numberofactiveusers',
150 /* Array of caching hints for ParserCache */
151 static public $mCacheTTLs = array (
152 'currentmonth' => 86400,
153 'currentmonth1' => 86400,
154 'currentmonthname' => 86400,
155 'currentmonthnamegen' => 86400,
156 'currentmonthabbrev' => 86400,
157 'currentday' => 3600,
158 'currentday2' => 3600,
159 'currentdayname' => 3600,
160 'currentyear' => 86400,
161 'currenttime' => 3600,
162 'currenthour' => 3600,
163 'localmonth' => 86400,
164 'localmonth1' => 86400,
165 'localmonthname' => 86400,
166 'localmonthnamegen' => 86400,
167 'localmonthabbrev' => 86400,
170 'localdayname' => 3600,
171 'localyear' => 86400,
174 'numberofarticles' => 3600,
175 'numberoffiles' => 3600,
176 'numberofedits' => 3600,
177 'currentweek' => 3600,
178 'currentdow' => 3600,
181 'numberofusers' => 3600,
182 'numberofactiveusers' => 3600,
183 'numberofpages' => 3600,
184 'currentversion' => 86400,
185 'currenttimestamp' => 3600,
186 'localtimestamp' => 3600,
187 'pagesinnamespace' => 3600,
188 'numberofadmins' => 3600,
189 'numberofviews' => 3600,
190 'numberingroup' => 3600,
193 static public $mDoubleUnderscoreIDs = array(
209 static public $mSubstIDs = array(
214 static public $mObjects = array();
215 static public $mDoubleUnderscoreArray = null;
219 function __construct($id = 0, $syn = array(), $cs = false) {
221 $this->mSynonyms
= (array)$syn;
222 $this->mCaseSensitive
= $cs;
226 * Factory: creates an object representing an ID
232 static function &get( $id ) {
233 if ( !isset( self
::$mObjects[$id] ) ) {
234 $mw = new MagicWord();
236 self
::$mObjects[$id] = $mw;
238 return self
::$mObjects[$id];
242 * Get an array of parser variable IDs
246 static function getVariableIDs() {
247 if ( !self
::$mVariableIDsInitialised ) {
249 wfRunHooks( 'MagicWordwgVariableIDs', array( &self
::$mVariableIDs ) );
250 self
::$mVariableIDsInitialised = true;
252 return self
::$mVariableIDs;
256 * Get an array of parser substitution modifier IDs
259 static function getSubstIDs() {
260 return self
::$mSubstIDs;
264 * Allow external reads of TTL array
269 static function getCacheTTL( $id ) {
270 if ( array_key_exists( $id, self
::$mCacheTTLs ) ) {
271 return self
::$mCacheTTLs[$id];
278 * Get a MagicWordArray of double-underscore entities
280 * @return MagicWordArray
282 static function getDoubleUnderscoreArray() {
283 if ( is_null( self
::$mDoubleUnderscoreArray ) ) {
284 self
::$mDoubleUnderscoreArray = new MagicWordArray( self
::$mDoubleUnderscoreIDs );
286 return self
::$mDoubleUnderscoreArray;
290 * Clear the self::$mObjects variable
291 * For use in parser tests
293 public static function clearCache() {
294 self
::$mObjects = array();
298 * Initialises this object with an ID
302 function load( $id ) {
304 wfProfileIn( __METHOD__
);
306 $wgContLang->getMagic( $this );
307 if ( !$this->mSynonyms
) {
308 $this->mSynonyms
= array( 'dkjsagfjsgashfajsh' );
309 throw new MWException( "Error: invalid magic word '$id'" );
310 #wfDebugLog( 'exception', "Error: invalid magic word '$id'\n" );
312 wfProfileOut( __METHOD__
);
316 * Preliminary initialisation
319 function initRegex() {
320 // Sort the synonyms by length, descending, so that the longest synonym
321 // matches in precedence to the shortest
322 $synonyms = $this->mSynonyms
;
323 usort( $synonyms, array( $this, 'compareStringLength' ) );
326 foreach ( $synonyms as $synonym )
327 // In case a magic word contains /, like that's going to happen;)
328 $escSyn[] = preg_quote( $synonym, '/' );
329 $this->mBaseRegex
= implode( '|', $escSyn );
331 $case = $this->mCaseSensitive ?
'' : 'iu';
332 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
333 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
334 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
335 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
336 "/^(?:{$this->mBaseRegex})$/{$case}" );
340 * A comparison function that returns -1, 0 or 1 depending on whether the
341 * first string is longer, the same length or shorter than the second
349 function compareStringLength( $s1, $s2 ) {
354 } elseif ( $l1 > $l2 ) {
362 * Gets a regex representing matching the word
366 function getRegex() {
367 if ($this->mRegex
== '' ) {
370 return $this->mRegex
;
374 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
375 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
376 * the complete expression
380 function getRegexCase() {
381 if ( $this->mRegex
=== '' )
384 return $this->mCaseSensitive ?
'' : 'iu';
388 * Gets a regex matching the word, if it is at the string start
392 function getRegexStart() {
393 if ($this->mRegex
== '' ) {
396 return $this->mRegexStart
;
400 * regex without the slashes and what not
404 function getBaseRegex() {
405 if ($this->mRegex
== '') {
408 return $this->mBaseRegex
;
412 * Returns true if the text contains the word
414 * @param $text string
418 function match( $text ) {
419 return (bool)preg_match( $this->getRegex(), $text );
423 * Returns true if the text starts with the word
425 * @param $text string
429 function matchStart( $text ) {
430 return (bool)preg_match( $this->getRegexStart(), $text );
434 * Returns NULL if there's no match, the value of $1 otherwise
435 * The return code is the matched string, if there's no variable
436 * part in the regex and the matched variable part ($1) if there
439 * @param $text string
443 function matchVariableStartToEnd( $text ) {
445 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
446 if ( $matchcount == 0 ) {
449 # multiple matched parts (variable match); some will be empty because of
450 # synonyms. The variable will be the second non-empty one so remove any
451 # blank elements and re-sort the indices.
454 $matches = array_values(array_filter($matches));
456 if ( count($matches) == 1 ) {
466 * Returns true if the text matches the word, and alters the
467 * input string, removing all instances of the word
469 * @param $text string
473 function matchAndRemove( &$text ) {
474 $this->mFound
= false;
475 $text = preg_replace_callback( $this->getRegex(), array( &$this, 'pregRemoveAndRecord' ), $text );
476 return $this->mFound
;
483 function matchStartAndRemove( &$text ) {
484 $this->mFound
= false;
485 $text = preg_replace_callback( $this->getRegexStart(), array( &$this, 'pregRemoveAndRecord' ), $text );
486 return $this->mFound
;
490 * Used in matchAndRemove()
494 function pregRemoveAndRecord() {
495 $this->mFound
= true;
500 * Replaces the word with something else
502 * @param $replacement
508 function replace( $replacement, $subject, $limit = -1 ) {
509 $res = preg_replace( $this->getRegex(), StringUtils
::escapeRegexReplacement( $replacement ), $subject, $limit );
510 $this->mModified
= !($res === $subject);
515 * Variable handling: {{SUBST:xxx}} style words
516 * Calls back a function to determine what to replace xxx with
517 * Input word must contain $1
519 * @param $text string
524 function substituteCallback( $text, $callback ) {
525 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
526 $this->mModified
= !($res === $text);
531 * Matches the word, where $1 is a wildcard
535 function getVariableRegex() {
536 if ( $this->mVariableRegex
== '' ) {
539 return $this->mVariableRegex
;
543 * Matches the entire string, where $1 is a wildcard
547 function getVariableStartToEndRegex() {
548 if ( $this->mVariableStartToEndRegex
== '' ) {
551 return $this->mVariableStartToEndRegex
;
555 * Accesses the synonym list directly
561 function getSynonym( $i ) {
562 return $this->mSynonyms
[$i];
568 function getSynonyms() {
569 return $this->mSynonyms
;
573 * Returns true if the last call to replace() or substituteCallback()
574 * returned a modified text, otherwise false.
578 function getWasModified(){
579 return $this->mModified
;
583 * $magicarr is an associative array of (magic word ID => replacement)
584 * This method uses the php feature to do several replacements at the same time,
585 * thereby gaining some efficiency. The result is placed in the out variable
586 * $result. The return value is true if something was replaced.
587 * @todo Should this be static? It doesn't seem to be used at all
595 function replaceMultiple( $magicarr, $subject, &$result ){
598 foreach( $magicarr as $id => $replacement ){
599 $mw = MagicWord
::get( $id );
600 $search[] = $mw->getRegex();
601 $replace[] = $replacement;
604 $result = preg_replace( $search, $replace, $subject );
605 return !($result === $subject);
609 * Adds all the synonyms of this MagicWord to an array, to allow quick
610 * lookup in a list of magic words
615 function addToArray( &$array, $value ) {
617 foreach ( $this->mSynonyms
as $syn ) {
618 $array[$wgContLang->lc($syn)] = $value;
625 function isCaseSensitive() {
626 return $this->mCaseSensitive
;
638 * Class for handling an array of magic words
641 class MagicWordArray
{
642 var $names = array();
644 var $baseRegex, $regex;
647 function __construct( $names = array() ) {
648 $this->names
= $names;
652 * Add a magic word by name
654 * @param $name string
656 public function add( $name ) {
657 $this->names
[] = $name;
658 $this->hash
= $this->baseRegex
= $this->regex
= null;
662 * Add a number of magic words by name
664 * @param $names array
666 public function addArray( $names ) {
667 $this->names
= array_merge( $this->names
, array_values( $names ) );
668 $this->hash
= $this->baseRegex
= $this->regex
= null;
672 * Get a 2-d hashtable for this array
675 if ( is_null( $this->hash
) ) {
677 $this->hash
= array( 0 => array(), 1 => array() );
678 foreach ( $this->names
as $name ) {
679 $magic = MagicWord
::get( $name );
680 $case = intval( $magic->isCaseSensitive() );
681 foreach ( $magic->getSynonyms() as $syn ) {
683 $syn = $wgContLang->lc( $syn );
685 $this->hash
[$case][$syn] = $name;
695 function getBaseRegex() {
696 if ( is_null( $this->baseRegex
) ) {
697 $this->baseRegex
= array( 0 => '', 1 => '' );
698 foreach ( $this->names
as $name ) {
699 $magic = MagicWord
::get( $name );
700 $case = intval( $magic->isCaseSensitive() );
701 foreach ( $magic->getSynonyms() as $i => $syn ) {
702 $group = "(?P<{$i}_{$name}>" . preg_quote( $syn, '/' ) . ')';
703 if ( $this->baseRegex
[$case] === '' ) {
704 $this->baseRegex
[$case] = $group;
706 $this->baseRegex
[$case] .= '|' . $group;
711 return $this->baseRegex
;
715 * Get an unanchored regex that does not match parameters
717 function getRegex() {
718 if ( is_null( $this->regex
) ) {
719 $base = $this->getBaseRegex();
720 $this->regex
= array( '', '' );
721 if ( $this->baseRegex
[0] !== '' ) {
722 $this->regex
[0] = "/{$base[0]}/iuS";
724 if ( $this->baseRegex
[1] !== '' ) {
725 $this->regex
[1] = "/{$base[1]}/S";
732 * Get a regex for matching variables with parameters
736 function getVariableRegex() {
737 return str_replace( "\\$1", "(.*?)", $this->getRegex() );
741 * Get a regex anchored to the start of the string that does not match parameters
745 function getRegexStart() {
746 $base = $this->getBaseRegex();
747 $newRegex = array( '', '' );
748 if ( $base[0] !== '' ) {
749 $newRegex[0] = "/^(?:{$base[0]})/iuS";
751 if ( $base[1] !== '' ) {
752 $newRegex[1] = "/^(?:{$base[1]})/S";
758 * Get an anchored regex for matching variables with parameters
762 function getVariableStartToEndRegex() {
763 $base = $this->getBaseRegex();
764 $newRegex = array( '', '' );
765 if ( $base[0] !== '' ) {
766 $newRegex[0] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[0]})$/iuS" );
768 if ( $base[1] !== '' ) {
769 $newRegex[1] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[1]})$/S" );
775 * Parse a match array from preg_match
776 * Returns array(magic word ID, parameter value)
777 * If there is no parameter value, that element will be false.
783 function parseMatch( $m ) {
785 while ( list( $key, $value ) = each( $m ) ) {
786 if ( $key === 0 ||
$value === '' ) {
789 $parts = explode( '_', $key, 2 );
790 if ( count( $parts ) != 2 ) {
791 // This shouldn't happen
793 throw new MWException( __METHOD__
. ': bad parameter name' );
795 list( /* $synIndex */, $magicName ) = $parts;
796 $paramValue = next( $m );
797 return array( $magicName, $paramValue );
799 // This shouldn't happen either
800 throw new MWException( __METHOD__
.': parameter not found' );
804 * Match some text, with parameter capture
805 * Returns an array with the magic word name in the first element and the
806 * parameter in the second element.
807 * Both elements are false if there was no match.
809 * @param $text string
813 public function matchVariableStartToEnd( $text ) {
814 $regexes = $this->getVariableStartToEndRegex();
815 foreach ( $regexes as $regex ) {
816 if ( $regex !== '' ) {
818 if ( preg_match( $regex, $text, $m ) ) {
819 return $this->parseMatch( $m );
823 return array( false, false );
827 * Match some text, without parameter capture
828 * Returns the magic word name, or false if there was no capture
830 * @param $text string
832 * @return string|bool False on failure
834 public function matchStartToEnd( $text ) {
835 $hash = $this->getHash();
836 if ( isset( $hash[1][$text] ) ) {
837 return $hash[1][$text];
840 $lc = $wgContLang->lc( $text );
841 if ( isset( $hash[0][$lc] ) ) {
842 return $hash[0][$lc];
848 * Returns an associative array, ID => param value, for all items that match
849 * Removes the matched items from the input string (passed by reference)
851 * @param $text string
855 public function matchAndRemove( &$text ) {
857 $regexes = $this->getRegex();
858 foreach ( $regexes as $regex ) {
859 if ( $regex === '' ) {
862 preg_match_all( $regex, $text, $matches, PREG_SET_ORDER
);
863 foreach ( $matches as $m ) {
864 list( $name, $param ) = $this->parseMatch( $m );
865 $found[$name] = $param;
867 $text = preg_replace( $regex, '', $text );
873 * Return the ID of the magic word at the start of $text, and remove
874 * the prefix from $text.
875 * Return false if no match found and $text is not modified.
876 * Does not match parameters.
878 * @param $text string
880 * @return int|bool False on failure
882 public function matchStartAndRemove( &$text ) {
883 $regexes = $this->getRegexStart();
884 foreach ( $regexes as $regex ) {
885 if ( $regex === '' ) {
888 if ( preg_match( $regex, $text, $m ) ) {
889 list( $id, ) = $this->parseMatch( $m );
890 if ( strlen( $m[0] ) >= strlen( $text ) ) {
893 $text = substr( $text, strlen( $m[0] ) );