MediaWiki  master
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 ?: MediaWikiServices::getInstance()->getMagicWordFactory();
53  }
54 
60  public function add( $name ) {
61  $this->names[] = $name;
62  $this->hash = $this->baseRegex = $this->regex = null;
63  }
64 
70  public function addArray( $names ) {
71  $this->names = array_merge( $this->names, array_values( $names ) );
72  $this->hash = $this->baseRegex = $this->regex = null;
73  }
74 
79  public function getHash() {
80  if ( is_null( $this->hash ) ) {
81  $this->hash = [ 0 => [], 1 => [] ];
82  foreach ( $this->names as $name ) {
83  $magic = $this->factory->get( $name );
84  $case = intval( $magic->isCaseSensitive() );
85  foreach ( $magic->getSynonyms() as $syn ) {
86  if ( !$case ) {
87  $syn = $this->factory->getContentLanguage()->lc( $syn );
88  }
89  $this->hash[$case][$syn] = $name;
90  }
91  }
92  }
93  return $this->hash;
94  }
95 
100  public function getBaseRegex() {
101  if ( is_null( $this->baseRegex ) ) {
102  $this->baseRegex = [ 0 => '', 1 => '' ];
103  $allGroups = [];
104  foreach ( $this->names as $name ) {
105  $magic = $this->factory->get( $name );
106  $case = intval( $magic->isCaseSensitive() );
107  foreach ( $magic->getSynonyms() as $i => $syn ) {
108  // Group name must start with a non-digit in PCRE 8.34+
109  $it = strtr( $i, '0123456789', 'abcdefghij' );
110  $groupName = $it . '_' . $name;
111  $group = '(?P<' . $groupName . '>' . preg_quote( $syn, '/' ) . ')';
112  // look for same group names to avoid same named subpatterns in the regex
113  if ( isset( $allGroups[$groupName] ) ) {
114  throw new MWException(
115  __METHOD__ . ': duplicate internal name in magic word array: ' . $name
116  );
117  }
118  $allGroups[$groupName] = true;
119  if ( $this->baseRegex[$case] === '' ) {
120  $this->baseRegex[$case] = $group;
121  } else {
122  $this->baseRegex[$case] .= '|' . $group;
123  }
124  }
125  }
126  }
127  return $this->baseRegex;
128  }
129 
134  public function getRegex() {
135  if ( is_null( $this->regex ) ) {
136  $base = $this->getBaseRegex();
137  $this->regex = [ '', '' ];
138  if ( $this->baseRegex[0] !== '' ) {
139  $this->regex[0] = "/{$base[0]}/iuS";
140  }
141  if ( $this->baseRegex[1] !== '' ) {
142  $this->regex[1] = "/{$base[1]}/S";
143  }
144  }
145  return $this->regex;
146  }
147 
153  public function getVariableRegex() {
154  return str_replace( "\\$1", "(.*?)", $this->getRegex() );
155  }
156 
162  public function getRegexStart() {
163  $base = $this->getBaseRegex();
164  $newRegex = [ '', '' ];
165  if ( $base[0] !== '' ) {
166  $newRegex[0] = "/^(?:{$base[0]})/iuS";
167  }
168  if ( $base[1] !== '' ) {
169  $newRegex[1] = "/^(?:{$base[1]})/S";
170  }
171  return $newRegex;
172  }
173 
179  public function getVariableStartToEndRegex() {
180  $base = $this->getBaseRegex();
181  $newRegex = [ '', '' ];
182  if ( $base[0] !== '' ) {
183  $newRegex[0] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[0]})$/iuS" );
184  }
185  if ( $base[1] !== '' ) {
186  $newRegex[1] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[1]})$/S" );
187  }
188  return $newRegex;
189  }
190 
195  public function getNames() {
196  return $this->names;
197  }
198 
209  public function parseMatch( $m ) {
210  reset( $m );
211  while ( ( $key = key( $m ) ) !== null ) {
212  $value = current( $m );
213  next( $m );
214  if ( $key === 0 || $value === '' ) {
215  continue;
216  }
217  $parts = explode( '_', $key, 2 );
218  if ( count( $parts ) != 2 ) {
219  // This shouldn't happen
220  // continue;
221  throw new MWException( __METHOD__ . ': bad parameter name' );
222  }
223  list( /* $synIndex */, $magicName ) = $parts;
224  $paramValue = next( $m );
225  return [ $magicName, $paramValue ];
226  }
227  // This shouldn't happen either
228  throw new MWException( __METHOD__ . ': parameter not found' );
229  }
230 
241  public function matchVariableStartToEnd( $text ) {
242  $regexes = $this->getVariableStartToEndRegex();
243  foreach ( $regexes as $regex ) {
244  if ( $regex !== '' ) {
245  $m = [];
246  if ( preg_match( $regex, $text, $m ) ) {
247  return $this->parseMatch( $m );
248  }
249  }
250  }
251  return [ false, false ];
252  }
253 
262  public function matchStartToEnd( $text ) {
263  $hash = $this->getHash();
264  if ( isset( $hash[1][$text] ) ) {
265  return $hash[1][$text];
266  }
267  $lc = $this->factory->getContentLanguage()->lc( $text );
268  return $hash[0][$lc] ?? false;
269  }
270 
279  public function matchAndRemove( &$text ) {
280  $found = [];
281  $regexes = $this->getRegex();
282  foreach ( $regexes as $regex ) {
283  if ( $regex === '' ) {
284  continue;
285  }
286  $matches = [];
287  $res = preg_match_all( $regex, $text, $matches, PREG_SET_ORDER );
288  if ( $res === false ) {
289  LoggerFactory::getInstance( 'parser' )->warning( 'preg_match_all returned false', [
290  'code' => preg_last_error(),
291  'regex' => $regex,
292  'text' => $text,
293  ] );
294  } elseif ( $res ) {
295  foreach ( $matches as $m ) {
296  list( $name, $param ) = $this->parseMatch( $m );
297  $found[$name] = $param;
298  }
299  }
300  $res = preg_replace( $regex, '', $text );
301  if ( $res === null ) {
302  LoggerFactory::getInstance( 'parser' )->warning( 'preg_replace returned null', [
303  'code' => preg_last_error(),
304  'regex' => $regex,
305  'text' => $text,
306  ] );
307  }
308  $text = $res;
309  }
310  return $found;
311  }
312 
323  public function matchStartAndRemove( &$text ) {
324  $regexes = $this->getRegexStart();
325  foreach ( $regexes as $regex ) {
326  if ( $regex === '' ) {
327  continue;
328  }
329  if ( preg_match( $regex, $text, $m ) ) {
330  list( $id, ) = $this->parseMatch( $m );
331  if ( strlen( $m[0] ) >= strlen( $text ) ) {
332  $text = '';
333  } else {
334  $text = substr( $text, strlen( $m[0] ) );
335  }
336  return $id;
337  }
338  }
339  return false;
340  }
341 }
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
getVariableRegex()
Get a regex for matching variables with parameters.
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
matchAndRemove(&$text)
Returns an associative array, ID => param value, for all items that match Removes the matched items f...
$value
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:2139
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
getRegexStart()
Get a regex anchored to the start of the string that does not match parameters.
matchStartAndRemove(&$text)
Return the ID of the magic word at the start of $text, and remove the prefix from $text...
getBaseRegex()
Get the base regex.
$res
Definition: database.txt:21
getHash()
Get a 2-d hashtable for this array.
string [] $names
add( $name)
Add a magic word by name.
__construct( $names=[], MagicWordFactory $factory=null)
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:767
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
Class for handling an array of magic words.
matchStartToEnd( $text)
Match some text, without parameter capture Returns the magic word name, or false if there was no capt...
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
matchVariableStartToEnd( $text)
Match some text, with parameter capture Returns an array with the magic word name in the first elemen...
addArray( $names)
Add a number of magic words by name.
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
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
MagicWordFactory $factory
getRegex()
Get an unanchored regex that does not match parameters.
within a display generated by the Derivative if and wherever such third party notices normally appear The contents of the NOTICE file are for informational purposes only and do not modify the License You may add Your own attribution notices within Derivative Works that You alongside or as an addendum to the NOTICE text from the provided that such additional attribution notices cannot be construed as modifying the License You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for or distribution of Your or for any such Derivative Works as a provided Your and distribution of the Work otherwise complies with the conditions stated in this License Submission of Contributions Unless You explicitly state any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this without any additional terms or conditions Notwithstanding the nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions Trademarks This License does not grant permission to use the trade names
getVariableStartToEndRegex()
Get an anchored regex for matching variables with parameters.
parseMatch( $m)
Parse a match array from preg_match Returns array(magic word ID, parameter value) If there is no para...
$matches