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',
101 static public $mObjects = array();
105 function MagicWord($id = 0, $syn = '', $cs = false) {
107 $this->mSynonyms
= (array)$syn;
108 $this->mCaseSensitive
= $cs;
110 $this->mRegexStart
= '';
111 $this->mVariableRegex
= '';
112 $this->mVariableStartToEndRegex
= '';
113 $this->mModified
= false;
117 * Factory: creates an object representing an ID
120 static function &get( $id ) {
121 if (!array_key_exists( $id, self
::$mObjects ) ) {
122 $mw = new MagicWord();
124 self
::$mObjects[$id] = $mw;
126 return self
::$mObjects[$id];
130 * Get an array of parser variable IDs
132 static function getVariableIDs() {
133 if ( !self
::$mVariableIDsInitialised ) {
134 # Deprecated constant definition hook, available for extensions that need it
135 $magicWords = array();
136 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
137 foreach ( $magicWords as $word ) {
138 define( $word, $word );
142 wfRunHooks( 'MagicWordwgVariableIDs', array( &self
::$mVariableIDs ) );
143 self
::$mVariableIDsInitialised = true;
145 return self
::$mVariableIDs;
148 # Initialises this object with an ID
149 function load( $id ) {
152 $wgContLang->getMagic( $this );
156 * Preliminary initialisation
159 function initRegex() {
160 #$variableClass = Title::legalChars();
161 # This was used for matching "$1" variables, but different uses of the feature will have
162 # different restrictions, which should be checked *after* the MagicWord has been matched,
166 foreach ( $this->mSynonyms
as $synonym )
167 // In case a magic word contains /, like that's going to happen;)
168 $escSyn[] = preg_quote( $synonym, '/' );
169 $this->mBaseRegex
= implode( '|', $escSyn );
171 $case = $this->mCaseSensitive ?
'' : 'i';
172 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
173 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
174 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
175 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
176 "/^(?:{$this->mBaseRegex})$/{$case}" );
180 * Gets a regex representing matching the word
182 function getRegex() {
183 if ($this->mRegex
== '' ) {
186 return $this->mRegex
;
190 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
191 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
192 * the complete expression
194 function getRegexCase() {
195 if ( $this->mRegex
=== '' )
198 return $this->mCaseSensitive ?
'' : 'i';
202 * Gets a regex matching the word, if it is at the string start
204 function getRegexStart() {
205 if ($this->mRegex
== '' ) {
208 return $this->mRegexStart
;
212 * regex without the slashes and what not
214 function getBaseRegex() {
215 if ($this->mRegex
== '') {
218 return $this->mBaseRegex
;
222 * Returns true if the text contains the word
225 function match( $text ) {
226 return preg_match( $this->getRegex(), $text );
230 * Returns true if the text starts with the word
233 function matchStart( $text ) {
234 return preg_match( $this->getRegexStart(), $text );
238 * Returns NULL if there's no match, the value of $1 otherwise
239 * The return code is the matched string, if there's no variable
240 * part in the regex and the matched variable part ($1) if there
243 function matchVariableStartToEnd( $text ) {
245 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
246 if ( $matchcount == 0 ) {
249 # multiple matched parts (variable match); some will be empty because of
250 # synonyms. The variable will be the second non-empty one so remove any
251 # blank elements and re-sort the indices.
254 $matches = array_values(array_filter($matches));
256 if ( count($matches) == 1 ) { return $matches[0]; }
257 else { return $matches[1]; }
263 * Returns true if the text matches the word, and alters the
264 * input string, removing all instances of the word
266 function matchAndRemove( &$text ) {
267 $this->mFound
= false;
268 $text = preg_replace_callback( $this->getRegex(), array( &$this, 'pregRemoveAndRecord' ), $text );
269 return $this->mFound
;
272 function matchStartAndRemove( &$text ) {
273 $this->mFound
= false;
274 $text = preg_replace_callback( $this->getRegexStart(), array( &$this, 'pregRemoveAndRecord' ), $text );
275 return $this->mFound
;
279 * Used in matchAndRemove()
282 function pregRemoveAndRecord( $match ) {
283 $this->mFound
= true;
288 * Replaces the word with something else
290 function replace( $replacement, $subject, $limit=-1 ) {
291 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject, $limit );
292 $this->mModified
= !($res === $subject);
297 * Variable handling: {{SUBST:xxx}} style words
298 * Calls back a function to determine what to replace xxx with
299 * Input word must contain $1
301 function substituteCallback( $text, $callback ) {
302 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
303 $this->mModified
= !($res === $text);
308 * Matches the word, where $1 is a wildcard
310 function getVariableRegex() {
311 if ( $this->mVariableRegex
== '' ) {
314 return $this->mVariableRegex
;
318 * Matches the entire string, where $1 is a wildcard
320 function getVariableStartToEndRegex() {
321 if ( $this->mVariableStartToEndRegex
== '' ) {
324 return $this->mVariableStartToEndRegex
;
328 * Accesses the synonym list directly
330 function getSynonym( $i ) {
331 return $this->mSynonyms
[$i];
334 function getSynonyms() {
335 return $this->mSynonyms
;
339 * Returns true if the last call to replace() or substituteCallback()
340 * returned a modified text, otherwise false.
342 function getWasModified(){
343 return $this->mModified
;
347 * $magicarr is an associative array of (magic word ID => replacement)
348 * This method uses the php feature to do several replacements at the same time,
349 * thereby gaining some efficiency. The result is placed in the out variable
350 * $result. The return value is true if something was replaced.
353 function replaceMultiple( $magicarr, $subject, &$result ){
356 foreach( $magicarr as $id => $replacement ){
357 $mw = MagicWord
::get( $id );
358 $search[] = $mw->getRegex();
359 $replace[] = $replacement;
362 $result = preg_replace( $search, $replace, $subject );
363 return !($result === $subject);
367 * Adds all the synonyms of this MagicWord to an array, to allow quick
368 * lookup in a list of magic words
370 function addToArray( &$array, $value ) {
371 foreach ( $this->mSynonyms
as $syn ) {
372 $array[$syn] = $value;
376 function isCaseSensitive() {
377 return $this->mCaseSensitive
;