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