don't just assume we get a valid title object
[lhc/web/wiklou.git] / includes / MagicWord.php
1 <?php
2 /**
3 * File for magic words
4 * @package MediaWiki
5 * @subpackage Parser
6 */
7
8 /**
9 * private
10 */
11 $wgMagicFound = false;
12
13 /** Actual keyword to be used is set in Language.php */
14 define('MAG_REDIRECT', 0);
15 define('MAG_NOTOC', 1);
16 define('MAG_START', 2);
17 define('MAG_CURRENTMONTH', 3);
18 define('MAG_CURRENTMONTHNAME', 4);
19 define('MAG_CURRENTMONTHNAMEGEN', 5);
20 define('MAG_CURRENTMONTHABBREV', 6);
21 define('MAG_CURRENTDAY', 7);
22 define('MAG_CURRENTDAYNAME', 8);
23 define('MAG_CURRENTYEAR', 9);
24 define('MAG_CURRENTTIME', 10);
25 define('MAG_NUMBEROFARTICLES', 11);
26 define('MAG_SUBST', 12);
27 define('MAG_MSG', 13);
28 define('MAG_MSGNW', 14);
29 define('MAG_NOEDITSECTION', 15);
30 define('MAG_END', 16);
31 define('MAG_IMG_THUMBNAIL', 17);
32 define('MAG_IMG_RIGHT', 18);
33 define('MAG_IMG_LEFT', 19);
34 define('MAG_IMG_NONE', 20);
35 define('MAG_IMG_WIDTH', 21);
36 define('MAG_IMG_CENTER', 22);
37 define('MAG_INT', 23);
38 define('MAG_FORCETOC', 24);
39 define('MAG_SITENAME', 25);
40 define('MAG_NS', 26);
41 define('MAG_LOCALURL', 27);
42 define('MAG_LOCALURLE', 28);
43 define('MAG_SERVER', 29);
44 define('MAG_IMG_FRAMED', 30);
45 define('MAG_PAGENAME', 31);
46 define('MAG_PAGENAMEE', 32);
47 define('MAG_NAMESPACE', 33);
48 define('MAG_TOC', 34);
49 define('MAG_GRAMMAR', 35);
50 define('MAG_NOTITLECONVERT', 36);
51 define('MAG_NOCONTENTCONVERT', 37);
52 define('MAG_CURRENTWEEK', 38);
53 define('MAG_CURRENTDOW', 39);
54 define('MAG_REVISIONID', 40);
55 define('MAG_SCRIPTPATH', 41);
56 define('MAG_SERVERNAME', 42);
57 define('MAG_NUMBEROFFILES', 43);
58
59 $wgVariableIDs = array(
60 MAG_CURRENTMONTH,
61 MAG_CURRENTMONTHNAME,
62 MAG_CURRENTMONTHNAMEGEN,
63 MAG_CURRENTMONTHABBREV,
64 MAG_CURRENTDAY,
65 MAG_CURRENTDAYNAME,
66 MAG_CURRENTYEAR,
67 MAG_CURRENTTIME,
68 MAG_NUMBEROFARTICLES,
69 MAG_NUMBEROFFILES,
70 MAG_SITENAME,
71 MAG_SERVER,
72 MAG_SERVERNAME,
73 MAG_SCRIPTPATH,
74 MAG_PAGENAME,
75 MAG_PAGENAMEE,
76 MAG_NAMESPACE,
77 MAG_CURRENTWEEK,
78 MAG_CURRENTDOW,
79 MAG_REVISIONID,
80 );
81
82 /**
83 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
84 * Usage:
85 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
86 *
87 * Possible future improvements:
88 * * Simultaneous searching for a number of magic words
89 * * $wgMagicWords in shared memory
90 *
91 * Please avoid reading the data out of one of these objects and then writing
92 * special case code. If possible, add another match()-like function here.
93 *
94 * @package MediaWiki
95 */
96 class MagicWord {
97 /**#@+
98 * @access private
99 */
100 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
101 var $mRegexStart, $mBaseRegex, $mVariableRegex;
102 var $mModified;
103 /**#@-*/
104
105 function MagicWord($id = 0, $syn = '', $cs = false) {
106 $this->mId = $id;
107 $this->mSynonyms = (array)$syn;
108 $this->mCaseSensitive = $cs;
109 $this->mRegex = '';
110 $this->mRegexStart = '';
111 $this->mVariableRegex = '';
112 $this->mVariableStartToEndRegex = '';
113 $this->mModified = false;
114 }
115
116 /**
117 * Factory: creates an object representing an ID
118 * @static
119 */
120 function &get( $id ) {
121 global $wgMagicWords;
122
123 if ( !is_array( $wgMagicWords ) ) {
124 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
125 }
126 if (!array_key_exists( $id, $wgMagicWords ) ) {
127 $mw = new MagicWord();
128 $mw->load( $id );
129 $wgMagicWords[$id] = $mw;
130 }
131 return $wgMagicWords[$id];
132 }
133
134 # Initialises this object with an ID
135 function load( $id ) {
136 global $wgContLang;
137 $this->mId = $id;
138 $wgContLang->getMagic( $this );
139 }
140
141 /**
142 * Preliminary initialisation
143 * @private
144 */
145 function initRegex() {
146 #$variableClass = Title::legalChars();
147 # This was used for matching "$1" variables, but different uses of the feature will have
148 # different restrictions, which should be checked *after* the MagicWord has been matched,
149 # not here. - IMSoP
150 $escSyn = array_map( 'preg_quote', $this->mSynonyms );
151 $this->mBaseRegex = implode( '|', $escSyn );
152 $case = $this->mCaseSensitive ? '' : 'i';
153 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
154 $this->mRegexStart = "/^(?:{$this->mBaseRegex})/{$case}";
155 $this->mVariableRegex = str_replace( "\\$1", "(.*?)", $this->mRegex );
156 $this->mVariableStartToEndRegex = str_replace( "\\$1", "(.*?)",
157 "/^(?:{$this->mBaseRegex})$/{$case}" );
158 }
159
160 /**
161 * Gets a regex representing matching the word
162 */
163 function getRegex() {
164 if ($this->mRegex == '' ) {
165 $this->initRegex();
166 }
167 return $this->mRegex;
168 }
169
170 /**
171 * Gets a regex matching the word, if it is at the string start
172 */
173 function getRegexStart() {
174 if ($this->mRegex == '' ) {
175 $this->initRegex();
176 }
177 return $this->mRegexStart;
178 }
179
180 /**
181 * regex without the slashes and what not
182 */
183 function getBaseRegex() {
184 if ($this->mRegex == '') {
185 $this->initRegex();
186 }
187 return $this->mBaseRegex;
188 }
189
190 /**
191 * Returns true if the text contains the word
192 * @return bool
193 */
194 function match( $text ) {
195 return preg_match( $this->getRegex(), $text );
196 }
197
198 /**
199 * Returns true if the text starts with the word
200 * @return bool
201 */
202 function matchStart( $text ) {
203 return preg_match( $this->getRegexStart(), $text );
204 }
205
206 /**
207 * Returns NULL if there's no match, the value of $1 otherwise
208 * The return code is the matched string, if there's no variable
209 * part in the regex and the matched variable part ($1) if there
210 * is one.
211 */
212 function matchVariableStartToEnd( $text ) {
213 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
214 if ( $matchcount == 0 ) {
215 return NULL;
216 } elseif ( count($matches) == 1 ) {
217 return $matches[0];
218 } else {
219 # multiple matched parts (variable match); some will be empty because of synonyms
220 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
221 $matches = array_values(array_filter($matches));
222 return $matches[1];
223 }
224 }
225
226
227 /**
228 * Returns true if the text matches the word, and alters the
229 * input string, removing all instances of the word
230 */
231 function matchAndRemove( &$text ) {
232 global $wgMagicFound;
233 $wgMagicFound = false;
234 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
235 return $wgMagicFound;
236 }
237
238 function matchStartAndRemove( &$text ) {
239 global $wgMagicFound;
240 $wgMagicFound = false;
241 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
242 return $wgMagicFound;
243 }
244
245
246 /**
247 * Replaces the word with something else
248 */
249 function replace( $replacement, $subject ) {
250 $res = preg_replace( $this->getRegex(), $replacement, $subject );
251 $this->mModified = !($res === $subject);
252 return $res;
253 }
254
255 /**
256 * Variable handling: {{SUBST:xxx}} style words
257 * Calls back a function to determine what to replace xxx with
258 * Input word must contain $1
259 */
260 function substituteCallback( $text, $callback ) {
261 $regex = $this->getVariableRegex();
262 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
263 $this->mModified = !($res === $text);
264 return $res;
265 }
266
267 /**
268 * Matches the word, where $1 is a wildcard
269 */
270 function getVariableRegex() {
271 if ( $this->mVariableRegex == '' ) {
272 $this->initRegex();
273 }
274 return $this->mVariableRegex;
275 }
276
277 /**
278 * Matches the entire string, where $1 is a wildcard
279 */
280 function getVariableStartToEndRegex() {
281 if ( $this->mVariableStartToEndRegex == '' ) {
282 $this->initRegex();
283 }
284 return $this->mVariableStartToEndRegex;
285 }
286
287 /**
288 * Accesses the synonym list directly
289 */
290 function getSynonym( $i ) {
291 return $this->mSynonyms[$i];
292 }
293
294 /**
295 * Returns true if the last call to replace() or substituteCallback()
296 * returned a modified text, otherwise false.
297 */
298 function getWasModified(){
299 return $this->mModified;
300 }
301
302 /**
303 * $magicarr is an associative array of (magic word ID => replacement)
304 * This method uses the php feature to do several replacements at the same time,
305 * thereby gaining some efficiency. The result is placed in the out variable
306 * $result. The return value is true if something was replaced.
307 * @static
308 **/
309 function replaceMultiple( $magicarr, $subject, &$result ){
310 $search = array();
311 $replace = array();
312 foreach( $magicarr as $id => $replacement ){
313 $mw = MagicWord::get( $id );
314 $search[] = $mw->getRegex();
315 $replace[] = $replacement;
316 }
317
318 $result = preg_replace( $search, $replace, $subject );
319 return !($result === $subject);
320 }
321
322 /**
323 * Adds all the synonyms of this MagicWord to an array, to allow quick
324 * lookup in a list of magic words
325 */
326 function addToArray( &$array, $value ) {
327 foreach ( $this->mSynonyms as $syn ) {
328 $array[$syn] = $value;
329 }
330 }
331 }
332
333 /**
334 * Used in matchAndRemove()
335 * @private
336 **/
337 function pregRemoveAndRecord( $match ) {
338 global $wgMagicFound;
339 $wgMagicFound = true;
340 return '';
341 }
342
343 ?>