3 # This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
5 # if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
7 # Possible future improvements:
8 # * Simultaneous searching for a number of magic words
9 # * $wgMagicWords in shared memory
11 # Please avoid reading the data out of one of these objects and then writing
12 # special case code. If possible, add another match()-like function here.
14 /*private*/ $wgMagicFound = false;
16 define("MAG_REDIRECT", 0);
17 define("MAG_NOTOC", 1);
18 define("MAG_START", 2);
19 define("MAG_CURRENTMONTH", 3);
20 define("MAG_CURRENTMONTHNAME", 4);
21 define("MAG_CURRENTDAY", 5);
22 define("MAG_CURRENTDAYNAME", 6);
23 define("MAG_CURRENTYEAR", 7);
24 define("MAG_CURRENTTIME", 8);
25 define("MAG_NUMBEROFARTICLES", 9);
26 define("MAG_CURRENTMONTHNAMEGEN", 10);
27 define("MAG_MSG", 11);
28 define("MAG_SUBST", 12);
29 define("MAG_MSGNW", 13);
30 define("MAG_NOEDITSECTION", 14);
31 define("MAG_END", 15);
32 define("MAG_IMG_THUMBNAIL", 16);
33 define("MAG_IMG_RIGHT", 17);
34 define("MAG_IMG_LEFT", 18);
35 define("MAG_IMG_NONE", 19);
36 define("MAG_IMG_WIDTH", 20);
37 define("MAG_IMG_CENTER", 21);
38 define("MAG_INT", 22);
39 define("MAG_FORCETOC", 23);
40 define("MAG_SITENAME", 24);
42 define("MAG_LOCALURL", 26);
43 define("MAG_LOCALURLE", 27);
44 define("MAG_SERVER", 28);
45 define("MAG_IMG_FRAMED", 29);
46 define("MAG_PAGENAME", 30);
47 define("MAG_NAMESPACE", 31);
49 $wgVariableIDs = array(
57 MAG_CURRENTMONTHNAMEGEN
,
65 /*private*/ var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
66 /*private*/ var $mRegexStart, $mBaseRegex, $mVariableRegex;
67 /*private*/ var $mModified;
69 function MagicWord($id = 0, $syn = "", $cs = false)
72 $this->mSynonyms
= (array)$syn;
73 $this->mCaseSensitive
= $cs;
75 $this->mRegexStart
= "";
76 $this->mVariableRegex
= "";
77 $this->mVariableStartToEndRegex
= "";
78 $this->mModified
= false;
81 # Factory: creates an object representing an ID
82 /*static*/ function &get( $id )
86 if (!array_key_exists( $id, $wgMagicWords ) ) {
87 $mw = new MagicWord();
89 $wgMagicWords[$id] = $mw;
91 return $wgMagicWords[$id];
94 # Initialises this object with an ID
99 $wgLang->getMagic( $this );
102 # Preliminary initialisation
103 /* private */ function initRegex()
105 $variableClass = Title
::legalChars();
106 $escSyn = array_map( "preg_quote", $this->mSynonyms
);
107 $this->mBaseRegex
= implode( "|", $escSyn );
108 $case = $this->mCaseSensitive ?
"" : "i";
109 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
110 $this->mRegexStart
= "/^{$this->mBaseRegex}/{$case}";
111 $this->mVariableRegex
= str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex
);
112 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "([$variableClass]*?)",
113 "/^{$this->mBaseRegex}$/{$case}" );
116 # Gets a regex representing matching the word
119 if ($this->mRegex
== "" ) {
122 return $this->mRegex
;
125 # Gets a regex matching the word, if it is at the
127 function getRegexStart()
129 if ($this->mRegex
== "" ) {
132 return $this->mRegexStart
;
135 # regex without the slashes and what not
136 function getBaseRegex()
138 if ($this->mRegex
== "") {
141 return $this->mBaseRegex
;
144 # Returns true if the text contains the word
145 function match( $text ) {
146 return preg_match( $this->getRegex(), $text );
149 # Returns true if the text starts with the word
150 function matchStart( $text )
152 return preg_match( $this->getRegexStart(), $text );
155 # Returns NULL if there's no match, the value of $1 otherwise
156 # The return code is the matched string, if there's no variable
157 # part in the regex and the matched variable part ($1) if there
159 function matchVariableStartToEnd( $text ) {
160 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
161 if ( $matchcount == 0 ) {
163 } elseif ( count($matches) == 1 ) {
171 # Returns true if the text matches the word, and alters the
172 # input string, removing all instances of the word
173 function matchAndRemove( &$text )
175 global $wgMagicFound;
176 $wgMagicFound = false;
177 $text = preg_replace_callback( $this->getRegex(), "pregRemoveAndRecord", $text );
178 return $wgMagicFound;
181 function matchStartAndRemove( &$text ) {
182 global $wgMagicFound;
183 $wgMagicFound = false;
184 $text = preg_replace_callback( $this->getRegexStart(), "pregRemoveAndRecord", $text );
185 return $wgMagicFound;
189 # Replaces the word with something else
190 function replace( $replacement, $subject )
192 $res = preg_replace( $this->getRegex(), $replacement, $subject );
193 $this->mModified
= !($res === $subject);
197 # Variable handling: {{SUBST:xxx}} style words
198 # Calls back a function to determine what to replace xxx with
199 # Input word must contain $1
200 function substituteCallback( $text, $callback ) {
201 $regex = $this->getVariableRegex();
202 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
203 $this->mModified
= !($res === $text);
207 # Matches the word, where $1 is a wildcard
208 function getVariableRegex()
210 if ( $this->mVariableRegex
== "" ) {
213 return $this->mVariableRegex
;
216 # Matches the entire string, where $1 is a wildcard
217 function getVariableStartToEndRegex()
219 if ( $this->mVariableStartToEndRegex
== "" ) {
222 return $this->mVariableStartToEndRegex
;
225 # Accesses the synonym list directly
226 function getSynonym( $i ) {
227 return $this->mSynonyms
[$i];
230 # Returns true if the last call to replace() or substituteCallback()
231 # returned a modified text, otherwise false.
232 function getWasModified(){
233 return $this->mModified
;
236 # $magicarr is an associative array of (magic word ID => replacement)
237 # This method uses the php feature to do several replacements at the same time,
238 # thereby gaining some efficiency. The result is placed in the out variable
239 # $result. The return value is true if something was replaced.
241 /* static */ function replaceMultiple( $magicarr, $subject, &$result ){
244 foreach( $magicarr as $id => $replacement ){
245 $mw = MagicWord
::get( $id );
246 $search[] = $mw->getRegex();
247 $replace[] = $replacement;
250 $result = preg_replace( $search, $replace, $subject );
251 return !($result === $subject);
254 # Adds all the synonyms of this MagicWord to an array, to allow quick lookup in a list of magic words
255 function addToArray( &$array, $value )
257 foreach ( $this->mSynonyms
as $syn ) {
258 $array[$syn] = $value;
263 # Used in matchAndRemove()
264 /*private*/ function pregRemoveAndRecord( $match )
266 global $wgMagicFound;
267 $wgMagicFound = true;