10 $wgMagicFound = false;
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);
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);
50 $wgVariableIDs = array(
58 MAG_CURRENTMONTHNAMEGEN
,
67 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
69 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
71 * Possible future improvements:
72 * * Simultaneous searching for a number of magic words
73 * * $wgMagicWords in shared memory
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.
84 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
85 var $mRegexStart, $mBaseRegex, $mVariableRegex;
89 function MagicWord($id = 0, $syn = '', $cs = false) {
91 $this->mSynonyms
= (array)$syn;
92 $this->mCaseSensitive
= $cs;
94 $this->mRegexStart
= '';
95 $this->mVariableRegex
= '';
96 $this->mVariableStartToEndRegex
= '';
97 $this->mModified
= false;
101 * Factory: creates an object representing an ID
104 function &get( $id ) {
105 global $wgMagicWords;
107 if ( !is_array( $wgMagicWords ) ) {
108 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
110 if (!array_key_exists( $id, $wgMagicWords ) ) {
111 $mw = new MagicWord();
113 $wgMagicWords[$id] = $mw;
115 return $wgMagicWords[$id];
118 # Initialises this object with an ID
119 function load( $id ) {
122 $wgContLang->getMagic( $this );
126 * Preliminary initialisation
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}" );
142 * Gets a regex representing matching the word
144 function getRegex() {
145 if ($this->mRegex
== '' ) {
148 return $this->mRegex
;
152 * Gets a regex matching the word, if it is at the string start
154 function getRegexStart() {
155 if ($this->mRegex
== '' ) {
158 return $this->mRegexStart
;
162 * regex without the slashes and what not
164 function getBaseRegex() {
165 if ($this->mRegex
== '') {
168 return $this->mBaseRegex
;
172 * Returns true if the text contains the word
175 function match( $text ) {
176 return preg_match( $this->getRegex(), $text );
180 * Returns true if the text starts with the word
183 function matchStart( $text ) {
184 return preg_match( $this->getRegexStart(), $text );
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
193 function matchVariableStartToEnd( $text ) {
194 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
195 if ( $matchcount == 0 ) {
197 } elseif ( count($matches) == 2 ) {
206 * Returns true if the text matches the word, and alters the
207 * input string, removing all instances of the word
209 function matchAndRemove( &$text ) {
210 global $wgMagicFound;
211 $wgMagicFound = false;
212 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
213 return $wgMagicFound;
216 function matchStartAndRemove( &$text ) {
217 global $wgMagicFound;
218 $wgMagicFound = false;
219 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
220 return $wgMagicFound;
225 * Replaces the word with something else
227 function replace( $replacement, $subject ) {
228 $res = preg_replace( $this->getRegex(), $replacement, $subject );
229 $this->mModified
= !($res === $subject);
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
238 function substituteCallback( $text, $callback ) {
239 $regex = $this->getVariableRegex();
240 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
241 $this->mModified
= !($res === $text);
246 * Matches the word, where $1 is a wildcard
248 function getVariableRegex() {
249 if ( $this->mVariableRegex
== '' ) {
252 return $this->mVariableRegex
;
256 * Matches the entire string, where $1 is a wildcard
258 function getVariableStartToEndRegex() {
259 if ( $this->mVariableStartToEndRegex
== '' ) {
262 return $this->mVariableStartToEndRegex
;
266 * Accesses the synonym list directly
268 function getSynonym( $i ) {
269 return $this->mSynonyms
[$i];
273 * Returns true if the last call to replace() or substituteCallback()
274 * returned a modified text, otherwise false.
276 function getWasModified(){
277 return $this->mModified
;
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.
287 function replaceMultiple( $magicarr, $subject, &$result ){
290 foreach( $magicarr as $id => $replacement ){
291 $mw = MagicWord
::get( $id );
292 $search[] = $mw->getRegex();
293 $replace[] = $replacement;
296 $result = preg_replace( $search, $replace, $subject );
297 return !($result === $subject);
301 * Adds all the synonyms of this MagicWord to an array, to allow quick
302 * lookup in a list of magic words
304 function addToArray( &$array, $value ) {
305 foreach ( $this->mSynonyms
as $syn ) {
306 $array[$syn] = $value;
312 * Used in matchAndRemove()
315 function pregRemoveAndRecord( $match ) {
316 global $wgMagicFound;
317 $wgMagicFound = true;