MediaWiki REL1_41
MagicWord.php
Go to the documentation of this file.
1<?php
25
26use Language;
28use MWException;
29use StringUtils;
30
65class MagicWord {
69 public $mId;
70
72 public $mSynonyms;
73
76
77 private ?string $mBaseRegex = null;
78
80 private $contLang;
81
94 public function __construct( $id = null, $syn = [], $cs = false, Language $contLang = null ) {
95 $this->mId = $id;
96 $this->mSynonyms = (array)$syn;
97 $this->mCaseSensitive = $cs;
98 $this->contLang = $contLang ?: MediaWikiServices::getInstance()->getContentLanguage();
99 }
100
107 public function load( $id ) {
108 $this->mId = $id;
109 $this->contLang->getMagic( $this );
110 if ( !$this->mSynonyms ) {
111 throw new MWException( "Error: invalid magic word '$id'" );
112 }
113 }
114
120 public function getRegex() {
121 return '/' . $this->getBaseRegex() . '/' . $this->getRegexCase();
122 }
123
131 public function getRegexCase() {
132 return $this->mCaseSensitive ? '' : 'iu';
133 }
134
140 public function getRegexStart() {
141 return '/^(?:' . $this->getBaseRegex() . ')/' . $this->getRegexCase();
142 }
143
150 public function getRegexStartToEnd() {
151 return '/^(?:' . $this->getBaseRegex() . ')$/' . $this->getRegexCase();
152 }
153
159 public function getBaseRegex() {
160 if ( $this->mBaseRegex === null ) {
161 // Sort the synonyms by length, descending, so that the longest synonym
162 // matches in precedence to the shortest
163 $synonyms = $this->mSynonyms;
164 usort( $synonyms, static fn ( $a, $b ) => strlen( $b ) <=> strlen( $a ) );
165 foreach ( $synonyms as &$synonym ) {
166 $synonym = preg_quote( $synonym, '/' );
167 }
168 $this->mBaseRegex = implode( '|', $synonyms );
169 }
170 return $this->mBaseRegex;
171 }
172
180 public function match( $text ) {
181 return (bool)preg_match( $this->getRegex(), $text );
182 }
183
192 public function matchStartToEnd( $text ) {
193 return (bool)preg_match( $this->getRegexStartToEnd(), $text );
194 }
195
204 public function matchAndRemove( &$text ) {
205 $text = preg_replace( $this->getRegex(), '', $text, -1, $count );
206 return (bool)$count;
207 }
208
213 public function matchStartAndRemove( &$text ) {
214 $text = preg_replace( $this->getRegexStart(), '', $text, -1, $count );
215 return (bool)$count;
216 }
217
227 public function replace( $replacement, $subject, $limit = -1 ) {
228 $res = preg_replace(
229 $this->getRegex(),
230 StringUtils::escapeRegexReplacement( $replacement ),
231 $subject,
232 $limit
233 );
234 return $res;
235 }
236
244 public function getSynonym( $i ) {
245 return $this->mSynonyms[$i];
246 }
247
251 public function getSynonyms() {
252 return $this->mSynonyms;
253 }
254
258 public function isCaseSensitive() {
260 }
261
265 public function getId() {
266 return $this->mId;
267 }
268}
269
273class_alias( MagicWord::class, 'MagicWord' );
Base class for language-specific code.
Definition Language.php:63
MediaWiki exception.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
This class encapsulates "magic words" such as "#redirect", NOTOC, etc.
Definition MagicWord.php:65
getRegexStartToEnd()
Gets a regex matching the word from start to end of a string.
getRegex()
Gets a regex representing matching the word.
matchStartToEnd( $text)
Returns true if the text matched the word.
matchAndRemove(&$text)
Returns true if the text matches the word, and alters the input string, removing all instances of the...
getRegexCase()
Gets the regexp case modifier to use, i.e.
__construct( $id=null, $syn=[], $cs=false, Language $contLang=null)
#-
Definition MagicWord.php:94
getSynonym( $i)
Accesses the synonym list directly.
load( $id)
Initialises this object with an ID.
match( $text)
Returns true if the text contains the word.
getRegexStart()
Gets a regex matching the word, if it is at the string start.
replace( $replacement, $subject, $limit=-1)
Replaces the word with something else.
getBaseRegex()
regex without the slashes and what not
A collection of static methods to play with strings.