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_SUBST', 12);
27 define('MAG_MSGNW', 13);
28 define('MAG_NOEDITSECTION', 14);
29 define('MAG_END', 15);
30 define('MAG_IMG_THUMBNAIL', 16);
31 define('MAG_IMG_RIGHT', 17);
32 define('MAG_IMG_LEFT', 18);
33 define('MAG_IMG_NONE', 19);
34 define('MAG_IMG_WIDTH', 20);
35 define('MAG_IMG_CENTER', 21);
36 define('MAG_INT', 22);
37 define('MAG_FORCETOC', 23);
38 define('MAG_SITENAME', 24);
40 define('MAG_LOCALURL', 26);
41 define('MAG_LOCALURLE', 27);
42 define('MAG_SERVER', 28);
43 define('MAG_IMG_FRAMED', 29);
44 define('MAG_PAGENAME', 30);
45 define('MAG_PAGENAMEE', 31);
46 define('MAG_NAMESPACE', 32);
47 define('MAG_TOC', 33);
48 define('MAG_GRAMMAR', 34);
49 define('MAG_NOTITLECONVERT', 35);
50 define('MAG_NOCONTENTCONVERT', 36);
51 define('MAG_CURRENTWEEK', 37);
52 define('MAG_CURRENTDOW', 38);
53 define('MAG_REVISIONID', 39);
55 $wgVariableIDs = array(
58 MAG_CURRENTMONTHNAMEGEN
,
59 MAG_CURRENTMONTHABBREV
,
76 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
78 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
80 * Possible future improvements:
81 * * Simultaneous searching for a number of magic words
82 * * $wgMagicWords in shared memory
84 * Please avoid reading the data out of one of these objects and then writing
85 * special case code. If possible, add another match()-like function here.
93 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
94 var $mRegexStart, $mBaseRegex, $mVariableRegex;
98 function MagicWord($id = 0, $syn = '', $cs = false) {
100 $this->mSynonyms
= (array)$syn;
101 $this->mCaseSensitive
= $cs;
103 $this->mRegexStart
= '';
104 $this->mVariableRegex
= '';
105 $this->mVariableStartToEndRegex
= '';
106 $this->mModified
= false;
110 * Factory: creates an object representing an ID
113 function &get( $id ) {
114 global $wgMagicWords;
116 if ( !is_array( $wgMagicWords ) ) {
117 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
119 if (!array_key_exists( $id, $wgMagicWords ) ) {
120 $mw = new MagicWord();
122 $wgMagicWords[$id] = $mw;
124 return $wgMagicWords[$id];
127 # Initialises this object with an ID
128 function load( $id ) {
131 $wgContLang->getMagic( $this );
135 * Preliminary initialisation
138 function initRegex() {
139 #$variableClass = Title::legalChars();
140 # This was used for matching "$1" variables, but different uses of the feature will have
141 # different restrictions, which should be checked *after* the MagicWord has been matched,
143 $escSyn = array_map( 'preg_quote', $this->mSynonyms
);
144 $this->mBaseRegex
= implode( '|', $escSyn );
145 $case = $this->mCaseSensitive ?
'' : 'i';
146 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
147 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
148 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
149 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
150 "/^(?:{$this->mBaseRegex})$/{$case}" );
154 * Gets a regex representing matching the word
156 function getRegex() {
157 if ($this->mRegex
== '' ) {
160 return $this->mRegex
;
164 * Gets a regex matching the word, if it is at the string start
166 function getRegexStart() {
167 if ($this->mRegex
== '' ) {
170 return $this->mRegexStart
;
174 * regex without the slashes and what not
176 function getBaseRegex() {
177 if ($this->mRegex
== '') {
180 return $this->mBaseRegex
;
184 * Returns true if the text contains the word
187 function match( $text ) {
188 return preg_match( $this->getRegex(), $text );
192 * Returns true if the text starts with the word
195 function matchStart( $text ) {
196 return preg_match( $this->getRegexStart(), $text );
200 * Returns NULL if there's no match, the value of $1 otherwise
201 * The return code is the matched string, if there's no variable
202 * part in the regex and the matched variable part ($1) if there
205 function matchVariableStartToEnd( $text ) {
206 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
207 if ( $matchcount == 0 ) {
209 } elseif ( count($matches) == 1 ) {
212 # multiple matched parts (variable match); some will be empty because of synonyms
213 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
214 $matches = array_values(array_filter($matches));
221 * Returns true if the text matches the word, and alters the
222 * input string, removing all instances of the word
224 function matchAndRemove( &$text ) {
225 global $wgMagicFound;
226 $wgMagicFound = false;
227 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
228 return $wgMagicFound;
231 function matchStartAndRemove( &$text ) {
232 global $wgMagicFound;
233 $wgMagicFound = false;
234 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
235 return $wgMagicFound;
240 * Replaces the word with something else
242 function replace( $replacement, $subject ) {
243 $res = preg_replace( $this->getRegex(), $replacement, $subject );
244 $this->mModified
= !($res === $subject);
249 * Variable handling: {{SUBST:xxx}} style words
250 * Calls back a function to determine what to replace xxx with
251 * Input word must contain $1
253 function substituteCallback( $text, $callback ) {
254 $regex = $this->getVariableRegex();
255 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
256 $this->mModified
= !($res === $text);
261 * Matches the word, where $1 is a wildcard
263 function getVariableRegex() {
264 if ( $this->mVariableRegex
== '' ) {
267 return $this->mVariableRegex
;
271 * Matches the entire string, where $1 is a wildcard
273 function getVariableStartToEndRegex() {
274 if ( $this->mVariableStartToEndRegex
== '' ) {
277 return $this->mVariableStartToEndRegex
;
281 * Accesses the synonym list directly
283 function getSynonym( $i ) {
284 return $this->mSynonyms
[$i];
288 * Returns true if the last call to replace() or substituteCallback()
289 * returned a modified text, otherwise false.
291 function getWasModified(){
292 return $this->mModified
;
296 * $magicarr is an associative array of (magic word ID => replacement)
297 * This method uses the php feature to do several replacements at the same time,
298 * thereby gaining some efficiency. The result is placed in the out variable
299 * $result. The return value is true if something was replaced.
302 function replaceMultiple( $magicarr, $subject, &$result ){
305 foreach( $magicarr as $id => $replacement ){
306 $mw = MagicWord
::get( $id );
307 $search[] = $mw->getRegex();
308 $replace[] = $replacement;
311 $result = preg_replace( $search, $replace, $subject );
312 return !($result === $subject);
316 * Adds all the synonyms of this MagicWord to an array, to allow quick
317 * lookup in a list of magic words
319 function addToArray( &$array, $value ) {
320 foreach ( $this->mSynonyms
as $syn ) {
321 $array[$syn] = $value;
327 * Used in matchAndRemove()
330 function pregRemoveAndRecord( $match ) {
331 global $wgMagicFound;
332 $wgMagicFound = true;