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