81 private $mRegexStart =
'';
84 private $mRegexStartToEnd =
'';
87 private $mBaseRegex =
'';
90 private $mVariableRegex =
'';
93 private $mVariableStartToEndRegex =
'';
96 private $mModified =
false;
99 private $mFound =
false;
118 $this->mSynonyms = (array)$syn;
119 $this->mCaseSensitive = $cs;
131 $this->contLang->getMagic( $this );
132 if ( !$this->mSynonyms ) {
133 $this->mSynonyms = [
'brionmademeputthishere' ];
134 throw new MWException(
"Error: invalid magic word '$id'" );
146 usort( $synonyms, [ $this,
'compareStringLength' ] );
149 foreach ( $synonyms as $synonym ) {
151 $escSyn[] = preg_quote( $synonym,
'/' );
153 $this->mBaseRegex = implode(
'|', $escSyn );
155 $case = $this->mCaseSensitive ?
'' :
'iu';
156 $this->mRegex =
"/{$this->mBaseRegex}/{$case}";
157 $this->mRegexStart =
"/^(?:{$this->mBaseRegex})/{$case}";
158 $this->mRegexStartToEnd =
"/^(?:{$this->mBaseRegex})$/{$case}";
159 $this->mVariableRegex = str_replace(
"\\$1",
"(.*?)", $this->mRegex );
160 $this->mVariableStartToEndRegex = str_replace(
163 "/^(?:{$this->mBaseRegex})$/{$case}"
189 if ( $this->mRegex ==
'' ) {
192 return $this->mRegex;
203 if ( $this->mRegex ===
'' ) {
207 return $this->mCaseSensitive ?
'' :
'iu';
216 if ( $this->mRegex ==
'' ) {
219 return $this->mRegexStart;
229 if ( $this->mRegexStartToEnd ==
'' ) {
232 return $this->mRegexStartToEnd;
241 if ( $this->mRegex ==
'' ) {
244 return $this->mBaseRegex;
255 return (
bool)preg_match( $this->
getRegex(), $text );
294 if ( $matchcount == 0 ) {
297 # multiple matched parts (variable match); some will be empty because of
298 # synonyms. The variable will be the second non-empty one so remove any
299 # blank elements and re-sort the indices.
321 $this->mFound =
false;
322 $text = preg_replace_callback(
324 [ $this,
'pregRemoveAndRecord' ],
328 return $this->mFound;
336 $this->mFound =
false;
337 $text = preg_replace_callback(
339 [ $this,
'pregRemoveAndRecord' ],
343 return $this->mFound;
352 $this->mFound =
true;
365 public function replace( $replacement, $subject, $limit = -1 ) {
372 $this->mModified = $res !== $subject;
387 $res = preg_replace_callback( $this->
getVariableRegex(), $callback, $text );
388 $this->mModified = $res !== $text;
398 if ( $this->mVariableRegex ==
'' ) {
401 return $this->mVariableRegex;
410 if ( $this->mVariableStartToEndRegex ==
'' ) {
413 return $this->mVariableStartToEndRegex;
424 return $this->mSynonyms[$i];
441 return $this->mModified;
452 foreach ( $this->mSynonyms as $syn ) {
453 $array[$this->contLang->lc( $syn )] = $value;
475 class_alias( MagicWord::class,
'MagicWord' );
Base class for language-specific code.
A collection of static methods to play with strings.
static escapeRegexReplacement( $string)
Escape a string to make it suitable for inclusion in a preg_replace() replacement parameter.