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