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
26 use MediaWiki\Logger\LoggerFactory
;
29 * This class encapsulates "magic words" such as "#redirect", __NOTOC__, etc.
33 * if (MagicWord::get( 'redirect' )->match( $text ) ) {
38 * Possible future improvements:
39 * * Simultaneous searching for a number of magic words
40 * * MagicWord::$mObjects in shared memory
42 * Please avoid reading the data out of one of these objects and then writing
43 * special case code. If possible, add another match()-like function here.
45 * To add magic words in an extension, use $magicWords in a file listed in
46 * $wgExtensionMessagesFiles[].
50 * $magicWords = array();
52 * $magicWords['en'] = array(
53 * 'magicwordkey' => array( 0, 'case_insensitive_magic_word' ),
54 * 'magicwordkey2' => array( 1, 'CASE_sensitive_magic_word2' ),
58 * For magic words which are also Parser variables, add a MagicWordwgVariableIDs
59 * hook. Use string keys.
73 public $mCaseSensitive;
79 private $mRegexStart = '';
82 private $mRegexStartToEnd = '';
85 private $mBaseRegex = '';
88 private $mVariableRegex = '';
91 private $mVariableStartToEndRegex = '';
94 private $mModified = false;
97 private $mFound = false;
99 static public $mVariableIDsInitialised = false;
100 static public $mVariableIDs = array(
105 'currentmonthnamegen',
106 'currentmonthabbrev',
165 'numberofactiveusers',
180 /* Array of caching hints for ParserCache */
181 static public $mCacheTTLs = array(
182 'currentmonth' => 86400,
183 'currentmonth1' => 86400,
184 'currentmonthname' => 86400,
185 'currentmonthnamegen' => 86400,
186 'currentmonthabbrev' => 86400,
187 'currentday' => 3600,
188 'currentday2' => 3600,
189 'currentdayname' => 3600,
190 'currentyear' => 86400,
191 'currenttime' => 3600,
192 'currenthour' => 3600,
193 'localmonth' => 86400,
194 'localmonth1' => 86400,
195 'localmonthname' => 86400,
196 'localmonthnamegen' => 86400,
197 'localmonthabbrev' => 86400,
200 'localdayname' => 3600,
201 'localyear' => 86400,
204 'numberofarticles' => 3600,
205 'numberoffiles' => 3600,
206 'numberofedits' => 3600,
207 'currentweek' => 3600,
208 'currentdow' => 3600,
211 'numberofusers' => 3600,
212 'numberofactiveusers' => 3600,
213 'numberofpages' => 3600,
214 'currentversion' => 86400,
215 'currenttimestamp' => 3600,
216 'localtimestamp' => 3600,
217 'pagesinnamespace' => 3600,
218 'numberofadmins' => 3600,
219 'numberingroup' => 3600,
222 static public $mDoubleUnderscoreIDs = array(
238 static public $mSubstIDs = array(
243 static public $mObjects = array();
244 static public $mDoubleUnderscoreArray = null;
248 function __construct( $id = 0, $syn = array(), $cs = false ) {
250 $this->mSynonyms
= (array)$syn;
251 $this->mCaseSensitive
= $cs;
255 * Factory: creates an object representing an ID
261 static function &get( $id ) {
262 if ( !isset( self
::$mObjects[$id] ) ) {
263 $mw = new MagicWord();
265 self
::$mObjects[$id] = $mw;
267 return self
::$mObjects[$id];
271 * Get an array of parser variable IDs
275 static function getVariableIDs() {
276 if ( !self
::$mVariableIDsInitialised ) {
278 Hooks
::run( 'MagicWordwgVariableIDs', array( &self
::$mVariableIDs ) );
279 self
::$mVariableIDsInitialised = true;
281 return self
::$mVariableIDs;
285 * Get an array of parser substitution modifier IDs
288 static function getSubstIDs() {
289 return self
::$mSubstIDs;
293 * Allow external reads of TTL array
298 static function getCacheTTL( $id ) {
299 if ( array_key_exists( $id, self
::$mCacheTTLs ) ) {
300 return self
::$mCacheTTLs[$id];
307 * Get a MagicWordArray of double-underscore entities
309 * @return MagicWordArray
311 static function getDoubleUnderscoreArray() {
312 if ( is_null( self
::$mDoubleUnderscoreArray ) ) {
313 Hooks
::run( 'GetDoubleUnderscoreIDs', array( &self
::$mDoubleUnderscoreIDs ) );
314 self
::$mDoubleUnderscoreArray = new MagicWordArray( self
::$mDoubleUnderscoreIDs );
316 return self
::$mDoubleUnderscoreArray;
320 * Clear the self::$mObjects variable
321 * For use in parser tests
323 public static function clearCache() {
324 self
::$mObjects = array();
328 * Initialises this object with an ID
331 * @throws MWException
333 function load( $id ) {
336 $wgContLang->getMagic( $this );
337 if ( !$this->mSynonyms
) {
338 $this->mSynonyms
= array( 'brionmademeputthishere' );
339 throw new MWException( "Error: invalid magic word '$id'" );
344 * Preliminary initialisation
347 function initRegex() {
348 // Sort the synonyms by length, descending, so that the longest synonym
349 // matches in precedence to the shortest
350 $synonyms = $this->mSynonyms
;
351 usort( $synonyms, array( $this, 'compareStringLength' ) );
354 foreach ( $synonyms as $synonym ) {
355 // In case a magic word contains /, like that's going to happen;)
356 $escSyn[] = preg_quote( $synonym, '/' );
358 $this->mBaseRegex
= implode( '|', $escSyn );
360 $case = $this->mCaseSensitive ?
'' : 'iu';
361 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
362 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
363 $this->mRegexStartToEnd
= "/^(?:{$this->mBaseRegex})$/{$case}";
364 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
365 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
366 "/^(?:{$this->mBaseRegex})$/{$case}" );
370 * A comparison function that returns -1, 0 or 1 depending on whether the
371 * first string is longer, the same length or shorter than the second
379 function compareStringLength( $s1, $s2 ) {
384 } elseif ( $l1 > $l2 ) {
392 * Gets a regex representing matching the word
396 function getRegex() {
397 if ( $this->mRegex
== '' ) {
400 return $this->mRegex
;
404 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
405 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
406 * the complete expression
410 function getRegexCase() {
411 if ( $this->mRegex
=== '' ) {
415 return $this->mCaseSensitive ?
'' : 'iu';
419 * Gets a regex matching the word, if it is at the string start
423 function getRegexStart() {
424 if ( $this->mRegex
== '' ) {
427 return $this->mRegexStart
;
431 * Gets a regex matching the word from start to end of a string
436 function getRegexStartToEnd() {
437 if ( $this->mRegexStartToEnd
== '' ) {
440 return $this->mRegexStartToEnd
;
444 * regex without the slashes and what not
448 function getBaseRegex() {
449 if ( $this->mRegex
== '' ) {
452 return $this->mBaseRegex
;
456 * Returns true if the text contains the word
458 * @param string $text
462 function match( $text ) {
463 return (bool)preg_match( $this->getRegex(), $text );
467 * Returns true if the text starts with the word
469 * @param string $text
473 function matchStart( $text ) {
474 return (bool)preg_match( $this->getRegexStart(), $text );
478 * Returns true if the text matched the word
480 * @param string $text
485 function matchStartToEnd( $text ) {
486 return (bool)preg_match( $this->getRegexStartToEnd(), $text );
490 * Returns NULL if there's no match, the value of $1 otherwise
491 * The return code is the matched string, if there's no variable
492 * part in the regex and the matched variable part ($1) if there
495 * @param string $text
499 function matchVariableStartToEnd( $text ) {
501 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
502 if ( $matchcount == 0 ) {
505 # multiple matched parts (variable match); some will be empty because of
506 # synonyms. The variable will be the second non-empty one so remove any
507 # blank elements and re-sort the indices.
510 $matches = array_values( array_filter( $matches ) );
512 if ( count( $matches ) == 1 ) {
521 * Returns true if the text matches the word, and alters the
522 * input string, removing all instances of the word
524 * @param string $text
528 function matchAndRemove( &$text ) {
529 $this->mFound
= false;
530 $text = preg_replace_callback(
532 array( &$this, 'pregRemoveAndRecord' ),
536 return $this->mFound
;
540 * @param string $text
543 function matchStartAndRemove( &$text ) {
544 $this->mFound
= false;
545 $text = preg_replace_callback(
546 $this->getRegexStart(),
547 array( &$this, 'pregRemoveAndRecord' ),
551 return $this->mFound
;
555 * Used in matchAndRemove()
559 function pregRemoveAndRecord() {
560 $this->mFound
= true;
565 * Replaces the word with something else
567 * @param string $replacement
568 * @param string $subject
573 function replace( $replacement, $subject, $limit = -1 ) {
576 StringUtils
::escapeRegexReplacement( $replacement ),
580 $this->mModified
= $res !== $subject;
585 * Variable handling: {{SUBST:xxx}} style words
586 * Calls back a function to determine what to replace xxx with
587 * Input word must contain $1
589 * @param string $text
590 * @param callable $callback
594 function substituteCallback( $text, $callback ) {
595 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
596 $this->mModified
= $res !== $text;
601 * Matches the word, where $1 is a wildcard
605 function getVariableRegex() {
606 if ( $this->mVariableRegex
== '' ) {
609 return $this->mVariableRegex
;
613 * Matches the entire string, where $1 is a wildcard
617 function getVariableStartToEndRegex() {
618 if ( $this->mVariableStartToEndRegex
== '' ) {
621 return $this->mVariableStartToEndRegex
;
625 * Accesses the synonym list directly
631 function getSynonym( $i ) {
632 return $this->mSynonyms
[$i];
638 function getSynonyms() {
639 return $this->mSynonyms
;
643 * Returns true if the last call to replace() or substituteCallback()
644 * returned a modified text, otherwise false.
648 function getWasModified() {
649 return $this->mModified
;
653 * $magicarr is an associative array of (magic word ID => replacement)
654 * This method uses the php feature to do several replacements at the same time,
655 * thereby gaining some efficiency. The result is placed in the out variable
656 * $result. The return value is true if something was replaced.
657 * @deprecated since 1.25, unused
659 * @param array $magicarr
660 * @param string $subject
661 * @param string $result
665 function replaceMultiple( $magicarr, $subject, &$result ) {
666 wfDeprecated( __METHOD__
, '1.25' );
669 foreach ( $magicarr as $id => $replacement ) {
670 $mw = MagicWord
::get( $id );
671 $search[] = $mw->getRegex();
672 $replace[] = $replacement;
675 $result = preg_replace( $search, $replace, $subject );
676 return $result !== $subject;
680 * Adds all the synonyms of this MagicWord to an array, to allow quick
681 * lookup in a list of magic words
683 * @param array $array
684 * @param string $value
686 function addToArray( &$array, $value ) {
688 foreach ( $this->mSynonyms
as $syn ) {
689 $array[$wgContLang->lc( $syn )] = $value;
696 function isCaseSensitive() {
697 return $this->mCaseSensitive
;
709 * Class for handling an array of magic words
712 class MagicWordArray
{
714 public $names = array();
724 * @param array $names
726 function __construct( $names = array() ) {
727 $this->names
= $names;
731 * Add a magic word by name
733 * @param string $name
735 public function add( $name ) {
736 $this->names
[] = $name;
737 $this->hash
= $this->baseRegex
= $this->regex
= null;
741 * Add a number of magic words by name
743 * @param array $names
745 public function addArray( $names ) {
746 $this->names
= array_merge( $this->names
, array_values( $names ) );
747 $this->hash
= $this->baseRegex
= $this->regex
= null;
751 * Get a 2-d hashtable for this array
755 if ( is_null( $this->hash
) ) {
757 $this->hash
= array( 0 => array(), 1 => array() );
758 foreach ( $this->names
as $name ) {
759 $magic = MagicWord
::get( $name );
760 $case = intval( $magic->isCaseSensitive() );
761 foreach ( $magic->getSynonyms() as $syn ) {
763 $syn = $wgContLang->lc( $syn );
765 $this->hash
[$case][$syn] = $name;
776 function getBaseRegex() {
777 if ( is_null( $this->baseRegex
) ) {
778 $this->baseRegex
= array( 0 => '', 1 => '' );
779 foreach ( $this->names
as $name ) {
780 $magic = MagicWord
::get( $name );
781 $case = intval( $magic->isCaseSensitive() );
782 foreach ( $magic->getSynonyms() as $i => $syn ) {
783 // Group name must start with a non-digit in PCRE 8.34+
784 $it = strtr( $i, '0123456789', 'abcdefghij' );
785 $group = "(?P<{$it}_{$name}>" . preg_quote( $syn, '/' ) . ')';
786 if ( $this->baseRegex
[$case] === '' ) {
787 $this->baseRegex
[$case] = $group;
789 $this->baseRegex
[$case] .= '|' . $group;
794 return $this->baseRegex
;
798 * Get an unanchored regex that does not match parameters
801 function getRegex() {
802 if ( is_null( $this->regex
) ) {
803 $base = $this->getBaseRegex();
804 $this->regex
= array( '', '' );
805 if ( $this->baseRegex
[0] !== '' ) {
806 $this->regex
[0] = "/{$base[0]}/iuS";
808 if ( $this->baseRegex
[1] !== '' ) {
809 $this->regex
[1] = "/{$base[1]}/S";
816 * Get a regex for matching variables with parameters
820 function getVariableRegex() {
821 return str_replace( "\\$1", "(.*?)", $this->getRegex() );
825 * Get a regex anchored to the start of the string that does not match parameters
829 function getRegexStart() {
830 $base = $this->getBaseRegex();
831 $newRegex = array( '', '' );
832 if ( $base[0] !== '' ) {
833 $newRegex[0] = "/^(?:{$base[0]})/iuS";
835 if ( $base[1] !== '' ) {
836 $newRegex[1] = "/^(?:{$base[1]})/S";
842 * Get an anchored regex for matching variables with parameters
846 function getVariableStartToEndRegex() {
847 $base = $this->getBaseRegex();
848 $newRegex = array( '', '' );
849 if ( $base[0] !== '' ) {
850 $newRegex[0] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[0]})$/iuS" );
852 if ( $base[1] !== '' ) {
853 $newRegex[1] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[1]})$/S" );
862 public function getNames() {
867 * Parse a match array from preg_match
868 * Returns array(magic word ID, parameter value)
869 * If there is no parameter value, that element will be false.
873 * @throws MWException
876 function parseMatch( $m ) {
878 while ( list( $key, $value ) = each( $m ) ) {
879 if ( $key === 0 ||
$value === '' ) {
882 $parts = explode( '_', $key, 2 );
883 if ( count( $parts ) != 2 ) {
884 // This shouldn't happen
886 throw new MWException( __METHOD__
. ': bad parameter name' );
888 list( /* $synIndex */, $magicName ) = $parts;
889 $paramValue = next( $m );
890 return array( $magicName, $paramValue );
892 // This shouldn't happen either
893 throw new MWException( __METHOD__
. ': parameter not found' );
897 * Match some text, with parameter capture
898 * Returns an array with the magic word name in the first element and the
899 * parameter in the second element.
900 * Both elements are false if there was no match.
902 * @param string $text
906 public function matchVariableStartToEnd( $text ) {
907 $regexes = $this->getVariableStartToEndRegex();
908 foreach ( $regexes as $regex ) {
909 if ( $regex !== '' ) {
911 if ( preg_match( $regex, $text, $m ) ) {
912 return $this->parseMatch( $m );
916 return array( false, false );
920 * Match some text, without parameter capture
921 * Returns the magic word name, or false if there was no capture
923 * @param string $text
925 * @return string|bool False on failure
927 public function matchStartToEnd( $text ) {
928 $hash = $this->getHash();
929 if ( isset( $hash[1][$text] ) ) {
930 return $hash[1][$text];
933 $lc = $wgContLang->lc( $text );
934 if ( isset( $hash[0][$lc] ) ) {
935 return $hash[0][$lc];
941 * Returns an associative array, ID => param value, for all items that match
942 * Removes the matched items from the input string (passed by reference)
944 * @param string $text
948 public function matchAndRemove( &$text ) {
950 $regexes = $this->getRegex();
951 foreach ( $regexes as $regex ) {
952 if ( $regex === '' ) {
956 $res = preg_match_all( $regex, $text, $matches, PREG_SET_ORDER
);
957 if ( $res === false ) {
958 LoggerFactory
::getInstance( 'parser' )->warning( 'preg_match_all returned false', array(
959 'code' => preg_last_error(),
964 foreach ( $matches as $m ) {
965 list( $name, $param ) = $this->parseMatch( $m );
966 $found[$name] = $param;
969 $res = preg_replace( $regex, '', $text );
970 if ( $res === null ) {
971 LoggerFactory
::getInstance( 'parser' )->warning( 'preg_replace returned null', array(
972 'code' => preg_last_error(),
983 * Return the ID of the magic word at the start of $text, and remove
984 * the prefix from $text.
985 * Return false if no match found and $text is not modified.
986 * Does not match parameters.
988 * @param string $text
990 * @return int|bool False on failure
992 public function matchStartAndRemove( &$text ) {
993 $regexes = $this->getRegexStart();
994 foreach ( $regexes as $regex ) {
995 if ( $regex === '' ) {
998 if ( preg_match( $regex, $text, $m ) ) {
999 list( $id, ) = $this->parseMatch( $m );
1000 if ( strlen( $m[0] ) >= strlen( $text ) ) {
1003 $text = substr( $text, strlen( $m[0] ) );