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);
48 define('MAG_NOTITLECONVERT', 35);
50 define('MAG_CURRENTWEEK', 37);
51 define('MAG_CURRENTDOW', 38);
53 $wgVariableIDs = array(
61 MAG_CURRENTMONTHNAMEGEN
,
72 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
74 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
76 * Possible future improvements:
77 * * Simultaneous searching for a number of magic words
78 * * $wgMagicWords in shared memory
80 * Please avoid reading the data out of one of these objects and then writing
81 * special case code. If possible, add another match()-like function here.
89 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
90 var $mRegexStart, $mBaseRegex, $mVariableRegex;
94 function MagicWord($id = 0, $syn = '', $cs = false) {
96 $this->mSynonyms
= (array)$syn;
97 $this->mCaseSensitive
= $cs;
99 $this->mRegexStart
= '';
100 $this->mVariableRegex
= '';
101 $this->mVariableStartToEndRegex
= '';
102 $this->mModified
= false;
106 * Factory: creates an object representing an ID
109 function &get( $id ) {
110 global $wgMagicWords;
112 if ( !is_array( $wgMagicWords ) ) {
113 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
115 if (!array_key_exists( $id, $wgMagicWords ) ) {
116 $mw = new MagicWord();
118 $wgMagicWords[$id] = $mw;
120 return $wgMagicWords[$id];
123 # Initialises this object with an ID
124 function load( $id ) {
127 $wgContLang->getMagic( $this );
131 * Preliminary initialisation
134 function initRegex() {
135 $variableClass = Title
::legalChars();
136 $escSyn = array_map( 'preg_quote', $this->mSynonyms
);
137 $this->mBaseRegex
= implode( '|', $escSyn );
138 $case = $this->mCaseSensitive ?
'' : 'i';
139 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
140 $this->mRegexStart
= "/^({$this->mBaseRegex})/{$case}";
141 $this->mVariableRegex
= str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex
);
142 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "([$variableClass]*?)",
143 "/^({$this->mBaseRegex})$/{$case}" );
147 * Gets a regex representing matching the word
149 function getRegex() {
150 if ($this->mRegex
== '' ) {
153 return $this->mRegex
;
157 * Gets a regex matching the word, if it is at the string start
159 function getRegexStart() {
160 if ($this->mRegex
== '' ) {
163 return $this->mRegexStart
;
167 * regex without the slashes and what not
169 function getBaseRegex() {
170 if ($this->mRegex
== '') {
173 return $this->mBaseRegex
;
177 * Returns true if the text contains the word
180 function match( $text ) {
181 return preg_match( $this->getRegex(), $text );
185 * Returns true if the text starts with the word
188 function matchStart( $text ) {
189 return preg_match( $this->getRegexStart(), $text );
193 * Returns NULL if there's no match, the value of $1 otherwise
194 * The return code is the matched string, if there's no variable
195 * part in the regex and the matched variable part ($1) if there
198 function matchVariableStartToEnd( $text ) {
199 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
200 if ( $matchcount == 0 ) {
202 } elseif ( count($matches) == 2 ) {
211 * Returns true if the text matches the word, and alters the
212 * input string, removing all instances of the word
214 function matchAndRemove( &$text ) {
215 global $wgMagicFound;
216 $wgMagicFound = false;
217 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
218 return $wgMagicFound;
221 function matchStartAndRemove( &$text ) {
222 global $wgMagicFound;
223 $wgMagicFound = false;
224 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
225 return $wgMagicFound;
230 * Replaces the word with something else
232 function replace( $replacement, $subject ) {
233 $res = preg_replace( $this->getRegex(), $replacement, $subject );
234 $this->mModified
= !($res === $subject);
239 * Variable handling: {{SUBST:xxx}} style words
240 * Calls back a function to determine what to replace xxx with
241 * Input word must contain $1
243 function substituteCallback( $text, $callback ) {
244 $regex = $this->getVariableRegex();
245 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
246 $this->mModified
= !($res === $text);
251 * Matches the word, where $1 is a wildcard
253 function getVariableRegex() {
254 if ( $this->mVariableRegex
== '' ) {
257 return $this->mVariableRegex
;
261 * Matches the entire string, where $1 is a wildcard
263 function getVariableStartToEndRegex() {
264 if ( $this->mVariableStartToEndRegex
== '' ) {
267 return $this->mVariableStartToEndRegex
;
271 * Accesses the synonym list directly
273 function getSynonym( $i ) {
274 return $this->mSynonyms
[$i];
278 * Returns true if the last call to replace() or substituteCallback()
279 * returned a modified text, otherwise false.
281 function getWasModified(){
282 return $this->mModified
;
286 * $magicarr is an associative array of (magic word ID => replacement)
287 * This method uses the php feature to do several replacements at the same time,
288 * thereby gaining some efficiency. The result is placed in the out variable
289 * $result. The return value is true if something was replaced.
292 function replaceMultiple( $magicarr, $subject, &$result ){
295 foreach( $magicarr as $id => $replacement ){
296 $mw = MagicWord
::get( $id );
297 $search[] = $mw->getRegex();
298 $replace[] = $replacement;
301 $result = preg_replace( $search, $replace, $subject );
302 return !($result === $subject);
306 * Adds all the synonyms of this MagicWord to an array, to allow quick
307 * lookup in a list of magic words
309 function addToArray( &$array, $value ) {
310 foreach ( $this->mSynonyms
as $syn ) {
311 $array[$syn] = $value;
317 * Used in matchAndRemove()
320 function pregRemoveAndRecord( $match ) {
321 global $wgMagicFound;
322 $wgMagicFound = true;