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