A lot more of static issues when using E_STRICT from the commandline.
[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
15 $magicWords = array(
16 'MAG_REDIRECT',
17 'MAG_NOTOC',
18 'MAG_START',
19 'MAG_CURRENTMONTH',
20 'MAG_CURRENTMONTHNAME',
21 'MAG_CURRENTMONTHNAMEGEN',
22 'MAG_CURRENTMONTHABBREV',
23 'MAG_CURRENTDAY',
24 'MAG_CURRENTDAY2',
25 'MAG_CURRENTDAYNAME',
26 'MAG_CURRENTYEAR',
27 'MAG_CURRENTTIME',
28 'MAG_NUMBEROFARTICLES',
29 'MAG_SUBST',
30 'MAG_MSG',
31 'MAG_MSGNW',
32 'MAG_NOEDITSECTION',
33 'MAG_END',
34 'MAG_IMG_THUMBNAIL',
35 'MAG_IMG_RIGHT',
36 'MAG_IMG_LEFT',
37 'MAG_IMG_NONE',
38 'MAG_IMG_WIDTH',
39 'MAG_IMG_CENTER',
40 'MAG_INT',
41 'MAG_FORCETOC',
42 'MAG_SITENAME',
43 'MAG_NS',
44 'MAG_LOCALURL',
45 'MAG_LOCALURLE',
46 'MAG_SERVER',
47 'MAG_IMG_FRAMED',
48 'MAG_PAGENAME',
49 'MAG_PAGENAMEE',
50 'MAG_NAMESPACE',
51 'MAG_NAMESPACEE',
52 'MAG_TOC',
53 'MAG_GRAMMAR',
54 'MAG_NOTITLECONVERT',
55 'MAG_NOCONTENTCONVERT',
56 'MAG_CURRENTWEEK',
57 'MAG_CURRENTDOW',
58 'MAG_REVISIONID',
59 'MAG_SCRIPTPATH',
60 'MAG_SERVERNAME',
61 'MAG_NUMBEROFFILES',
62 'MAG_IMG_MANUALTHUMB',
63 'MAG_PLURAL',
64 'MAG_FULLURL',
65 'MAG_FULLURLE',
66 'MAG_LCFIRST',
67 'MAG_UCFIRST',
68 'MAG_LC',
69 'MAG_UC',
70 'MAG_FULLPAGENAME',
71 'MAG_FULLPAGENAMEE',
72 'MAG_RAW',
73 'MAG_SUBPAGENAME',
74 'MAG_SUBPAGENAMEE',
75 'MAG_DISPLAYTITLE',
76 'MAG_TALKSPACE',
77 'MAG_TALKSPACEE',
78 'MAG_SUBJECTSPACE',
79 'MAG_SUBJECTSPACEE',
80 'MAG_TALKPAGENAME',
81 'MAG_TALKPAGENAMEE',
82 'MAG_SUBJECTPAGENAME',
83 'MAG_SUBJECTPAGENAMEE',
84 'MAG_NUMBEROFUSERS',
85 'MAG_RAWSUFFIX',
86 'MAG_NEWSECTIONLINK',
87 'MAG_NUMBEROFPAGES',
88 'MAG_CURRENTVERSION',
89 'MAG_BASEPAGENAME',
90 'MAG_BASEPAGENAMEE',
91 'MAG_URLENCODE',
92 'MAG_CURRENTTIMESTAMP',
93 'MAG_DIRECTIONMARK',
94 'MAG_LANGUAGE',
95 'MAG_CONTENTLANGUAGE',
96 'MAG_PAGESINNAMESPACE',
97 'MAG_NOGALLERY',
98 'MAG_NUMBEROFADMINS',
99 'MAG_FORMATNUM',
100 );
101 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
102 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
103
104 for ( $i = 0; $i < count( $magicWords ); ++$i )
105 define( $magicWords[$i], $i );
106
107 $wgVariableIDs = array(
108 MAG_CURRENTMONTH,
109 MAG_CURRENTMONTHNAME,
110 MAG_CURRENTMONTHNAMEGEN,
111 MAG_CURRENTMONTHABBREV,
112 MAG_CURRENTDAY,
113 MAG_CURRENTDAY2,
114 MAG_CURRENTDAYNAME,
115 MAG_CURRENTYEAR,
116 MAG_CURRENTTIME,
117 MAG_NUMBEROFARTICLES,
118 MAG_NUMBEROFFILES,
119 MAG_SITENAME,
120 MAG_SERVER,
121 MAG_SERVERNAME,
122 MAG_SCRIPTPATH,
123 MAG_PAGENAME,
124 MAG_PAGENAMEE,
125 MAG_FULLPAGENAME,
126 MAG_FULLPAGENAMEE,
127 MAG_NAMESPACE,
128 MAG_NAMESPACEE,
129 MAG_CURRENTWEEK,
130 MAG_CURRENTDOW,
131 MAG_REVISIONID,
132 MAG_SUBPAGENAME,
133 MAG_SUBPAGENAMEE,
134 MAG_DISPLAYTITLE,
135 MAG_TALKSPACE,
136 MAG_TALKSPACEE,
137 MAG_SUBJECTSPACE,
138 MAG_SUBJECTSPACEE,
139 MAG_TALKPAGENAME,
140 MAG_TALKPAGENAMEE,
141 MAG_SUBJECTPAGENAME,
142 MAG_SUBJECTPAGENAMEE,
143 MAG_NUMBEROFUSERS,
144 MAG_RAWSUFFIX,
145 MAG_NEWSECTIONLINK,
146 MAG_NUMBEROFPAGES,
147 MAG_CURRENTVERSION,
148 MAG_BASEPAGENAME,
149 MAG_BASEPAGENAMEE,
150 MAG_URLENCODE,
151 MAG_CURRENTTIMESTAMP,
152 MAG_DIRECTIONMARK,
153 MAG_LANGUAGE,
154 MAG_CONTENTLANGUAGE,
155 MAG_PAGESINNAMESPACE,
156 MAG_NUMBEROFADMINS,
157 );
158 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
159 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
160
161 /**
162 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
163 * Usage:
164 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
165 *
166 * Possible future improvements:
167 * * Simultaneous searching for a number of magic words
168 * * $wgMagicWords in shared memory
169 *
170 * Please avoid reading the data out of one of these objects and then writing
171 * special case code. If possible, add another match()-like function here.
172 *
173 * @package MediaWiki
174 */
175 class MagicWord {
176 /**#@+
177 * @private
178 */
179 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
180 var $mRegexStart, $mBaseRegex, $mVariableRegex;
181 var $mModified;
182 /**#@-*/
183
184 function MagicWord($id = 0, $syn = '', $cs = false) {
185 $this->mId = $id;
186 $this->mSynonyms = (array)$syn;
187 $this->mCaseSensitive = $cs;
188 $this->mRegex = '';
189 $this->mRegexStart = '';
190 $this->mVariableRegex = '';
191 $this->mVariableStartToEndRegex = '';
192 $this->mModified = false;
193 }
194
195 /**
196 * Factory: creates an object representing an ID
197 * @static
198 */
199 static function &get( $id ) {
200 global $wgMagicWords;
201
202 if ( !is_array( $wgMagicWords ) ) {
203 throw new MWException( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
204 }
205 if (!array_key_exists( $id, $wgMagicWords ) ) {
206 $mw = new MagicWord();
207 $mw->load( $id );
208 $wgMagicWords[$id] = $mw;
209 }
210 return $wgMagicWords[$id];
211 }
212
213 # Initialises this object with an ID
214 function load( $id ) {
215 global $wgContLang;
216 $this->mId = $id;
217 $wgContLang->getMagic( $this );
218 }
219
220 /**
221 * Preliminary initialisation
222 * @private
223 */
224 function initRegex() {
225 #$variableClass = Title::legalChars();
226 # This was used for matching "$1" variables, but different uses of the feature will have
227 # different restrictions, which should be checked *after* the MagicWord has been matched,
228 # not here. - IMSoP
229
230 $escSyn = array();
231 foreach ( $this->mSynonyms as $synonym )
232 // In case a magic word contains /, like that's going to happen;)
233 $escSyn[] = preg_quote( $synonym, '/' );
234 $this->mBaseRegex = implode( '|', $escSyn );
235
236 $case = $this->mCaseSensitive ? '' : 'i';
237 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
238 $this->mRegexStart = "/^(?:{$this->mBaseRegex})/{$case}";
239 $this->mVariableRegex = str_replace( "\\$1", "(.*?)", $this->mRegex );
240 $this->mVariableStartToEndRegex = str_replace( "\\$1", "(.*?)",
241 "/^(?:{$this->mBaseRegex})$/{$case}" );
242 }
243
244 /**
245 * Gets a regex representing matching the word
246 */
247 function getRegex() {
248 if ($this->mRegex == '' ) {
249 $this->initRegex();
250 }
251 return $this->mRegex;
252 }
253
254 /**
255 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
256 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
257 * the complete expression
258 */
259 function getRegexCase() {
260 if ( $this->mRegex === '' )
261 $this->initRegex();
262
263 return $this->mCaseSensitive ? '' : 'i';
264 }
265
266 /**
267 * Gets a regex matching the word, if it is at the string start
268 */
269 function getRegexStart() {
270 if ($this->mRegex == '' ) {
271 $this->initRegex();
272 }
273 return $this->mRegexStart;
274 }
275
276 /**
277 * regex without the slashes and what not
278 */
279 function getBaseRegex() {
280 if ($this->mRegex == '') {
281 $this->initRegex();
282 }
283 return $this->mBaseRegex;
284 }
285
286 /**
287 * Returns true if the text contains the word
288 * @return bool
289 */
290 function match( $text ) {
291 return preg_match( $this->getRegex(), $text );
292 }
293
294 /**
295 * Returns true if the text starts with the word
296 * @return bool
297 */
298 function matchStart( $text ) {
299 return preg_match( $this->getRegexStart(), $text );
300 }
301
302 /**
303 * Returns NULL if there's no match, the value of $1 otherwise
304 * The return code is the matched string, if there's no variable
305 * part in the regex and the matched variable part ($1) if there
306 * is one.
307 */
308 function matchVariableStartToEnd( $text ) {
309 $matches = array();
310 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
311 if ( $matchcount == 0 ) {
312 return NULL;
313 } elseif ( count($matches) == 1 ) {
314 return $matches[0];
315 } else {
316 # multiple matched parts (variable match); some will be empty because of
317 # synonyms. The variable will be the second non-empty one so remove any
318 # blank elements and re-sort the indices.
319 $matches = array_values(array_filter($matches));
320 return $matches[1];
321 }
322 }
323
324
325 /**
326 * Returns true if the text matches the word, and alters the
327 * input string, removing all instances of the word
328 */
329 function matchAndRemove( &$text ) {
330 global $wgMagicFound;
331 $wgMagicFound = false;
332 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
333 return $wgMagicFound;
334 }
335
336 function matchStartAndRemove( &$text ) {
337 global $wgMagicFound;
338 $wgMagicFound = false;
339 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
340 return $wgMagicFound;
341 }
342
343
344 /**
345 * Replaces the word with something else
346 */
347 function replace( $replacement, $subject, $limit=-1 ) {
348 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject, $limit );
349 $this->mModified = !($res === $subject);
350 return $res;
351 }
352
353 /**
354 * Variable handling: {{SUBST:xxx}} style words
355 * Calls back a function to determine what to replace xxx with
356 * Input word must contain $1
357 */
358 function substituteCallback( $text, $callback ) {
359 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
360 $this->mModified = !($res === $text);
361 return $res;
362 }
363
364 /**
365 * Matches the word, where $1 is a wildcard
366 */
367 function getVariableRegex() {
368 if ( $this->mVariableRegex == '' ) {
369 $this->initRegex();
370 }
371 return $this->mVariableRegex;
372 }
373
374 /**
375 * Matches the entire string, where $1 is a wildcard
376 */
377 function getVariableStartToEndRegex() {
378 if ( $this->mVariableStartToEndRegex == '' ) {
379 $this->initRegex();
380 }
381 return $this->mVariableStartToEndRegex;
382 }
383
384 /**
385 * Accesses the synonym list directly
386 */
387 function getSynonym( $i ) {
388 return $this->mSynonyms[$i];
389 }
390
391 function getSynonyms() {
392 return $this->mSynonyms;
393 }
394
395 /**
396 * Returns true if the last call to replace() or substituteCallback()
397 * returned a modified text, otherwise false.
398 */
399 function getWasModified(){
400 return $this->mModified;
401 }
402
403 /**
404 * $magicarr is an associative array of (magic word ID => replacement)
405 * This method uses the php feature to do several replacements at the same time,
406 * thereby gaining some efficiency. The result is placed in the out variable
407 * $result. The return value is true if something was replaced.
408 * @static
409 **/
410 function replaceMultiple( $magicarr, $subject, &$result ){
411 $search = array();
412 $replace = array();
413 foreach( $magicarr as $id => $replacement ){
414 $mw = MagicWord::get( $id );
415 $search[] = $mw->getRegex();
416 $replace[] = $replacement;
417 }
418
419 $result = preg_replace( $search, $replace, $subject );
420 return !($result === $subject);
421 }
422
423 /**
424 * Adds all the synonyms of this MagicWord to an array, to allow quick
425 * lookup in a list of magic words
426 */
427 function addToArray( &$array, $value ) {
428 foreach ( $this->mSynonyms as $syn ) {
429 $array[$syn] = $value;
430 }
431 }
432
433 function isCaseSensitive() {
434 return $this->mCaseSensitive;
435 }
436 }
437
438 /**
439 * Used in matchAndRemove()
440 * @private
441 **/
442 function pregRemoveAndRecord( $match ) {
443 global $wgMagicFound;
444 $wgMagicFound = true;
445 return '';
446 }
447
448 ?>