* Requested by LeonWP
[mediawiki.git] / includes / MessageCache.php
blobdbae08d086b00d8943f8e74619953c9463b81ea7
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 $res = $dbr->select( array( 'page', 'revision', 'text' ),
302 array( 'page_title', 'old_text', 'old_flags' ),
303 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI.' AND page_latest=rev_id AND rev_text_id=old_id',
304 $fname
307 $this->mCache = array();
308 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
309 $this->mCache[$row->page_title] = Revision::getRevisionText( $row );
312 # Negative caching
313 # Go through the language array and the extension array and make a note of
314 # any keys missing from the cache
315 $allMessages = Language::getMessagesFor( 'en' );
316 foreach ( $allMessages as $key => $unused ) {
317 $uckey = $wgLang->ucfirst( $key );
318 if ( !array_key_exists( $uckey, $this->mCache ) ) {
319 $this->mCache[$uckey] = false;
323 # Make sure all extension messages are available
324 MessageCache::loadAllMessages();
326 # Add them to the cache
327 foreach ( $this->mExtensionMessages as $key => $unused ) {
328 $uckey = $wgLang->ucfirst( $key );
329 if ( !array_key_exists( $uckey, $this->mCache ) &&
330 ( isset( $this->mExtensionMessages[$key][$wgLang->getCode()] ) || isset( $this->mExtensionMessages[$key]['en'] ) ) ) {
331 $this->mCache[$uckey] = false;
335 $dbr->freeResult( $res );
339 * Not really needed anymore
341 function getKeys() {
342 global $wgContLang;
343 if ( !$this->mKeys ) {
344 $this->mKeys = array();
345 $allMessages = Language::getMessagesFor( 'en' );
346 foreach ( $allMessages as $key => $unused ) {
347 $title = $wgContLang->ucfirst( $key );
348 array_push( $this->mKeys, $title );
351 return $this->mKeys;
355 * @deprecated
357 function isCacheable( $key ) {
358 return true;
361 function replace( $title, $text ) {
362 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc;
364 $this->lock();
365 $this->load();
366 $parserMemc->delete(wfMemcKey('sidebar'));
367 if ( is_array( $this->mCache ) ) {
368 $this->mCache[$title] = $text;
369 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
371 # Save to local cache
372 if ( $wgLocalMessageCache !== false ) {
373 $serialized = serialize( $this->mCache );
374 $hash = md5( $serialized );
375 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
376 if ($wgLocalMessageCacheSerialized) {
377 $this->saveToLocal( $serialized,$hash );
378 } else {
379 $this->saveToScript( $this->mCache, $hash );
385 $this->unlock();
389 * Returns success
390 * Represents a write lock on the messages key
392 function lock() {
393 if ( !$this->mUseCache ) {
394 return true;
397 $lockKey = $this->mMemcKey . 'lock';
398 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
399 sleep(1);
402 return $i >= MSG_WAIT_TIMEOUT;
405 function unlock() {
406 if ( !$this->mUseCache ) {
407 return;
410 $lockKey = $this->mMemcKey . 'lock';
411 $this->mMemc->delete( $lockKey );
414 function get( $key, $useDB = true, $forcontent = true, $isfullkey = false ) {
415 global $wgContLanguageCode, $wgContLang, $wgLang;
416 if( $forcontent ) {
417 $lang =& $wgContLang;
418 } else {
419 $lang =& $wgLang;
421 $langcode = $lang->getCode();
422 # If uninitialised, someone is trying to call this halfway through Setup.php
423 if( !$this->mInitialised ) {
424 return '&lt;' . htmlspecialchars($key) . '&gt;';
426 # If cache initialization was deferred, start it now.
427 if( $this->mDeferred && !$this->mDisable && $useDB ) {
428 $this->load();
431 $message = false;
432 if( !$this->mDisable && $useDB ) {
433 $title = $wgContLang->ucfirst( $key );
434 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
435 $title .= '/' . $langcode;
437 $message = $this->getFromCache( $title );
439 # Try the extension array
440 if( $message === false && array_key_exists( $key, $this->mExtensionMessages ) ) {
441 if ( isset( $this->mExtensionMessages[$key][$langcode] ) ) {
442 $message = $this->mExtensionMessages[$key][$langcode];
443 } elseif ( isset( $this->mExtensionMessages[$key]['en'] ) ) {
444 $message = $this->mExtensionMessages[$key]['en'];
448 # Try the array in the language object
449 if( $message === false ) {
450 #wfDebug( "Trying language object for message $key\n" );
451 wfSuppressWarnings();
452 $message = $lang->getMessage( $key );
453 wfRestoreWarnings();
454 if ( is_null( $message ) ) {
455 $message = false;
459 # Try the array of another language
460 if( $message === false && strpos( $key, '/' ) ) {
461 $message = explode( '/', $key );
462 if ( $message[1] ) {
463 wfSuppressWarnings();
464 $message = Language::getMessageFor( $message[0], $message[1] );
465 wfRestoreWarnings();
466 if ( is_null( $message ) ) {
467 $message = false;
469 } else {
470 $message = false;
474 # Is this a custom message? Try the default language in the db...
475 if( ($message === false || $message === '-' ) &&
476 !$this->mDisable && $useDB &&
477 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
478 $message = $this->getFromCache( $wgContLang->ucfirst( $key ) );
481 # Final fallback
482 if( $message === false ) {
483 return '&lt;' . htmlspecialchars($key) . '&gt;';
486 # Replace brace tags
487 $message = $this->transform( $message );
488 return $message;
491 function getFromCache( $title ) {
492 $message = false;
494 # Try the cache
495 if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
496 return $this->mCache[$title];
499 # Try individual message cache
500 if ( $this->mUseCache ) {
501 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
502 if ( $message == '###NONEXISTENT###' ) {
503 $this->mCache[$title] = false;
504 return false;
505 } elseif( !is_null( $message ) ) {
506 $this->mCache[$title] = $message;
507 return $message;
508 } else {
509 $message = false;
513 # Call message Hooks, in case they are defined
514 wfRunHooks('MessagesPreLoad',array($title,&$message));
516 # If it wasn't in the cache, load each message from the DB individually
517 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
518 if( $revision ) {
519 $message = $revision->getText();
520 if ($this->mUseCache) {
521 $this->mCache[$title]=$message;
522 /* individual messages may be often
523 recached until proper purge code exists
525 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
527 } else {
528 # Negative caching
529 # Use some special text instead of false, because false gets converted to '' somewhere
530 $this->mMemc->set( $this->mMemcKey . ':' . $title, '###NONEXISTENT###', $this->mExpiry );
531 $this->mCache[$title] = false;
534 return $message;
537 function transform( $message ) {
538 global $wgParser;
539 if ( !$this->mParser && isset( $wgParser ) ) {
540 # Do some initialisation so that we don't have to do it twice
541 $wgParser->firstCallInit();
542 # Clone it and store it
543 $this->mParser = clone $wgParser;
545 if ( !$this->mDisableTransform && $this->mParser ) {
546 if( strpos( $message, '{{' ) !== false ) {
547 $message = $this->mParser->transformMsg( $message, $this->getParserOptions() );
550 return $message;
553 function disable() { $this->mDisable = true; }
554 function enable() { $this->mDisable = false; }
555 function disableTransform() { $this->mDisableTransform = true; }
556 function enableTransform() { $this->mDisableTransform = false; }
557 function setTransform( $x ) { $this->mDisableTransform = $x; }
558 function getTransform() { return $this->mDisableTransform; }
561 * Add a message to the cache
563 * @param mixed $key
564 * @param mixed $value
565 * @param string $lang The messages language, English by default
567 function addMessage( $key, $value, $lang = 'en' ) {
568 $this->mExtensionMessages[$key][$lang] = $value;
572 * Add an associative array of message to the cache
574 * @param array $messages An associative array of key => values to be added
575 * @param string $lang The messages language, English by default
577 function addMessages( $messages, $lang = 'en' ) {
578 wfProfileIn( __METHOD__ );
579 foreach ( $messages as $key => $value ) {
580 $this->addMessage( $key, $value, $lang );
582 wfProfileOut( __METHOD__ );
586 * Get the extension messages for a specific language
588 * @param string $lang The messages language, English by default
590 function getExtensionMessagesFor( $lang = 'en' ) {
591 wfProfileIn( __METHOD__ );
592 $messages = array();
593 foreach( $this->mExtensionMessages as $key => $message ) {
594 if ( isset( $message[$lang] ) ) {
595 $messages[$key] = $message[$lang];
596 } elseif ( isset( $message['en'] ) ) {
597 $messages[$key] = $message['en'];
600 wfProfileOut( __METHOD__ );
601 return $messages;
605 * Clear all stored messages. Mainly used after a mass rebuild.
607 function clear() {
608 global $wgLocalMessageCache;
609 if( $this->mUseCache ) {
610 # Global cache
611 $this->mMemc->delete( $this->mMemcKey );
612 # Invalidate all local caches
613 $this->mMemc->delete( "{$this->mMemcKey}-hash" );
617 static function loadAllMessages() {
618 # Some extensions will load their messages when you load their class file
619 wfLoadAllExtensions();
620 # Others will respond to this hook
621 wfRunHooks( 'LoadAllMessages' );
622 # Still others will respond to neither, they are EVIL. We sometimes need to know!