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