Some documentation. Let's talk about it in wikitech-l.
[mediawiki.git] / includes / MagicWord.php
blob64a2e83e1abab61a42e6821f6862ffcabf4f8d0b
1 <?php
2 /**
3 * File for magic words
4 */
6 /**
7 * private
8 */
9 $wgMagicFound = false;
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);
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
64 /**
65 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
66 * Usage:
67 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
69 * Possible future improvements:
70 * * Simultaneous searching for a number of magic words
71 * * $wgMagicWords in shared memory
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.
76 class MagicWord {
77 /*private*/ var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
78 /*private*/ var $mRegexStart, $mBaseRegex, $mVariableRegex;
79 /*private*/ var $mModified;
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;
92 /**
93 * Factory: creates an object representing an ID
94 * @static
96 function &get( $id ) {
97 global $wgMagicWords;
99 if ( !is_array( $wgMagicWords ) ) {
100 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
102 if (!array_key_exists( $id, $wgMagicWords ) ) {
103 $mw = new MagicWord();
104 $mw->load( $id );
105 $wgMagicWords[$id] = $mw;
107 return $wgMagicWords[$id];
110 # Initialises this object with an ID
111 function load( $id ) {
112 global $wgLang;
113 $this->mId = $id;
114 $wgLang->getMagic( $this );
118 * Preliminary initialisation
119 * @private
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}" );
134 * Gets a regex representing matching the word
136 function getRegex() {
137 if ($this->mRegex == '' ) {
138 $this->initRegex();
140 return $this->mRegex;
144 * Gets a regex matching the word, if it is at the string start
146 function getRegexStart() {
147 if ($this->mRegex == '' ) {
148 $this->initRegex();
150 return $this->mRegexStart;
154 * regex without the slashes and what not
156 function getBaseRegex() {
157 if ($this->mRegex == '') {
158 $this->initRegex();
160 return $this->mBaseRegex;
164 * Returns true if the text contains the word
165 * @return bool
167 function match( $text ) {
168 return preg_match( $this->getRegex(), $text );
172 * Returns true if the text starts with the word
173 * @return bool
175 function matchStart( $text ) {
176 return preg_match( $this->getRegexStart(), $text );
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.
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];
198 * Returns true if the text matches the word, and alters the
199 * input string, removing all instances of the word
201 function matchAndRemove( &$text ) {
202 global $wgMagicFound;
203 $wgMagicFound = false;
204 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
205 return $wgMagicFound;
208 function matchStartAndRemove( &$text ) {
209 global $wgMagicFound;
210 $wgMagicFound = false;
211 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
212 return $wgMagicFound;
217 * Replaces the word with something else
219 function replace( $replacement, $subject ) {
220 $res = preg_replace( $this->getRegex(), $replacement, $subject );
221 $this->mModified = !($res === $subject);
222 return $res;
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
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;
238 * Matches the word, where $1 is a wildcard
240 function getVariableRegex() {
241 if ( $this->mVariableRegex == '' ) {
242 $this->initRegex();
244 return $this->mVariableRegex;
248 * Matches the entire string, where $1 is a wildcard
250 function getVariableStartToEndRegex() {
251 if ( $this->mVariableStartToEndRegex == '' ) {
252 $this->initRegex();
254 return $this->mVariableStartToEndRegex;
258 * Accesses the synonym list directly
260 function getSynonym( $i ) {
261 return $this->mSynonyms[$i];
265 * Returns true if the last call to replace() or substituteCallback()
266 * returned a modified text, otherwise false.
268 function getWasModified(){
269 return $this->mModified;
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
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;
288 $result = preg_replace( $search, $replace, $subject );
289 return !($result === $subject);
293 * Adds all the synonyms of this MagicWord to an array, to allow quick
294 * lookup in a list of magic words
296 function addToArray( &$array, $value ) {
297 foreach ( $this->mSynonyms as $syn ) {
298 $array[$syn] = $value;
304 * Used in matchAndRemove()
305 * @private
307 function pregRemoveAndRecord( $match ) {
308 global $wgMagicFound;
309 $wgMagicFound = true;
310 return '';