11 $wgMagicFound = false;
14 * To add magic words in an extension, use the LanguageGetMagic hook. For
15 * magic words which are also Parser variables, add a MagicWordwgVariableIDs
16 * hook. Use string keys. Do use the interface immediately below,
17 * MagicWordMagicWords. It is deprecated.
20 $magicWords = array();
21 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
22 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
24 for ( $i = 0; $i < count( $magicWords ); ++
$i )
25 define( $magicWords[$i], $magicWords[$i] );
28 $wgVariableIDs = array(
31 'currentmonthnamegen',
79 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
80 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
83 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
85 * if (MagicWord::get( 'redirect' )->match( $text ) )
87 * Possible future improvements:
88 * * Simultaneous searching for a number of magic words
89 * * $wgMagicWords in shared memory
91 * Please avoid reading the data out of one of these objects and then writing
92 * special case code. If possible, add another match()-like function here.
100 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
101 var $mRegexStart, $mBaseRegex, $mVariableRegex;
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 global $wgMagicWords;
123 if ( !is_array( $wgMagicWords ) ) {
124 throw new MWException( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
126 if (!array_key_exists( $id, $wgMagicWords ) ) {
127 $mw = new MagicWord();
129 $wgMagicWords[$id] = $mw;
131 return $wgMagicWords[$id];
134 # Initialises this object with an ID
135 function load( $id ) {
138 $wgContLang->getMagic( $this );
142 * Preliminary initialisation
145 function initRegex() {
146 #$variableClass = Title::legalChars();
147 # This was used for matching "$1" variables, but different uses of the feature will have
148 # different restrictions, which should be checked *after* the MagicWord has been matched,
152 foreach ( $this->mSynonyms
as $synonym )
153 // In case a magic word contains /, like that's going to happen;)
154 $escSyn[] = preg_quote( $synonym, '/' );
155 $this->mBaseRegex
= implode( '|', $escSyn );
157 $case = $this->mCaseSensitive ?
'' : 'i';
158 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
159 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
160 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
161 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
162 "/^(?:{$this->mBaseRegex})$/{$case}" );
166 * Gets a regex representing matching the word
168 function getRegex() {
169 if ($this->mRegex
== '' ) {
172 return $this->mRegex
;
176 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
177 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
178 * the complete expression
180 function getRegexCase() {
181 if ( $this->mRegex
=== '' )
184 return $this->mCaseSensitive ?
'' : 'i';
188 * Gets a regex matching the word, if it is at the string start
190 function getRegexStart() {
191 if ($this->mRegex
== '' ) {
194 return $this->mRegexStart
;
198 * regex without the slashes and what not
200 function getBaseRegex() {
201 if ($this->mRegex
== '') {
204 return $this->mBaseRegex
;
208 * Returns true if the text contains the word
211 function match( $text ) {
212 return preg_match( $this->getRegex(), $text );
216 * Returns true if the text starts with the word
219 function matchStart( $text ) {
220 return preg_match( $this->getRegexStart(), $text );
224 * Returns NULL if there's no match, the value of $1 otherwise
225 * The return code is the matched string, if there's no variable
226 * part in the regex and the matched variable part ($1) if there
229 function matchVariableStartToEnd( $text ) {
231 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
232 if ( $matchcount == 0 ) {
235 # multiple matched parts (variable match); some will be empty because of
236 # synonyms. The variable will be the second non-empty one so remove any
237 # blank elements and re-sort the indices.
240 $matches = array_values(array_filter($matches));
242 if ( count($matches) == 1 ) { return $matches[0]; }
243 else { return $matches[1]; }
249 * Returns true if the text matches the word, and alters the
250 * input string, removing all instances of the word
252 function matchAndRemove( &$text ) {
253 global $wgMagicFound;
254 $wgMagicFound = false;
255 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
256 return $wgMagicFound;
259 function matchStartAndRemove( &$text ) {
260 global $wgMagicFound;
261 $wgMagicFound = false;
262 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
263 return $wgMagicFound;
268 * Replaces the word with something else
270 function replace( $replacement, $subject, $limit=-1 ) {
271 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject, $limit );
272 $this->mModified
= !($res === $subject);
277 * Variable handling: {{SUBST:xxx}} style words
278 * Calls back a function to determine what to replace xxx with
279 * Input word must contain $1
281 function substituteCallback( $text, $callback ) {
282 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
283 $this->mModified
= !($res === $text);
288 * Matches the word, where $1 is a wildcard
290 function getVariableRegex() {
291 if ( $this->mVariableRegex
== '' ) {
294 return $this->mVariableRegex
;
298 * Matches the entire string, where $1 is a wildcard
300 function getVariableStartToEndRegex() {
301 if ( $this->mVariableStartToEndRegex
== '' ) {
304 return $this->mVariableStartToEndRegex
;
308 * Accesses the synonym list directly
310 function getSynonym( $i ) {
311 return $this->mSynonyms
[$i];
314 function getSynonyms() {
315 return $this->mSynonyms
;
319 * Returns true if the last call to replace() or substituteCallback()
320 * returned a modified text, otherwise false.
322 function getWasModified(){
323 return $this->mModified
;
327 * $magicarr is an associative array of (magic word ID => replacement)
328 * This method uses the php feature to do several replacements at the same time,
329 * thereby gaining some efficiency. The result is placed in the out variable
330 * $result. The return value is true if something was replaced.
333 function replaceMultiple( $magicarr, $subject, &$result ){
336 foreach( $magicarr as $id => $replacement ){
337 $mw = MagicWord
::get( $id );
338 $search[] = $mw->getRegex();
339 $replace[] = $replacement;
342 $result = preg_replace( $search, $replace, $subject );
343 return !($result === $subject);
347 * Adds all the synonyms of this MagicWord to an array, to allow quick
348 * lookup in a list of magic words
350 function addToArray( &$array, $value ) {
351 foreach ( $this->mSynonyms
as $syn ) {
352 $array[$syn] = $value;
356 function isCaseSensitive() {
357 return $this->mCaseSensitive
;
362 * Used in matchAndRemove()
365 function pregRemoveAndRecord( $match ) {
366 global $wgMagicFound;
367 $wgMagicFound = true;