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