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