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