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