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