826a7e9d0419643e355c3753b78025475781bc63
9 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
11 * if (MagicWord::get( 'redirect' )->match( $text ) )
13 * Possible future improvements:
14 * * Simultaneous searching for a number of magic words
15 * * MagicWord::$mObjects in shared memory
17 * Please avoid reading the data out of one of these objects and then writing
18 * special case code. If possible, add another match()-like function here.
20 * To add magic words in an extension, use the LanguageGetMagic hook. For
21 * magic words which are also Parser variables, add a MagicWordwgVariableIDs
22 * hook. Use string keys.
30 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
31 var $mRegexStart, $mBaseRegex, $mVariableRegex;
32 var $mModified, $mFound;
34 static public $mVariableIDsInitialised = false;
35 static public $mVariableIDs = array(
38 'currentmonthnamegen',
106 static public $mObjects = array();
110 function MagicWord($id = 0, $syn = '', $cs = false) {
112 $this->mSynonyms
= (array)$syn;
113 $this->mCaseSensitive
= $cs;
115 $this->mRegexStart
= '';
116 $this->mVariableRegex
= '';
117 $this->mVariableStartToEndRegex
= '';
118 $this->mModified
= false;
122 * Factory: creates an object representing an ID
125 static function &get( $id ) {
126 if (!array_key_exists( $id, self
::$mObjects ) ) {
127 $mw = new MagicWord();
129 self
::$mObjects[$id] = $mw;
131 return self
::$mObjects[$id];
135 * Get an array of parser variable IDs
137 static function getVariableIDs() {
138 if ( !self
::$mVariableIDsInitialised ) {
139 # Deprecated constant definition hook, available for extensions that need it
140 $magicWords = array();
141 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
142 foreach ( $magicWords as $word ) {
143 define( $word, $word );
147 wfRunHooks( 'MagicWordwgVariableIDs', array( &self
::$mVariableIDs ) );
148 self
::$mVariableIDsInitialised = true;
150 return self
::$mVariableIDs;
153 # Initialises this object with an ID
154 function load( $id ) {
157 $wgContLang->getMagic( $this );
161 * Preliminary initialisation
164 function initRegex() {
165 #$variableClass = Title::legalChars();
166 # This was used for matching "$1" variables, but different uses of the feature will have
167 # different restrictions, which should be checked *after* the MagicWord has been matched,
171 foreach ( $this->mSynonyms
as $synonym )
172 // In case a magic word contains /, like that's going to happen;)
173 $escSyn[] = preg_quote( $synonym, '/' );
174 $this->mBaseRegex
= implode( '|', $escSyn );
176 $case = $this->mCaseSensitive ?
'' : 'i';
177 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
178 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
179 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
180 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
181 "/^(?:{$this->mBaseRegex})$/{$case}" );
185 * Gets a regex representing matching the word
187 function getRegex() {
188 if ($this->mRegex
== '' ) {
191 return $this->mRegex
;
195 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
196 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
197 * the complete expression
199 function getRegexCase() {
200 if ( $this->mRegex
=== '' )
203 return $this->mCaseSensitive ?
'' : 'i';
207 * Gets a regex matching the word, if it is at the string start
209 function getRegexStart() {
210 if ($this->mRegex
== '' ) {
213 return $this->mRegexStart
;
217 * regex without the slashes and what not
219 function getBaseRegex() {
220 if ($this->mRegex
== '') {
223 return $this->mBaseRegex
;
227 * Returns true if the text contains the word
230 function match( $text ) {
231 return preg_match( $this->getRegex(), $text );
235 * Returns true if the text starts with the word
238 function matchStart( $text ) {
239 return preg_match( $this->getRegexStart(), $text );
243 * Returns NULL if there's no match, the value of $1 otherwise
244 * The return code is the matched string, if there's no variable
245 * part in the regex and the matched variable part ($1) if there
248 function matchVariableStartToEnd( $text ) {
250 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
251 if ( $matchcount == 0 ) {
254 # multiple matched parts (variable match); some will be empty because of
255 # synonyms. The variable will be the second non-empty one so remove any
256 # blank elements and re-sort the indices.
259 $matches = array_values(array_filter($matches));
261 if ( count($matches) == 1 ) { return $matches[0]; }
262 else { return $matches[1]; }
268 * Returns true if the text matches the word, and alters the
269 * input string, removing all instances of the word
271 function matchAndRemove( &$text ) {
272 $this->mFound
= false;
273 $text = preg_replace_callback( $this->getRegex(), array( &$this, 'pregRemoveAndRecord' ), $text );
274 return $this->mFound
;
277 function matchStartAndRemove( &$text ) {
278 $this->mFound
= false;
279 $text = preg_replace_callback( $this->getRegexStart(), array( &$this, 'pregRemoveAndRecord' ), $text );
280 return $this->mFound
;
284 * Used in matchAndRemove()
287 function pregRemoveAndRecord( $match ) {
288 $this->mFound
= true;
293 * Replaces the word with something else
295 function replace( $replacement, $subject, $limit=-1 ) {
296 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject, $limit );
297 $this->mModified
= !($res === $subject);
302 * Variable handling: {{SUBST:xxx}} style words
303 * Calls back a function to determine what to replace xxx with
304 * Input word must contain $1
306 function substituteCallback( $text, $callback ) {
307 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
308 $this->mModified
= !($res === $text);
313 * Matches the word, where $1 is a wildcard
315 function getVariableRegex() {
316 if ( $this->mVariableRegex
== '' ) {
319 return $this->mVariableRegex
;
323 * Matches the entire string, where $1 is a wildcard
325 function getVariableStartToEndRegex() {
326 if ( $this->mVariableStartToEndRegex
== '' ) {
329 return $this->mVariableStartToEndRegex
;
333 * Accesses the synonym list directly
335 function getSynonym( $i ) {
336 return $this->mSynonyms
[$i];
339 function getSynonyms() {
340 return $this->mSynonyms
;
344 * Returns true if the last call to replace() or substituteCallback()
345 * returned a modified text, otherwise false.
347 function getWasModified(){
348 return $this->mModified
;
352 * $magicarr is an associative array of (magic word ID => replacement)
353 * This method uses the php feature to do several replacements at the same time,
354 * thereby gaining some efficiency. The result is placed in the out variable
355 * $result. The return value is true if something was replaced.
358 function replaceMultiple( $magicarr, $subject, &$result ){
361 foreach( $magicarr as $id => $replacement ){
362 $mw = MagicWord
::get( $id );
363 $search[] = $mw->getRegex();
364 $replace[] = $replacement;
367 $result = preg_replace( $search, $replace, $subject );
368 return !($result === $subject);
372 * Adds all the synonyms of this MagicWord to an array, to allow quick
373 * lookup in a list of magic words
375 function addToArray( &$array, $value ) {
376 foreach ( $this->mSynonyms
as $syn ) {
377 $array[$syn] = $value;
381 function isCaseSensitive() {
382 return $this->mCaseSensitive
;