11 $wgMagicFound = false;
13 /** Actual keyword to be used is set in Language.php */
14 define('MAG_REDIRECT', 0);
15 define('MAG_NOTOC', 1);
16 define('MAG_START', 2);
17 define('MAG_CURRENTMONTH', 3);
18 define('MAG_CURRENTMONTHNAME', 4);
19 define('MAG_CURRENTMONTHNAMEGEN', 5);
20 define('MAG_CURRENTMONTHABBREV', 6);
21 define('MAG_CURRENTDAY', 7);
22 define('MAG_CURRENTDAYNAME', 8);
23 define('MAG_CURRENTYEAR', 9);
24 define('MAG_CURRENTTIME', 10);
25 define('MAG_NUMBEROFARTICLES', 11);
26 define('MAG_MSG', 12);
27 define('MAG_SUBST', 13);
28 define('MAG_MSGNW', 14);
29 define('MAG_NOEDITSECTION', 15);
30 define('MAG_END', 16);
31 define('MAG_IMG_THUMBNAIL', 17);
32 define('MAG_IMG_RIGHT', 18);
33 define('MAG_IMG_LEFT', 19);
34 define('MAG_IMG_NONE', 20);
35 define('MAG_IMG_WIDTH', 21);
36 define('MAG_IMG_CENTER', 22);
37 define('MAG_INT', 23);
38 define('MAG_FORCETOC', 24);
39 define('MAG_SITENAME', 25);
41 define('MAG_LOCALURL', 27);
42 define('MAG_LOCALURLE', 28);
43 define('MAG_SERVER', 29);
44 define('MAG_IMG_FRAMED', 30);
45 define('MAG_PAGENAME', 31);
46 define('MAG_PAGENAMEE', 32);
47 define('MAG_NAMESPACE', 33);
48 define('MAG_TOC', 34);
49 define('MAG_GRAMMAR', 35);
50 define('MAG_NOTITLECONVERT', 36);
51 define('MAG_NOCONTENTCONVERT', 37);
52 define('MAG_CURRENTWEEK', 38);
53 define('MAG_CURRENTDOW', 39);
54 define('MAG_REVISIONID', 40);
56 $wgVariableIDs = array(
59 MAG_CURRENTMONTHNAMEGEN
,
60 MAG_CURRENTMONTHABBREV
,
77 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
79 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
81 * Possible future improvements:
82 * * Simultaneous searching for a number of magic words
83 * * $wgMagicWords in shared memory
85 * Please avoid reading the data out of one of these objects and then writing
86 * special case code. If possible, add another match()-like function here.
94 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
95 var $mRegexStart, $mBaseRegex, $mVariableRegex;
99 function MagicWord($id = 0, $syn = '', $cs = false) {
101 $this->mSynonyms
= (array)$syn;
102 $this->mCaseSensitive
= $cs;
104 $this->mRegexStart
= '';
105 $this->mVariableRegex
= '';
106 $this->mVariableStartToEndRegex
= '';
107 $this->mModified
= false;
111 * Factory: creates an object representing an ID
114 function &get( $id ) {
115 global $wgMagicWords;
117 if ( !is_array( $wgMagicWords ) ) {
118 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
120 if (!array_key_exists( $id, $wgMagicWords ) ) {
121 $mw = new MagicWord();
123 $wgMagicWords[$id] = $mw;
125 return $wgMagicWords[$id];
128 # Initialises this object with an ID
129 function load( $id ) {
132 $wgContLang->getMagic( $this );
136 * Preliminary initialisation
139 function initRegex() {
140 $variableClass = Title
::legalChars();
141 $escSyn = array_map( 'preg_quote', $this->mSynonyms
);
142 $this->mBaseRegex
= implode( '|', $escSyn );
143 $case = $this->mCaseSensitive ?
'' : 'i';
144 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
145 $this->mRegexStart
= "/^({$this->mBaseRegex})/{$case}";
146 $this->mVariableRegex
= str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex
);
147 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "([$variableClass]*?)",
148 "/^({$this->mBaseRegex})$/{$case}" );
152 * Gets a regex representing matching the word
154 function getRegex() {
155 if ($this->mRegex
== '' ) {
158 return $this->mRegex
;
162 * Gets a regex matching the word, if it is at the string start
164 function getRegexStart() {
165 if ($this->mRegex
== '' ) {
168 return $this->mRegexStart
;
172 * regex without the slashes and what not
174 function getBaseRegex() {
175 if ($this->mRegex
== '') {
178 return $this->mBaseRegex
;
182 * Returns true if the text contains the word
185 function match( $text ) {
186 return preg_match( $this->getRegex(), $text );
190 * Returns true if the text starts with the word
193 function matchStart( $text ) {
194 return preg_match( $this->getRegexStart(), $text );
198 * Returns NULL if there's no match, the value of $1 otherwise
199 * The return code is the matched string, if there's no variable
200 * part in the regex and the matched variable part ($1) if there
203 function matchVariableStartToEnd( $text ) {
204 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
205 if ( $matchcount == 0 ) {
207 } elseif ( count($matches) == 2 ) {
216 * Returns true if the text matches the word, and alters the
217 * input string, removing all instances of the word
219 function matchAndRemove( &$text ) {
220 global $wgMagicFound;
221 $wgMagicFound = false;
222 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
223 return $wgMagicFound;
226 function matchStartAndRemove( &$text ) {
227 global $wgMagicFound;
228 $wgMagicFound = false;
229 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
230 return $wgMagicFound;
235 * Replaces the word with something else
237 function replace( $replacement, $subject ) {
238 $res = preg_replace( $this->getRegex(), $replacement, $subject );
239 $this->mModified
= !($res === $subject);
244 * Variable handling: {{SUBST:xxx}} style words
245 * Calls back a function to determine what to replace xxx with
246 * Input word must contain $1
248 function substituteCallback( $text, $callback ) {
249 $regex = $this->getVariableRegex();
250 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
251 $this->mModified
= !($res === $text);
256 * Matches the word, where $1 is a wildcard
258 function getVariableRegex() {
259 if ( $this->mVariableRegex
== '' ) {
262 return $this->mVariableRegex
;
266 * Matches the entire string, where $1 is a wildcard
268 function getVariableStartToEndRegex() {
269 if ( $this->mVariableStartToEndRegex
== '' ) {
272 return $this->mVariableStartToEndRegex
;
276 * Accesses the synonym list directly
278 function getSynonym( $i ) {
279 return $this->mSynonyms
[$i];
283 * Returns true if the last call to replace() or substituteCallback()
284 * returned a modified text, otherwise false.
286 function getWasModified(){
287 return $this->mModified
;
291 * $magicarr is an associative array of (magic word ID => replacement)
292 * This method uses the php feature to do several replacements at the same time,
293 * thereby gaining some efficiency. The result is placed in the out variable
294 * $result. The return value is true if something was replaced.
297 function replaceMultiple( $magicarr, $subject, &$result ){
300 foreach( $magicarr as $id => $replacement ){
301 $mw = MagicWord
::get( $id );
302 $search[] = $mw->getRegex();
303 $replace[] = $replacement;
306 $result = preg_replace( $search, $replace, $subject );
307 return !($result === $subject);
311 * Adds all the synonyms of this MagicWord to an array, to allow quick
312 * lookup in a list of magic words
314 function addToArray( &$array, $value ) {
315 foreach ( $this->mSynonyms
as $syn ) {
316 $array[$syn] = $value;
322 * Used in matchAndRemove()
325 function pregRemoveAndRecord( $match ) {
326 global $wgMagicFound;
327 $wgMagicFound = true;