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