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