MediaWiki  master
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  wfDeprecated( __METHOD__, '1.32' );
129  return MediaWikiServices::getInstance()->getMagicWordFactory()->get( $id );
130  }
131 
138  public static function getVariableIDs() {
139  wfDeprecated( __METHOD__, '1.32' );
140  return MediaWikiServices::getInstance()->getMagicWordFactory()->getVariableIDs();
141  }
142 
148  public static function getSubstIDs() {
149  wfDeprecated( __METHOD__, '1.32' );
150  return MediaWikiServices::getInstance()->getMagicWordFactory()->getSubstIDs();
151  }
152 
160  public static function getCacheTTL( $id ) {
161  wfDeprecated( __METHOD__, '1.32' );
162  return MediaWikiServices::getInstance()->getMagicWordFactory()->getCacheTTL( $id );
163  }
164 
171  public static function getDoubleUnderscoreArray() {
172  wfDeprecated( __METHOD__, '1.32' );
173  return MediaWikiServices::getInstance()->getMagicWordFactory()->getDoubleUnderscoreArray();
174  }
175 
182  public function load( $id ) {
183  $this->mId = $id;
184  $this->contLang->getMagic( $this );
185  if ( !$this->mSynonyms ) {
186  $this->mSynonyms = [ 'brionmademeputthishere' ];
187  throw new MWException( "Error: invalid magic word '$id'" );
188  }
189  }
190 
195  public function initRegex() {
196  // Sort the synonyms by length, descending, so that the longest synonym
197  // matches in precedence to the shortest
198  $synonyms = $this->mSynonyms;
199  usort( $synonyms, [ $this, 'compareStringLength' ] );
200 
201  $escSyn = [];
202  foreach ( $synonyms as $synonym ) {
203  // In case a magic word contains /, like that's going to happen;)
204  $escSyn[] = preg_quote( $synonym, '/' );
205  }
206  $this->mBaseRegex = implode( '|', $escSyn );
207 
208  $case = $this->mCaseSensitive ? '' : 'iu';
209  $this->mRegex = "/{$this->mBaseRegex}/{$case}";
210  $this->mRegexStart = "/^(?:{$this->mBaseRegex})/{$case}";
211  $this->mRegexStartToEnd = "/^(?:{$this->mBaseRegex})$/{$case}";
212  $this->mVariableRegex = str_replace( "\\$1", "(.*?)", $this->mRegex );
213  $this->mVariableStartToEndRegex = str_replace( "\\$1", "(.*?)",
214  "/^(?:{$this->mBaseRegex})$/{$case}" );
215  }
216 
227  public function compareStringLength( $s1, $s2 ) {
228  $l1 = strlen( $s1 );
229  $l2 = strlen( $s2 );
230  return $l2 <=> $l1; // descending
231  }
232 
238  public function getRegex() {
239  if ( $this->mRegex == '' ) {
240  $this->initRegex();
241  }
242  return $this->mRegex;
243  }
244 
252  public function getRegexCase() {
253  if ( $this->mRegex === '' ) {
254  $this->initRegex();
255  }
256 
257  return $this->mCaseSensitive ? '' : 'iu';
258  }
259 
265  public function getRegexStart() {
266  if ( $this->mRegex == '' ) {
267  $this->initRegex();
268  }
269  return $this->mRegexStart;
270  }
271 
278  public function getRegexStartToEnd() {
279  if ( $this->mRegexStartToEnd == '' ) {
280  $this->initRegex();
281  }
283  }
284 
290  public function getBaseRegex() {
291  if ( $this->mRegex == '' ) {
292  $this->initRegex();
293  }
294  return $this->mBaseRegex;
295  }
296 
304  public function match( $text ) {
305  return (bool)preg_match( $this->getRegex(), $text );
306  }
307 
315  public function matchStart( $text ) {
316  return (bool)preg_match( $this->getRegexStart(), $text );
317  }
318 
327  public function matchStartToEnd( $text ) {
328  return (bool)preg_match( $this->getRegexStartToEnd(), $text );
329  }
330 
341  public function matchVariableStartToEnd( $text ) {
342  $matches = [];
343  $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
344  if ( $matchcount == 0 ) {
345  return null;
346  } else {
347  # multiple matched parts (variable match); some will be empty because of
348  # synonyms. The variable will be the second non-empty one so remove any
349  # blank elements and re-sort the indices.
350  # See also T8526
351 
352  $matches = array_values( array_filter( $matches ) );
353 
354  if ( count( $matches ) == 1 ) {
355  return $matches[0];
356  } else {
357  return $matches[1];
358  }
359  }
360  }
361 
370  public function matchAndRemove( &$text ) {
371  $this->mFound = false;
372  $text = preg_replace_callback(
373  $this->getRegex(),
374  [ $this, 'pregRemoveAndRecord' ],
375  $text
376  );
377 
378  return $this->mFound;
379  }
380 
385  public function matchStartAndRemove( &$text ) {
386  $this->mFound = false;
387  $text = preg_replace_callback(
388  $this->getRegexStart(),
389  [ $this, 'pregRemoveAndRecord' ],
390  $text
391  );
392 
393  return $this->mFound;
394  }
395 
401  public function pregRemoveAndRecord() {
402  $this->mFound = true;
403  return '';
404  }
405 
415  public function replace( $replacement, $subject, $limit = -1 ) {
416  $res = preg_replace(
417  $this->getRegex(),
418  StringUtils::escapeRegexReplacement( $replacement ),
419  $subject,
420  $limit
421  );
422  $this->mModified = $res !== $subject;
423  return $res;
424  }
425 
436  public function substituteCallback( $text, $callback ) {
437  $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
438  $this->mModified = $res !== $text;
439  return $res;
440  }
441 
447  public function getVariableRegex() {
448  if ( $this->mVariableRegex == '' ) {
449  $this->initRegex();
450  }
451  return $this->mVariableRegex;
452  }
453 
459  public function getVariableStartToEndRegex() {
460  if ( $this->mVariableStartToEndRegex == '' ) {
461  $this->initRegex();
462  }
464  }
465 
473  public function getSynonym( $i ) {
474  return $this->mSynonyms[$i];
475  }
476 
480  public function getSynonyms() {
481  return $this->mSynonyms;
482  }
483 
490  public function getWasModified() {
491  return $this->mModified;
492  }
493 
501  public function addToArray( &$array, $value ) {
502  foreach ( $this->mSynonyms as $syn ) {
503  $array[$this->contLang->lc( $syn )] = $value;
504  }
505  }
506 
510  public function isCaseSensitive() {
511  return $this->mCaseSensitive;
512  }
513 
517  public function getId() {
518  return $this->mId;
519  }
520 }
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))
string $mVariableStartToEndRegex
Definition: MagicWord.php:85
static getVariableIDs()
Get an array of parser variable IDs.
Definition: MagicWord.php:138
replace( $replacement, $subject, $limit=-1)
Replaces the word with something else.
Definition: MagicWord.php:415
matchVariableStartToEnd( $text)
Returns NULL if there&#39;s no match, the value of $1 otherwise The return code is the matched string...
Definition: MagicWord.php:341
getRegexStart()
Gets a regex matching the word, if it is at the string start.
Definition: MagicWord.php:265
getRegex()
Gets a regex representing matching the word.
Definition: MagicWord.php:238
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static escapeRegexReplacement( $string)
Escape a string to make it suitable for inclusion in a preg_replace() replacement parameter...
getBaseRegex()
regex without the slashes and what not
Definition: MagicWord.php:290
$value
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
string [] $mSynonyms
Definition: MagicWord.php:64
string $mRegexStart
Definition: MagicWord.php:73
load( $id)
Initialises this object with an ID.
Definition: MagicWord.php:182
isCaseSensitive()
Definition: MagicWord.php:510
string $mBaseRegex
Definition: MagicWord.php:79
static getDoubleUnderscoreArray()
Get a MagicWordArray of double-underscore entities.
Definition: MagicWord.php:171
string $mId
#-
Definition: MagicWord.php:61
pregRemoveAndRecord()
Used in matchAndRemove()
Definition: MagicWord.php:401
$res
Definition: database.txt:21
Language $contLang
Definition: MagicWord.php:94
match( $text)
Returns true if the text contains the word.
Definition: MagicWord.php:304
string $mRegex
Definition: MagicWord.php:70
getWasModified()
Returns true if the last call to replace() or substituteCallback() returned a modified text...
Definition: MagicWord.php:490
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
substituteCallback( $text, $callback)
Variable handling: {{SUBST:xxx}} style words Calls back a function to determine what to replace xxx w...
Definition: MagicWord.php:436
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
matchStartAndRemove(&$text)
Definition: MagicWord.php:385
getVariableStartToEndRegex()
Matches the entire string, where $1 is a wildcard.
Definition: MagicWord.php:459
static getCacheTTL( $id)
Allow external reads of TTL array.
Definition: MagicWord.php:160
static getSubstIDs()
Get an array of parser substitution modifier IDs.
Definition: MagicWord.php:148
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
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:501
__construct( $id=null, $syn=[], $cs=false, Language $contLang=null)
#-
Definition: MagicWord.php:108
string $mRegexStartToEnd
Definition: MagicWord.php:76
matchStartToEnd( $text)
Returns true if the text matched the word.
Definition: MagicWord.php:327
matchStart( $text)
Returns true if the text starts with the word.
Definition: MagicWord.php:315
bool $mFound
Definition: MagicWord.php:91
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
getRegexCase()
Gets the regexp case modifier to use, i.e.
Definition: MagicWord.php:252
initRegex()
Preliminary initialisation.
Definition: MagicWord.php:195
getVariableRegex()
Matches the word, where $1 is a wildcard.
Definition: MagicWord.php:447
bool $mCaseSensitive
Definition: MagicWord.php:67
matchAndRemove(&$text)
Returns true if the text matches the word, and alters the input string, removing all instances of the...
Definition: MagicWord.php:370
compareStringLength( $s1, $s2)
A comparison function that returns -1, 0 or 1 depending on whether the first string is longer...
Definition: MagicWord.php:227
getSynonym( $i)
Accesses the synonym list directly.
Definition: MagicWord.php:473
string $mVariableRegex
Definition: MagicWord.php:82
getRegexStartToEnd()
Gets a regex matching the word from start to end of a string.
Definition: MagicWord.php:278
bool $mModified
Definition: MagicWord.php:88
This class encapsulates "magic words" such as "#redirect", NOTOC, etc.
Definition: MagicWord.php:57
$matches