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