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