10 $wgMagicFound = false;
13 define('MAG_REDIRECT', 0);
14 define('MAG_NOTOC', 1);
15 define('MAG_START', 2);
16 define('MAG_CURRENTMONTH', 3);
17 define('MAG_CURRENTMONTHNAME', 4);
18 define('MAG_CURRENTDAY', 5);
19 define('MAG_CURRENTDAYNAME', 6);
20 define('MAG_CURRENTYEAR', 7);
21 define('MAG_CURRENTTIME', 8);
22 define('MAG_NUMBEROFARTICLES', 9);
23 define('MAG_CURRENTMONTHNAMEGEN', 10);
24 define('MAG_MSG', 11);
25 define('MAG_SUBST', 12);
26 define('MAG_MSGNW', 13);
27 define('MAG_NOEDITSECTION', 14);
28 define('MAG_END', 15);
29 define('MAG_IMG_THUMBNAIL', 16);
30 define('MAG_IMG_RIGHT', 17);
31 define('MAG_IMG_LEFT', 18);
32 define('MAG_IMG_NONE', 19);
33 define('MAG_IMG_WIDTH', 20);
34 define('MAG_IMG_CENTER', 21);
35 define('MAG_INT', 22);
36 define('MAG_FORCETOC', 23);
37 define('MAG_SITENAME', 24);
39 define('MAG_LOCALURL', 26);
40 define('MAG_LOCALURLE', 27);
41 define('MAG_SERVER', 28);
42 define('MAG_IMG_FRAMED', 29);
43 define('MAG_PAGENAME', 30);
44 define('MAG_PAGENAMEE', 31);
45 define('MAG_NAMESPACE', 32);
46 define('MAG_TOC', 33);
47 define('MAG_GRAMMAR', 34);
49 $wgVariableIDs = array(
57 MAG_CURRENTMONTHNAMEGEN
,
66 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
68 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
70 * Possible future improvements:
71 * * Simultaneous searching for a number of magic words
72 * * $wgMagicWords in shared memory
74 * Please avoid reading the data out of one of these objects and then writing
75 * special case code. If possible, add another match()-like function here.
83 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
84 var $mRegexStart, $mBaseRegex, $mVariableRegex;
88 function MagicWord($id = 0, $syn = '', $cs = false) {
90 $this->mSynonyms
= (array)$syn;
91 $this->mCaseSensitive
= $cs;
93 $this->mRegexStart
= '';
94 $this->mVariableRegex
= '';
95 $this->mVariableStartToEndRegex
= '';
96 $this->mModified
= false;
100 * Factory: creates an object representing an ID
103 function &get( $id ) {
104 global $wgMagicWords;
106 if ( !is_array( $wgMagicWords ) ) {
107 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
109 if (!array_key_exists( $id, $wgMagicWords ) ) {
110 $mw = new MagicWord();
112 $wgMagicWords[$id] = $mw;
114 return $wgMagicWords[$id];
117 # Initialises this object with an ID
118 function load( $id ) {
121 $wgLang->getMagic( $this );
125 * Preliminary initialisation
128 function initRegex() {
129 $variableClass = Title
::legalChars();
130 $escSyn = array_map( 'preg_quote', $this->mSynonyms
);
131 $this->mBaseRegex
= implode( '|', $escSyn );
132 $case = $this->mCaseSensitive ?
'' : 'i';
133 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
134 $this->mRegexStart
= "/^({$this->mBaseRegex})/{$case}";
135 $this->mVariableRegex
= str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex
);
136 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "([$variableClass]*?)",
137 "/^({$this->mBaseRegex})$/{$case}" );
141 * Gets a regex representing matching the word
143 function getRegex() {
144 if ($this->mRegex
== '' ) {
147 return $this->mRegex
;
151 * Gets a regex matching the word, if it is at the string start
153 function getRegexStart() {
154 if ($this->mRegex
== '' ) {
157 return $this->mRegexStart
;
161 * regex without the slashes and what not
163 function getBaseRegex() {
164 if ($this->mRegex
== '') {
167 return $this->mBaseRegex
;
171 * Returns true if the text contains the word
174 function match( $text ) {
175 return preg_match( $this->getRegex(), $text );
179 * Returns true if the text starts with the word
182 function matchStart( $text ) {
183 return preg_match( $this->getRegexStart(), $text );
187 * Returns NULL if there's no match, the value of $1 otherwise
188 * The return code is the matched string, if there's no variable
189 * part in the regex and the matched variable part ($1) if there
192 function matchVariableStartToEnd( $text ) {
193 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
194 if ( $matchcount == 0 ) {
196 } elseif ( count($matches) == 1 ) {
205 * Returns true if the text matches the word, and alters the
206 * input string, removing all instances of the word
208 function matchAndRemove( &$text ) {
209 global $wgMagicFound;
210 $wgMagicFound = false;
211 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
212 return $wgMagicFound;
215 function matchStartAndRemove( &$text ) {
216 global $wgMagicFound;
217 $wgMagicFound = false;
218 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
219 return $wgMagicFound;
224 * Replaces the word with something else
226 function replace( $replacement, $subject ) {
227 $res = preg_replace( $this->getRegex(), $replacement, $subject );
228 $this->mModified
= !($res === $subject);
233 * Variable handling: {{SUBST:xxx}} style words
234 * Calls back a function to determine what to replace xxx with
235 * Input word must contain $1
237 function substituteCallback( $text, $callback ) {
238 $regex = $this->getVariableRegex();
239 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
240 $this->mModified
= !($res === $text);
245 * Matches the word, where $1 is a wildcard
247 function getVariableRegex() {
248 if ( $this->mVariableRegex
== '' ) {
251 return $this->mVariableRegex
;
255 * Matches the entire string, where $1 is a wildcard
257 function getVariableStartToEndRegex() {
258 if ( $this->mVariableStartToEndRegex
== '' ) {
261 return $this->mVariableStartToEndRegex
;
265 * Accesses the synonym list directly
267 function getSynonym( $i ) {
268 return $this->mSynonyms
[$i];
272 * Returns true if the last call to replace() or substituteCallback()
273 * returned a modified text, otherwise false.
275 function getWasModified(){
276 return $this->mModified
;
280 * $magicarr is an associative array of (magic word ID => replacement)
281 * This method uses the php feature to do several replacements at the same time,
282 * thereby gaining some efficiency. The result is placed in the out variable
283 * $result. The return value is true if something was replaced.
286 function replaceMultiple( $magicarr, $subject, &$result ){
289 foreach( $magicarr as $id => $replacement ){
290 $mw = MagicWord
::get( $id );
291 $search[] = $mw->getRegex();
292 $replace[] = $replacement;
295 $result = preg_replace( $search, $replace, $subject );
296 return !($result === $subject);
300 * Adds all the synonyms of this MagicWord to an array, to allow quick
301 * lookup in a list of magic words
303 function addToArray( &$array, $value ) {
304 foreach ( $this->mSynonyms
as $syn ) {
305 $array[$syn] = $value;
311 * Used in matchAndRemove()
314 function pregRemoveAndRecord( $match ) {
315 global $wgMagicFound;
316 $wgMagicFound = true;