template arguments, various improvements to handling of recursive inclusion
[lhc/web/wiklou.git] / includes / MagicWord.php
1 <?php
2
3 # This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
4 # Usage:
5 # if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
6 #
7 # Possible future improvements:
8 # * Simultaneous searching for a number of magic words
9 # * $wgMagicWords in shared memory
10 #
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.
13
14 /*private*/ $wgMagicFound = false;
15
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);
41 define("MAG_NS", 25);
42 define("MAG_LOCALURL", 26);
43 define("MAG_LOCALURLE", 27);
44 define("MAG_SERVER", 28);
45
46 $wgVariableIDs = array(
47 MAG_CURRENTMONTH,
48 MAG_CURRENTMONTHNAME,
49 MAG_CURRENTDAY,
50 MAG_CURRENTDAYNAME,
51 MAG_CURRENTYEAR,
52 MAG_CURRENTTIME,
53 MAG_NUMBEROFARTICLES,
54 MAG_CURRENTMONTHNAMEGEN,
55 MAG_SITENAME,
56 MAG_SERVER
57 );
58
59 class MagicWord {
60 /*private*/ var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
61 /*private*/ var $mRegexStart, $mBaseRegex, $mVariableRegex;
62 /*private*/ var $mModified;
63
64 function MagicWord($id = 0, $syn = "", $cs = false)
65 {
66 $this->mId = $id;
67 $this->mSynonyms = (array)$syn;
68 $this->mCaseSensitive = $cs;
69 $this->mRegex = "";
70 $this->mRegexStart = "";
71 $this->mVariableRegex = "";
72 $this->mVariableStartToEndRegex = "";
73 $this->mModified = false;
74 }
75
76 # Factory: creates an object representing an ID
77 /*static*/ function &get( $id )
78 {
79 global $wgMagicWords;
80
81 if (!array_key_exists( $id, $wgMagicWords ) ) {
82 $mw = new MagicWord();
83 $mw->load( $id );
84 $wgMagicWords[$id] = $mw;
85 }
86 return $wgMagicWords[$id];
87 }
88
89 # Initialises this object with an ID
90 function load( $id )
91 {
92 global $wgLang;
93 $this->mId = $id;
94 $wgLang->getMagic( $this );
95 }
96
97 # Preliminary initialisation
98 /* private */ function initRegex()
99 {
100 $variableClass = Title::legalChars();
101 $escSyn = array_map( "preg_quote", $this->mSynonyms );
102 $this->mBaseRegex = implode( "|", $escSyn );
103 $case = $this->mCaseSensitive ? "" : "i";
104 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
105 $this->mRegexStart = "/^{$this->mBaseRegex}/{$case}";
106 $this->mVariableRegex = str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex );
107 $this->mVariableStartToEndRegex = str_replace( "\\$1", "([$variableClass]*?)",
108 "/^{$this->mBaseRegex}$/{$case}" );
109 }
110
111 # Gets a regex representing matching the word
112 function getRegex()
113 {
114 if ($this->mRegex == "" ) {
115 $this->initRegex();
116 }
117 return $this->mRegex;
118 }
119
120 # Gets a regex matching the word, if it is at the
121 # string start
122 function getRegexStart()
123 {
124 if ($this->mRegex == "" ) {
125 $this->initRegex();
126 }
127 return $this->mRegexStart;
128 }
129
130 # regex without the slashes and what not
131 function getBaseRegex()
132 {
133 if ($this->mRegex == "") {
134 $this->initRegex();
135 }
136 return $this->mBaseRegex;
137 }
138
139 # Returns true if the text contains the word
140 function match( $text ) {
141 return preg_match( $this->getRegex(), $text );
142 }
143
144 # Returns true if the text starts with the word
145 function matchStart( $text )
146 {
147 return preg_match( $this->getRegexStart(), $text );
148 }
149
150 # Returns NULL if there's no match, the value of $1 otherwise
151 # The return code is the matched string, if there's no variable
152 # part in the regex and the matched variable part ($1) if there
153 # is one.
154 function matchVariableStartToEnd( $text ) {
155 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
156 if ( $matchcount == 0 ) {
157 return NULL;
158 } elseif ( count($matches) == 1 ) {
159 return $matches[0];
160 } else {
161 return $matches[1];
162 }
163 }
164
165
166 # Returns true if the text matches the word, and alters the
167 # input string, removing all instances of the word
168 function matchAndRemove( &$text )
169 {
170 global $wgMagicFound;
171 $wgMagicFound = false;
172 $text = preg_replace_callback( $this->getRegex(), "pregRemoveAndRecord", $text );
173 return $wgMagicFound;
174 }
175
176 function matchStartAndRemove( &$text ) {
177 global $wgMagicFound;
178 $wgMagicFound = false;
179 $text = preg_replace_callback( $this->getRegexStart(), "pregRemoveAndRecord", $text );
180 return $wgMagicFound;
181 }
182
183
184 # Replaces the word with something else
185 function replace( $replacement, $subject )
186 {
187 $res = preg_replace( $this->getRegex(), $replacement, $subject );
188 $this->mModified = !($res === $subject);
189 return $res;
190 }
191
192 # Variable handling: {{SUBST:xxx}} style words
193 # Calls back a function to determine what to replace xxx with
194 # Input word must contain $1
195 function substituteCallback( $text, $callback ) {
196 $regex = $this->getVariableRegex();
197 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
198 $this->mModified = !($res === $text);
199 return $res;
200 }
201
202 # Matches the word, where $1 is a wildcard
203 function getVariableRegex()
204 {
205 if ( $this->mVariableRegex == "" ) {
206 $this->initRegex();
207 }
208 return $this->mVariableRegex;
209 }
210
211 # Matches the entire string, where $1 is a wildcard
212 function getVariableStartToEndRegex()
213 {
214 if ( $this->mVariableStartToEndRegex == "" ) {
215 $this->initRegex();
216 }
217 return $this->mVariableStartToEndRegex;
218 }
219
220 # Accesses the synonym list directly
221 function getSynonym( $i ) {
222 return $this->mSynonyms[$i];
223 }
224
225 # Returns true if the last call to replace() or substituteCallback()
226 # returned a modified text, otherwise false.
227 function getWasModified(){
228 return $this->mModified;
229 }
230
231 # $magicarr is an associative array of (magic word ID => replacement)
232 # This method uses the php feature to do several replacements at the same time,
233 # thereby gaining some efficiency. The result is placed in the out variable
234 # $result. The return value is true if something was replaced.
235
236 /* static */ function replaceMultiple( $magicarr, $subject, &$result ){
237 $search = array();
238 $replace = array();
239 foreach( $magicarr as $id => $replacement ){
240 $mw = MagicWord::get( $id );
241 $search[] = $mw->getRegex();
242 $replace[] = $replacement;
243 }
244
245 $result = preg_replace( $search, $replace, $subject );
246 return !($result === $subject);
247 }
248
249 # Adds all the synonyms of this MagicWord to an array, to allow quick lookup in a list of magic words
250 function addToArray( &$array, $value )
251 {
252 foreach ( $this->mSynonyms as $syn ) {
253 $array[$syn] = $value;
254 }
255 }
256 }
257
258 # Used in matchAndRemove()
259 /*private*/ function pregRemoveAndRecord( $match )
260 {
261 global $wgMagicFound;
262 $wgMagicFound = true;
263 return "";
264 }
265
266 ?>