* (bug 6618) Improve permissions/error detection in Special:Lockdb
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /**
9 *
10 */
11 define( 'MSG_LOAD_TIMEOUT', 60);
12 define( 'MSG_LOCK_TIMEOUT', 10);
13 define( 'MSG_WAIT_TIMEOUT', 10);
14
15 /**
16 * Message cache
17 * Performs various useful MediaWiki namespace-related functions
18 *
19 * @package MediaWiki
20 */
21 class MessageCache {
22 var $mCache, $mUseCache, $mDisable, $mExpiry;
23 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
24 var $mExtensionMessages = array();
25 var $mInitialised = false;
26 var $mDeferred = true;
27
28 function __construct( &$memCached, $useDB, $expiry, $memcPrefix) {
29 wfProfileIn( __METHOD__ );
30
31 $this->mUseCache = !is_null( $memCached );
32 $this->mMemc = &$memCached;
33 $this->mDisable = !$useDB;
34 $this->mExpiry = $expiry;
35 $this->mDisableTransform = false;
36 $this->mMemcKey = $memcPrefix.':messages';
37 $this->mKeys = false; # initialised on demand
38 $this->mInitialised = true;
39
40 wfProfileIn( __METHOD__.'-parseropt' );
41 $this->mParserOptions = new ParserOptions( $u=NULL );
42 wfProfileOut( __METHOD__.'-parseropt' );
43 $this->mParser = null;
44
45 # When we first get asked for a message,
46 # then we'll fill up the cache. If we
47 # can return a cache hit, this saves
48 # some extra milliseconds
49 $this->mDeferred = true;
50
51 wfProfileOut( __METHOD__ );
52 }
53
54 /**
55 * Try to load the cache from a local file
56 */
57 function loadFromLocal( $hash ) {
58 global $wgLocalMessageCache, $wgDBname;
59
60 $this->mCache = false;
61 if ( $wgLocalMessageCache === false ) {
62 return;
63 }
64
65 $filename = "$wgLocalMessageCache/messages-$wgDBname";
66
67 wfSuppressWarnings();
68 $file = fopen( $filename, 'r' );
69 wfRestoreWarnings();
70 if ( !$file ) {
71 return;
72 }
73
74 // Check to see if the file has the hash specified
75 $localHash = fread( $file, 32 );
76 if ( $hash == $localHash ) {
77 // All good, get the rest of it
78 $serialized = fread( $file, 1000000 );
79 $this->mCache = unserialize( $serialized );
80 }
81 fclose( $file );
82 }
83
84 /**
85 * Save the cache to a local file
86 */
87 function saveToLocal( $serialized, $hash ) {
88 global $wgLocalMessageCache, $wgDBname;
89
90 if ( $wgLocalMessageCache === false ) {
91 return;
92 }
93
94 $filename = "$wgLocalMessageCache/messages-$wgDBname";
95 $oldUmask = umask( 0 );
96 wfMkdirParents( $wgLocalMessageCache, 0777 );
97 umask( $oldUmask );
98
99 $file = fopen( $filename, 'w' );
100 if ( !$file ) {
101 wfDebug( "Unable to open local cache file for writing\n" );
102 return;
103 }
104
105 fwrite( $file, $hash . $serialized );
106 fclose( $file );
107 @chmod( $filename, 0666 );
108 }
109
110 function loadFromScript( $hash ) {
111 global $wgLocalMessageCache, $wgDBname;
112 if ( $wgLocalMessageCache === false ) {
113 return;
114 }
115
116 $filename = "$wgLocalMessageCache/messages-$wgDBname";
117
118 wfSuppressWarnings();
119 $file = fopen( $filename, 'r' );
120 wfRestoreWarnings();
121 if ( !$file ) {
122 return;
123 }
124 $localHash=substr(fread($file,40),8);
125 fclose($file);
126 if ($hash!=$localHash) {
127 return;
128 }
129 require("$wgLocalMessageCache/messages-$wgDBname");
130 }
131
132 function saveToScript($array, $hash) {
133 global $wgLocalMessageCache, $wgDBname;
134 if ( $wgLocalMessageCache === false ) {
135 return;
136 }
137
138 $filename = "$wgLocalMessageCache/messages-$wgDBname";
139 $oldUmask = umask( 0 );
140 wfMkdirParents( $wgLocalMessageCache, 0777 );
141 umask( $oldUmask );
142 $file = fopen( $filename.'.tmp', 'w');
143 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
144
145 foreach ($array as $key => $message) {
146 fwrite($file, "'". $this->escapeForScript($key).
147 "' => '" . $this->escapeForScript($message).
148 "',\n");
149 }
150 fwrite($file,");\n?>");
151 fclose($file);
152 rename($filename.'.tmp',$filename);
153 }
154
155 function escapeForScript($string) {
156 $string = str_replace( '\\', '\\\\', $string );
157 $string = str_replace( '\'', '\\\'', $string );
158 return $string;
159 }
160
161 /**
162 * Loads messages either from memcached or the database, if not disabled
163 * On error, quietly switches to a fallback mode
164 * Returns false for a reportable error, true otherwise
165 */
166 function load() {
167 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
168
169 if ( $this->mDisable ) {
170 static $shownDisabled = false;
171 if ( !$shownDisabled ) {
172 wfDebug( "MessageCache::load(): disabled\n" );
173 $shownDisabled = true;
174 }
175 return true;
176 }
177 $fname = 'MessageCache::load';
178 wfProfileIn( $fname );
179 $success = true;
180
181 if ( $this->mUseCache ) {
182 $this->mCache = false;
183
184 # Try local cache
185 wfProfileIn( $fname.'-fromlocal' );
186 $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
187 if ( $hash ) {
188 if ($wgLocalMessageCacheSerialized) {
189 $this->loadFromLocal( $hash );
190 } else {
191 $this->loadFromScript( $hash );
192 }
193 }
194 wfProfileOut( $fname.'-fromlocal' );
195
196 # Try memcached
197 if ( !$this->mCache ) {
198 wfProfileIn( $fname.'-fromcache' );
199 $this->mCache = $this->mMemc->get( $this->mMemcKey );
200
201 # Save to local cache
202 if ( $wgLocalMessageCache !== false ) {
203 $serialized = serialize( $this->mCache );
204 if ( !$hash ) {
205 $hash = md5( $serialized );
206 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
207 }
208 if ($wgLocalMessageCacheSerialized) {
209 $this->saveToLocal( $serialized,$hash );
210 } else {
211 $this->saveToScript( $this->mCache, $hash );
212 }
213 }
214 wfProfileOut( $fname.'-fromcache' );
215 }
216
217
218 # If there's nothing in memcached, load all the messages from the database
219 if ( !$this->mCache ) {
220 wfDebug( "MessageCache::load(): loading all messages\n" );
221 $this->lock();
222 # Other threads don't need to load the messages if another thread is doing it.
223 $success = $this->mMemc->add( $this->mMemcKey.'-status', "loading", MSG_LOAD_TIMEOUT );
224 if ( $success ) {
225 wfProfileIn( $fname.'-load' );
226 $this->loadFromDB();
227 wfProfileOut( $fname.'-load' );
228
229 # Save in memcached
230 # Keep trying if it fails, this is kind of important
231 wfProfileIn( $fname.'-save' );
232 for ($i=0; $i<20 &&
233 !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
234 $i++ ) {
235 usleep(mt_rand(500000,1500000));
236 }
237
238 # Save to local cache
239 if ( $wgLocalMessageCache !== false ) {
240 $serialized = serialize( $this->mCache );
241 $hash = md5( $serialized );
242 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
243 if ($wgLocalMessageCacheSerialized) {
244 $this->saveToLocal( $serialized,$hash );
245 } else {
246 $this->saveToScript( $this->mCache, $hash );
247 }
248 }
249
250 wfProfileOut( $fname.'-save' );
251 if ( $i == 20 ) {
252 $this->mMemc->set( $this->mMemcKey.'-status', 'error', 60*5 );
253 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
254 }
255 }
256 $this->unlock();
257 }
258
259 if ( !is_array( $this->mCache ) ) {
260 wfDebug( "MessageCache::load(): individual message mode\n" );
261 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
262 # Causing too much DB load, disabling -- TS
263 $this->mDisable = true;
264 /*
265 if ( $this->mCache == "loading" ) {
266 $this->mUseCache = false;
267 } elseif ( $this->mCache == "error" ) {
268 $this->mUseCache = false;
269 $success = false;
270 } else {
271 $this->mDisable = true;
272 $success = false;
273 }*/
274 $this->mCache = false;
275 }
276 }
277 wfProfileOut( $fname );
278 $this->mDeferred = false;
279 return $success;
280 }
281
282 /**
283 * Loads all or main part of cacheable messages from the database
284 */
285 function loadFromDB() {
286 global $wgAllMessagesEn, $wgLang;
287
288 $fname = 'MessageCache::loadFromDB';
289 $dbr =& wfGetDB( DB_SLAVE );
290 if ( !$dbr ) {
291 throw new MWException( 'Invalid database object' );
292 }
293 $conditions = array( 'page_is_redirect' => 0,
294 'page_namespace' => NS_MEDIAWIKI);
295 $res = $dbr->select( array( 'page', 'revision', 'text' ),
296 array( 'page_title', 'old_text', 'old_flags' ),
297 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI.' AND page_latest=rev_id AND rev_text_id=old_id',
298 $fname
299 );
300
301 $this->mCache = array();
302 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
303 $this->mCache[$row->page_title] = Revision::getRevisionText( $row );
304 }
305
306 # Negative caching
307 # Go through the language array and the extension array and make a note of
308 # any keys missing from the cache
309 foreach ( $wgAllMessagesEn as $key => $value ) {
310 $uckey = $wgLang->ucfirst( $key );
311 if ( !array_key_exists( $uckey, $this->mCache ) ) {
312 $this->mCache[$uckey] = false;
313 }
314 }
315
316 # Make sure all extension messages are available
317 wfLoadAllExtensions();
318
319 # Add them to the cache
320 foreach ( $this->mExtensionMessages as $key => $value ) {
321 $uckey = $wgLang->ucfirst( $key );
322 if ( !array_key_exists( $uckey, $this->mCache ) &&
323 ( isset( $this->mExtensionMessages[$key][$wgLang->getCode()] ) || isset( $this->mExtensionMessages[$key]['en'] ) ) ) {
324 $this->mCache[$uckey] = false;
325 }
326 }
327
328 $dbr->freeResult( $res );
329 }
330
331 /**
332 * Not really needed anymore
333 */
334 function getKeys() {
335 global $wgAllMessagesEn, $wgContLang;
336 if ( !$this->mKeys ) {
337 $this->mKeys = array();
338 foreach ( $wgAllMessagesEn as $key => $value ) {
339 $title = $wgContLang->ucfirst( $key );
340 array_push( $this->mKeys, $title );
341 }
342 }
343 return $this->mKeys;
344 }
345
346 /**
347 * @deprecated
348 */
349 function isCacheable( $key ) {
350 return true;
351 }
352
353 function replace( $title, $text ) {
354 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc, $wgDBname;
355
356 $this->lock();
357 $this->load();
358 $parserMemc->delete("$wgDBname:sidebar");
359 if ( is_array( $this->mCache ) ) {
360 $this->mCache[$title] = $text;
361 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
362
363 # Save to local cache
364 if ( $wgLocalMessageCache !== false ) {
365 $serialized = serialize( $this->mCache );
366 $hash = md5( $serialized );
367 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
368 if ($wgLocalMessageCacheSerialized) {
369 $this->saveToLocal( $serialized,$hash );
370 } else {
371 $this->saveToScript( $this->mCache, $hash );
372 }
373 }
374
375
376 }
377 $this->unlock();
378 }
379
380 /**
381 * Returns success
382 * Represents a write lock on the messages key
383 */
384 function lock() {
385 if ( !$this->mUseCache ) {
386 return true;
387 }
388
389 $lockKey = $this->mMemcKey . 'lock';
390 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
391 sleep(1);
392 }
393
394 return $i >= MSG_WAIT_TIMEOUT;
395 }
396
397 function unlock() {
398 if ( !$this->mUseCache ) {
399 return;
400 }
401
402 $lockKey = $this->mMemcKey . 'lock';
403 $this->mMemc->delete( $lockKey );
404 }
405
406 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
407 global $wgContLanguageCode;
408 if( $forcontent ) {
409 global $wgContLang;
410 $lang =& $wgContLang;
411 $langcode = $wgContLanguageCode;
412 } else {
413 global $wgLang, $wgLanguageCode;
414 $lang =& $wgLang;
415 $langcode = $wgLanguageCode;
416 }
417 # If uninitialised, someone is trying to call this halfway through Setup.php
418 if( !$this->mInitialised ) {
419 return '&lt;' . htmlspecialchars($key) . '&gt;';
420 }
421 # If cache initialization was deferred, start it now.
422 if( $this->mDeferred && !$this->mDisable && $useDB ) {
423 $this->load();
424 }
425
426 $message = false;
427 if( !$this->mDisable && $useDB ) {
428 $title = $lang->ucfirst( $key );
429 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
430 $title .= '/' . $langcode;
431 }
432 $message = $this->getFromCache( $title );
433 }
434 # Try the extension array
435 if( $message === false && array_key_exists( $key, $this->mExtensionMessages ) ) {
436 if ( isset( $this->mExtensionMessages[$key][$langcode] ) ) {
437 $message = $this->mExtensionMessages[$key][$langcode];
438 } elseif ( isset( $this->mExtensionMessages[$key]['en'] ) ) {
439 $message = $this->mExtensionMessages[$key]['en'];
440 }
441 }
442
443 # Try the array in the language object
444 if( $message === false ) {
445 wfSuppressWarnings();
446 $message = $lang->getMessage( $key );
447 wfRestoreWarnings();
448 if ( is_null( $message ) ) {
449 $message = false;
450 }
451 }
452
453 # Try the English array
454 if( $message === false && $langcode != 'en' ) {
455 wfSuppressWarnings();
456 $message = Language::getMessage( $key );
457 wfRestoreWarnings();
458 if ( is_null( $message ) ) {
459 $message = false;
460 }
461 }
462
463 # Is this a custom message? Try the default language in the db...
464 if( ($message === false || $message === '-' ) &&
465 !$this->mDisable && $useDB &&
466 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
467 $message = $this->getFromCache( $lang->ucfirst( $key ) );
468 }
469
470 # Final fallback
471 if( $message === false ) {
472 return '&lt;' . htmlspecialchars($key) . '&gt;';
473 }
474
475 # Replace brace tags
476 $message = $this->transform( $message );
477 return $message;
478 }
479
480 function getFromCache( $title ) {
481 $message = false;
482
483 # Try the cache
484 if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
485 return $this->mCache[$title];
486 }
487
488 # Try individual message cache
489 if ( $this->mUseCache ) {
490 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
491 if ( $message == '###NONEXISTENT###' ) {
492 return false;
493 } elseif( !is_null( $message ) ) {
494 $this->mCache[$title] = $message;
495 return $message;
496 } else {
497 $message = false;
498 }
499 }
500
501 # Call message Hooks, in case they are defined
502 wfRunHooks('MessagesPreLoad',array($title,&$message));
503
504 # If it wasn't in the cache, load each message from the DB individually
505 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
506 if( $revision ) {
507 $message = $revision->getText();
508 if ($this->mUseCache) {
509 $this->mCache[$title]=$message;
510 /* individual messages may be often
511 recached until proper purge code exists
512 */
513 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
514 }
515 } else {
516 # Negative caching
517 # Use some special text instead of false, because false gets converted to '' somewhere
518 $this->mMemc->set( $this->mMemcKey . ':' . $title, '###NONEXISTENT###', $this->mExpiry );
519 }
520
521 return $message;
522 }
523
524 function transform( $message ) {
525 global $wgParser;
526 if ( !$this->mParser && isset( $wgParser ) ) {
527 # Do some initialisation so that we don't have to do it twice
528 $wgParser->firstCallInit();
529 # Clone it and store it
530 $this->mParser = clone $wgParser;
531 }
532 if ( !$this->mDisableTransform && $this->mParser ) {
533 if( strpos( $message, '{{' ) !== false ) {
534 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
535 }
536 }
537 return $message;
538 }
539
540 function disable() { $this->mDisable = true; }
541 function enable() { $this->mDisable = false; }
542 function disableTransform() { $this->mDisableTransform = true; }
543 function enableTransform() { $this->mDisableTransform = false; }
544 function setTransform( $x ) { $this->mDisableTransform = $x; }
545 function getTransform() { return $this->mDisableTransform; }
546
547 /**
548 * Add a message to the cache
549 *
550 * @param mixed $key
551 * @param mixed $value
552 * @param string $lang The messages language, English by default
553 */
554 function addMessage( $key, $value, $lang = 'en' ) {
555 $this->mExtensionMessages[$key][$lang] = $value;
556 }
557
558 /**
559 * Add an associative array of message to the cache
560 *
561 * @param array $messages An associative array of key => values to be added
562 * @param string $lang The messages language, English by default
563 */
564 function addMessages( $messages, $lang = 'en' ) {
565 wfProfileIn( __METHOD__ );
566 foreach ( $messages as $key => $value ) {
567 $this->addMessage( $key, $value, $lang );
568 }
569 wfProfileOut( __METHOD__ );
570 }
571
572 /**
573 * Clear all stored messages. Mainly used after a mass rebuild.
574 */
575 function clear() {
576 if( $this->mUseCache ) {
577 $this->mMemc->delete( $this->mMemcKey );
578 }
579 }
580 }
581 ?>