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