New parameter 'framed' for Extended Image Syntax
[lhc/web/wiklou.git] / includes / MagicWord.php
1 <?php
2
3 # This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
4 # Usage:
5 # if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
6 #
7 # Possible future improvements:
8 # * Simultaneous searching for a number of magic words
9 # * $wgMagicWords in shared memory
10 #
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.
13
14 /*private*/ $wgMagicFound = false;
15
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);
41 define("MAG_NS", 25);
42 define("MAG_LOCALURL", 26);
43 define("MAG_LOCALURLE", 27);
44 define("MAG_SERVER", 28);
45 define("MAG_IMG_FRAMED", 29);
46
47 $wgVariableIDs = array(
48 MAG_CURRENTMONTH,
49 MAG_CURRENTMONTHNAME,
50 MAG_CURRENTDAY,
51 MAG_CURRENTDAYNAME,
52 MAG_CURRENTYEAR,
53 MAG_CURRENTTIME,
54 MAG_NUMBEROFARTICLES,
55 MAG_CURRENTMONTHNAMEGEN,
56 MAG_SITENAME,
57 MAG_SERVER
58 );
59
60 class MagicWord {
61 /*private*/ var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
62 /*private*/ var $mRegexStart, $mBaseRegex, $mVariableRegex;
63 /*private*/ var $mModified;
64
65 function MagicWord($id = 0, $syn = "", $cs = false)
66 {
67 $this->mId = $id;
68 $this->mSynonyms = (array)$syn;
69 $this->mCaseSensitive = $cs;
70 $this->mRegex = "";
71 $this->mRegexStart = "";
72 $this->mVariableRegex = "";
73 $this->mVariableStartToEndRegex = "";
74 $this->mModified = false;
75 }
76
77 # Factory: creates an object representing an ID
78 /*static*/ function &get( $id )
79 {
80 global $wgMagicWords;
81
82 if (!array_key_exists( $id, $wgMagicWords ) ) {
83 $mw = new MagicWord();
84 $mw->load( $id );
85 $wgMagicWords[$id] = $mw;
86 }
87 return $wgMagicWords[$id];
88 }
89
90 # Initialises this object with an ID
91 function load( $id )
92 {
93 global $wgLang;
94 $this->mId = $id;
95 $wgLang->getMagic( $this );
96 }
97
98 # Preliminary initialisation
99 /* private */ function initRegex()
100 {
101 $variableClass = Title::legalChars();
102 $escSyn = array_map( "preg_quote", $this->mSynonyms );
103 $this->mBaseRegex = implode( "|", $escSyn );
104 $case = $this->mCaseSensitive ? "" : "i";
105 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
106 $this->mRegexStart = "/^{$this->mBaseRegex}/{$case}";
107 $this->mVariableRegex = str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex );
108 $this->mVariableStartToEndRegex = str_replace( "\\$1", "([$variableClass]*?)",
109 "/^{$this->mBaseRegex}$/{$case}" );
110 }
111
112 # Gets a regex representing matching the word
113 function getRegex()
114 {
115 if ($this->mRegex == "" ) {
116 $this->initRegex();
117 }
118 return $this->mRegex;
119 }
120
121 # Gets a regex matching the word, if it is at the
122 # string start
123 function getRegexStart()
124 {
125 if ($this->mRegex == "" ) {
126 $this->initRegex();
127 }
128 return $this->mRegexStart;
129 }
130
131 # regex without the slashes and what not
132 function getBaseRegex()
133 {
134 if ($this->mRegex == "") {
135 $this->initRegex();
136 }
137 return $this->mBaseRegex;
138 }
139
140 # Returns true if the text contains the word
141 function match( $text ) {
142 return preg_match( $this->getRegex(), $text );
143 }
144
145 # Returns true if the text starts with the word
146 function matchStart( $text )
147 {
148 return preg_match( $this->getRegexStart(), $text );
149 }
150
151 # Returns NULL if there's no match, the value of $1 otherwise
152 # The return code is the matched string, if there's no variable
153 # part in the regex and the matched variable part ($1) if there
154 # is one.
155 function matchVariableStartToEnd( $text ) {
156 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
157 if ( $matchcount == 0 ) {
158 return NULL;
159 } elseif ( count($matches) == 1 ) {
160 return $matches[0];
161 } else {
162 return $matches[1];
163 }
164 }
165
166
167 # Returns true if the text matches the word, and alters the
168 # input string, removing all instances of the word
169 function matchAndRemove( &$text )
170 {
171 global $wgMagicFound;
172 $wgMagicFound = false;
173 $text = preg_replace_callback( $this->getRegex(), "pregRemoveAndRecord", $text );
174 return $wgMagicFound;
175 }
176
177 function matchStartAndRemove( &$text ) {
178 global $wgMagicFound;
179 $wgMagicFound = false;
180 $text = preg_replace_callback( $this->getRegexStart(), "pregRemoveAndRecord", $text );
181 return $wgMagicFound;
182 }
183
184
185 # Replaces the word with something else
186 function replace( $replacement, $subject )
187 {
188 $res = preg_replace( $this->getRegex(), $replacement, $subject );
189 $this->mModified = !($res === $subject);
190 return $res;
191 }
192
193 # Variable handling: {{SUBST:xxx}} style words
194 # Calls back a function to determine what to replace xxx with
195 # Input word must contain $1
196 function substituteCallback( $text, $callback ) {
197 $regex = $this->getVariableRegex();
198 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
199 $this->mModified = !($res === $text);
200 return $res;
201 }
202
203 # Matches the word, where $1 is a wildcard
204 function getVariableRegex()
205 {
206 if ( $this->mVariableRegex == "" ) {
207 $this->initRegex();
208 }
209 return $this->mVariableRegex;
210 }
211
212 # Matches the entire string, where $1 is a wildcard
213 function getVariableStartToEndRegex()
214 {
215 if ( $this->mVariableStartToEndRegex == "" ) {
216 $this->initRegex();
217 }
218 return $this->mVariableStartToEndRegex;
219 }
220
221 # Accesses the synonym list directly
222 function getSynonym( $i ) {
223 return $this->mSynonyms[$i];
224 }
225
226 # Returns true if the last call to replace() or substituteCallback()
227 # returned a modified text, otherwise false.
228 function getWasModified(){
229 return $this->mModified;
230 }
231
232 # $magicarr is an associative array of (magic word ID => replacement)
233 # This method uses the php feature to do several replacements at the same time,
234 # thereby gaining some efficiency. The result is placed in the out variable
235 # $result. The return value is true if something was replaced.
236
237 /* static */ function replaceMultiple( $magicarr, $subject, &$result ){
238 $search = array();
239 $replace = array();
240 foreach( $magicarr as $id => $replacement ){
241 $mw = MagicWord::get( $id );
242 $search[] = $mw->getRegex();
243 $replace[] = $replacement;
244 }
245
246 $result = preg_replace( $search, $replace, $subject );
247 return !($result === $subject);
248 }
249
250 # Adds all the synonyms of this MagicWord to an array, to allow quick lookup in a list of magic words
251 function addToArray( &$array, $value )
252 {
253 foreach ( $this->mSynonyms as $syn ) {
254 $array[$syn] = $value;
255 }
256 }
257 }
258
259 # Used in matchAndRemove()
260 /*private*/ function pregRemoveAndRecord( $match )
261 {
262 global $wgMagicFound;
263 $wgMagicFound = true;
264 return "";
265 }
266
267 ?>