* (bug 1970) Don't show move tab for immobile pages
[lhc/web/wiklou.git] / includes / MagicWord.php
1 <?php
2 /**
3 * File for magic words
4 * @package MediaWiki
5 */
6
7 /**
8 * private
9 */
10 $wgMagicFound = false;
11
12 /** Actual keyword to be used is set in Language.php */
13 define('MAG_REDIRECT', 0);
14 define('MAG_NOTOC', 1);
15 define('MAG_START', 2);
16 define('MAG_CURRENTMONTH', 3);
17 define('MAG_CURRENTMONTHNAME', 4);
18 define('MAG_CURRENTMONTHNAMEGEN', 5);
19 define('MAG_CURRENTMONTHABBREV', 6);
20 define('MAG_CURRENTDAY', 7);
21 define('MAG_CURRENTDAYNAME', 8);
22 define('MAG_CURRENTYEAR', 9);
23 define('MAG_CURRENTTIME', 10);
24 define('MAG_NUMBEROFARTICLES', 11);
25 define('MAG_MSG', 12);
26 define('MAG_SUBST', 13);
27 define('MAG_MSGNW', 14);
28 define('MAG_NOEDITSECTION', 15);
29 define('MAG_END', 16);
30 define('MAG_IMG_THUMBNAIL', 17);
31 define('MAG_IMG_RIGHT', 18);
32 define('MAG_IMG_LEFT', 19);
33 define('MAG_IMG_NONE', 20);
34 define('MAG_IMG_WIDTH', 21);
35 define('MAG_IMG_CENTER', 22);
36 define('MAG_INT', 23);
37 define('MAG_FORCETOC', 24);
38 define('MAG_SITENAME', 25);
39 define('MAG_NS', 26);
40 define('MAG_LOCALURL', 27);
41 define('MAG_LOCALURLE', 28);
42 define('MAG_SERVER', 29);
43 define('MAG_IMG_FRAMED', 30);
44 define('MAG_PAGENAME', 31);
45 define('MAG_PAGENAMEE', 32);
46 define('MAG_NAMESPACE', 33);
47 define('MAG_TOC', 34);
48 define('MAG_GRAMMAR', 35);
49 define('MAG_NOTITLECONVERT', 36);
50 define('MAG_NOCONTENTCONVERT', 37);
51 define('MAG_CURRENTWEEK', 38);
52 define('MAG_CURRENTDOW', 39);
53 define('MAG_REVISIONID', 40);
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 $escSyn = array_map( 'preg_quote', $this->mSynonyms );
141 $this->mBaseRegex = implode( '|', $escSyn );
142 $case = $this->mCaseSensitive ? '' : 'i';
143 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
144 $this->mRegexStart = "/^({$this->mBaseRegex})/{$case}";
145 $this->mVariableRegex = str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex );
146 $this->mVariableStartToEndRegex = str_replace( "\\$1", "([$variableClass]*?)",
147 "/^({$this->mBaseRegex})$/{$case}" );
148 }
149
150 /**
151 * Gets a regex representing matching the word
152 */
153 function getRegex() {
154 if ($this->mRegex == '' ) {
155 $this->initRegex();
156 }
157 return $this->mRegex;
158 }
159
160 /**
161 * Gets a regex matching the word, if it is at the string start
162 */
163 function getRegexStart() {
164 if ($this->mRegex == '' ) {
165 $this->initRegex();
166 }
167 return $this->mRegexStart;
168 }
169
170 /**
171 * regex without the slashes and what not
172 */
173 function getBaseRegex() {
174 if ($this->mRegex == '') {
175 $this->initRegex();
176 }
177 return $this->mBaseRegex;
178 }
179
180 /**
181 * Returns true if the text contains the word
182 * @return bool
183 */
184 function match( $text ) {
185 return preg_match( $this->getRegex(), $text );
186 }
187
188 /**
189 * Returns true if the text starts with the word
190 * @return bool
191 */
192 function matchStart( $text ) {
193 return preg_match( $this->getRegexStart(), $text );
194 }
195
196 /**
197 * Returns NULL if there's no match, the value of $1 otherwise
198 * The return code is the matched string, if there's no variable
199 * part in the regex and the matched variable part ($1) if there
200 * is one.
201 */
202 function matchVariableStartToEnd( $text ) {
203 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
204 if ( $matchcount == 0 ) {
205 return NULL;
206 } elseif ( count($matches) == 2 ) {
207 return $matches[0];
208 } else {
209 return $matches[2];
210 }
211 }
212
213
214 /**
215 * Returns true if the text matches the word, and alters the
216 * input string, removing all instances of the word
217 */
218 function matchAndRemove( &$text ) {
219 global $wgMagicFound;
220 $wgMagicFound = false;
221 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
222 return $wgMagicFound;
223 }
224
225 function matchStartAndRemove( &$text ) {
226 global $wgMagicFound;
227 $wgMagicFound = false;
228 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
229 return $wgMagicFound;
230 }
231
232
233 /**
234 * Replaces the word with something else
235 */
236 function replace( $replacement, $subject ) {
237 $res = preg_replace( $this->getRegex(), $replacement, $subject );
238 $this->mModified = !($res === $subject);
239 return $res;
240 }
241
242 /**
243 * Variable handling: {{SUBST:xxx}} style words
244 * Calls back a function to determine what to replace xxx with
245 * Input word must contain $1
246 */
247 function substituteCallback( $text, $callback ) {
248 $regex = $this->getVariableRegex();
249 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
250 $this->mModified = !($res === $text);
251 return $res;
252 }
253
254 /**
255 * Matches the word, where $1 is a wildcard
256 */
257 function getVariableRegex() {
258 if ( $this->mVariableRegex == '' ) {
259 $this->initRegex();
260 }
261 return $this->mVariableRegex;
262 }
263
264 /**
265 * Matches the entire string, where $1 is a wildcard
266 */
267 function getVariableStartToEndRegex() {
268 if ( $this->mVariableStartToEndRegex == '' ) {
269 $this->initRegex();
270 }
271 return $this->mVariableStartToEndRegex;
272 }
273
274 /**
275 * Accesses the synonym list directly
276 */
277 function getSynonym( $i ) {
278 return $this->mSynonyms[$i];
279 }
280
281 /**
282 * Returns true if the last call to replace() or substituteCallback()
283 * returned a modified text, otherwise false.
284 */
285 function getWasModified(){
286 return $this->mModified;
287 }
288
289 /**
290 * $magicarr is an associative array of (magic word ID => replacement)
291 * This method uses the php feature to do several replacements at the same time,
292 * thereby gaining some efficiency. The result is placed in the out variable
293 * $result. The return value is true if something was replaced.
294 * @static
295 **/
296 function replaceMultiple( $magicarr, $subject, &$result ){
297 $search = array();
298 $replace = array();
299 foreach( $magicarr as $id => $replacement ){
300 $mw = MagicWord::get( $id );
301 $search[] = $mw->getRegex();
302 $replace[] = $replacement;
303 }
304
305 $result = preg_replace( $search, $replace, $subject );
306 return !($result === $subject);
307 }
308
309 /**
310 * Adds all the synonyms of this MagicWord to an array, to allow quick
311 * lookup in a list of magic words
312 */
313 function addToArray( &$array, $value ) {
314 foreach ( $this->mSynonyms as $syn ) {
315 $array[$syn] = $value;
316 }
317 }
318 }
319
320 /**
321 * Used in matchAndRemove()
322 * @private
323 **/
324 function pregRemoveAndRecord( $match ) {
325 global $wgMagicFound;
326 $wgMagicFound = true;
327 return '';
328 }
329
330 ?>