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