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