3 * See docs/magicword.txt.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * This class encapsulates "magic words" such as "#redirect", __NOTOC__, etc.
29 * if (MagicWord::get( 'redirect' )->match( $text ) ) {
34 * Possible future improvements:
35 * * Simultaneous searching for a number of magic words
36 * * MagicWord::$mObjects in shared memory
38 * Please avoid reading the data out of one of these objects and then writing
39 * special case code. If possible, add another match()-like function here.
41 * To add magic words in an extension, use $magicWords in a file listed in
42 * $wgExtensionMessagesFiles[].
46 * $magicWords = array();
48 * $magicWords['en'] = array(
49 * 'magicwordkey' => array( 0, 'case_insensitive_magic_word' ),
50 * 'magicwordkey2' => array( 1, 'CASE_sensitive_magic_word2' ),
54 * For magic words which are also Parser variables, add a MagicWordwgVariableIDs
55 * hook. Use string keys.
69 public $mCaseSensitive;
75 private $mRegexStart = '';
78 private $mRegexStartToEnd = '';
81 private $mBaseRegex = '';
84 private $mVariableRegex = '';
87 private $mVariableStartToEndRegex = '';
90 private $mModified = false;
93 private $mFound = false;
95 public static $mVariableIDsInitialised = false;
96 public static $mVariableIDs = [
101 'currentmonthnamegen',
102 'currentmonthabbrev',
161 'numberofactiveusers',
176 /* Array of caching hints for ParserCache */
177 public static $mCacheTTLs = [
178 'currentmonth' => 86400,
179 'currentmonth1' => 86400,
180 'currentmonthname' => 86400,
181 'currentmonthnamegen' => 86400,
182 'currentmonthabbrev' => 86400,
183 'currentday' => 3600,
184 'currentday2' => 3600,
185 'currentdayname' => 3600,
186 'currentyear' => 86400,
187 'currenttime' => 3600,
188 'currenthour' => 3600,
189 'localmonth' => 86400,
190 'localmonth1' => 86400,
191 'localmonthname' => 86400,
192 'localmonthnamegen' => 86400,
193 'localmonthabbrev' => 86400,
196 'localdayname' => 3600,
197 'localyear' => 86400,
200 'numberofarticles' => 3600,
201 'numberoffiles' => 3600,
202 'numberofedits' => 3600,
203 'currentweek' => 3600,
204 'currentdow' => 3600,
207 'numberofusers' => 3600,
208 'numberofactiveusers' => 3600,
209 'numberofpages' => 3600,
210 'currentversion' => 86400,
211 'currenttimestamp' => 3600,
212 'localtimestamp' => 3600,
213 'pagesinnamespace' => 3600,
214 'numberofadmins' => 3600,
215 'numberingroup' => 3600,
218 public static $mDoubleUnderscoreIDs = [
234 public static $mSubstIDs = [
239 public static $mObjects = [];
240 public static $mDoubleUnderscoreArray = null;
244 public function __construct( $id = 0, $syn = [], $cs = false ) {
246 $this->mSynonyms
= (array)$syn;
247 $this->mCaseSensitive
= $cs;
251 * Factory: creates an object representing an ID
257 public static function &get( $id ) {
258 if ( !isset( self
::$mObjects[$id] ) ) {
259 $mw = new MagicWord();
261 self
::$mObjects[$id] = $mw;
263 return self
::$mObjects[$id];
267 * Get an array of parser variable IDs
271 public static function getVariableIDs() {
272 if ( !self
::$mVariableIDsInitialised ) {
274 Hooks
::run( 'MagicWordwgVariableIDs', [ &self
::$mVariableIDs ] );
275 self
::$mVariableIDsInitialised = true;
277 return self
::$mVariableIDs;
281 * Get an array of parser substitution modifier IDs
284 public static function getSubstIDs() {
285 return self
::$mSubstIDs;
289 * Allow external reads of TTL array
294 public static function getCacheTTL( $id ) {
295 if ( array_key_exists( $id, self
::$mCacheTTLs ) ) {
296 return self
::$mCacheTTLs[$id];
303 * Get a MagicWordArray of double-underscore entities
305 * @return MagicWordArray
307 public static function getDoubleUnderscoreArray() {
308 if ( is_null( self
::$mDoubleUnderscoreArray ) ) {
309 Hooks
::run( 'GetDoubleUnderscoreIDs', [ &self
::$mDoubleUnderscoreIDs ] );
310 self
::$mDoubleUnderscoreArray = new MagicWordArray( self
::$mDoubleUnderscoreIDs );
312 return self
::$mDoubleUnderscoreArray;
316 * Clear the self::$mObjects variable
317 * For use in parser tests
319 public static function clearCache() {
320 self
::$mObjects = [];
324 * Initialises this object with an ID
327 * @throws MWException
329 public function load( $id ) {
332 $wgContLang->getMagic( $this );
333 if ( !$this->mSynonyms
) {
334 $this->mSynonyms
= [ 'brionmademeputthishere' ];
335 throw new MWException( "Error: invalid magic word '$id'" );
340 * Preliminary initialisation
343 public function initRegex() {
344 // Sort the synonyms by length, descending, so that the longest synonym
345 // matches in precedence to the shortest
346 $synonyms = $this->mSynonyms
;
347 usort( $synonyms, [ $this, 'compareStringLength' ] );
350 foreach ( $synonyms as $synonym ) {
351 // In case a magic word contains /, like that's going to happen;)
352 $escSyn[] = preg_quote( $synonym, '/' );
354 $this->mBaseRegex
= implode( '|', $escSyn );
356 $case = $this->mCaseSensitive ?
'' : 'iu';
357 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
358 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
359 $this->mRegexStartToEnd
= "/^(?:{$this->mBaseRegex})$/{$case}";
360 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
361 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
362 "/^(?:{$this->mBaseRegex})$/{$case}" );
366 * A comparison function that returns -1, 0 or 1 depending on whether the
367 * first string is longer, the same length or shorter than the second
375 public function compareStringLength( $s1, $s2 ) {
380 } elseif ( $l1 > $l2 ) {
388 * Gets a regex representing matching the word
392 public function getRegex() {
393 if ( $this->mRegex
== '' ) {
396 return $this->mRegex
;
400 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
401 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
402 * the complete expression
406 public function getRegexCase() {
407 if ( $this->mRegex
=== '' ) {
411 return $this->mCaseSensitive ?
'' : 'iu';
415 * Gets a regex matching the word, if it is at the string start
419 public function getRegexStart() {
420 if ( $this->mRegex
== '' ) {
423 return $this->mRegexStart
;
427 * Gets a regex matching the word from start to end of a string
432 public function getRegexStartToEnd() {
433 if ( $this->mRegexStartToEnd
== '' ) {
436 return $this->mRegexStartToEnd
;
440 * regex without the slashes and what not
444 public function getBaseRegex() {
445 if ( $this->mRegex
== '' ) {
448 return $this->mBaseRegex
;
452 * Returns true if the text contains the word
454 * @param string $text
458 public function match( $text ) {
459 return (bool)preg_match( $this->getRegex(), $text );
463 * Returns true if the text starts with the word
465 * @param string $text
469 public function matchStart( $text ) {
470 return (bool)preg_match( $this->getRegexStart(), $text );
474 * Returns true if the text matched the word
476 * @param string $text
481 public function matchStartToEnd( $text ) {
482 return (bool)preg_match( $this->getRegexStartToEnd(), $text );
486 * Returns NULL if there's no match, the value of $1 otherwise
487 * The return code is the matched string, if there's no variable
488 * part in the regex and the matched variable part ($1) if there
491 * @param string $text
495 public function matchVariableStartToEnd( $text ) {
497 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
498 if ( $matchcount == 0 ) {
501 # multiple matched parts (variable match); some will be empty because of
502 # synonyms. The variable will be the second non-empty one so remove any
503 # blank elements and re-sort the indices.
506 $matches = array_values( array_filter( $matches ) );
508 if ( count( $matches ) == 1 ) {
517 * Returns true if the text matches the word, and alters the
518 * input string, removing all instances of the word
520 * @param string $text
524 public function matchAndRemove( &$text ) {
525 $this->mFound
= false;
526 $text = preg_replace_callback(
528 [ &$this, 'pregRemoveAndRecord' ],
532 return $this->mFound
;
536 * @param string $text
539 public function matchStartAndRemove( &$text ) {
540 $this->mFound
= false;
541 $text = preg_replace_callback(
542 $this->getRegexStart(),
543 [ &$this, 'pregRemoveAndRecord' ],
547 return $this->mFound
;
551 * Used in matchAndRemove()
555 public function pregRemoveAndRecord() {
556 $this->mFound
= true;
561 * Replaces the word with something else
563 * @param string $replacement
564 * @param string $subject
569 public function replace( $replacement, $subject, $limit = -1 ) {
572 StringUtils
::escapeRegexReplacement( $replacement ),
576 $this->mModified
= $res !== $subject;
581 * Variable handling: {{SUBST:xxx}} style words
582 * Calls back a function to determine what to replace xxx with
583 * Input word must contain $1
585 * @param string $text
586 * @param callable $callback
590 public function substituteCallback( $text, $callback ) {
591 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
592 $this->mModified
= $res !== $text;
597 * Matches the word, where $1 is a wildcard
601 public function getVariableRegex() {
602 if ( $this->mVariableRegex
== '' ) {
605 return $this->mVariableRegex
;
609 * Matches the entire string, where $1 is a wildcard
613 public function getVariableStartToEndRegex() {
614 if ( $this->mVariableStartToEndRegex
== '' ) {
617 return $this->mVariableStartToEndRegex
;
621 * Accesses the synonym list directly
627 public function getSynonym( $i ) {
628 return $this->mSynonyms
[$i];
634 public function getSynonyms() {
635 return $this->mSynonyms
;
639 * Returns true if the last call to replace() or substituteCallback()
640 * returned a modified text, otherwise false.
644 public function getWasModified() {
645 return $this->mModified
;
649 * $magicarr is an associative array of (magic word ID => replacement)
650 * This method uses the php feature to do several replacements at the same time,
651 * thereby gaining some efficiency. The result is placed in the out variable
652 * $result. The return value is true if something was replaced.
653 * @deprecated since 1.25, unused
655 * @param array $magicarr
656 * @param string $subject
657 * @param string $result
661 public function replaceMultiple( $magicarr, $subject, &$result ) {
662 wfDeprecated( __METHOD__
, '1.25' );
665 foreach ( $magicarr as $id => $replacement ) {
666 $mw = MagicWord
::get( $id );
667 $search[] = $mw->getRegex();
668 $replace[] = $replacement;
671 $result = preg_replace( $search, $replace, $subject );
672 return $result !== $subject;
676 * Adds all the synonyms of this MagicWord to an array, to allow quick
677 * lookup in a list of magic words
679 * @param array $array
680 * @param string $value
682 public function addToArray( &$array, $value ) {
684 foreach ( $this->mSynonyms
as $syn ) {
685 $array[$wgContLang->lc( $syn )] = $value;
692 public function isCaseSensitive() {
693 return $this->mCaseSensitive
;
699 public function getId() {