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