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