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