MediaWiki  1.27.2
MagicWordArray.php
Go to the documentation of this file.
1 <?php
2 
26 
33  public $names = [];
34 
36  private $hash;
37 
38  private $baseRegex;
39 
40  private $regex;
41 
45  public function __construct( $names = [] ) {
46  $this->names = $names;
47  }
48 
54  public function add( $name ) {
55  $this->names[] = $name;
56  $this->hash = $this->baseRegex = $this->regex = null;
57  }
58 
64  public function addArray( $names ) {
65  $this->names = array_merge( $this->names, array_values( $names ) );
66  $this->hash = $this->baseRegex = $this->regex = null;
67  }
68 
73  public function getHash() {
74  if ( is_null( $this->hash ) ) {
76  $this->hash = [ 0 => [], 1 => [] ];
77  foreach ( $this->names as $name ) {
78  $magic = MagicWord::get( $name );
79  $case = intval( $magic->isCaseSensitive() );
80  foreach ( $magic->getSynonyms() as $syn ) {
81  if ( !$case ) {
82  $syn = $wgContLang->lc( $syn );
83  }
84  $this->hash[$case][$syn] = $name;
85  }
86  }
87  }
88  return $this->hash;
89  }
90 
95  public function getBaseRegex() {
96  if ( is_null( $this->baseRegex ) ) {
97  $this->baseRegex = [ 0 => '', 1 => '' ];
98  foreach ( $this->names as $name ) {
99  $magic = MagicWord::get( $name );
100  $case = intval( $magic->isCaseSensitive() );
101  foreach ( $magic->getSynonyms() as $i => $syn ) {
102  // Group name must start with a non-digit in PCRE 8.34+
103  $it = strtr( $i, '0123456789', 'abcdefghij' );
104  $group = "(?P<{$it}_{$name}>" . preg_quote( $syn, '/' ) . ')';
105  if ( $this->baseRegex[$case] === '' ) {
106  $this->baseRegex[$case] = $group;
107  } else {
108  $this->baseRegex[$case] .= '|' . $group;
109  }
110  }
111  }
112  }
113  return $this->baseRegex;
114  }
115 
120  public function getRegex() {
121  if ( is_null( $this->regex ) ) {
122  $base = $this->getBaseRegex();
123  $this->regex = [ '', '' ];
124  if ( $this->baseRegex[0] !== '' ) {
125  $this->regex[0] = "/{$base[0]}/iuS";
126  }
127  if ( $this->baseRegex[1] !== '' ) {
128  $this->regex[1] = "/{$base[1]}/S";
129  }
130  }
131  return $this->regex;
132  }
133 
139  public function getVariableRegex() {
140  return str_replace( "\\$1", "(.*?)", $this->getRegex() );
141  }
142 
148  public function getRegexStart() {
149  $base = $this->getBaseRegex();
150  $newRegex = [ '', '' ];
151  if ( $base[0] !== '' ) {
152  $newRegex[0] = "/^(?:{$base[0]})/iuS";
153  }
154  if ( $base[1] !== '' ) {
155  $newRegex[1] = "/^(?:{$base[1]})/S";
156  }
157  return $newRegex;
158  }
159 
165  public function getVariableStartToEndRegex() {
166  $base = $this->getBaseRegex();
167  $newRegex = [ '', '' ];
168  if ( $base[0] !== '' ) {
169  $newRegex[0] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[0]})$/iuS" );
170  }
171  if ( $base[1] !== '' ) {
172  $newRegex[1] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[1]})$/S" );
173  }
174  return $newRegex;
175  }
176 
181  public function getNames() {
182  return $this->names;
183  }
184 
195  public function parseMatch( $m ) {
196  reset( $m );
197  while ( list( $key, $value ) = each( $m ) ) {
198  if ( $key === 0 || $value === '' ) {
199  continue;
200  }
201  $parts = explode( '_', $key, 2 );
202  if ( count( $parts ) != 2 ) {
203  // This shouldn't happen
204  // continue;
205  throw new MWException( __METHOD__ . ': bad parameter name' );
206  }
207  list( /* $synIndex */, $magicName ) = $parts;
208  $paramValue = next( $m );
209  return [ $magicName, $paramValue ];
210  }
211  // This shouldn't happen either
212  throw new MWException( __METHOD__ . ': parameter not found' );
213  }
214 
225  public function matchVariableStartToEnd( $text ) {
226  $regexes = $this->getVariableStartToEndRegex();
227  foreach ( $regexes as $regex ) {
228  if ( $regex !== '' ) {
229  $m = [];
230  if ( preg_match( $regex, $text, $m ) ) {
231  return $this->parseMatch( $m );
232  }
233  }
234  }
235  return [ false, false ];
236  }
237 
246  public function matchStartToEnd( $text ) {
247  $hash = $this->getHash();
248  if ( isset( $hash[1][$text] ) ) {
249  return $hash[1][$text];
250  }
252  $lc = $wgContLang->lc( $text );
253  if ( isset( $hash[0][$lc] ) ) {
254  return $hash[0][$lc];
255  }
256  return false;
257  }
258 
267  public function matchAndRemove( &$text ) {
268  $found = [];
269  $regexes = $this->getRegex();
270  foreach ( $regexes as $regex ) {
271  if ( $regex === '' ) {
272  continue;
273  }
274  $matches = [];
275  $res = preg_match_all( $regex, $text, $matches, PREG_SET_ORDER );
276  if ( $res === false ) {
277  LoggerFactory::getInstance( 'parser' )->warning( 'preg_match_all returned false', [
278  'code' => preg_last_error(),
279  'regex' => $regex,
280  'text' => $text,
281  ] );
282  } elseif ( $res ) {
283  foreach ( $matches as $m ) {
284  list( $name, $param ) = $this->parseMatch( $m );
285  $found[$name] = $param;
286  }
287  }
288  $res = preg_replace( $regex, '', $text );
289  if ( $res === null ) {
290  LoggerFactory::getInstance( 'parser' )->warning( 'preg_replace returned null', [
291  'code' => preg_last_error(),
292  'regex' => $regex,
293  'text' => $text,
294  ] );
295  }
296  $text = $res;
297  }
298  return $found;
299  }
300 
311  public function matchStartAndRemove( &$text ) {
312  $regexes = $this->getRegexStart();
313  foreach ( $regexes as $regex ) {
314  if ( $regex === '' ) {
315  continue;
316  }
317  if ( preg_match( $regex, $text, $m ) ) {
318  list( $id, ) = $this->parseMatch( $m );
319  if ( strlen( $m[0] ) >= strlen( $text ) ) {
320  $text = '';
321  } else {
322  $text = substr( $text, strlen( $m[0] ) );
323  }
324  return $id;
325  }
326  }
327  return false;
328  }
329 }
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
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
getVariableRegex()
Get a regex for matching variables with parameters.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
add($name)
Add a magic word by name.
matchAndRemove(&$text)
Returns an associative array, ID => param value, for all items that match Removes the matched items f...
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
$value
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
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.
addArray($names)
Add a number of magic words by name.
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
matchStartToEnd($text)
Match some text, without parameter capture Returns the magic word name, or false if there was no capt...
Class for handling an array of magic words.
static & get($id)
Factory: creates an object representing an ID.
Definition: MagicWord.php:257
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
__construct($names=[])
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition: design.txt:56
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
matchVariableStartToEnd($text)
Match some text, with parameter capture Returns an array with the magic word name in the first elemen...
parseMatch($m)
Parse a match array from preg_match Returns array(magic word ID, parameter value) If there is no para...
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.
$matches
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310