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