76 private $mRegexStart =
'';
79 private $mRegexStartToEnd =
'';
82 private $mBaseRegex =
'';
85 private $mVariableRegex =
'';
88 private $mVariableStartToEndRegex =
'';
91 private $mModified =
false;
94 private $mFound =
false;
113 $this->mSynonyms = (array)$syn;
114 $this->mCaseSensitive = $cs;
115 $this->contLang = $contLang ?: MediaWikiServices::getInstance()->getContentLanguage();
126 $this->contLang->getMagic( $this );
127 if ( !$this->mSynonyms ) {
128 $this->mSynonyms = [
'brionmademeputthishere' ];
129 throw new MWException(
"Error: invalid magic word '$id'" );
141 usort( $synonyms, [ $this,
'compareStringLength' ] );
144 foreach ( $synonyms as $synonym ) {
146 $escSyn[] = preg_quote( $synonym,
'/' );
148 $this->mBaseRegex = implode(
'|', $escSyn );
150 $case = $this->mCaseSensitive ?
'' :
'iu';
151 $this->mRegex =
"/{$this->mBaseRegex}/{$case}";
152 $this->mRegexStart =
"/^(?:{$this->mBaseRegex})/{$case}";
153 $this->mRegexStartToEnd =
"/^(?:{$this->mBaseRegex})$/{$case}";
154 $this->mVariableRegex = str_replace(
"\\$1",
"(.*?)", $this->mRegex );
155 $this->mVariableStartToEndRegex = str_replace(
"\\$1",
"(.*?)",
156 "/^(?:{$this->mBaseRegex})$/{$case}" );
181 if ( $this->mRegex ==
'' ) {
184 return $this->mRegex;
195 if ( $this->mRegex ===
'' ) {
199 return $this->mCaseSensitive ?
'' :
'iu';
208 if ( $this->mRegex ==
'' ) {
211 return $this->mRegexStart;
221 if ( $this->mRegexStartToEnd ==
'' ) {
224 return $this->mRegexStartToEnd;
233 if ( $this->mRegex ==
'' ) {
236 return $this->mBaseRegex;
247 return (
bool)preg_match( $this->
getRegex(), $text );
286 if ( $matchcount == 0 ) {
289 # multiple matched parts (variable match); some will be empty because of
290 # synonyms. The variable will be the second non-empty one so remove any
291 # blank elements and re-sort the indices.
313 $this->mFound =
false;
314 $text = preg_replace_callback(
316 [ $this,
'pregRemoveAndRecord' ],
320 return $this->mFound;
328 $this->mFound =
false;
329 $text = preg_replace_callback(
331 [ $this,
'pregRemoveAndRecord' ],
335 return $this->mFound;
344 $this->mFound =
true;
357 public function replace( $replacement, $subject, $limit = -1 ) {
364 $this->mModified =
$res !== $subject;
380 $this->mModified =
$res !== $text;
390 if ( $this->mVariableRegex ==
'' ) {
393 return $this->mVariableRegex;
402 if ( $this->mVariableStartToEndRegex ==
'' ) {
405 return $this->mVariableStartToEndRegex;
416 return $this->mSynonyms[$i];
433 return $this->mModified;
444 foreach ( $this->mSynonyms as $syn ) {
445 $array[$this->contLang->lc( $syn )] = $value;
Base class for language-specific 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
matchStartAndRemove(&$text)
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.
match( $text)
Returns true if the text contains the word.
pregRemoveAndRecord()
Used in matchAndRemove()
matchAndRemove(&$text)
Returns true if the text matches the word, and alters the input string, removing all instances of the...
static escapeRegexReplacement( $string)
Escape a string to make it suitable for inclusion in a preg_replace() replacement parameter.