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