MediaWiki  1.33.0
MagicWordArray.php
Go to the documentation of this file.
1 <?php
2 
27 
34  public $names = [];
35 
37  private $factory;
38 
40  private $hash;
41 
42  private $baseRegex;
43 
44  private $regex;
45 
50  public function __construct( $names = [], MagicWordFactory $factory = null ) {
51  $this->names = $names;
52  $this->factory = $factory;
53  if ( !$factory ) {
54  $this->factory = MediaWikiServices::getInstance()->getMagicWordFactory();
55  }
56  }
57 
63  public function add( $name ) {
64  $this->names[] = $name;
65  $this->hash = $this->baseRegex = $this->regex = null;
66  }
67 
73  public function addArray( $names ) {
74  $this->names = array_merge( $this->names, array_values( $names ) );
75  $this->hash = $this->baseRegex = $this->regex = null;
76  }
77 
82  public function getHash() {
83  if ( is_null( $this->hash ) ) {
84  $this->hash = [ 0 => [], 1 => [] ];
85  foreach ( $this->names as $name ) {
86  $magic = $this->factory->get( $name );
87  $case = intval( $magic->isCaseSensitive() );
88  foreach ( $magic->getSynonyms() as $syn ) {
89  if ( !$case ) {
90  $syn = $this->factory->getContentLanguage()->lc( $syn );
91  }
92  $this->hash[$case][$syn] = $name;
93  }
94  }
95  }
96  return $this->hash;
97  }
98 
103  public function getBaseRegex() {
104  if ( is_null( $this->baseRegex ) ) {
105  $this->baseRegex = [ 0 => '', 1 => '' ];
106  $allGroups = [];
107  foreach ( $this->names as $name ) {
108  $magic = $this->factory->get( $name );
109  $case = intval( $magic->isCaseSensitive() );
110  foreach ( $magic->getSynonyms() as $i => $syn ) {
111  // Group name must start with a non-digit in PCRE 8.34+
112  $it = strtr( $i, '0123456789', 'abcdefghij' );
113  $groupName = $it . '_' . $name;
114  $group = '(?P<' . $groupName . '>' . preg_quote( $syn, '/' ) . ')';
115  // look for same group names to avoid same named subpatterns in the regex
116  if ( isset( $allGroups[$groupName] ) ) {
117  throw new MWException(
118  __METHOD__ . ': duplicate internal name in magic word array: ' . $name
119  );
120  }
121  $allGroups[$groupName] = true;
122  if ( $this->baseRegex[$case] === '' ) {
123  $this->baseRegex[$case] = $group;
124  } else {
125  $this->baseRegex[$case] .= '|' . $group;
126  }
127  }
128  }
129  }
130  return $this->baseRegex;
131  }
132 
137  public function getRegex() {
138  if ( is_null( $this->regex ) ) {
139  $base = $this->getBaseRegex();
140  $this->regex = [ '', '' ];
141  if ( $this->baseRegex[0] !== '' ) {
142  $this->regex[0] = "/{$base[0]}/iuS";
143  }
144  if ( $this->baseRegex[1] !== '' ) {
145  $this->regex[1] = "/{$base[1]}/S";
146  }
147  }
148  return $this->regex;
149  }
150 
156  public function getVariableRegex() {
157  return str_replace( "\\$1", "(.*?)", $this->getRegex() );
158  }
159 
165  public function getRegexStart() {
166  $base = $this->getBaseRegex();
167  $newRegex = [ '', '' ];
168  if ( $base[0] !== '' ) {
169  $newRegex[0] = "/^(?:{$base[0]})/iuS";
170  }
171  if ( $base[1] !== '' ) {
172  $newRegex[1] = "/^(?:{$base[1]})/S";
173  }
174  return $newRegex;
175  }
176 
182  public function getVariableStartToEndRegex() {
183  $base = $this->getBaseRegex();
184  $newRegex = [ '', '' ];
185  if ( $base[0] !== '' ) {
186  $newRegex[0] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[0]})$/iuS" );
187  }
188  if ( $base[1] !== '' ) {
189  $newRegex[1] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[1]})$/S" );
190  }
191  return $newRegex;
192  }
193 
198  public function getNames() {
199  return $this->names;
200  }
201 
212  public function parseMatch( $m ) {
213  reset( $m );
214  while ( ( $key = key( $m ) ) !== null ) {
215  $value = current( $m );
216  next( $m );
217  if ( $key === 0 || $value === '' ) {
218  continue;
219  }
220  $parts = explode( '_', $key, 2 );
221  if ( count( $parts ) != 2 ) {
222  // This shouldn't happen
223  // continue;
224  throw new MWException( __METHOD__ . ': bad parameter name' );
225  }
226  list( /* $synIndex */, $magicName ) = $parts;
227  $paramValue = next( $m );
228  return [ $magicName, $paramValue ];
229  }
230  // This shouldn't happen either
231  throw new MWException( __METHOD__ . ': parameter not found' );
232  }
233 
244  public function matchVariableStartToEnd( $text ) {
245  $regexes = $this->getVariableStartToEndRegex();
246  foreach ( $regexes as $regex ) {
247  if ( $regex !== '' ) {
248  $m = [];
249  if ( preg_match( $regex, $text, $m ) ) {
250  return $this->parseMatch( $m );
251  }
252  }
253  }
254  return [ false, false ];
255  }
256 
265  public function matchStartToEnd( $text ) {
266  $hash = $this->getHash();
267  if ( isset( $hash[1][$text] ) ) {
268  return $hash[1][$text];
269  }
270  $lc = $this->factory->getContentLanguage()->lc( $text );
271  return $hash[0][$lc] ?? false;
272  }
273 
282  public function matchAndRemove( &$text ) {
283  $found = [];
284  $regexes = $this->getRegex();
285  foreach ( $regexes as $regex ) {
286  if ( $regex === '' ) {
287  continue;
288  }
289  $matches = [];
290  $res = preg_match_all( $regex, $text, $matches, PREG_SET_ORDER );
291  if ( $res === false ) {
292  LoggerFactory::getInstance( 'parser' )->warning( 'preg_match_all returned false', [
293  'code' => preg_last_error(),
294  'regex' => $regex,
295  'text' => $text,
296  ] );
297  } elseif ( $res ) {
298  foreach ( $matches as $m ) {
299  list( $name, $param ) = $this->parseMatch( $m );
300  $found[$name] = $param;
301  }
302  }
303  $res = preg_replace( $regex, '', $text );
304  if ( $res === null ) {
305  LoggerFactory::getInstance( 'parser' )->warning( 'preg_replace returned null', [
306  'code' => preg_last_error(),
307  'regex' => $regex,
308  'text' => $text,
309  ] );
310  }
311  $text = $res;
312  }
313  return $found;
314  }
315 
326  public function matchStartAndRemove( &$text ) {
327  $regexes = $this->getRegexStart();
328  foreach ( $regexes as $regex ) {
329  if ( $regex === '' ) {
330  continue;
331  }
332  if ( preg_match( $regex, $text, $m ) ) {
333  list( $id, ) = $this->parseMatch( $m );
334  if ( strlen( $m[0] ) >= strlen( $text ) ) {
335  $text = '';
336  } else {
337  $text = substr( $text, strlen( $m[0] ) );
338  }
339  return $id;
340  }
341  }
342  return false;
343  }
344 }
MagicWordArray\__construct
__construct( $names=[], MagicWordFactory $factory=null)
Definition: MagicWordArray.php:50
MagicWordArray
Class for handling an array of magic words.
Definition: MagicWordArray.php:32
MagicWordArray\getVariableStartToEndRegex
getVariableStartToEndRegex()
Get an anchored regex for matching variables with parameters.
Definition: MagicWordArray.php:182
MagicWordArray\getNames
getNames()
Definition: MagicWordArray.php:198
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
MagicWordArray\matchStartAndRemove
matchStartAndRemove(&$text)
Return the ID of the magic word at the start of $text, and remove the prefix from $text.
Definition: MagicWordArray.php:326
MagicWordFactory
A factory that stores information about MagicWords, and creates them on demand with caching.
Definition: MagicWordFactory.php:34
MagicWordArray\getHash
getHash()
Get a 2-d hashtable for this array.
Definition: MagicWordArray.php:82
MagicWordArray\getRegexStart
getRegexStart()
Get a regex anchored to the start of the string that does not match parameters.
Definition: MagicWordArray.php:165
captcha-old.count
count
Definition: captcha-old.py:249
$res
$res
Definition: database.txt:21
MagicWordArray\$factory
MagicWordFactory $factory
Definition: MagicWordArray.php:37
MagicWordArray\$baseRegex
$baseRegex
Definition: MagicWordArray.php:42
$base
$base
Definition: generateLocalAutoload.php:11
MagicWordArray\matchStartToEnd
matchStartToEnd( $text)
Match some text, without parameter capture Returns the magic word name, or false if there was no capt...
Definition: MagicWordArray.php:265
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
MWException
MediaWiki exception.
Definition: MWException.php:26
$matches
$matches
Definition: NoLocalSettings.php:24
MagicWordArray\$hash
array $hash
Definition: MagicWordArray.php:40
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
MagicWordArray\add
add( $name)
Add a magic word by name.
Definition: MagicWordArray.php:63
MagicWordArray\getBaseRegex
getBaseRegex()
Get the base regex.
Definition: MagicWordArray.php:103
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))
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
key
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message key
Definition: hooks.txt:2154
MagicWordArray\getRegex
getRegex()
Get an unanchored regex that does not match parameters.
Definition: MagicWordArray.php:137
MagicWordArray\matchAndRemove
matchAndRemove(&$text)
Returns an associative array, ID => param value, for all items that match Removes the matched items f...
Definition: MagicWordArray.php:282
$value
$value
Definition: styleTest.css.php:49
MagicWordArray\$names
array $names
Definition: MagicWordArray.php:34
MagicWordArray\getVariableRegex
getVariableRegex()
Get a regex for matching variables with parameters.
Definition: MagicWordArray.php:156
MagicWordArray\parseMatch
parseMatch( $m)
Parse a match array from preg_match Returns array(magic word ID, parameter value) If there is no para...
Definition: MagicWordArray.php:212
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
LoggerFactory
MediaWiki Logger LoggerFactory implements a PSR[0] compatible message logging system Named Psr Log LoggerInterface instances can be obtained from the MediaWiki Logger LoggerFactory::getInstance() static method. MediaWiki\Logger\LoggerFactory expects a class implementing the MediaWiki\Logger\Spi interface to act as a factory for new Psr\Log\LoggerInterface instances. The "Spi" in MediaWiki\Logger\Spi stands for "service provider interface". An SPI is an API intended to be implemented or extended by a third party. This software design pattern is intended to enable framework extension and replaceable components. It is specifically used in the MediaWiki\Logger\LoggerFactory service to allow alternate PSR-3 logging implementations to be easily integrated with MediaWiki. The service provider interface allows the backend logging library to be implemented in multiple ways. The $wgMWLoggerDefaultSpi global provides the classname of the default MediaWiki\Logger\Spi implementation to be loaded at runtime. This can either be the name of a class implementing the MediaWiki\Logger\Spi with a zero argument const ructor or a callable that will return an MediaWiki\Logger\Spi instance. Alternately the MediaWiki\Logger\LoggerFactory MediaWiki Logger LoggerFactory
Definition: logger.txt:5
MagicWordArray\$regex
$regex
Definition: MagicWordArray.php:44
MagicWordArray\matchVariableStartToEnd
matchVariableStartToEnd( $text)
Match some text, with parameter capture Returns an array with the magic word name in the first elemen...
Definition: MagicWordArray.php:244
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
MagicWordArray\addArray
addArray( $names)
Add a number of magic words by name.
Definition: MagicWordArray.php:73
names
alter the names
Definition: COPYING.txt:329