fixed potential XSS vulnerability
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /** */
9 require_once( 'Revision.php' );
10
11 /**
12 *
13 */
14 define( 'MSG_LOAD_TIMEOUT', 60);
15 define( 'MSG_LOCK_TIMEOUT', 10);
16 define( 'MSG_WAIT_TIMEOUT', 10);
17
18 /**
19 * Message cache
20 * Performs various useful MediaWiki namespace-related functions
21 *
22 * @package MediaWiki
23 */
24 class MessageCache
25 {
26 var $mCache, $mUseCache, $mDisable, $mExpiry;
27 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
28 var $mExtensionMessages;
29 var $mInitialised = false;
30 var $mDeferred = true;
31
32 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
33 $fname = 'MessageCache::initialise';
34 wfProfileIn( $fname );
35
36 $this->mUseCache = !is_null( $memCached );
37 $this->mMemc = &$memCached;
38 $this->mDisable = !$useDB;
39 $this->mExpiry = $expiry;
40 $this->mDisableTransform = false;
41 $this->mMemcKey = $memcPrefix.':messages';
42 $this->mKeys = false; # initialised on demand
43 $this->mInitialised = true;
44
45 wfProfileIn( $fname.'-parseropt' );
46 $this->mParserOptions = ParserOptions::newFromUser( $u=NULL );
47 wfProfileOut( $fname.'-parseropt' );
48 wfProfileIn( $fname.'-parser' );
49 $this->mParser = new Parser;
50 wfProfileOut( $fname.'-parser' );
51
52 # When we first get asked for a message,
53 # then we'll fill up the cache. If we
54 # can return a cache hit, this saves
55 # some extra milliseconds
56 $this->mDeferred = true;
57
58 wfProfileOut( $fname );
59 }
60
61 /**
62 * Loads messages either from memcached or the database, if not disabled
63 * On error, quietly switches to a fallback mode
64 * Returns false for a reportable error, true otherwise
65 */
66 function load() {
67 global $wgAllMessagesEn;
68
69 if ( $this->mDisable ) {
70 wfDebug( "MessageCache::load(): disabled\n" );
71 return true;
72 }
73 $fname = 'MessageCache::load';
74 wfProfileIn( $fname );
75 $success = true;
76
77 if ( $this->mUseCache ) {
78 wfProfileIn( $fname.'-fromcache' );
79 $this->mCache = $this->mMemc->get( $this->mMemcKey );
80 wfProfileOut( $fname.'-fromcache' );
81
82 # If there's nothing in memcached, load all the messages from the database
83 if ( !$this->mCache ) {
84 wfDebug( "MessageCache::load(): loading all messages\n" );
85 $this->lock();
86 # Other threads don't need to load the messages if another thread is doing it.
87 $success = $this->mMemc->add( $this->mMemcKey, "loading", MSG_LOAD_TIMEOUT );
88 if ( $success ) {
89 wfProfileIn( $fname.'-load' );
90 $this->loadFromDB();
91 wfProfileOut( $fname.'-load' );
92 # Save in memcached
93 # Keep trying if it fails, this is kind of important
94 wfProfileIn( $fname.'-save' );
95 for ( $i=0; $i<20 && !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ); $i++ ) {
96 usleep(mt_rand(500000,1500000));
97 }
98 wfProfileOut( $fname.'-save' );
99 if ( $i == 20 ) {
100 $this->mMemc->set( $this->mMemcKey, 'error', 60*5 );
101 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
102 }
103 }
104 $this->unlock();
105 }
106
107 if ( !is_array( $this->mCache ) ) {
108 wfDebug( "MessageCache::load(): individual message mode\n" );
109 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
110 # Causing too much DB load, disabling -- TS
111 $this->mDisable = true;
112 /*
113 if ( $this->mCache == "loading" ) {
114 $this->mUseCache = false;
115 } elseif ( $this->mCache == "error" ) {
116 $this->mUseCache = false;
117 $success = false;
118 } else {
119 $this->mDisable = true;
120 $success = false;
121 }*/
122 $this->mCache = false;
123 }
124 }
125 wfProfileOut( $fname );
126 $this->mDeferred = false;
127 return $success;
128 }
129
130 /**
131 * Loads all or main part of cacheable messages from the database
132 */
133 function loadFromDB() {
134 $fname = 'MessageCache::loadFromDB';
135 $dbr =& wfGetDB( DB_SLAVE );
136 $conditions = array( 'page_is_redirect' => 0,
137 'page_namespace' => NS_MEDIAWIKI);
138 $res = $dbr->select( array( 'page', 'revision', 'text' ),
139 array( 'page_title', 'old_text', 'old_flags' ),
140 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI.' AND page_latest=rev_id AND rev_text_id=old_id',
141 $fname
142 );
143
144 $this->mCache = array();
145 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
146 $this->mCache[$row->page_title] = Revision::getRevisionText( $row );
147 }
148
149 $dbr->freeResult( $res );
150 /*
151 # FIXME: This is too slow currently.
152 # We need to bulk-fetch revisions, but in a portable way...
153 $resultSet = Revision::fetchFromConds( $dbr, array(
154 'page_namespace' => NS_MEDIAWIKI,
155 'page_is_redirect' => 0,
156 'page_id=rev_page' ) );
157 while( $row = $resultSet->fetchObject() ) {
158 $revision = new Revision( $row );
159 $title = $revision->getTitle();
160 $this->mCache[$title->getDBkey()] = $revision->getText();
161 }
162 $resultSet->free();
163 */
164 }
165
166 /**
167 * Not really needed anymore
168 */
169 function getKeys() {
170 global $wgAllMessagesEn, $wgContLang;
171 if ( !$this->mKeys ) {
172 $this->mKeys = array();
173 foreach ( $wgAllMessagesEn as $key => $value ) {
174 $title = $wgContLang->ucfirst( $key );
175 array_push( $this->mKeys, $title );
176 }
177 }
178 return $this->mKeys;
179 }
180
181 /**
182 * Obsolete
183 */
184 function isCacheable( $key ) {
185 return true;
186 }
187
188 function replace( $title, $text ) {
189 $this->lock();
190 $this->load();
191 if ( is_array( $this->mCache ) ) {
192 $this->mCache[$title] = $text;
193 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
194 }
195 $this->unlock();
196 }
197
198 /**
199 * Returns success
200 * Represents a write lock on the messages key
201 */
202 function lock() {
203 if ( !$this->mUseCache ) {
204 return true;
205 }
206
207 $lockKey = $this->mMemcKey . 'lock';
208 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
209 sleep(1);
210 }
211
212 return $i >= MSG_WAIT_TIMEOUT;
213 }
214
215 function unlock() {
216 if ( !$this->mUseCache ) {
217 return;
218 }
219
220 $lockKey = $this->mMemcKey . 'lock';
221 $this->mMemc->delete( $lockKey );
222 }
223
224 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
225 global $wgContLanguageCode;
226 if( $forcontent ) {
227 global $wgContLang;
228 $lang =& $wgContLang;
229 $langcode = $wgContLanguageCode;
230 } else {
231 global $wgLang, $wgLanguageCode;
232 $lang =& $wgLang;
233 $langcode = $wgLanguageCode;
234 }
235 # If uninitialised, someone is trying to call this halfway through Setup.php
236 if( !$this->mInitialised ) {
237 return '&lt;' . htmlspecialchars($key) . '&gt;';
238 }
239 # If cache initialization was deferred, start it now.
240 if( $this->mDeferred ) {
241 $this->load();
242 }
243
244 $message = false;
245 if( !$this->mDisable && $useDB ) {
246 $title = $lang->ucfirst( $key );
247 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
248 $title .= '/' . $langcode;
249 }
250 $message = $this->getFromCache( $title );
251 }
252 # Try the extension array
253 if( !$message ) {
254 $message = @$this->mExtensionMessages[$key];
255 }
256
257 # Try the array in the language object
258 if( !$message ) {
259 wfSuppressWarnings();
260 $message = $lang->getMessage( $key );
261 wfRestoreWarnings();
262 }
263
264 # Try the English array
265 if( !$message && $langcode != 'en' ) {
266 wfSuppressWarnings();
267 $message = Language::getMessage( $key );
268 wfRestoreWarnings();
269 }
270
271 # Is this a custom message? Try the default language in the db...
272 if( !$message &&
273 !$this->mDisable && $useDB &&
274 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
275 $message = $this->getFromCache( $lang->ucfirst( $key ) );
276 }
277
278 # Final fallback
279 if( !$message ) {
280 return '&lt;' . htmlspecialchars($key) . '&gt;';
281 }
282
283 # Replace brace tags
284 $message = $this->transform( $message );
285 return $message;
286 }
287
288 function getFromCache( $title ) {
289 $message = false;
290
291 # Try the cache
292 if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
293 $message = $this->mCache[$title];
294 }
295
296 if ( !$message && $this->mUseCache ) {
297 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
298 if( $message ) {
299 $this->mCache[$title] = $message;
300 }
301 }
302
303 # If it wasn't in the cache, load each message from the DB individually
304 if ( !$message ) {
305 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
306 if( $revision ) {
307 $message = $revision->getText();
308 if ($this->mUseCache) {
309 $this->mCache[$title]=$message;
310 /* individual messages may be often
311 recached until proper purge code exists
312 */
313 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
314 }
315 }
316 }
317
318 return $message;
319 }
320
321 function transform( $message ) {
322 if( !$this->mDisableTransform ) {
323 if( strpos( $message, '{{' ) !== false ) {
324 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
325 }
326 }
327 return $message;
328 }
329
330 function disable() { $this->mDisable = true; }
331 function enable() { $this->mDisable = false; }
332 function disableTransform() { $this->mDisableTransform = true; }
333 function enableTransform() { $this->mDisableTransform = false; }
334
335 function addMessage( $key, $value ) {
336 $this->mExtensionMessages[$key] = $value;
337 }
338
339 function addMessages( $messages ) {
340 foreach ( $messages as $key => $value ) {
341 $this->mExtensionMessages[$key] = $value;
342 }
343 }
344
345 /**
346 * Clear all stored messages. Mainly used after a mass rebuild.
347 */
348 function clear() {
349 if( $this->mUseCache ) {
350 $this->mMemc->delete( $this->mMemcKey );
351 }
352 }
353 }
354 ?>