MediaWiki  REL1_31
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  $allGroups = [];
99  foreach ( $this->names as $name ) {
100  $magic = MagicWord::get( $name );
101  $case = intval( $magic->isCaseSensitive() );
102  foreach ( $magic->getSynonyms() as $i => $syn ) {
103  // Group name must start with a non-digit in PCRE 8.34+
104  $it = strtr( $i, '0123456789', 'abcdefghij' );
105  $groupName = $it . '_' . $name;
106  $group = '(?P<' . $groupName . '>' . preg_quote( $syn, '/' ) . ')';
107  // look for same group names to avoid same named subpatterns in the regex
108  if ( isset( $allGroups[$groupName] ) ) {
109  throw new MWException(
110  __METHOD__ . ': duplicate internal name in magic word array: ' . $name
111  );
112  }
113  $allGroups[$groupName] = true;
114  if ( $this->baseRegex[$case] === '' ) {
115  $this->baseRegex[$case] = $group;
116  } else {
117  $this->baseRegex[$case] .= '|' . $group;
118  }
119  }
120  }
121  }
122  return $this->baseRegex;
123  }
124 
129  public function getRegex() {
130  if ( is_null( $this->regex ) ) {
131  $base = $this->getBaseRegex();
132  $this->regex = [ '', '' ];
133  if ( $this->baseRegex[0] !== '' ) {
134  $this->regex[0] = "/{$base[0]}/iuS";
135  }
136  if ( $this->baseRegex[1] !== '' ) {
137  $this->regex[1] = "/{$base[1]}/S";
138  }
139  }
140  return $this->regex;
141  }
142 
148  public function getVariableRegex() {
149  return str_replace( "\\$1", "(.*?)", $this->getRegex() );
150  }
151 
157  public function getRegexStart() {
158  $base = $this->getBaseRegex();
159  $newRegex = [ '', '' ];
160  if ( $base[0] !== '' ) {
161  $newRegex[0] = "/^(?:{$base[0]})/iuS";
162  }
163  if ( $base[1] !== '' ) {
164  $newRegex[1] = "/^(?:{$base[1]})/S";
165  }
166  return $newRegex;
167  }
168 
174  public function getVariableStartToEndRegex() {
175  $base = $this->getBaseRegex();
176  $newRegex = [ '', '' ];
177  if ( $base[0] !== '' ) {
178  $newRegex[0] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[0]})$/iuS" );
179  }
180  if ( $base[1] !== '' ) {
181  $newRegex[1] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[1]})$/S" );
182  }
183  return $newRegex;
184  }
185 
190  public function getNames() {
191  return $this->names;
192  }
193 
204  public function parseMatch( $m ) {
205  reset( $m );
206  while ( ( $key = key( $m ) ) !== null ) {
207  $value = current( $m );
208  next( $m );
209  if ( $key === 0 || $value === '' ) {
210  continue;
211  }
212  $parts = explode( '_', $key, 2 );
213  if ( count( $parts ) != 2 ) {
214  // This shouldn't happen
215  // continue;
216  throw new MWException( __METHOD__ . ': bad parameter name' );
217  }
218  list( /* $synIndex */, $magicName ) = $parts;
219  $paramValue = next( $m );
220  return [ $magicName, $paramValue ];
221  }
222  // This shouldn't happen either
223  throw new MWException( __METHOD__ . ': parameter not found' );
224  }
225 
236  public function matchVariableStartToEnd( $text ) {
237  $regexes = $this->getVariableStartToEndRegex();
238  foreach ( $regexes as $regex ) {
239  if ( $regex !== '' ) {
240  $m = [];
241  if ( preg_match( $regex, $text, $m ) ) {
242  return $this->parseMatch( $m );
243  }
244  }
245  }
246  return [ false, false ];
247  }
248 
257  public function matchStartToEnd( $text ) {
258  $hash = $this->getHash();
259  if ( isset( $hash[1][$text] ) ) {
260  return $hash[1][$text];
261  }
263  $lc = $wgContLang->lc( $text );
264  if ( isset( $hash[0][$lc] ) ) {
265  return $hash[0][$lc];
266  }
267  return false;
268  }
269 
278  public function matchAndRemove( &$text ) {
279  $found = [];
280  $regexes = $this->getRegex();
281  foreach ( $regexes as $regex ) {
282  if ( $regex === '' ) {
283  continue;
284  }
285  $matches = [];
286  $res = preg_match_all( $regex, $text, $matches, PREG_SET_ORDER );
287  if ( $res === false ) {
288  LoggerFactory::getInstance( 'parser' )->warning( 'preg_match_all returned false', [
289  'code' => preg_last_error(),
290  'regex' => $regex,
291  'text' => $text,
292  ] );
293  } elseif ( $res ) {
294  foreach ( $matches as $m ) {
295  list( $name, $param ) = $this->parseMatch( $m );
296  $found[$name] = $param;
297  }
298  }
299  $res = preg_replace( $regex, '', $text );
300  if ( $res === null ) {
301  LoggerFactory::getInstance( 'parser' )->warning( 'preg_replace returned null', [
302  'code' => preg_last_error(),
303  'regex' => $regex,
304  'text' => $text,
305  ] );
306  }
307  $text = $res;
308  }
309  return $found;
310  }
311 
322  public function matchStartAndRemove( &$text ) {
323  $regexes = $this->getRegexStart();
324  foreach ( $regexes as $regex ) {
325  if ( $regex === '' ) {
326  continue;
327  }
328  if ( preg_match( $regex, $text, $m ) ) {
329  list( $id, ) = $this->parseMatch( $m );
330  if ( strlen( $m[0] ) >= strlen( $text ) ) {
331  $text = '';
332  } else {
333  $text = substr( $text, strlen( $m[0] ) );
334  }
335  return $id;
336  }
337  }
338  return false;
339  }
340 }
MagicWordArray
Class for handling an array of magic words.
Definition: MagicWordArray.php:31
MagicWordArray\getVariableStartToEndRegex
getVariableStartToEndRegex()
Get an anchored regex for matching variables with parameters.
Definition: MagicWordArray.php:174
MagicWordArray\getNames
getNames()
Definition: MagicWordArray.php:190
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:322
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
MagicWordArray\getHash
getHash()
Get a 2-d hashtable for this array.
Definition: MagicWordArray.php:73
names
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
Definition: APACHE-LICENSE-2.0.txt:140
MagicWordArray\getRegexStart
getRegexStart()
Get a regex anchored to the start of the string that does not match parameters.
Definition: MagicWordArray.php:157
array
the array() calling protocol came about after MediaWiki 1.4rc1.
MagicWord\get
static & get( $id)
Factory: creates an object representing an ID.
Definition: MagicWord.php:277
$res
$res
Definition: database.txt:21
MagicWordArray\$baseRegex
$baseRegex
Definition: MagicWordArray.php:38
$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:257
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:37
key
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition: design.txt:26
MWException
MediaWiki exception.
Definition: MWException.php:26
$matches
$matches
Definition: NoLocalSettings.php:24
MagicWordArray\$hash
array $hash
Definition: MagicWordArray.php:36
MagicWordArray\add
add( $name)
Add a magic word by name.
Definition: MagicWordArray.php:54
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
MagicWordArray\getBaseRegex
getBaseRegex()
Get the base regex.
Definition: MagicWordArray.php:95
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
MagicWordArray\getRegex
getRegex()
Get an unanchored regex that does not match parameters.
Definition: MagicWordArray.php:129
MagicWordArray\matchAndRemove
matchAndRemove(&$text)
Returns an associative array, ID => param value, for all items that match Removes the matched items f...
Definition: MagicWordArray.php:278
$value
$value
Definition: styleTest.css.php:45
MagicWordArray\$names
array $names
Definition: MagicWordArray.php:33
MagicWordArray\getVariableRegex
getVariableRegex()
Get a regex for matching variables with parameters.
Definition: MagicWordArray.php:148
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:204
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
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:22
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:40
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:236
MagicWordArray\addArray
addArray( $names)
Add a number of magic words by name.
Definition: MagicWordArray.php:64
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
MagicWordArray\__construct
__construct( $names=[])
Definition: MagicWordArray.php:45
$wgContLang
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 content language as $wgContLang
Definition: design.txt:57