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