* Fixed bug, $this->mBaseRegex would contain an invalid regular expression if
[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
177 $escSyn = array();
178 foreach ( $this->mSynonyms as $synonym )
179 // In case a magic word contains /, like that's going to happen;)
180 $escSyn[] = preg_quote( $synonym, '/' );
181 $this->mBaseRegex = implode( '|', $escSyn );
182
183 $case = $this->mCaseSensitive ? '' : 'i';
184 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
185 $this->mRegexStart = "/^(?:{$this->mBaseRegex})/{$case}";
186 $this->mVariableRegex = str_replace( "\\$1", "(.*?)", $this->mRegex );
187 $this->mVariableStartToEndRegex = str_replace( "\\$1", "(.*?)",
188 "/^(?:{$this->mBaseRegex})$/{$case}" );
189 }
190
191 /**
192 * Gets a regex representing matching the word
193 */
194 function getRegex() {
195 if ($this->mRegex == '' ) {
196 $this->initRegex();
197 }
198 return $this->mRegex;
199 }
200
201 /**
202 * Gets a regex matching the word, if it is at the string start
203 */
204 function getRegexStart() {
205 if ($this->mRegex == '' ) {
206 $this->initRegex();
207 }
208 return $this->mRegexStart;
209 }
210
211 /**
212 * regex without the slashes and what not
213 */
214 function getBaseRegex() {
215 if ($this->mRegex == '') {
216 $this->initRegex();
217 }
218 return $this->mBaseRegex;
219 }
220
221 /**
222 * Returns true if the text contains the word
223 * @return bool
224 */
225 function match( $text ) {
226 return preg_match( $this->getRegex(), $text );
227 }
228
229 /**
230 * Returns true if the text starts with the word
231 * @return bool
232 */
233 function matchStart( $text ) {
234 return preg_match( $this->getRegexStart(), $text );
235 }
236
237 /**
238 * Returns NULL if there's no match, the value of $1 otherwise
239 * The return code is the matched string, if there's no variable
240 * part in the regex and the matched variable part ($1) if there
241 * is one.
242 */
243 function matchVariableStartToEnd( $text ) {
244 $matches = array();
245 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
246 if ( $matchcount == 0 ) {
247 return NULL;
248 } elseif ( count($matches) == 1 ) {
249 return $matches[0];
250 } else {
251 # multiple matched parts (variable match); some will be empty because of synonyms
252 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
253 $matches = array_values(array_filter($matches));
254 return $matches[1];
255 }
256 }
257
258
259 /**
260 * Returns true if the text matches the word, and alters the
261 * input string, removing all instances of the word
262 */
263 function matchAndRemove( &$text ) {
264 global $wgMagicFound;
265 $wgMagicFound = false;
266 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
267 return $wgMagicFound;
268 }
269
270 function matchStartAndRemove( &$text ) {
271 global $wgMagicFound;
272 $wgMagicFound = false;
273 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
274 return $wgMagicFound;
275 }
276
277
278 /**
279 * Replaces the word with something else
280 */
281 function replace( $replacement, $subject ) {
282 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject );
283 $this->mModified = !($res === $subject);
284 return $res;
285 }
286
287 /**
288 * Variable handling: {{SUBST:xxx}} style words
289 * Calls back a function to determine what to replace xxx with
290 * Input word must contain $1
291 */
292 function substituteCallback( $text, $callback ) {
293 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
294 $this->mModified = !($res === $text);
295 return $res;
296 }
297
298 /**
299 * Matches the word, where $1 is a wildcard
300 */
301 function getVariableRegex() {
302 if ( $this->mVariableRegex == '' ) {
303 $this->initRegex();
304 }
305 return $this->mVariableRegex;
306 }
307
308 /**
309 * Matches the entire string, where $1 is a wildcard
310 */
311 function getVariableStartToEndRegex() {
312 if ( $this->mVariableStartToEndRegex == '' ) {
313 $this->initRegex();
314 }
315 return $this->mVariableStartToEndRegex;
316 }
317
318 /**
319 * Accesses the synonym list directly
320 */
321 function getSynonym( $i ) {
322 return $this->mSynonyms[$i];
323 }
324
325 /**
326 * Returns true if the last call to replace() or substituteCallback()
327 * returned a modified text, otherwise false.
328 */
329 function getWasModified(){
330 return $this->mModified;
331 }
332
333 /**
334 * $magicarr is an associative array of (magic word ID => replacement)
335 * This method uses the php feature to do several replacements at the same time,
336 * thereby gaining some efficiency. The result is placed in the out variable
337 * $result. The return value is true if something was replaced.
338 * @static
339 **/
340 function replaceMultiple( $magicarr, $subject, &$result ){
341 $search = array();
342 $replace = array();
343 foreach( $magicarr as $id => $replacement ){
344 $mw = MagicWord::get( $id );
345 $search[] = $mw->getRegex();
346 $replace[] = $replacement;
347 }
348
349 $result = preg_replace( $search, $replace, $subject );
350 return !($result === $subject);
351 }
352
353 /**
354 * Adds all the synonyms of this MagicWord to an array, to allow quick
355 * lookup in a list of magic words
356 */
357 function addToArray( &$array, $value ) {
358 foreach ( $this->mSynonyms as $syn ) {
359 $array[$syn] = $value;
360 }
361 }
362 }
363
364 /**
365 * Used in matchAndRemove()
366 * @access private
367 **/
368 function pregRemoveAndRecord( $match ) {
369 global $wgMagicFound;
370 $wgMagicFound = true;
371 return '';
372 }
373
374 ?>