767331da6f25e7f742570a7571d8c7c115d02e6c
[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_PAGENAMEE", 31);
48 define("MAG_NAMESPACE", 32);
49 define("MAG_TOC", 33);
50
51 $wgVariableIDs = array(
52 MAG_CURRENTMONTH,
53 MAG_CURRENTMONTHNAME,
54 MAG_CURRENTDAY,
55 MAG_CURRENTDAYNAME,
56 MAG_CURRENTYEAR,
57 MAG_CURRENTTIME,
58 MAG_NUMBEROFARTICLES,
59 MAG_CURRENTMONTHNAMEGEN,
60 MAG_SITENAME,
61 MAG_SERVER,
62 MAG_PAGENAME,
63 MAG_PAGENAMEE,
64 MAG_NAMESPACE
65 );
66
67 class MagicWord {
68 /*private*/ var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
69 /*private*/ var $mRegexStart, $mBaseRegex, $mVariableRegex;
70 /*private*/ var $mModified;
71
72 function MagicWord($id = 0, $syn = '', $cs = false) {
73 $this->mId = $id;
74 $this->mSynonyms = (array)$syn;
75 $this->mCaseSensitive = $cs;
76 $this->mRegex = '';
77 $this->mRegexStart = '';
78 $this->mVariableRegex = '';
79 $this->mVariableStartToEndRegex = '';
80 $this->mModified = false;
81 }
82
83 # Factory: creates an object representing an ID
84 /*static*/ function &get( $id ) {
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 global $wgLang;
101 $this->mId = $id;
102 $wgLang->getMagic( $this );
103 }
104
105 # Preliminary initialisation
106 /* private */ function initRegex() {
107 $variableClass = Title::legalChars();
108 $escSyn = array_map( "preg_quote", $this->mSynonyms );
109 $this->mBaseRegex = implode( "|", $escSyn );
110 $case = $this->mCaseSensitive ? "" : "i";
111 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
112 $this->mRegexStart = "/^{$this->mBaseRegex}/{$case}";
113 $this->mVariableRegex = str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex );
114 $this->mVariableStartToEndRegex = str_replace( "\\$1", "([$variableClass]*?)",
115 "/^({$this->mBaseRegex})$/{$case}" );
116 }
117
118 # Gets a regex representing matching the word
119 function getRegex() {
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 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 if ($this->mRegex == '') {
138 $this->initRegex();
139 }
140 return $this->mBaseRegex;
141 }
142
143 # Returns true if the text contains the word
144 function match( $text ) {
145 return preg_match( $this->getRegex(), $text );
146 }
147
148 # Returns true if the text starts with the word
149 function matchStart( $text ) {
150 return preg_match( $this->getRegexStart(), $text );
151 }
152
153 # Returns NULL if there's no match, the value of $1 otherwise
154 # The return code is the matched string, if there's no variable
155 # part in the regex and the matched variable part ($1) if there
156 # is one.
157 function matchVariableStartToEnd( $text ) {
158 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
159 if ( $matchcount == 0 ) {
160 return NULL;
161 } elseif ( count($matches) == 1 ) {
162 return $matches[0];
163 } else {
164 return $matches[1];
165 }
166 }
167
168
169 # Returns true if the text matches the word, and alters the
170 # input string, removing all instances of the word
171 function matchAndRemove( &$text ) {
172 global $wgMagicFound;
173 $wgMagicFound = false;
174 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
175 return $wgMagicFound;
176 }
177
178 function matchStartAndRemove( &$text ) {
179 global $wgMagicFound;
180 $wgMagicFound = false;
181 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
182 return $wgMagicFound;
183 }
184
185
186 # Replaces the word with something else
187 function replace( $replacement, $subject ) {
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 if ( $this->mVariableRegex == '' ) {
206 $this->initRegex();
207 }
208 return $this->mVariableRegex;
209 }
210
211 # Matches the entire string, where $1 is a wildcard
212 function getVariableStartToEndRegex() {
213 if ( $this->mVariableStartToEndRegex == '' ) {
214 $this->initRegex();
215 }
216 return $this->mVariableStartToEndRegex;
217 }
218
219 # Accesses the synonym list directly
220 function getSynonym( $i ) {
221 return $this->mSynonyms[$i];
222 }
223
224 # Returns true if the last call to replace() or substituteCallback()
225 # returned a modified text, otherwise false.
226 function getWasModified(){
227 return $this->mModified;
228 }
229
230 # $magicarr is an associative array of (magic word ID => replacement)
231 # This method uses the php feature to do several replacements at the same time,
232 # thereby gaining some efficiency. The result is placed in the out variable
233 # $result. The return value is true if something was replaced.
234
235 /* static */ function replaceMultiple( $magicarr, $subject, &$result ){
236 $search = array();
237 $replace = array();
238 foreach( $magicarr as $id => $replacement ){
239 $mw = MagicWord::get( $id );
240 $search[] = $mw->getRegex();
241 $replace[] = $replacement;
242 }
243
244 $result = preg_replace( $search, $replace, $subject );
245 return !($result === $subject);
246 }
247
248 # Adds all the synonyms of this MagicWord to an array, to allow quick lookup in a list of magic words
249 function addToArray( &$array, $value ) {
250 foreach ( $this->mSynonyms as $syn ) {
251 $array[$syn] = $value;
252 }
253 }
254 }
255
256 # Used in matchAndRemove()
257 /*private*/ function pregRemoveAndRecord( $match ) {
258 global $wgMagicFound;
259 $wgMagicFound = true;
260 return '';
261 }
262
263 ?>