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