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',
138 'numberofactiveusers',
153 /* Array of caching hints for ParserCache */
154 static public $mCacheTTLs = array(
155 'currentmonth' => 86400,
156 'currentmonth1' => 86400,
157 'currentmonthname' => 86400,
158 'currentmonthnamegen' => 86400,
159 'currentmonthabbrev' => 86400,
160 'currentday' => 3600,
161 'currentday2' => 3600,
162 'currentdayname' => 3600,
163 'currentyear' => 86400,
164 'currenttime' => 3600,
165 'currenthour' => 3600,
166 'localmonth' => 86400,
167 'localmonth1' => 86400,
168 'localmonthname' => 86400,
169 'localmonthnamegen' => 86400,
170 'localmonthabbrev' => 86400,
173 'localdayname' => 3600,
174 'localyear' => 86400,
177 'numberofarticles' => 3600,
178 'numberoffiles' => 3600,
179 'numberofedits' => 3600,
180 'currentweek' => 3600,
181 'currentdow' => 3600,
184 'numberofusers' => 3600,
185 'numberofactiveusers' => 3600,
186 'numberofpages' => 3600,
187 'currentversion' => 86400,
188 'currenttimestamp' => 3600,
189 'localtimestamp' => 3600,
190 'pagesinnamespace' => 3600,
191 'numberofadmins' => 3600,
192 'numberofviews' => 3600,
193 'numberingroup' => 3600,
196 static public $mDoubleUnderscoreIDs = array(
212 static public $mSubstIDs = array(
217 static public $mObjects = array();
218 static public $mDoubleUnderscoreArray = null;
222 function __construct( $id = 0, $syn = array(), $cs = false ) {
224 $this->mSynonyms
= (array)$syn;
225 $this->mCaseSensitive
= $cs;
229 * Factory: creates an object representing an ID
235 static function &get( $id ) {
236 if ( !isset( self
::$mObjects[$id] ) ) {
237 $mw = new MagicWord();
239 self
::$mObjects[$id] = $mw;
241 return self
::$mObjects[$id];
245 * Get an array of parser variable IDs
249 static function getVariableIDs() {
250 if ( !self
::$mVariableIDsInitialised ) {
252 wfRunHooks( 'MagicWordwgVariableIDs', array( &self
::$mVariableIDs ) );
253 self
::$mVariableIDsInitialised = true;
255 return self
::$mVariableIDs;
259 * Get an array of parser substitution modifier IDs
262 static function getSubstIDs() {
263 return self
::$mSubstIDs;
267 * Allow external reads of TTL array
272 static function getCacheTTL( $id ) {
273 if ( array_key_exists( $id, self
::$mCacheTTLs ) ) {
274 return self
::$mCacheTTLs[$id];
281 * Get a MagicWordArray of double-underscore entities
283 * @return MagicWordArray
285 static function getDoubleUnderscoreArray() {
286 if ( is_null( self
::$mDoubleUnderscoreArray ) ) {
287 wfRunHooks( 'GetDoubleUnderscoreIDs', array( &self
::$mDoubleUnderscoreIDs ) );
288 self
::$mDoubleUnderscoreArray = new MagicWordArray( self
::$mDoubleUnderscoreIDs );
290 return self
::$mDoubleUnderscoreArray;
294 * Clear the self::$mObjects variable
295 * For use in parser tests
297 public static function clearCache() {
298 self
::$mObjects = array();
302 * Initialises this object with an ID
305 * @throws MWException
307 function load( $id ) {
309 wfProfileIn( __METHOD__
);
311 $wgContLang->getMagic( $this );
312 if ( !$this->mSynonyms
) {
313 $this->mSynonyms
= array( 'dkjsagfjsgashfajsh' );
314 wfProfileOut( __METHOD__
);
315 throw new MWException( "Error: invalid magic word '$id'" );
316 #wfDebugLog( 'exception', "Error: invalid magic word '$id'\n" );
318 wfProfileOut( __METHOD__
);
322 * Preliminary initialisation
325 function initRegex() {
326 // Sort the synonyms by length, descending, so that the longest synonym
327 // matches in precedence to the shortest
328 $synonyms = $this->mSynonyms
;
329 usort( $synonyms, array( $this, 'compareStringLength' ) );
332 foreach ( $synonyms as $synonym ) {
333 // In case a magic word contains /, like that's going to happen;)
334 $escSyn[] = preg_quote( $synonym, '/' );
336 $this->mBaseRegex
= implode( '|', $escSyn );
338 $case = $this->mCaseSensitive ?
'' : 'iu';
339 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
340 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
341 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
342 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
343 "/^(?:{$this->mBaseRegex})$/{$case}" );
347 * A comparison function that returns -1, 0 or 1 depending on whether the
348 * first string is longer, the same length or shorter than the second
356 function compareStringLength( $s1, $s2 ) {
361 } elseif ( $l1 > $l2 ) {
369 * Gets a regex representing matching the word
373 function getRegex() {
374 if ( $this->mRegex
== '' ) {
377 return $this->mRegex
;
381 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
382 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
383 * the complete expression
387 function getRegexCase() {
388 if ( $this->mRegex
=== '' ) {
392 return $this->mCaseSensitive ?
'' : 'iu';
396 * Gets a regex matching the word, if it is at the string start
400 function getRegexStart() {
401 if ( $this->mRegex
== '' ) {
404 return $this->mRegexStart
;
408 * regex without the slashes and what not
412 function getBaseRegex() {
413 if ( $this->mRegex
== '' ) {
416 return $this->mBaseRegex
;
420 * Returns true if the text contains the word
422 * @param $text string
426 function match( $text ) {
427 return (bool)preg_match( $this->getRegex(), $text );
431 * Returns true if the text starts with the word
433 * @param $text string
437 function matchStart( $text ) {
438 return (bool)preg_match( $this->getRegexStart(), $text );
442 * Returns NULL if there's no match, the value of $1 otherwise
443 * The return code is the matched string, if there's no variable
444 * part in the regex and the matched variable part ($1) if there
447 * @param $text string
451 function matchVariableStartToEnd( $text ) {
453 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
454 if ( $matchcount == 0 ) {
457 # multiple matched parts (variable match); some will be empty because of
458 # synonyms. The variable will be the second non-empty one so remove any
459 # blank elements and re-sort the indices.
462 $matches = array_values( array_filter( $matches ) );
464 if ( count( $matches ) == 1 ) {
473 * Returns true if the text matches the word, and alters the
474 * input string, removing all instances of the word
476 * @param $text string
480 function matchAndRemove( &$text ) {
481 $this->mFound
= false;
482 $text = preg_replace_callback( $this->getRegex(), array( &$this, 'pregRemoveAndRecord' ), $text );
483 return $this->mFound
;
490 function matchStartAndRemove( &$text ) {
491 $this->mFound
= false;
492 $text = preg_replace_callback( $this->getRegexStart(), array( &$this, 'pregRemoveAndRecord' ), $text );
493 return $this->mFound
;
497 * Used in matchAndRemove()
501 function pregRemoveAndRecord() {
502 $this->mFound
= true;
507 * Replaces the word with something else
509 * @param $replacement
515 function replace( $replacement, $subject, $limit = -1 ) {
516 $res = preg_replace( $this->getRegex(), StringUtils
::escapeRegexReplacement( $replacement ), $subject, $limit );
517 $this->mModified
= $res !== $subject;
522 * Variable handling: {{SUBST:xxx}} style words
523 * Calls back a function to determine what to replace xxx with
524 * Input word must contain $1
526 * @param $text string
531 function substituteCallback( $text, $callback ) {
532 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
533 $this->mModified
= $res !== $text;
538 * Matches the word, where $1 is a wildcard
542 function getVariableRegex() {
543 if ( $this->mVariableRegex
== '' ) {
546 return $this->mVariableRegex
;
550 * Matches the entire string, where $1 is a wildcard
554 function getVariableStartToEndRegex() {
555 if ( $this->mVariableStartToEndRegex
== '' ) {
558 return $this->mVariableStartToEndRegex
;
562 * Accesses the synonym list directly
568 function getSynonym( $i ) {
569 return $this->mSynonyms
[$i];
575 function getSynonyms() {
576 return $this->mSynonyms
;
580 * Returns true if the last call to replace() or substituteCallback()
581 * returned a modified text, otherwise false.
585 function getWasModified() {
586 return $this->mModified
;
590 * $magicarr is an associative array of (magic word ID => replacement)
591 * This method uses the php feature to do several replacements at the same time,
592 * thereby gaining some efficiency. The result is placed in the out variable
593 * $result. The return value is true if something was replaced.
594 * @todo Should this be static? It doesn't seem to be used at all
602 function replaceMultiple( $magicarr, $subject, &$result ) {
605 foreach ( $magicarr as $id => $replacement ) {
606 $mw = MagicWord
::get( $id );
607 $search[] = $mw->getRegex();
608 $replace[] = $replacement;
611 $result = preg_replace( $search, $replace, $subject );
612 return $result !== $subject;
616 * Adds all the synonyms of this MagicWord to an array, to allow quick
617 * lookup in a list of magic words
622 function addToArray( &$array, $value ) {
624 foreach ( $this->mSynonyms
as $syn ) {
625 $array[$wgContLang->lc( $syn )] = $value;
632 function isCaseSensitive() {
633 return $this->mCaseSensitive
;
645 * Class for handling an array of magic words
648 class MagicWordArray
{
649 var $names = array();
651 var $baseRegex, $regex;
655 * @param $names array
657 function __construct( $names = array() ) {
658 $this->names
= $names;
662 * Add a magic word by name
664 * @param $name string
666 public function add( $name ) {
667 $this->names
[] = $name;
668 $this->hash
= $this->baseRegex
= $this->regex
= null;
672 * Add a number of magic words by name
674 * @param $names array
676 public function addArray( $names ) {
677 $this->names
= array_merge( $this->names
, array_values( $names ) );
678 $this->hash
= $this->baseRegex
= $this->regex
= null;
682 * Get a 2-d hashtable for this array
685 if ( is_null( $this->hash
) ) {
687 $this->hash
= array( 0 => array(), 1 => array() );
688 foreach ( $this->names
as $name ) {
689 $magic = MagicWord
::get( $name );
690 $case = intval( $magic->isCaseSensitive() );
691 foreach ( $magic->getSynonyms() as $syn ) {
693 $syn = $wgContLang->lc( $syn );
695 $this->hash
[$case][$syn] = $name;
705 function getBaseRegex() {
706 if ( is_null( $this->baseRegex
) ) {
707 $this->baseRegex
= array( 0 => '', 1 => '' );
708 foreach ( $this->names
as $name ) {
709 $magic = MagicWord
::get( $name );
710 $case = intval( $magic->isCaseSensitive() );
711 foreach ( $magic->getSynonyms() as $i => $syn ) {
712 $group = "(?P<{$i}_{$name}>" . preg_quote( $syn, '/' ) . ')';
713 if ( $this->baseRegex
[$case] === '' ) {
714 $this->baseRegex
[$case] = $group;
716 $this->baseRegex
[$case] .= '|' . $group;
721 return $this->baseRegex
;
725 * Get an unanchored regex that does not match parameters
727 function getRegex() {
728 if ( is_null( $this->regex
) ) {
729 $base = $this->getBaseRegex();
730 $this->regex
= array( '', '' );
731 if ( $this->baseRegex
[0] !== '' ) {
732 $this->regex
[0] = "/{$base[0]}/iuS";
734 if ( $this->baseRegex
[1] !== '' ) {
735 $this->regex
[1] = "/{$base[1]}/S";
742 * Get a regex for matching variables with parameters
746 function getVariableRegex() {
747 return str_replace( "\\$1", "(.*?)", $this->getRegex() );
751 * Get a regex anchored to the start of the string that does not match parameters
755 function getRegexStart() {
756 $base = $this->getBaseRegex();
757 $newRegex = array( '', '' );
758 if ( $base[0] !== '' ) {
759 $newRegex[0] = "/^(?:{$base[0]})/iuS";
761 if ( $base[1] !== '' ) {
762 $newRegex[1] = "/^(?:{$base[1]})/S";
768 * Get an anchored regex for matching variables with parameters
772 function getVariableStartToEndRegex() {
773 $base = $this->getBaseRegex();
774 $newRegex = array( '', '' );
775 if ( $base[0] !== '' ) {
776 $newRegex[0] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[0]})$/iuS" );
778 if ( $base[1] !== '' ) {
779 $newRegex[1] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[1]})$/S" );
788 public function getNames() {
793 * Parse a match array from preg_match
794 * Returns array(magic word ID, parameter value)
795 * If there is no parameter value, that element will be false.
799 * @throws MWException
802 function parseMatch( $m ) {
804 while ( list( $key, $value ) = each( $m ) ) {
805 if ( $key === 0 ||
$value === '' ) {
808 $parts = explode( '_', $key, 2 );
809 if ( count( $parts ) != 2 ) {
810 // This shouldn't happen
812 throw new MWException( __METHOD__
. ': bad parameter name' );
814 list( /* $synIndex */, $magicName ) = $parts;
815 $paramValue = next( $m );
816 return array( $magicName, $paramValue );
818 // This shouldn't happen either
819 throw new MWException( __METHOD__
. ': parameter not found' );
823 * Match some text, with parameter capture
824 * Returns an array with the magic word name in the first element and the
825 * parameter in the second element.
826 * Both elements are false if there was no match.
828 * @param $text string
832 public function matchVariableStartToEnd( $text ) {
833 $regexes = $this->getVariableStartToEndRegex();
834 foreach ( $regexes as $regex ) {
835 if ( $regex !== '' ) {
837 if ( preg_match( $regex, $text, $m ) ) {
838 return $this->parseMatch( $m );
842 return array( false, false );
846 * Match some text, without parameter capture
847 * Returns the magic word name, or false if there was no capture
849 * @param $text string
851 * @return string|bool False on failure
853 public function matchStartToEnd( $text ) {
854 $hash = $this->getHash();
855 if ( isset( $hash[1][$text] ) ) {
856 return $hash[1][$text];
859 $lc = $wgContLang->lc( $text );
860 if ( isset( $hash[0][$lc] ) ) {
861 return $hash[0][$lc];
867 * Returns an associative array, ID => param value, for all items that match
868 * Removes the matched items from the input string (passed by reference)
870 * @param $text string
874 public function matchAndRemove( &$text ) {
876 $regexes = $this->getRegex();
877 foreach ( $regexes as $regex ) {
878 if ( $regex === '' ) {
881 preg_match_all( $regex, $text, $matches, PREG_SET_ORDER
);
882 foreach ( $matches as $m ) {
883 list( $name, $param ) = $this->parseMatch( $m );
884 $found[$name] = $param;
886 $text = preg_replace( $regex, '', $text );
892 * Return the ID of the magic word at the start of $text, and remove
893 * the prefix from $text.
894 * Return false if no match found and $text is not modified.
895 * Does not match parameters.
897 * @param $text string
899 * @return int|bool False on failure
901 public function matchStartAndRemove( &$text ) {
902 $regexes = $this->getRegexStart();
903 foreach ( $regexes as $regex ) {
904 if ( $regex === '' ) {
907 if ( preg_match( $regex, $text, $m ) ) {
908 list( $id, ) = $this->parseMatch( $m );
909 if ( strlen( $m[0] ) >= strlen( $text ) ) {
912 $text = substr( $text, strlen( $m[0] ) );