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