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