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