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