110 $this->mSynonyms = (array)$syn;
111 $this->mCaseSensitive = $cs;
112 $this->contLang =
$contLang ?: MediaWikiServices::getInstance()->getContentLanguage();
123 public static function get( $id ) {
125 return MediaWikiServices::getInstance()->getMagicWordFactory()->get( $id );
136 return MediaWikiServices::getInstance()->getMagicWordFactory()->getVariableIDs();
146 return MediaWikiServices::getInstance()->getMagicWordFactory()->getSubstIDs();
158 return MediaWikiServices::getInstance()->getMagicWordFactory()->getCacheTTL( $id );
169 return MediaWikiServices::getInstance()->getMagicWordFactory()->getDoubleUnderscoreArray();
180 $this->contLang->getMagic( $this );
181 if ( !$this->mSynonyms ) {
182 $this->mSynonyms = [
'brionmademeputthishere' ];
183 throw new MWException(
"Error: invalid magic word '$id'" );
195 usort( $synonyms, [ $this,
'compareStringLength' ] );
198 foreach ( $synonyms as $synonym ) {
200 $escSyn[] = preg_quote( $synonym,
'/' );
202 $this->mBaseRegex = implode(
'|', $escSyn );
204 $case = $this->mCaseSensitive ?
'' :
'iu';
205 $this->mRegex =
"/{$this->mBaseRegex}/{$case}";
206 $this->mRegexStart =
"/^(?:{$this->mBaseRegex})/{$case}";
207 $this->mRegexStartToEnd =
"/^(?:{$this->mBaseRegex})$/{$case}";
208 $this->mVariableRegex = str_replace(
"\\$1",
"(.*?)", $this->mRegex );
209 $this->mVariableStartToEndRegex = str_replace(
"\\$1",
"(.*?)",
210 "/^(?:{$this->mBaseRegex})$/{$case}" );
235 if ( $this->mRegex ==
'' ) {
249 if ( $this->mRegex ===
'' ) {
253 return $this->mCaseSensitive ?
'' :
'iu';
262 if ( $this->mRegex ==
'' ) {
275 if ( $this->mRegexStartToEnd ==
'' ) {
287 if ( $this->mRegex ==
'' ) {
301 return (
bool)preg_match( $this->
getRegex(), $text );
340 if ( $matchcount == 0 ) {
343 # multiple matched parts (variable match); some will be empty because of
344 # synonyms. The variable will be the second non-empty one so remove any
345 # blank elements and re-sort the indices.
367 $this->mFound =
false;
368 $text = preg_replace_callback(
370 [ $this,
'pregRemoveAndRecord' ],
382 $this->mFound =
false;
383 $text = preg_replace_callback(
385 [ $this,
'pregRemoveAndRecord' ],
398 $this->mFound =
true;
411 public function replace( $replacement, $subject, $limit = -1 ) {
414 StringUtils::escapeRegexReplacement( $replacement ),
418 $this->mModified =
$res !== $subject;
434 $this->mModified =
$res !== $text;
444 if ( $this->mVariableRegex ==
'' ) {
456 if ( $this->mVariableStartToEndRegex ==
'' ) {
470 return $this->mSynonyms[$i];
498 foreach ( $this->mSynonyms as $syn ) {
499 $array[$this->contLang->lc( $syn )] = $value;
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Internationalisation code.
This class encapsulates "magic words" such as "#redirect", NOTOC, etc.
getVariableRegex()
Matches the word, where $1 is a wildcard.
compareStringLength( $s1, $s2)
A comparison function that returns -1, 0 or 1 depending on whether the first string is longer,...
initRegex()
Preliminary initialisation.
getWasModified()
Returns true if the last call to replace() or substituteCallback() returned a modified text,...
replace( $replacement, $subject, $limit=-1)
Replaces the word with something else.
getVariableStartToEndRegex()
Matches the entire string, where $1 is a wildcard.
getRegexStartToEnd()
Gets a regex matching the word from start to end of a string.
__construct( $id=null, $syn=[], $cs=false, Language $contLang=null)
#-
substituteCallback( $text, $callback)
Variable handling: {{SUBST:xxx}} style words Calls back a function to determine what to replace xxx w...
getBaseRegex()
regex without the slashes and what not
static getCacheTTL( $id)
Allow external reads of TTL array.
matchStartAndRemove(&$text)
static getVariableIDs()
Get an array of parser variable IDs.
getRegexStart()
Gets a regex matching the word, if it is at the string start.
matchStart( $text)
Returns true if the text starts with the word.
getSynonym( $i)
Accesses the synonym list directly.
getRegex()
Gets a regex representing matching the word.
load( $id)
Initialises this object with an ID.
matchVariableStartToEnd( $text)
Returns NULL if there's no match, the value of $1 otherwise The return code is the matched string,...
getRegexCase()
Gets the regexp case modifier to use, i.e.
addToArray(&$array, $value)
Adds all the synonyms of this MagicWord to an array, to allow quick lookup in a list of magic words.
matchStartToEnd( $text)
Returns true if the text matched the word.
string $mVariableStartToEndRegex
static getDoubleUnderscoreArray()
Get a MagicWordArray of double-underscore entities.
match( $text)
Returns true if the text contains the word.
static getSubstIDs()
Get an array of parser substitution modifier IDs.
pregRemoveAndRecord()
Used in matchAndRemove()
matchAndRemove(&$text)
Returns true if the text matches the word, and alters the input string, removing all instances of the...