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