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