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_CURRENTMONTHNAMEGEN', 5);
19 define('MAG_CURRENTMONTHABBREV', 6);
20 define('MAG_CURRENTDAY', 7);
21 define('MAG_CURRENTDAYNAME', 8);
22 define('MAG_CURRENTYEAR', 9);
23 define('MAG_CURRENTTIME', 10);
24 define('MAG_NUMBEROFARTICLES', 11);
25 define('MAG_MSG', 12);
26 define('MAG_SUBST', 13);
27 define('MAG_MSGNW', 14);
28 define('MAG_NOEDITSECTION', 15);
29 define('MAG_END', 16);
30 define('MAG_IMG_THUMBNAIL', 17);
31 define('MAG_IMG_RIGHT', 18);
32 define('MAG_IMG_LEFT', 19);
33 define('MAG_IMG_NONE', 20);
34 define('MAG_IMG_WIDTH', 21);
35 define('MAG_IMG_CENTER', 22);
36 define('MAG_INT', 23);
37 define('MAG_FORCETOC', 24);
38 define('MAG_SITENAME', 25);
40 define('MAG_LOCALURL', 27);
41 define('MAG_LOCALURLE', 28);
42 define('MAG_SERVER', 29);
43 define('MAG_IMG_FRAMED', 30);
44 define('MAG_PAGENAME', 31);
45 define('MAG_PAGENAMEE', 32);
46 define('MAG_NAMESPACE', 33);
47 define('MAG_TOC', 34);
48 define('MAG_GRAMMAR', 35);
49 define('MAG_NOTITLECONVERT', 36);
50 define('MAG_NOCONTENTCONVERT', 37);
51 define('MAG_CURRENTWEEK', 38);
52 define('MAG_CURRENTDOW', 39);
54 $wgVariableIDs = array(
57 MAG_CURRENTMONTHNAMEGEN
,
58 MAG_CURRENTMONTHABBREV
,
74 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
76 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
78 * Possible future improvements:
79 * * Simultaneous searching for a number of magic words
80 * * $wgMagicWords in shared memory
82 * Please avoid reading the data out of one of these objects and then writing
83 * special case code. If possible, add another match()-like function here.
91 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
92 var $mRegexStart, $mBaseRegex, $mVariableRegex;
96 function MagicWord($id = 0, $syn = '', $cs = false) {
98 $this->mSynonyms
= (array)$syn;
99 $this->mCaseSensitive
= $cs;
101 $this->mRegexStart
= '';
102 $this->mVariableRegex
= '';
103 $this->mVariableStartToEndRegex
= '';
104 $this->mModified
= false;
108 * Factory: creates an object representing an ID
111 function &get( $id ) {
112 global $wgMagicWords;
114 if ( !is_array( $wgMagicWords ) ) {
115 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
117 if (!array_key_exists( $id, $wgMagicWords ) ) {
118 $mw = new MagicWord();
120 $wgMagicWords[$id] = $mw;
122 return $wgMagicWords[$id];
125 # Initialises this object with an ID
126 function load( $id ) {
129 $wgContLang->getMagic( $this );
133 * Preliminary initialisation
136 function initRegex() {
137 $variableClass = Title
::legalChars();
138 $escSyn = array_map( 'preg_quote', $this->mSynonyms
);
139 $this->mBaseRegex
= implode( '|', $escSyn );
140 $case = $this->mCaseSensitive ?
'' : 'i';
141 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
142 $this->mRegexStart
= "/^({$this->mBaseRegex})/{$case}";
143 $this->mVariableRegex
= str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex
);
144 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "([$variableClass]*?)",
145 "/^({$this->mBaseRegex})$/{$case}" );
149 * Gets a regex representing matching the word
151 function getRegex() {
152 if ($this->mRegex
== '' ) {
155 return $this->mRegex
;
159 * Gets a regex matching the word, if it is at the string start
161 function getRegexStart() {
162 if ($this->mRegex
== '' ) {
165 return $this->mRegexStart
;
169 * regex without the slashes and what not
171 function getBaseRegex() {
172 if ($this->mRegex
== '') {
175 return $this->mBaseRegex
;
179 * Returns true if the text contains the word
182 function match( $text ) {
183 return preg_match( $this->getRegex(), $text );
187 * Returns true if the text starts with the word
190 function matchStart( $text ) {
191 return preg_match( $this->getRegexStart(), $text );
195 * Returns NULL if there's no match, the value of $1 otherwise
196 * The return code is the matched string, if there's no variable
197 * part in the regex and the matched variable part ($1) if there
200 function matchVariableStartToEnd( $text ) {
201 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
202 if ( $matchcount == 0 ) {
204 } elseif ( count($matches) == 2 ) {
213 * Returns true if the text matches the word, and alters the
214 * input string, removing all instances of the word
216 function matchAndRemove( &$text ) {
217 global $wgMagicFound;
218 $wgMagicFound = false;
219 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
220 return $wgMagicFound;
223 function matchStartAndRemove( &$text ) {
224 global $wgMagicFound;
225 $wgMagicFound = false;
226 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
227 return $wgMagicFound;
232 * Replaces the word with something else
234 function replace( $replacement, $subject ) {
235 $res = preg_replace( $this->getRegex(), $replacement, $subject );
236 $this->mModified
= !($res === $subject);
241 * Variable handling: {{SUBST:xxx}} style words
242 * Calls back a function to determine what to replace xxx with
243 * Input word must contain $1
245 function substituteCallback( $text, $callback ) {
246 $regex = $this->getVariableRegex();
247 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
248 $this->mModified
= !($res === $text);
253 * Matches the word, where $1 is a wildcard
255 function getVariableRegex() {
256 if ( $this->mVariableRegex
== '' ) {
259 return $this->mVariableRegex
;
263 * Matches the entire string, where $1 is a wildcard
265 function getVariableStartToEndRegex() {
266 if ( $this->mVariableStartToEndRegex
== '' ) {
269 return $this->mVariableStartToEndRegex
;
273 * Accesses the synonym list directly
275 function getSynonym( $i ) {
276 return $this->mSynonyms
[$i];
280 * Returns true if the last call to replace() or substituteCallback()
281 * returned a modified text, otherwise false.
283 function getWasModified(){
284 return $this->mModified
;
288 * $magicarr is an associative array of (magic word ID => replacement)
289 * This method uses the php feature to do several replacements at the same time,
290 * thereby gaining some efficiency. The result is placed in the out variable
291 * $result. The return value is true if something was replaced.
294 function replaceMultiple( $magicarr, $subject, &$result ){
297 foreach( $magicarr as $id => $replacement ){
298 $mw = MagicWord
::get( $id );
299 $search[] = $mw->getRegex();
300 $replace[] = $replacement;
303 $result = preg_replace( $search, $replace, $subject );
304 return !($result === $subject);
308 * Adds all the synonyms of this MagicWord to an array, to allow quick
309 * lookup in a list of magic words
311 function addToArray( &$array, $value ) {
312 foreach ( $this->mSynonyms
as $syn ) {
313 $array[$syn] = $value;
319 * Used in matchAndRemove()
322 function pregRemoveAndRecord( $match ) {
323 global $wgMagicFound;
324 $wgMagicFound = true;