Added description for two new tables
[mediawiki.git] / includes / MessageCache.php
blob9cab222ba5092b6f7610e4d60b04fbbc4cda0943
1 <?php
2 /**
4 * @package MediaWiki
5 * @subpackage Cache
6 */
8 /**
11 define( 'MSG_LOAD_TIMEOUT', 60);
12 define( 'MSG_LOCK_TIMEOUT', 10);
13 define( 'MSG_WAIT_TIMEOUT', 10);
15 /**
16 * Message cache
17 * Performs various useful MediaWiki namespace-related functions
19 * @package MediaWiki
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;
28 function __construct( &$memCached, $useDB, $expiry, $memcPrefix) {
29 wfProfileIn( __METHOD__ );
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 $this->mParser = null;
41 # When we first get asked for a message,
42 # then we'll fill up the cache. If we
43 # can return a cache hit, this saves
44 # some extra milliseconds
45 $this->mDeferred = true;
47 wfProfileOut( __METHOD__ );
50 function getParserOptions() {
51 if ( !$this->mParserOptions ) {
52 $this->mParserOptions = new ParserOptions;
54 return $this->mParserOptions;
57 /**
58 * Try to load the cache from a local file
60 function loadFromLocal( $hash ) {
61 global $wgLocalMessageCache;
63 $this->mCache = false;
64 if ( $wgLocalMessageCache === false ) {
65 return;
68 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
70 wfSuppressWarnings();
71 $file = fopen( $filename, 'r' );
72 wfRestoreWarnings();
73 if ( !$file ) {
74 return;
77 // Check to see if the file has the hash specified
78 $localHash = fread( $file, 32 );
79 if ( $hash == $localHash ) {
80 // All good, get the rest of it
81 $serialized = fread( $file, 10000000 );
82 $this->mCache = unserialize( $serialized );
84 fclose( $file );
87 /**
88 * Save the cache to a local file
90 function saveToLocal( $serialized, $hash ) {
91 global $wgLocalMessageCache;
93 if ( $wgLocalMessageCache === false ) {
94 return;
97 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
98 $oldUmask = umask( 0 );
99 wfMkdirParents( $wgLocalMessageCache, 0777 );
100 umask( $oldUmask );
102 $file = fopen( $filename, 'w' );
103 if ( !$file ) {
104 wfDebug( "Unable to open local cache file for writing\n" );
105 return;
108 fwrite( $file, $hash . $serialized );
109 fclose( $file );
110 @chmod( $filename, 0666 );
113 function loadFromScript( $hash ) {
114 global $wgLocalMessageCache;
115 if ( $wgLocalMessageCache === false ) {
116 return;
119 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
121 wfSuppressWarnings();
122 $file = fopen( $filename, 'r' );
123 wfRestoreWarnings();
124 if ( !$file ) {
125 return;
127 $localHash=substr(fread($file,40),8);
128 fclose($file);
129 if ($hash!=$localHash) {
130 return;
132 require("$wgLocalMessageCache/messages-" . wfWikiID());
135 function saveToScript($array, $hash) {
136 global $wgLocalMessageCache;
137 if ( $wgLocalMessageCache === false ) {
138 return;
141 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
142 $oldUmask = umask( 0 );
143 wfMkdirParents( $wgLocalMessageCache, 0777 );
144 umask( $oldUmask );
145 $file = fopen( $filename.'.tmp', 'w');
146 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
148 foreach ($array as $key => $message) {
149 fwrite($file, "'". $this->escapeForScript($key).
150 "' => '" . $this->escapeForScript($message).
151 "',\n");
153 fwrite($file,");\n?>");
154 fclose($file);
155 rename($filename.'.tmp',$filename);
158 function escapeForScript($string) {
159 $string = str_replace( '\\', '\\\\', $string );
160 $string = str_replace( '\'', '\\\'', $string );
161 return $string;
165 * Loads messages either from memcached or the database, if not disabled
166 * On error, quietly switches to a fallback mode
167 * Returns false for a reportable error, true otherwise
169 function load() {
170 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
172 if ( $this->mDisable ) {
173 static $shownDisabled = false;
174 if ( !$shownDisabled ) {
175 wfDebug( "MessageCache::load(): disabled\n" );
176 $shownDisabled = true;
178 return true;
180 $fname = 'MessageCache::load';
181 wfProfileIn( $fname );
182 $success = true;
184 if ( $this->mUseCache ) {
185 $this->mCache = false;
187 # Try local cache
188 wfProfileIn( $fname.'-fromlocal' );
189 $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
190 if ( $hash ) {
191 if ($wgLocalMessageCacheSerialized) {
192 $this->loadFromLocal( $hash );
193 } else {
194 $this->loadFromScript( $hash );
196 if ( $this->mCache ) {
197 wfDebug( "MessageCache::load(): got from local cache\n" );
200 wfProfileOut( $fname.'-fromlocal' );
202 # Try memcached
203 if ( !$this->mCache ) {
204 wfProfileIn( $fname.'-fromcache' );
205 $this->mCache = $this->mMemc->get( $this->mMemcKey );
206 if ( $this->mCache ) {
207 wfDebug( "MessageCache::load(): got from global cache\n" );
208 # Save to local cache
209 if ( $wgLocalMessageCache !== false ) {
210 $serialized = serialize( $this->mCache );
211 if ( !$hash ) {
212 $hash = md5( $serialized );
213 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
215 if ($wgLocalMessageCacheSerialized) {
216 $this->saveToLocal( $serialized,$hash );
217 } else {
218 $this->saveToScript( $this->mCache, $hash );
222 wfProfileOut( $fname.'-fromcache' );
226 # If there's nothing in memcached, load all the messages from the database
227 if ( !$this->mCache ) {
228 wfDebug( "MessageCache::load(): loading all messages\n" );
229 $this->lock();
230 # Other threads don't need to load the messages if another thread is doing it.
231 $success = $this->mMemc->add( $this->mMemcKey.'-status', "loading", MSG_LOAD_TIMEOUT );
232 if ( $success ) {
233 wfProfileIn( $fname.'-load' );
234 $this->loadFromDB();
235 wfProfileOut( $fname.'-load' );
237 # Save in memcached
238 # Keep trying if it fails, this is kind of important
239 wfProfileIn( $fname.'-save' );
240 for ($i=0; $i<20 &&
241 !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
242 $i++ ) {
243 usleep(mt_rand(500000,1500000));
246 # Save to local cache
247 if ( $wgLocalMessageCache !== false ) {
248 $serialized = serialize( $this->mCache );
249 $hash = md5( $serialized );
250 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
251 if ($wgLocalMessageCacheSerialized) {
252 $this->saveToLocal( $serialized,$hash );
253 } else {
254 $this->saveToScript( $this->mCache, $hash );
258 wfProfileOut( $fname.'-save' );
259 if ( $i == 20 ) {
260 $this->mMemc->set( $this->mMemcKey.'-status', 'error', 60*5 );
261 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
264 $this->unlock();
267 if ( !is_array( $this->mCache ) ) {
268 wfDebug( "MessageCache::load(): individual message mode\n" );
269 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
270 # Causing too much DB load, disabling -- TS
271 $this->mDisable = true;
273 if ( $this->mCache == "loading" ) {
274 $this->mUseCache = false;
275 } elseif ( $this->mCache == "error" ) {
276 $this->mUseCache = false;
277 $success = false;
278 } else {
279 $this->mDisable = true;
280 $success = false;
282 $this->mCache = false;
285 wfProfileOut( $fname );
286 $this->mDeferred = false;
287 return $success;
291 * Loads all or main part of cacheable messages from the database
293 function loadFromDB() {
294 global $wgLang;
296 $fname = 'MessageCache::loadFromDB';
297 $dbr =& wfGetDB( DB_SLAVE );
298 if ( !$dbr ) {
299 throw new MWException( 'Invalid database object' );
301 $conditions = array( 'page_is_redirect' => 0,
302 'page_namespace' => NS_MEDIAWIKI);
303 $res = $dbr->select( array( 'page', 'revision', 'text' ),
304 array( 'page_title', 'old_text', 'old_flags' ),
305 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI.' AND page_latest=rev_id AND rev_text_id=old_id',
306 $fname
309 $this->mCache = array();
310 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
311 $this->mCache[$row->page_title] = Revision::getRevisionText( $row );
314 # Negative caching
315 # Go through the language array and the extension array and make a note of
316 # any keys missing from the cache
317 $allMessages = Language::getMessagesFor( 'en' );
318 foreach ( $allMessages as $key => $value ) {
319 $uckey = $wgLang->ucfirst( $key );
320 if ( !array_key_exists( $uckey, $this->mCache ) ) {
321 $this->mCache[$uckey] = false;
325 # Make sure all extension messages are available
326 MessageCache::loadAllMessages();
328 # Add them to the cache
329 foreach ( $this->mExtensionMessages as $key => $value ) {
330 $uckey = $wgLang->ucfirst( $key );
331 if ( !array_key_exists( $uckey, $this->mCache ) &&
332 ( isset( $this->mExtensionMessages[$key][$wgLang->getCode()] ) || isset( $this->mExtensionMessages[$key]['en'] ) ) ) {
333 $this->mCache[$uckey] = false;
337 $dbr->freeResult( $res );
341 * Not really needed anymore
343 function getKeys() {
344 global $wgContLang;
345 if ( !$this->mKeys ) {
346 $this->mKeys = array();
347 $allMessages = Language::getMessagesFor( 'en' );
348 foreach ( $allMessages as $key => $value ) {
349 $title = $wgContLang->ucfirst( $key );
350 array_push( $this->mKeys, $title );
353 return $this->mKeys;
357 * @deprecated
359 function isCacheable( $key ) {
360 return true;
363 function replace( $title, $text ) {
364 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc;
366 $this->lock();
367 $this->load();
368 $parserMemc->delete(wfMemcKey('sidebar'));
369 if ( is_array( $this->mCache ) ) {
370 $this->mCache[$title] = $text;
371 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
373 # Save to local cache
374 if ( $wgLocalMessageCache !== false ) {
375 $serialized = serialize( $this->mCache );
376 $hash = md5( $serialized );
377 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
378 if ($wgLocalMessageCacheSerialized) {
379 $this->saveToLocal( $serialized,$hash );
380 } else {
381 $this->saveToScript( $this->mCache, $hash );
387 $this->unlock();
391 * Returns success
392 * Represents a write lock on the messages key
394 function lock() {
395 if ( !$this->mUseCache ) {
396 return true;
399 $lockKey = $this->mMemcKey . 'lock';
400 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
401 sleep(1);
404 return $i >= MSG_WAIT_TIMEOUT;
407 function unlock() {
408 if ( !$this->mUseCache ) {
409 return;
412 $lockKey = $this->mMemcKey . 'lock';
413 $this->mMemc->delete( $lockKey );
416 function get( $key, $useDB = true, $forcontent = true, $isfullkey = false ) {
417 global $wgContLanguageCode, $wgContLang, $wgLang;
418 if( $forcontent ) {
419 $lang =& $wgContLang;
420 } else {
421 $lang =& $wgLang;
423 $langcode = $lang->getCode();
424 # If uninitialised, someone is trying to call this halfway through Setup.php
425 if( !$this->mInitialised ) {
426 return '&lt;' . htmlspecialchars($key) . '&gt;';
428 # If cache initialization was deferred, start it now.
429 if( $this->mDeferred && !$this->mDisable && $useDB ) {
430 $this->load();
433 $message = false;
434 if( !$this->mDisable && $useDB ) {
435 $title = $wgContLang->ucfirst( $key );
436 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
437 $title .= '/' . $langcode;
439 $message = $this->getFromCache( $title );
441 # Try the extension array
442 if( $message === false && array_key_exists( $key, $this->mExtensionMessages ) ) {
443 if ( isset( $this->mExtensionMessages[$key][$langcode] ) ) {
444 $message = $this->mExtensionMessages[$key][$langcode];
445 } elseif ( isset( $this->mExtensionMessages[$key]['en'] ) ) {
446 $message = $this->mExtensionMessages[$key]['en'];
450 # Try the array in the language object
451 if( $message === false ) {
452 #wfDebug( "Trying language object for message $key\n" );
453 wfSuppressWarnings();
454 $message = $lang->getMessage( $key );
455 wfRestoreWarnings();
456 if ( is_null( $message ) ) {
457 $message = false;
461 # Try the English array
462 if( $message === false && $langcode != 'en' ) {
463 wfSuppressWarnings();
464 $message = Language::getMessage( $key );
465 wfRestoreWarnings();
466 if ( is_null( $message ) ) {
467 $message = false;
471 # Try the array of another language
472 if( $message === false && strpos( $key, '/' ) ) {
473 $message = explode( '/', $key );
474 if ( $message[1] ) {
475 wfSuppressWarnings();
476 $message = Language::getMessageFor( $message[0], $message[1] );
477 wfRestoreWarnings();
478 if ( is_null( $message ) ) {
479 $message = false;
481 } else {
482 $message = false;
486 # Is this a custom message? Try the default language in the db...
487 if( ($message === false || $message === '-' ) &&
488 !$this->mDisable && $useDB &&
489 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
490 $message = $this->getFromCache( $wgContLang->ucfirst( $key ) );
493 # Final fallback
494 if( $message === false ) {
495 return '&lt;' . htmlspecialchars($key) . '&gt;';
498 # Replace brace tags
499 $message = $this->transform( $message );
500 return $message;
503 function getFromCache( $title ) {
504 $message = false;
506 # Try the cache
507 if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
508 return $this->mCache[$title];
511 # Try individual message cache
512 if ( $this->mUseCache ) {
513 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
514 if ( $message == '###NONEXISTENT###' ) {
515 $this->mCache[$title] = false;
516 return false;
517 } elseif( !is_null( $message ) ) {
518 $this->mCache[$title] = $message;
519 return $message;
520 } else {
521 $message = false;
525 # Call message Hooks, in case they are defined
526 wfRunHooks('MessagesPreLoad',array($title,&$message));
528 # If it wasn't in the cache, load each message from the DB individually
529 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
530 if( $revision ) {
531 $message = $revision->getText();
532 if ($this->mUseCache) {
533 $this->mCache[$title]=$message;
534 /* individual messages may be often
535 recached until proper purge code exists
537 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
539 } else {
540 # Negative caching
541 # Use some special text instead of false, because false gets converted to '' somewhere
542 $this->mMemc->set( $this->mMemcKey . ':' . $title, '###NONEXISTENT###', $this->mExpiry );
543 $this->mCache[$title] = false;
546 return $message;
549 function transform( $message ) {
550 global $wgParser;
551 if ( !$this->mParser && isset( $wgParser ) ) {
552 # Do some initialisation so that we don't have to do it twice
553 $wgParser->firstCallInit();
554 # Clone it and store it
555 $this->mParser = clone $wgParser;
557 if ( !$this->mDisableTransform && $this->mParser ) {
558 if( strpos( $message, '{{' ) !== false ) {
559 $message = $this->mParser->transformMsg( $message, $this->getParserOptions() );
562 return $message;
565 function disable() { $this->mDisable = true; }
566 function enable() { $this->mDisable = false; }
567 function disableTransform() { $this->mDisableTransform = true; }
568 function enableTransform() { $this->mDisableTransform = false; }
569 function setTransform( $x ) { $this->mDisableTransform = $x; }
570 function getTransform() { return $this->mDisableTransform; }
573 * Add a message to the cache
575 * @param mixed $key
576 * @param mixed $value
577 * @param string $lang The messages language, English by default
579 function addMessage( $key, $value, $lang = 'en' ) {
580 $this->mExtensionMessages[$key][$lang] = $value;
584 * Add an associative array of message to the cache
586 * @param array $messages An associative array of key => values to be added
587 * @param string $lang The messages language, English by default
589 function addMessages( $messages, $lang = 'en' ) {
590 wfProfileIn( __METHOD__ );
591 foreach ( $messages as $key => $value ) {
592 $this->addMessage( $key, $value, $lang );
594 wfProfileOut( __METHOD__ );
598 * Get the extension messages for a specific language
600 * @param string $lang The messages language, English by default
602 function getExtensionMessagesFor( $lang = 'en' ) {
603 wfProfileIn( __METHOD__ );
604 $messages = array();
605 foreach( $this->mExtensionMessages as $key => $message ) {
606 if ( isset( $message[$lang] ) ) {
607 $messages[$key] = $message[$lang];
608 } elseif ( isset( $message['en'] ) ) {
609 $messages[$key] = $message['en'];
612 wfProfileOut( __METHOD__ );
613 return $messages;
617 * Clear all stored messages. Mainly used after a mass rebuild.
619 function clear() {
620 global $wgLocalMessageCache;
621 if( $this->mUseCache ) {
622 # Global cache
623 $this->mMemc->delete( $this->mMemcKey );
624 # Invalidate all local caches
625 $this->mMemc->delete( "{$this->mMemcKey}-hash" );
629 static function loadAllMessages() {
630 # Some extensions will load their messages when you load their class file
631 wfLoadAllExtensions();
632 # Others will respond to this hook
633 wfRunHooks( 'LoadAllMessages' );
634 # Still others will respond to neither, they are EVIL. We sometimes need to know!