10 $wgMagicFound = false;
12 /** Actual keyword to be used is set in Language.php */
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);
53 define('MAG_REVISIONID', 40);
55 $wgVariableIDs = array(
58 MAG_CURRENTMONTHNAMEGEN
,
59 MAG_CURRENTMONTHABBREV
,
76 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
78 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
80 * Possible future improvements:
81 * * Simultaneous searching for a number of magic words
82 * * $wgMagicWords in shared memory
84 * Please avoid reading the data out of one of these objects and then writing
85 * special case code. If possible, add another match()-like function here.
93 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
94 var $mRegexStart, $mBaseRegex, $mVariableRegex;
98 function MagicWord($id = 0, $syn = '', $cs = false) {
100 $this->mSynonyms
= (array)$syn;
101 $this->mCaseSensitive
= $cs;
103 $this->mRegexStart
= '';
104 $this->mVariableRegex
= '';
105 $this->mVariableStartToEndRegex
= '';
106 $this->mModified
= false;
110 * Factory: creates an object representing an ID
113 function &get( $id ) {
114 global $wgMagicWords;
116 if ( !is_array( $wgMagicWords ) ) {
117 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
119 if (!array_key_exists( $id, $wgMagicWords ) ) {
120 $mw = new MagicWord();
122 $wgMagicWords[$id] = $mw;
124 return $wgMagicWords[$id];
127 # Initialises this object with an ID
128 function load( $id ) {
131 $wgContLang->getMagic( $this );
135 * Preliminary initialisation
138 function initRegex() {
139 $variableClass = Title
::legalChars();
140 $escSyn = array_map( 'preg_quote', $this->mSynonyms
);
141 $this->mBaseRegex
= implode( '|', $escSyn );
142 $case = $this->mCaseSensitive ?
'' : 'i';
143 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
144 $this->mRegexStart
= "/^({$this->mBaseRegex})/{$case}";
145 $this->mVariableRegex
= str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex
);
146 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "([$variableClass]*?)",
147 "/^({$this->mBaseRegex})$/{$case}" );
151 * Gets a regex representing matching the word
153 function getRegex() {
154 if ($this->mRegex
== '' ) {
157 return $this->mRegex
;
161 * Gets a regex matching the word, if it is at the string start
163 function getRegexStart() {
164 if ($this->mRegex
== '' ) {
167 return $this->mRegexStart
;
171 * regex without the slashes and what not
173 function getBaseRegex() {
174 if ($this->mRegex
== '') {
177 return $this->mBaseRegex
;
181 * Returns true if the text contains the word
184 function match( $text ) {
185 return preg_match( $this->getRegex(), $text );
189 * Returns true if the text starts with the word
192 function matchStart( $text ) {
193 return preg_match( $this->getRegexStart(), $text );
197 * Returns NULL if there's no match, the value of $1 otherwise
198 * The return code is the matched string, if there's no variable
199 * part in the regex and the matched variable part ($1) if there
202 function matchVariableStartToEnd( $text ) {
203 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
204 if ( $matchcount == 0 ) {
206 } elseif ( count($matches) == 2 ) {
215 * Returns true if the text matches the word, and alters the
216 * input string, removing all instances of the word
218 function matchAndRemove( &$text ) {
219 global $wgMagicFound;
220 $wgMagicFound = false;
221 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
222 return $wgMagicFound;
225 function matchStartAndRemove( &$text ) {
226 global $wgMagicFound;
227 $wgMagicFound = false;
228 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
229 return $wgMagicFound;
234 * Replaces the word with something else
236 function replace( $replacement, $subject ) {
237 $res = preg_replace( $this->getRegex(), $replacement, $subject );
238 $this->mModified
= !($res === $subject);
243 * Variable handling: {{SUBST:xxx}} style words
244 * Calls back a function to determine what to replace xxx with
245 * Input word must contain $1
247 function substituteCallback( $text, $callback ) {
248 $regex = $this->getVariableRegex();
249 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
250 $this->mModified
= !($res === $text);
255 * Matches the word, where $1 is a wildcard
257 function getVariableRegex() {
258 if ( $this->mVariableRegex
== '' ) {
261 return $this->mVariableRegex
;
265 * Matches the entire string, where $1 is a wildcard
267 function getVariableStartToEndRegex() {
268 if ( $this->mVariableStartToEndRegex
== '' ) {
271 return $this->mVariableStartToEndRegex
;
275 * Accesses the synonym list directly
277 function getSynonym( $i ) {
278 return $this->mSynonyms
[$i];
282 * Returns true if the last call to replace() or substituteCallback()
283 * returned a modified text, otherwise false.
285 function getWasModified(){
286 return $this->mModified
;
290 * $magicarr is an associative array of (magic word ID => replacement)
291 * This method uses the php feature to do several replacements at the same time,
292 * thereby gaining some efficiency. The result is placed in the out variable
293 * $result. The return value is true if something was replaced.
296 function replaceMultiple( $magicarr, $subject, &$result ){
299 foreach( $magicarr as $id => $replacement ){
300 $mw = MagicWord
::get( $id );
301 $search[] = $mw->getRegex();
302 $replace[] = $replacement;
305 $result = preg_replace( $search, $replace, $subject );
306 return !($result === $subject);
310 * Adds all the synonyms of this MagicWord to an array, to allow quick
311 * lookup in a list of magic words
313 function addToArray( &$array, $value ) {
314 foreach ( $this->mSynonyms
as $syn ) {
315 $array[$syn] = $value;
321 * Used in matchAndRemove()
324 function pregRemoveAndRecord( $match ) {
325 global $wgMagicFound;
326 $wgMagicFound = true;