MediaWiki  1.32.0
MagicWord.php
Go to the documentation of this file.
1 <?php
25 
57 class MagicWord {
61  public $mId;
62 
64  public $mSynonyms;
65 
68 
70  private $mRegex = '';
71 
73  private $mRegexStart = '';
74 
76  private $mRegexStartToEnd = '';
77 
79  private $mBaseRegex = '';
80 
82  private $mVariableRegex = '';
83 
86 
88  private $mModified = false;
89 
91  private $mFound = false;
92 
94  private $contLang;
95 
108  public function __construct( $id = null, $syn = [], $cs = false, Language $contLang = null ) {
109  $this->mId = $id;
110  $this->mSynonyms = (array)$syn;
111  $this->mCaseSensitive = $cs;
112  $this->contLang = $contLang;
113 
114  if ( !$contLang ) {
115  $this->contLang = MediaWikiServices::getInstance()->getContentLanguage();
116  }
117  }
118 
127  public static function get( $id ) {
128  return MediaWikiServices::getInstance()->getMagicWordFactory()->get( $id );
129  }
130 
137  public static function getVariableIDs() {
138  return MediaWikiServices::getInstance()->getMagicWordFactory()->getVariableIDs();
139  }
140 
146  public static function getSubstIDs() {
147  return MediaWikiServices::getInstance()->getMagicWordFactory()->getSubstIDs();
148  }
149 
157  public static function getCacheTTL( $id ) {
158  return MediaWikiServices::getInstance()->getMagicWordFactory()->getCacheTTL( $id );
159  }
160 
167  public static function getDoubleUnderscoreArray() {
168  return MediaWikiServices::getInstance()->getMagicWordFactory()->getDoubleUnderscoreArray();
169  }
170 
177  public function load( $id ) {
178  $this->mId = $id;
179  $this->contLang->getMagic( $this );
180  if ( !$this->mSynonyms ) {
181  $this->mSynonyms = [ 'brionmademeputthishere' ];
182  throw new MWException( "Error: invalid magic word '$id'" );
183  }
184  }
185 
190  public function initRegex() {
191  // Sort the synonyms by length, descending, so that the longest synonym
192  // matches in precedence to the shortest
193  $synonyms = $this->mSynonyms;
194  usort( $synonyms, [ $this, 'compareStringLength' ] );
195 
196  $escSyn = [];
197  foreach ( $synonyms as $synonym ) {
198  // In case a magic word contains /, like that's going to happen;)
199  $escSyn[] = preg_quote( $synonym, '/' );
200  }
201  $this->mBaseRegex = implode( '|', $escSyn );
202 
203  $case = $this->mCaseSensitive ? '' : 'iu';
204  $this->mRegex = "/{$this->mBaseRegex}/{$case}";
205  $this->mRegexStart = "/^(?:{$this->mBaseRegex})/{$case}";
206  $this->mRegexStartToEnd = "/^(?:{$this->mBaseRegex})$/{$case}";
207  $this->mVariableRegex = str_replace( "\\$1", "(.*?)", $this->mRegex );
208  $this->mVariableStartToEndRegex = str_replace( "\\$1", "(.*?)",
209  "/^(?:{$this->mBaseRegex})$/{$case}" );
210  }
211 
222  public function compareStringLength( $s1, $s2 ) {
223  $l1 = strlen( $s1 );
224  $l2 = strlen( $s2 );
225  return $l2 <=> $l1; // descending
226  }
227 
233  public function getRegex() {
234  if ( $this->mRegex == '' ) {
235  $this->initRegex();
236  }
237  return $this->mRegex;
238  }
239 
247  public function getRegexCase() {
248  if ( $this->mRegex === '' ) {
249  $this->initRegex();
250  }
251 
252  return $this->mCaseSensitive ? '' : 'iu';
253  }
254 
260  public function getRegexStart() {
261  if ( $this->mRegex == '' ) {
262  $this->initRegex();
263  }
264  return $this->mRegexStart;
265  }
266 
273  public function getRegexStartToEnd() {
274  if ( $this->mRegexStartToEnd == '' ) {
275  $this->initRegex();
276  }
278  }
279 
285  public function getBaseRegex() {
286  if ( $this->mRegex == '' ) {
287  $this->initRegex();
288  }
289  return $this->mBaseRegex;
290  }
291 
299  public function match( $text ) {
300  return (bool)preg_match( $this->getRegex(), $text );
301  }
302 
310  public function matchStart( $text ) {
311  return (bool)preg_match( $this->getRegexStart(), $text );
312  }
313 
322  public function matchStartToEnd( $text ) {
323  return (bool)preg_match( $this->getRegexStartToEnd(), $text );
324  }
325 
336  public function matchVariableStartToEnd( $text ) {
337  $matches = [];
338  $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
339  if ( $matchcount == 0 ) {
340  return null;
341  } else {
342  # multiple matched parts (variable match); some will be empty because of
343  # synonyms. The variable will be the second non-empty one so remove any
344  # blank elements and re-sort the indices.
345  # See also T8526
346 
347  $matches = array_values( array_filter( $matches ) );
348 
349  if ( count( $matches ) == 1 ) {
350  return $matches[0];
351  } else {
352  return $matches[1];
353  }
354  }
355  }
356 
365  public function matchAndRemove( &$text ) {
366  $this->mFound = false;
367  $text = preg_replace_callback(
368  $this->getRegex(),
369  [ $this, 'pregRemoveAndRecord' ],
370  $text
371  );
372 
373  return $this->mFound;
374  }
375 
380  public function matchStartAndRemove( &$text ) {
381  $this->mFound = false;
382  $text = preg_replace_callback(
383  $this->getRegexStart(),
384  [ $this, 'pregRemoveAndRecord' ],
385  $text
386  );
387 
388  return $this->mFound;
389  }
390 
396  public function pregRemoveAndRecord() {
397  $this->mFound = true;
398  return '';
399  }
400 
410  public function replace( $replacement, $subject, $limit = -1 ) {
411  $res = preg_replace(
412  $this->getRegex(),
413  StringUtils::escapeRegexReplacement( $replacement ),
414  $subject,
415  $limit
416  );
417  $this->mModified = $res !== $subject;
418  return $res;
419  }
420 
431  public function substituteCallback( $text, $callback ) {
432  $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
433  $this->mModified = $res !== $text;
434  return $res;
435  }
436 
442  public function getVariableRegex() {
443  if ( $this->mVariableRegex == '' ) {
444  $this->initRegex();
445  }
446  return $this->mVariableRegex;
447  }
448 
454  public function getVariableStartToEndRegex() {
455  if ( $this->mVariableStartToEndRegex == '' ) {
456  $this->initRegex();
457  }
459  }
460 
468  public function getSynonym( $i ) {
469  return $this->mSynonyms[$i];
470  }
471 
475  public function getSynonyms() {
476  return $this->mSynonyms;
477  }
478 
485  public function getWasModified() {
486  return $this->mModified;
487  }
488 
496  public function addToArray( &$array, $value ) {
497  foreach ( $this->mSynonyms as $syn ) {
498  $array[$this->contLang->lc( $syn )] = $value;
499  }
500  }
501 
505  public function isCaseSensitive() {
506  return $this->mCaseSensitive;
507  }
508 
512  public function getId() {
513  return $this->mId;
514  }
515 }
MagicWord\$contLang
Language $contLang
Definition: MagicWord.php:94
captcha-old.count
count
Definition: captcha-old.py:249
MagicWord\substituteCallback
substituteCallback( $text, $callback)
Variable handling: {{SUBST:xxx}} style words Calls back a function to determine what to replace xxx w...
Definition: MagicWord.php:431
MagicWord\matchVariableStartToEnd
matchVariableStartToEnd( $text)
Returns NULL if there's no match, the value of $1 otherwise The return code is the matched string,...
Definition: MagicWord.php:336
MagicWord\getSynonyms
getSynonyms()
Definition: MagicWord.php:475
MagicWord\load
load( $id)
Initialises this object with an ID.
Definition: MagicWord.php:177
MagicWord\isCaseSensitive
isCaseSensitive()
Definition: MagicWord.php:505
MagicWord\getRegexStartToEnd
getRegexStartToEnd()
Gets a regex matching the word from start to end of a string.
Definition: MagicWord.php:273
MagicWord\$mRegex
string $mRegex
Definition: MagicWord.php:70
StringUtils\escapeRegexReplacement
static escapeRegexReplacement( $string)
Escape a string to make it suitable for inclusion in a preg_replace() replacement parameter.
Definition: StringUtils.php:323
MagicWord\getRegex
getRegex()
Gets a regex representing matching the word.
Definition: MagicWord.php:233
MagicWord\$mRegexStartToEnd
string $mRegexStartToEnd
Definition: MagicWord.php:76
$res
$res
Definition: database.txt:21
MagicWord\__construct
__construct( $id=null, $syn=[], $cs=false, Language $contLang=null)
#-
Definition: MagicWord.php:108
MagicWord\getId
getId()
Definition: MagicWord.php:512
MagicWord\$mBaseRegex
string $mBaseRegex
Definition: MagicWord.php:79
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MagicWord\$mSynonyms
string[] $mSynonyms
Definition: MagicWord.php:64
MagicWord\initRegex
initRegex()
Preliminary initialisation.
Definition: MagicWord.php:190
MagicWord\replace
replace( $replacement, $subject, $limit=-1)
Replaces the word with something else.
Definition: MagicWord.php:410
MagicWord\matchStartToEnd
matchStartToEnd( $text)
Returns true if the text matched the word.
Definition: MagicWord.php:322
MagicWord\getVariableIDs
static getVariableIDs()
Get an array of parser variable IDs.
Definition: MagicWord.php:137
MagicWord\$mId
string $mId
#-
Definition: MagicWord.php:61
MWException
MediaWiki exception.
Definition: MWException.php:26
MagicWord\getCacheTTL
static getCacheTTL( $id)
Allow external reads of TTL array.
Definition: MagicWord.php:157
$matches
$matches
Definition: NoLocalSettings.php:24
MagicWord\getWasModified
getWasModified()
Returns true if the last call to replace() or substituteCallback() returned a modified text,...
Definition: MagicWord.php:485
MagicWord\$mVariableStartToEndRegex
string $mVariableStartToEndRegex
Definition: MagicWord.php:85
MagicWord\$mModified
bool $mModified
Definition: MagicWord.php:88
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MagicWord\$mFound
bool $mFound
Definition: MagicWord.php:91
MagicWord\pregRemoveAndRecord
pregRemoveAndRecord()
Used in matchAndRemove()
Definition: MagicWord.php:396
MagicWord
This class encapsulates "magic words" such as "#redirect", NOTOC, etc.
Definition: MagicWord.php:57
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
MagicWord\getRegexStart
getRegexStart()
Gets a regex matching the word, if it is at the string start.
Definition: MagicWord.php:260
MagicWord\getBaseRegex
getBaseRegex()
regex without the slashes and what not
Definition: MagicWord.php:285
$value
$value
Definition: styleTest.css.php:49
MagicWord\matchStartAndRemove
matchStartAndRemove(&$text)
Definition: MagicWord.php:380
MagicWord\getVariableRegex
getVariableRegex()
Matches the word, where $1 is a wildcard.
Definition: MagicWord.php:442
MagicWord\$mCaseSensitive
bool $mCaseSensitive
Definition: MagicWord.php:67
MagicWord\addToArray
addToArray(&$array, $value)
Adds all the synonyms of this MagicWord to an array, to allow quick lookup in a list of magic words.
Definition: MagicWord.php:496
MagicWord\getSubstIDs
static getSubstIDs()
Get an array of parser substitution modifier IDs.
Definition: MagicWord.php:146
MagicWord\$mRegexStart
string $mRegexStart
Definition: MagicWord.php:73
MagicWord\$mVariableRegex
string $mVariableRegex
Definition: MagicWord.php:82
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
MagicWord\matchStart
matchStart( $text)
Returns true if the text starts with the word.
Definition: MagicWord.php:310
MagicWord\getDoubleUnderscoreArray
static getDoubleUnderscoreArray()
Get a MagicWordArray of double-underscore entities.
Definition: MagicWord.php:167
MagicWord\getRegexCase
getRegexCase()
Gets the regexp case modifier to use, i.e.
Definition: MagicWord.php:247
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
MagicWord\getVariableStartToEndRegex
getVariableStartToEndRegex()
Matches the entire string, where $1 is a wildcard.
Definition: MagicWord.php:454
MagicWord\getSynonym
getSynonym( $i)
Accesses the synonym list directly.
Definition: MagicWord.php:468
MagicWord\compareStringLength
compareStringLength( $s1, $s2)
A comparison function that returns -1, 0 or 1 depending on whether the first string is longer,...
Definition: MagicWord.php:222
MagicWord\matchAndRemove
matchAndRemove(&$text)
Returns true if the text matches the word, and alters the input string, removing all instances of the...
Definition: MagicWord.php:365
MagicWord\match
match( $text)
Returns true if the text contains the word.
Definition: MagicWord.php:299
Language
Internationalisation code.
Definition: Language.php:35