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