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