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
24 use MediaWiki\MediaWikiServices
;
27 * This class encapsulates "magic words" such as "#redirect", __NOTOC__, etc.
31 * if ( $magicWordFactory->get( 'redirect' )->match( $text ) ) {
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[].
46 * $magicWords['en'] = [
47 * 'magicwordkey' => [ 0, 'case_insensitive_magic_word' ],
48 * 'magicwordkey2' => [ 1, 'CASE_sensitive_magic_word2' ],
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.
70 public $mCaseSensitive;
76 private $mRegexStart = '';
79 private $mRegexStartToEnd = '';
82 private $mBaseRegex = '';
85 private $mVariableRegex = '';
88 private $mVariableStartToEndRegex = '';
91 private $mModified = false;
94 private $mFound = false;
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 ) {
113 $this->mSynonyms
= (array)$syn;
114 $this->mCaseSensitive
= $cs;
115 $this->contLang
= $contLang ?
: MediaWikiServices
::getInstance()->getContentLanguage();
119 * Initialises this object with an ID
122 * @throws MWException
124 public function load( $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
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' ] );
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
169 public function compareStringLength( $s1, $s2 ) {
172 return $l2 <=> $l1; // descending
176 * Gets a regex representing matching the word
180 public function getRegex() {
181 if ( $this->mRegex
== '' ) {
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
194 public function getRegexCase() {
195 if ( $this->mRegex
=== '' ) {
199 return $this->mCaseSensitive ?
'' : 'iu';
203 * Gets a regex matching the word, if it is at the string start
207 public function getRegexStart() {
208 if ( $this->mRegex
== '' ) {
211 return $this->mRegexStart
;
215 * Gets a regex matching the word from start to end of a string
220 public function getRegexStartToEnd() {
221 if ( $this->mRegexStartToEnd
== '' ) {
224 return $this->mRegexStartToEnd
;
228 * regex without the slashes and what not
232 public function getBaseRegex() {
233 if ( $this->mRegex
== '' ) {
236 return $this->mBaseRegex
;
240 * Returns true if the text contains the word
242 * @param string $text
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
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
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
279 * @param string $text
281 * @return string|null
283 public function matchVariableStartToEnd( $text ) {
285 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
286 if ( $matchcount == 0 ) {
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.
294 $matches = array_values( array_filter( $matches ) );
296 if ( count( $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
312 public function matchAndRemove( &$text ) {
313 $this->mFound
= false;
314 $text = preg_replace_callback(
316 [ $this, 'pregRemoveAndRecord' ],
320 return $this->mFound
;
324 * @param string &$text
327 public function matchStartAndRemove( &$text ) {
328 $this->mFound
= false;
329 $text = preg_replace_callback(
330 $this->getRegexStart(),
331 [ $this, 'pregRemoveAndRecord' ],
335 return $this->mFound
;
339 * Used in matchAndRemove()
343 public function pregRemoveAndRecord() {
344 $this->mFound
= true;
349 * Replaces the word with something else
351 * @param string $replacement
352 * @param string $subject
357 public function replace( $replacement, $subject, $limit = -1 ) {
360 StringUtils
::escapeRegexReplacement( $replacement ),
364 $this->mModified
= $res !== $subject;
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
378 public function substituteCallback( $text, $callback ) {
379 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
380 $this->mModified
= $res !== $text;
385 * Matches the word, where $1 is a wildcard
389 public function getVariableRegex() {
390 if ( $this->mVariableRegex
== '' ) {
393 return $this->mVariableRegex
;
397 * Matches the entire string, where $1 is a wildcard
401 public function getVariableStartToEndRegex() {
402 if ( $this->mVariableStartToEndRegex
== '' ) {
405 return $this->mVariableStartToEndRegex
;
409 * Accesses the synonym list directly
415 public function getSynonym( $i ) {
416 return $this->mSynonyms
[$i];
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.
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;
452 public function isCaseSensitive() {
453 return $this->mCaseSensitive
;
459 public function getId() {