3 # This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
5 # if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
7 # Possible future improvements:
8 # * Simultaneous searching for a number of magic words
9 # * $wgMagicWords in shared memory
11 # Please avoid reading the data out of one of these objects and then writing
12 # special case code. If possible, add another match()-like function here.
14 /*private*/ $wgMagicFound = false;
16 define("MAG_REDIRECT", 0);
17 define("MAG_NOTOC", 1);
18 define("MAG_START", 2);
19 define("MAG_CURRENTMONTH", 3);
20 define("MAG_CURRENTMONTHNAME", 4);
21 define("MAG_CURRENTDAY", 5);
22 define("MAG_CURRENTDAYNAME", 6);
23 define("MAG_CURRENTYEAR", 7);
24 define("MAG_CURRENTTIME", 8);
25 define("MAG_NUMBEROFARTICLES", 9);
26 define("MAG_CURRENTMONTHNAMEGEN", 10);
27 define("MAG_MSG", 11);
28 define("MAG_SUBST", 12);
29 define("MAG_MSGNW", 13);
30 define("MAG_NOEDITSECTION", 14);
31 define("MAG_END", 15);
32 define("MAG_IMG_THUMBNAIL", 16);
33 define("MAG_IMG_RIGHT", 17);
34 define("MAG_IMG_LEFT", 18);
35 define("MAG_IMG_NONE", 19);
36 define("MAG_IMG_WIDTH", 20);
37 define("MAG_IMG_CENTER", 21);
38 define("MAG_INT", 22);
39 define("MAG_FORCETOC", 23);
40 define("MAG_SITENAME", 24);
42 define("MAG_LOCALURL", 26);
43 define("MAG_LOCALURLE", 27);
44 define("MAG_SERVER", 28);
46 $wgVariableIDs = array(
54 MAG_CURRENTMONTHNAMEGEN
,
60 /*private*/ var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
61 /*private*/ var $mRegexStart, $mBaseRegex, $mVariableRegex;
62 /*private*/ var $mModified;
64 function MagicWord($id = 0, $syn = "", $cs = false)
67 $this->mSynonyms
= (array)$syn;
68 $this->mCaseSensitive
= $cs;
70 $this->mRegexStart
= "";
71 $this->mVariableRegex
= "";
72 $this->mVariableStartToEndRegex
= "";
73 $this->mModified
= false;
76 # Factory: creates an object representing an ID
77 /*static*/ function &get( $id )
81 if (!array_key_exists( $id, $wgMagicWords ) ) {
82 $mw = new MagicWord();
84 $wgMagicWords[$id] = $mw;
86 return $wgMagicWords[$id];
89 # Initialises this object with an ID
94 $wgLang->getMagic( $this );
97 # Preliminary initialisation
98 /* private */ function initRegex()
100 $variableClass = Title
::legalChars();
101 $escSyn = array_map( "preg_quote", $this->mSynonyms
);
102 $this->mBaseRegex
= implode( "|", $escSyn );
103 $case = $this->mCaseSensitive ?
"" : "i";
104 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
105 $this->mRegexStart
= "/^{$this->mBaseRegex}/{$case}";
106 $this->mVariableRegex
= str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex
);
107 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "([$variableClass]*?)",
108 "/^{$this->mBaseRegex}$/{$case}" );
111 # Gets a regex representing matching the word
114 if ($this->mRegex
== "" ) {
117 return $this->mRegex
;
120 # Gets a regex matching the word, if it is at the
122 function getRegexStart()
124 if ($this->mRegex
== "" ) {
127 return $this->mRegexStart
;
130 # regex without the slashes and what not
131 function getBaseRegex()
133 if ($this->mRegex
== "") {
136 return $this->mBaseRegex
;
139 # Returns true if the text contains the word
140 function match( $text ) {
141 return preg_match( $this->getRegex(), $text );
144 # Returns true if the text starts with the word
145 function matchStart( $text )
147 return preg_match( $this->getRegexStart(), $text );
150 # Returns NULL if there's no match, the value of $1 otherwise
151 # The return code is the matched string, if there's no variable
152 # part in the regex and the matched variable part ($1) if there
154 function matchVariableStartToEnd( $text ) {
155 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
156 if ( $matchcount == 0 ) {
158 } elseif ( count($matches) == 1 ) {
166 # Returns true if the text matches the word, and alters the
167 # input string, removing all instances of the word
168 function matchAndRemove( &$text )
170 global $wgMagicFound;
171 $wgMagicFound = false;
172 $text = preg_replace_callback( $this->getRegex(), "pregRemoveAndRecord", $text );
173 return $wgMagicFound;
176 function matchStartAndRemove( &$text ) {
177 global $wgMagicFound;
178 $wgMagicFound = false;
179 $text = preg_replace_callback( $this->getRegexStart(), "pregRemoveAndRecord", $text );
180 return $wgMagicFound;
184 # Replaces the word with something else
185 function replace( $replacement, $subject )
187 $res = preg_replace( $this->getRegex(), $replacement, $subject );
188 $this->mModified
= !($res === $subject);
192 # Variable handling: {{SUBST:xxx}} style words
193 # Calls back a function to determine what to replace xxx with
194 # Input word must contain $1
195 function substituteCallback( $text, $callback ) {
196 $regex = $this->getVariableRegex();
197 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
198 $this->mModified
= !($res === $text);
202 # Matches the word, where $1 is a wildcard
203 function getVariableRegex()
205 if ( $this->mVariableRegex
== "" ) {
208 return $this->mVariableRegex
;
211 # Matches the entire string, where $1 is a wildcard
212 function getVariableStartToEndRegex()
214 if ( $this->mVariableStartToEndRegex
== "" ) {
217 return $this->mVariableStartToEndRegex
;
220 # Accesses the synonym list directly
221 function getSynonym( $i ) {
222 return $this->mSynonyms
[$i];
225 # Returns true if the last call to replace() or substituteCallback()
226 # returned a modified text, otherwise false.
227 function getWasModified(){
228 return $this->mModified
;
231 # $magicarr is an associative array of (magic word ID => replacement)
232 # This method uses the php feature to do several replacements at the same time,
233 # thereby gaining some efficiency. The result is placed in the out variable
234 # $result. The return value is true if something was replaced.
236 /* static */ function replaceMultiple( $magicarr, $subject, &$result ){
239 foreach( $magicarr as $id => $replacement ){
240 $mw = MagicWord
::get( $id );
241 $search[] = $mw->getRegex();
242 $replace[] = $replacement;
245 $result = preg_replace( $search, $replace, $subject );
246 return !($result === $subject);
249 # Adds all the synonyms of this MagicWord to an array, to allow quick lookup in a list of magic words
250 function addToArray( &$array, $value )
252 foreach ( $this->mSynonyms
as $syn ) {
253 $array[$syn] = $value;
258 # Used in matchAndRemove()
259 /*private*/ function pregRemoveAndRecord( $match )
261 global $wgMagicFound;
262 $wgMagicFound = true;