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