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