Sync up with Parsoid parserTests.txt
[mediawiki.git] / includes / MagicWord.php
blob85760bb8413c13178acbfe389f296bbe184223e9
1 <?php
2 /**
3 * See docs/magicword.md.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Parser
24 use MediaWiki\MediaWikiServices;
26 /**
27 * This class encapsulates "magic words" such as "#redirect", __NOTOC__, etc.
29 * @par Usage:
30 * @code
31 * if ( $magicWordFactory->get( 'redirect' )->match( $text ) ) {
32 * // some code
33 * }
34 * @endcode
36 * Please avoid reading the data out of one of these objects and then writing
37 * special case code. If possible, add another match()-like function here.
39 * To add magic words in an extension, use $magicWords in a file listed in
40 * $wgExtensionMessagesFiles[].
42 * @par Example:
43 * @code
44 * $magicWords = [];
46 * $magicWords['en'] = [
47 * 'magicwordkey' => [ 0, 'case_insensitive_magic_word' ],
48 * 'magicwordkey2' => [ 1, 'CASE_sensitive_magic_word2' ],
49 * ];
50 * @endcode
52 * For magic words which name Parser double underscore names, add a
53 * GetDoubleUnderscoreIDs hook. Use string keys.
55 * For magic words which name Parser magic variables, add a GetMagicVariableIDs
56 * hook. Use string keys.
58 * @ingroup Parser
60 class MagicWord {
61 /** #@- */
63 /** @var string */
64 public $mId;
66 /** @var string[] */
67 public $mSynonyms;
69 /** @var bool */
70 public $mCaseSensitive;
72 /** @var string */
73 private $mRegex = '';
75 /** @var string */
76 private $mRegexStart = '';
78 /** @var string */
79 private $mRegexStartToEnd = '';
81 /** @var string */
82 private $mBaseRegex = '';
84 /** @var string */
85 private $mVariableRegex = '';
87 /** @var string */
88 private $mVariableStartToEndRegex = '';
90 /** @var bool */
91 private $mModified = false;
93 /** @var bool */
94 private $mFound = false;
96 /** @var Language */
97 private $contLang;
99 /** #@- */
102 * Create a new MagicWord object
104 * Use factory instead: MagicWordFactory::get
106 * @param string|null $id The internal name of the magic word
107 * @param string[]|string $syn synonyms for the magic word
108 * @param bool $cs If magic word is case sensitive
109 * @param Language|null $contLang Content language
111 public function __construct( $id = null, $syn = [], $cs = false, Language $contLang = null ) {
112 $this->mId = $id;
113 $this->mSynonyms = (array)$syn;
114 $this->mCaseSensitive = $cs;
115 $this->contLang = $contLang ?: MediaWikiServices::getInstance()->getContentLanguage();
119 * Initialises this object with an ID
121 * @param string $id
122 * @throws MWException
124 public function load( $id ) {
125 $this->mId = $id;
126 $this->contLang->getMagic( $this );
127 if ( !$this->mSynonyms ) {
128 $this->mSynonyms = [ 'brionmademeputthishere' ];
129 throw new MWException( "Error: invalid magic word '$id'" );
134 * Preliminary initialisation
135 * @internal
137 public function initRegex() {
138 // Sort the synonyms by length, descending, so that the longest synonym
139 // matches in precedence to the shortest
140 $synonyms = $this->mSynonyms;
141 usort( $synonyms, [ $this, 'compareStringLength' ] );
143 $escSyn = [];
144 foreach ( $synonyms as $synonym ) {
145 // In case a magic word contains /, like that's going to happen;)
146 $escSyn[] = preg_quote( $synonym, '/' );
148 $this->mBaseRegex = implode( '|', $escSyn );
150 $case = $this->mCaseSensitive ? '' : 'iu';
151 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
152 $this->mRegexStart = "/^(?:{$this->mBaseRegex})/{$case}";
153 $this->mRegexStartToEnd = "/^(?:{$this->mBaseRegex})$/{$case}";
154 $this->mVariableRegex = str_replace( "\\$1", "(.*?)", $this->mRegex );
155 $this->mVariableStartToEndRegex = str_replace( "\\$1", "(.*?)",
156 "/^(?:{$this->mBaseRegex})$/{$case}" );
160 * A comparison function that returns -1, 0 or 1 depending on whether the
161 * first string is longer, the same length or shorter than the second
162 * string.
164 * @param string $s1
165 * @param string $s2
167 * @return int
169 public function compareStringLength( $s1, $s2 ) {
170 $l1 = strlen( $s1 );
171 $l2 = strlen( $s2 );
172 return $l2 <=> $l1; // descending
176 * Gets a regex representing matching the word
178 * @return string
180 public function getRegex() {
181 if ( $this->mRegex == '' ) {
182 $this->initRegex();
184 return $this->mRegex;
188 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
189 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
190 * the complete expression
192 * @return string
194 public function getRegexCase() {
195 if ( $this->mRegex === '' ) {
196 $this->initRegex();
199 return $this->mCaseSensitive ? '' : 'iu';
203 * Gets a regex matching the word, if it is at the string start
205 * @return string
207 public function getRegexStart() {
208 if ( $this->mRegex == '' ) {
209 $this->initRegex();
211 return $this->mRegexStart;
215 * Gets a regex matching the word from start to end of a string
217 * @return string
218 * @since 1.23
220 public function getRegexStartToEnd() {
221 if ( $this->mRegexStartToEnd == '' ) {
222 $this->initRegex();
224 return $this->mRegexStartToEnd;
228 * regex without the slashes and what not
230 * @return string
232 public function getBaseRegex() {
233 if ( $this->mRegex == '' ) {
234 $this->initRegex();
236 return $this->mBaseRegex;
240 * Returns true if the text contains the word
242 * @param string $text
244 * @return bool
246 public function match( $text ) {
247 return (bool)preg_match( $this->getRegex(), $text );
251 * Returns true if the text starts with the word
253 * @param string $text
255 * @return bool
257 public function matchStart( $text ) {
258 return (bool)preg_match( $this->getRegexStart(), $text );
262 * Returns true if the text matched the word
264 * @param string $text
266 * @return bool
267 * @since 1.23
269 public function matchStartToEnd( $text ) {
270 return (bool)preg_match( $this->getRegexStartToEnd(), $text );
274 * Returns NULL if there's no match, the value of $1 otherwise
275 * The return code is the matched string, if there's no variable
276 * part in the regex and the matched variable part ($1) if there
277 * is one.
279 * @param string $text
281 * @return string|null
283 public function matchVariableStartToEnd( $text ) {
284 $matches = [];
285 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
286 if ( $matchcount == 0 ) {
287 return null;
288 } else {
289 # multiple matched parts (variable match); some will be empty because of
290 # synonyms. The variable will be the second non-empty one so remove any
291 # blank elements and re-sort the indices.
292 # See also T8526
294 $matches = array_values( array_filter( $matches ) );
296 if ( count( $matches ) == 1 ) {
297 return $matches[0];
298 } else {
299 return $matches[1];
305 * Returns true if the text matches the word, and alters the
306 * input string, removing all instances of the word
308 * @param string &$text
310 * @return bool
312 public function matchAndRemove( &$text ) {
313 $this->mFound = false;
314 $text = preg_replace_callback(
315 $this->getRegex(),
316 [ $this, 'pregRemoveAndRecord' ],
317 $text
320 return $this->mFound;
324 * @param string &$text
325 * @return bool
327 public function matchStartAndRemove( &$text ) {
328 $this->mFound = false;
329 $text = preg_replace_callback(
330 $this->getRegexStart(),
331 [ $this, 'pregRemoveAndRecord' ],
332 $text
335 return $this->mFound;
339 * Used in matchAndRemove()
341 * @return string
343 public function pregRemoveAndRecord() {
344 $this->mFound = true;
345 return '';
349 * Replaces the word with something else
351 * @param string $replacement
352 * @param string $subject
353 * @param int $limit
355 * @return string
357 public function replace( $replacement, $subject, $limit = -1 ) {
358 $res = preg_replace(
359 $this->getRegex(),
360 StringUtils::escapeRegexReplacement( $replacement ),
361 $subject,
362 $limit
364 $this->mModified = $res !== $subject;
365 return $res;
369 * Variable handling: {{SUBST:xxx}} style words
370 * Calls back a function to determine what to replace xxx with
371 * Input word must contain $1
373 * @param string $text
374 * @param callable $callback
376 * @return string
378 public function substituteCallback( $text, $callback ) {
379 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
380 $this->mModified = $res !== $text;
381 return $res;
385 * Matches the word, where $1 is a wildcard
387 * @return string
389 public function getVariableRegex() {
390 if ( $this->mVariableRegex == '' ) {
391 $this->initRegex();
393 return $this->mVariableRegex;
397 * Matches the entire string, where $1 is a wildcard
399 * @return string
401 public function getVariableStartToEndRegex() {
402 if ( $this->mVariableStartToEndRegex == '' ) {
403 $this->initRegex();
405 return $this->mVariableStartToEndRegex;
409 * Accesses the synonym list directly
411 * @param int $i
413 * @return string
415 public function getSynonym( $i ) {
416 return $this->mSynonyms[$i];
420 * @return string[]
422 public function getSynonyms() {
423 return $this->mSynonyms;
427 * Returns true if the last call to replace() or substituteCallback()
428 * returned a modified text, otherwise false.
430 * @return bool
432 public function getWasModified() {
433 return $this->mModified;
437 * Adds all the synonyms of this MagicWord to an array, to allow quick
438 * lookup in a list of magic words
440 * @param string[] &$array
441 * @param string $value
443 public function addToArray( &$array, $value ) {
444 foreach ( $this->mSynonyms as $syn ) {
445 $array[$this->contLang->lc( $syn )] = $value;
450 * @return bool
452 public function isCaseSensitive() {
453 return $this->mCaseSensitive;
457 * @return string
459 public function getId() {
460 return $this->mId;