* Documented
[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_MSG', 12);
27 define('MAG_SUBST', 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 $escSyn = array_map( 'preg_quote', $this->mSynonyms );
142 $this->mBaseRegex = implode( '|', $escSyn );
143 $case = $this->mCaseSensitive ? '' : 'i';
144 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
145 $this->mRegexStart = "/^({$this->mBaseRegex})/{$case}";
146 $this->mVariableRegex = str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex );
147 $this->mVariableStartToEndRegex = str_replace( "\\$1", "([$variableClass]*?)",
148 "/^({$this->mBaseRegex})$/{$case}" );
149 }
150
151 /**
152 * Gets a regex representing matching the word
153 */
154 function getRegex() {
155 if ($this->mRegex == '' ) {
156 $this->initRegex();
157 }
158 return $this->mRegex;
159 }
160
161 /**
162 * Gets a regex matching the word, if it is at the string start
163 */
164 function getRegexStart() {
165 if ($this->mRegex == '' ) {
166 $this->initRegex();
167 }
168 return $this->mRegexStart;
169 }
170
171 /**
172 * regex without the slashes and what not
173 */
174 function getBaseRegex() {
175 if ($this->mRegex == '') {
176 $this->initRegex();
177 }
178 return $this->mBaseRegex;
179 }
180
181 /**
182 * Returns true if the text contains the word
183 * @return bool
184 */
185 function match( $text ) {
186 return preg_match( $this->getRegex(), $text );
187 }
188
189 /**
190 * Returns true if the text starts with the word
191 * @return bool
192 */
193 function matchStart( $text ) {
194 return preg_match( $this->getRegexStart(), $text );
195 }
196
197 /**
198 * Returns NULL if there's no match, the value of $1 otherwise
199 * The return code is the matched string, if there's no variable
200 * part in the regex and the matched variable part ($1) if there
201 * is one.
202 */
203 function matchVariableStartToEnd( $text ) {
204 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
205 if ( $matchcount == 0 ) {
206 return NULL;
207 } elseif ( count($matches) == 2 ) {
208 return $matches[0];
209 } else {
210 return $matches[2];
211 }
212 }
213
214
215 /**
216 * Returns true if the text matches the word, and alters the
217 * input string, removing all instances of the word
218 */
219 function matchAndRemove( &$text ) {
220 global $wgMagicFound;
221 $wgMagicFound = false;
222 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
223 return $wgMagicFound;
224 }
225
226 function matchStartAndRemove( &$text ) {
227 global $wgMagicFound;
228 $wgMagicFound = false;
229 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
230 return $wgMagicFound;
231 }
232
233
234 /**
235 * Replaces the word with something else
236 */
237 function replace( $replacement, $subject ) {
238 $res = preg_replace( $this->getRegex(), $replacement, $subject );
239 $this->mModified = !($res === $subject);
240 return $res;
241 }
242
243 /**
244 * Variable handling: {{SUBST:xxx}} style words
245 * Calls back a function to determine what to replace xxx with
246 * Input word must contain $1
247 */
248 function substituteCallback( $text, $callback ) {
249 $regex = $this->getVariableRegex();
250 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
251 $this->mModified = !($res === $text);
252 return $res;
253 }
254
255 /**
256 * Matches the word, where $1 is a wildcard
257 */
258 function getVariableRegex() {
259 if ( $this->mVariableRegex == '' ) {
260 $this->initRegex();
261 }
262 return $this->mVariableRegex;
263 }
264
265 /**
266 * Matches the entire string, where $1 is a wildcard
267 */
268 function getVariableStartToEndRegex() {
269 if ( $this->mVariableStartToEndRegex == '' ) {
270 $this->initRegex();
271 }
272 return $this->mVariableStartToEndRegex;
273 }
274
275 /**
276 * Accesses the synonym list directly
277 */
278 function getSynonym( $i ) {
279 return $this->mSynonyms[$i];
280 }
281
282 /**
283 * Returns true if the last call to replace() or substituteCallback()
284 * returned a modified text, otherwise false.
285 */
286 function getWasModified(){
287 return $this->mModified;
288 }
289
290 /**
291 * $magicarr is an associative array of (magic word ID => replacement)
292 * This method uses the php feature to do several replacements at the same time,
293 * thereby gaining some efficiency. The result is placed in the out variable
294 * $result. The return value is true if something was replaced.
295 * @static
296 **/
297 function replaceMultiple( $magicarr, $subject, &$result ){
298 $search = array();
299 $replace = array();
300 foreach( $magicarr as $id => $replacement ){
301 $mw = MagicWord::get( $id );
302 $search[] = $mw->getRegex();
303 $replace[] = $replacement;
304 }
305
306 $result = preg_replace( $search, $replace, $subject );
307 return !($result === $subject);
308 }
309
310 /**
311 * Adds all the synonyms of this MagicWord to an array, to allow quick
312 * lookup in a list of magic words
313 */
314 function addToArray( &$array, $value ) {
315 foreach ( $this->mSynonyms as $syn ) {
316 $array[$syn] = $value;
317 }
318 }
319 }
320
321 /**
322 * Used in matchAndRemove()
323 * @private
324 **/
325 function pregRemoveAndRecord( $match ) {
326 global $wgMagicFound;
327 $wgMagicFound = true;
328 return '';
329 }
330
331 ?>