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