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