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