Split files and classes in different packages for phpdocumentor. I probably changed...
[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
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_CURRENTDAY', 5);
19 define('MAG_CURRENTDAYNAME', 6);
20 define('MAG_CURRENTYEAR', 7);
21 define('MAG_CURRENTTIME', 8);
22 define('MAG_NUMBEROFARTICLES', 9);
23 define('MAG_CURRENTMONTHNAMEGEN', 10);
24 define('MAG_MSG', 11);
25 define('MAG_SUBST', 12);
26 define('MAG_MSGNW', 13);
27 define('MAG_NOEDITSECTION', 14);
28 define('MAG_END', 15);
29 define('MAG_IMG_THUMBNAIL', 16);
30 define('MAG_IMG_RIGHT', 17);
31 define('MAG_IMG_LEFT', 18);
32 define('MAG_IMG_NONE', 19);
33 define('MAG_IMG_WIDTH', 20);
34 define('MAG_IMG_CENTER', 21);
35 define('MAG_INT', 22);
36 define('MAG_FORCETOC', 23);
37 define('MAG_SITENAME', 24);
38 define('MAG_NS', 25);
39 define('MAG_LOCALURL', 26);
40 define('MAG_LOCALURLE', 27);
41 define('MAG_SERVER', 28);
42 define('MAG_IMG_FRAMED', 29);
43 define('MAG_PAGENAME', 30);
44 define('MAG_PAGENAMEE', 31);
45 define('MAG_NAMESPACE', 32);
46 define('MAG_TOC', 33);
47 define('MAG_GRAMMAR', 34);
48
49 $wgVariableIDs = array(
50 MAG_CURRENTMONTH,
51 MAG_CURRENTMONTHNAME,
52 MAG_CURRENTDAY,
53 MAG_CURRENTDAYNAME,
54 MAG_CURRENTYEAR,
55 MAG_CURRENTTIME,
56 MAG_NUMBEROFARTICLES,
57 MAG_CURRENTMONTHNAMEGEN,
58 MAG_SITENAME,
59 MAG_SERVER,
60 MAG_PAGENAME,
61 MAG_PAGENAMEE,
62 MAG_NAMESPACE
63 );
64
65 /**
66 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
67 * Usage:
68 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
69 *
70 * Possible future improvements:
71 * * Simultaneous searching for a number of magic words
72 * * $wgMagicWords in shared memory
73 *
74 * Please avoid reading the data out of one of these objects and then writing
75 * special case code. If possible, add another match()-like function here.
76 *
77 * @package MediaWiki
78 */
79 class MagicWord {
80 /**#@+
81 * @access private
82 */
83 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
84 var $mRegexStart, $mBaseRegex, $mVariableRegex;
85 var $mModified;
86 /**#@-*/
87
88 function MagicWord($id = 0, $syn = '', $cs = false) {
89 $this->mId = $id;
90 $this->mSynonyms = (array)$syn;
91 $this->mCaseSensitive = $cs;
92 $this->mRegex = '';
93 $this->mRegexStart = '';
94 $this->mVariableRegex = '';
95 $this->mVariableStartToEndRegex = '';
96 $this->mModified = false;
97 }
98
99 /**
100 * Factory: creates an object representing an ID
101 * @static
102 */
103 function &get( $id ) {
104 global $wgMagicWords;
105
106 if ( !is_array( $wgMagicWords ) ) {
107 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
108 }
109 if (!array_key_exists( $id, $wgMagicWords ) ) {
110 $mw = new MagicWord();
111 $mw->load( $id );
112 $wgMagicWords[$id] = $mw;
113 }
114 return $wgMagicWords[$id];
115 }
116
117 # Initialises this object with an ID
118 function load( $id ) {
119 global $wgLang;
120 $this->mId = $id;
121 $wgLang->getMagic( $this );
122 }
123
124 /**
125 * Preliminary initialisation
126 * @private
127 */
128 function initRegex() {
129 $variableClass = Title::legalChars();
130 $escSyn = array_map( 'preg_quote', $this->mSynonyms );
131 $this->mBaseRegex = implode( '|', $escSyn );
132 $case = $this->mCaseSensitive ? '' : 'i';
133 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
134 $this->mRegexStart = "/^{$this->mBaseRegex}/{$case}";
135 $this->mVariableRegex = str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex );
136 $this->mVariableStartToEndRegex = str_replace( "\\$1", "([$variableClass]*?)",
137 "/^({$this->mBaseRegex})$/{$case}" );
138 }
139
140 /**
141 * Gets a regex representing matching the word
142 */
143 function getRegex() {
144 if ($this->mRegex == '' ) {
145 $this->initRegex();
146 }
147 return $this->mRegex;
148 }
149
150 /**
151 * Gets a regex matching the word, if it is at the string start
152 */
153 function getRegexStart() {
154 if ($this->mRegex == '' ) {
155 $this->initRegex();
156 }
157 return $this->mRegexStart;
158 }
159
160 /**
161 * regex without the slashes and what not
162 */
163 function getBaseRegex() {
164 if ($this->mRegex == '') {
165 $this->initRegex();
166 }
167 return $this->mBaseRegex;
168 }
169
170 /**
171 * Returns true if the text contains the word
172 * @return bool
173 */
174 function match( $text ) {
175 return preg_match( $this->getRegex(), $text );
176 }
177
178 /**
179 * Returns true if the text starts with the word
180 * @return bool
181 */
182 function matchStart( $text ) {
183 return preg_match( $this->getRegexStart(), $text );
184 }
185
186 /**
187 * Returns NULL if there's no match, the value of $1 otherwise
188 * The return code is the matched string, if there's no variable
189 * part in the regex and the matched variable part ($1) if there
190 * is one.
191 */
192 function matchVariableStartToEnd( $text ) {
193 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
194 if ( $matchcount == 0 ) {
195 return NULL;
196 } elseif ( count($matches) == 1 ) {
197 return $matches[0];
198 } else {
199 return $matches[1];
200 }
201 }
202
203
204 /**
205 * Returns true if the text matches the word, and alters the
206 * input string, removing all instances of the word
207 */
208 function matchAndRemove( &$text ) {
209 global $wgMagicFound;
210 $wgMagicFound = false;
211 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
212 return $wgMagicFound;
213 }
214
215 function matchStartAndRemove( &$text ) {
216 global $wgMagicFound;
217 $wgMagicFound = false;
218 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
219 return $wgMagicFound;
220 }
221
222
223 /**
224 * Replaces the word with something else
225 */
226 function replace( $replacement, $subject ) {
227 $res = preg_replace( $this->getRegex(), $replacement, $subject );
228 $this->mModified = !($res === $subject);
229 return $res;
230 }
231
232 /**
233 * Variable handling: {{SUBST:xxx}} style words
234 * Calls back a function to determine what to replace xxx with
235 * Input word must contain $1
236 */
237 function substituteCallback( $text, $callback ) {
238 $regex = $this->getVariableRegex();
239 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
240 $this->mModified = !($res === $text);
241 return $res;
242 }
243
244 /**
245 * Matches the word, where $1 is a wildcard
246 */
247 function getVariableRegex() {
248 if ( $this->mVariableRegex == '' ) {
249 $this->initRegex();
250 }
251 return $this->mVariableRegex;
252 }
253
254 /**
255 * Matches the entire string, where $1 is a wildcard
256 */
257 function getVariableStartToEndRegex() {
258 if ( $this->mVariableStartToEndRegex == '' ) {
259 $this->initRegex();
260 }
261 return $this->mVariableStartToEndRegex;
262 }
263
264 /**
265 * Accesses the synonym list directly
266 */
267 function getSynonym( $i ) {
268 return $this->mSynonyms[$i];
269 }
270
271 /**
272 * Returns true if the last call to replace() or substituteCallback()
273 * returned a modified text, otherwise false.
274 */
275 function getWasModified(){
276 return $this->mModified;
277 }
278
279 /**
280 * $magicarr is an associative array of (magic word ID => replacement)
281 * This method uses the php feature to do several replacements at the same time,
282 * thereby gaining some efficiency. The result is placed in the out variable
283 * $result. The return value is true if something was replaced.
284 * @static
285 **/
286 function replaceMultiple( $magicarr, $subject, &$result ){
287 $search = array();
288 $replace = array();
289 foreach( $magicarr as $id => $replacement ){
290 $mw = MagicWord::get( $id );
291 $search[] = $mw->getRegex();
292 $replace[] = $replacement;
293 }
294
295 $result = preg_replace( $search, $replace, $subject );
296 return !($result === $subject);
297 }
298
299 /**
300 * Adds all the synonyms of this MagicWord to an array, to allow quick
301 * lookup in a list of magic words
302 */
303 function addToArray( &$array, $value ) {
304 foreach ( $this->mSynonyms as $syn ) {
305 $array[$syn] = $value;
306 }
307 }
308 }
309
310 /**
311 * Used in matchAndRemove()
312 * @private
313 **/
314 function pregRemoveAndRecord( $match ) {
315 global $wgMagicFound;
316 $wgMagicFound = true;
317 return '';
318 }
319
320 ?>