Localisation updates for core and extension messages from translatewiki.net (2010...
[mediawiki.git] / includes / MessageCache.php
blob34e13887c9243bc189a7560e1ce5adabfbca76c3
1 <?php
2 /**
3 * @file
4 * @ingroup Cache
5 */
7 /**
9 */
10 define( 'MSG_LOAD_TIMEOUT', 60);
11 define( 'MSG_LOCK_TIMEOUT', 10);
12 define( 'MSG_WAIT_TIMEOUT', 10);
13 define( 'MSG_CACHE_VERSION', 1 );
15 /**
16 * Message cache
17 * Performs various MediaWiki namespace-related functions
18 * @ingroup Cache
20 class MessageCache {
21 /**
22 * Process local cache of loaded messages that are defined in
23 * MediaWiki namespace. First array level is a language code,
24 * second level is message key and the values are either message
25 * content prefixed with space, or !NONEXISTENT for negative
26 * caching.
28 protected $mCache;
30 // Should mean that database cannot be used, but check
31 protected $mDisable;
33 /// Lifetime for cache, used by object caching
34 protected $mExpiry;
36 /**
37 * Message cache has it's own parser which it uses to transform
38 * messages.
40 protected $mParserOptions, $mParser;
42 /// Variable for tracking which variables are already loaded
43 protected $mLoadedLanguages = array();
45 function __construct( $memCached, $useDB, $expiry ) {
46 if ( !$memCached ) {
47 $memCached = wfGetCache( CACHE_NONE );
50 $this->mMemc = $memCached;
51 $this->mDisable = !$useDB;
52 $this->mExpiry = $expiry;
56 /**
57 * ParserOptions is lazy initialised.
59 function getParserOptions() {
60 if ( !$this->mParserOptions ) {
61 $this->mParserOptions = new ParserOptions;
63 return $this->mParserOptions;
66 /**
67 * Try to load the cache from a local file.
68 * Actual format of the file depends on the $wgLocalMessageCacheSerialized
69 * setting.
71 * @param $hash String: the hash of contents, to check validity.
72 * @param $code Mixed: Optional language code, see documenation of load().
73 * @return false on failure.
75 function loadFromLocal( $hash, $code ) {
76 global $wgCacheDirectory, $wgLocalMessageCacheSerialized;
78 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
80 # Check file existence
81 wfSuppressWarnings();
82 $file = fopen( $filename, 'r' );
83 wfRestoreWarnings();
84 if ( !$file ) {
85 return false; // No cache file
88 if ( $wgLocalMessageCacheSerialized ) {
89 // Check to see if the file has the hash specified
90 $localHash = fread( $file, 32 );
91 if ( $hash === $localHash ) {
92 // All good, get the rest of it
93 $serialized = '';
94 while ( !feof( $file ) ) {
95 $serialized .= fread( $file, 100000 );
97 fclose( $file );
98 return $this->setCache( unserialize( $serialized ), $code );
99 } else {
100 fclose( $file );
101 return false; // Wrong hash
103 } else {
104 $localHash=substr(fread($file,40),8);
105 fclose($file);
106 if ($hash!=$localHash) {
107 return false; // Wrong hash
110 # Require overwrites the member variable or just shadows it?
111 require( $filename );
112 return $this->setCache( $this->mCache, $code );
117 * Save the cache to a local file.
119 function saveToLocal( $serialized, $hash, $code ) {
120 global $wgCacheDirectory;
122 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
123 wfMkdirParents( $wgCacheDirectory ); // might fail
125 wfSuppressWarnings();
126 $file = fopen( $filename, 'w' );
127 wfRestoreWarnings();
129 if ( !$file ) {
130 wfDebug( "Unable to open local cache file for writing\n" );
131 return;
134 fwrite( $file, $hash . $serialized );
135 fclose( $file );
136 @chmod( $filename, 0666 );
139 function saveToScript( $array, $hash, $code ) {
140 global $wgCacheDirectory;
142 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
143 $tempFilename = $filename . '.tmp';
144 wfMkdirParents( $wgCacheDirectory ); // might fail
146 wfSuppressWarnings();
147 $file = fopen( $tempFilename, 'w');
148 wfRestoreWarnings();
150 if ( !$file ) {
151 wfDebug( "Unable to open local cache file for writing\n" );
152 return;
155 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
157 foreach ($array as $key => $message) {
158 $key = $this->escapeForScript($key);
159 $messages = $this->escapeForScript($message);
160 fwrite($file, "'$key' => '$message',\n");
163 fwrite($file,");\n?>");
164 fclose($file);
165 rename($tempFilename, $filename);
168 function escapeForScript($string) {
169 $string = str_replace( '\\', '\\\\', $string );
170 $string = str_replace( '\'', '\\\'', $string );
171 return $string;
175 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
177 function setCache( $cache, $code ) {
178 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION ) {
179 $this->mCache[$code] = $cache;
180 return true;
181 } else {
182 return false;
187 * Loads messages from caches or from database in this order:
188 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
189 * (2) memcached
190 * (3) from the database.
192 * When succesfully loading from (2) or (3), all higher level caches are
193 * updated for the newest version.
195 * Nothing is loaded if member variable mDisable is true, either manually
196 * set by calling code or if message loading fails (is this possible?).
198 * Returns true if cache is already populated or it was succesfully populated,
199 * or false if populating empty cache fails. Also returns true if MessageCache
200 * is disabled.
202 * @param $code String: language to which load messages
204 function load( $code = false ) {
205 global $wgUseLocalMessageCache;
207 if( !is_string( $code ) ) {
208 # This isn't really nice, so at least make a note about it and try to
209 # fall back
210 wfDebug( __METHOD__ . " called without providing a language code\n" );
211 $code = 'en';
214 # Don't do double loading...
215 if ( isset($this->mLoadedLanguages[$code]) ) return true;
217 # 8 lines of code just to say (once) that message cache is disabled
218 if ( $this->mDisable ) {
219 static $shownDisabled = false;
220 if ( !$shownDisabled ) {
221 wfDebug( __METHOD__ . ": disabled\n" );
222 $shownDisabled = true;
224 return true;
227 # Loading code starts
228 wfProfileIn( __METHOD__ );
229 $success = false; # Keep track of success
230 $where = array(); # Debug info, delayed to avoid spamming debug log too much
231 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
234 # (1) local cache
235 # Hash of the contents is stored in memcache, to detect if local cache goes
236 # out of date (due to update in other thread?)
237 if ( $wgUseLocalMessageCache ) {
238 wfProfileIn( __METHOD__ . '-fromlocal' );
240 $hash = $this->mMemc->get( wfMemcKey( 'messages', $code, 'hash' ) );
241 if ( $hash ) {
242 $success = $this->loadFromLocal( $hash, $code );
243 if ( $success ) $where[] = 'got from local cache';
245 wfProfileOut( __METHOD__ . '-fromlocal' );
248 # (2) memcache
249 # Fails if nothing in cache, or in the wrong version.
250 if ( !$success ) {
251 wfProfileIn( __METHOD__ . '-fromcache' );
252 $cache = $this->mMemc->get( $cacheKey );
253 $success = $this->setCache( $cache, $code );
254 if ( $success ) {
255 $where[] = 'got from global cache';
256 $this->saveToCaches( $cache, false, $code );
258 wfProfileOut( __METHOD__ . '-fromcache' );
262 # (3)
263 # Nothing in caches... so we need create one and store it in caches
264 if ( !$success ) {
265 $where[] = 'cache is empty';
266 $where[] = 'loading from database';
268 $this->lock($cacheKey);
270 # Limit the concurrency of loadFromDB to a single process
271 # This prevents the site from going down when the cache expires
272 $statusKey = wfMemcKey( 'messages', $code, 'status' );
273 $success = $this->mMemc->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT );
274 if ( $success ) {
275 $cache = $this->loadFromDB( $code );
276 $success = $this->setCache( $cache, $code );
278 if ( $success ) {
279 $success = $this->saveToCaches( $cache, true, $code );
280 if ( $success ) {
281 $this->mMemc->delete( $statusKey );
282 } else {
283 $this->mMemc->set( $statusKey, 'error', 60*5 );
284 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
287 $this->unlock($cacheKey);
290 if ( !$success ) {
291 # Bad luck... this should not happen
292 $where[] = 'loading FAILED - cache is disabled';
293 $info = implode( ', ', $where );
294 wfDebug( __METHOD__ . ": Loading $code... $info\n" );
295 $this->mDisable = true;
296 $this->mCache = false;
297 } else {
298 # All good, just record the success
299 $info = implode( ', ', $where );
300 wfDebug( __METHOD__ . ": Loading $code... $info\n" );
301 $this->mLoadedLanguages[$code] = true;
303 wfProfileOut( __METHOD__ );
304 return $success;
308 * Loads cacheable messages from the database. Messages bigger than
309 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
310 * on-demand from the database later.
312 * @param $code Optional language code, see documenation of load().
313 * @return Array: Loaded messages for storing in caches.
315 function loadFromDB( $code = false ) {
316 wfProfileIn( __METHOD__ );
317 global $wgMaxMsgCacheEntrySize, $wgContLanguageCode;
318 $dbr = wfGetDB( DB_SLAVE );
319 $cache = array();
321 # Common conditions
322 $conds = array(
323 'page_is_redirect' => 0,
324 'page_namespace' => NS_MEDIAWIKI,
327 if ( $code ) {
328 # Is this fast enough. Should not matter if the filtering is done in the
329 # database or in code.
330 if ( $code !== $wgContLanguageCode ) {
331 # Messages for particular language
332 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), "/$code" );
333 } else {
334 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
335 # other than language code.
336 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
340 # Conditions to fetch oversized pages to ignore them
341 $bigConds = $conds;
342 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
344 # Load titles for all oversized pages in the MediaWiki namespace
345 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__ . "($code)-big" );
346 foreach ( $res as $row ) {
347 $cache[$row->page_title] = '!TOO BIG';
350 # Conditions to load the remaining pages with their contents
351 $smallConds = $conds;
352 $smallConds[] = 'page_latest=rev_id';
353 $smallConds[] = 'rev_text_id=old_id';
354 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
356 $res = $dbr->select( array( 'page', 'revision', 'text' ),
357 array( 'page_title', 'old_text', 'old_flags' ),
358 $smallConds, __METHOD__ . "($code)-small" );
360 foreach ( $res as $row ) {
361 $cache[$row->page_title] = ' ' . Revision::getRevisionText( $row );
364 $cache['VERSION'] = MSG_CACHE_VERSION;
365 wfProfileOut( __METHOD__ );
366 return $cache;
370 * Updates cache as necessary when message page is changed
372 * @param $title String: name of the page changed.
373 * @param $text Mixed: new contents of the page.
375 public function replace( $title, $text ) {
376 global $wgMaxMsgCacheEntrySize;
377 wfProfileIn( __METHOD__ );
379 if ( $this->mDisable ) {
380 return;
383 list( , $code ) = $this->figureMessage( $title );
385 $cacheKey = wfMemcKey( 'messages', $code );
386 $this->load( $code );
387 $this->lock( $cacheKey );
389 $titleKey = wfMemcKey( 'messages', 'individual', $title );
391 if ( $text === false ) {
392 # Article was deleted
393 $this->mCache[$code][$title] = '!NONEXISTENT';
394 $this->mMemc->delete( $titleKey );
396 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
397 # Check for size
398 $this->mCache[$code][$title] = '!TOO BIG';
399 $this->mMemc->set( $titleKey, ' ' . $text, $this->mExpiry );
401 } else {
402 $this->mCache[$code][$title] = ' ' . $text;
403 $this->mMemc->delete( $titleKey );
406 # Update caches
407 $this->saveToCaches( $this->mCache[$code], true, $code );
408 $this->unlock( $cacheKey );
410 // Also delete cached sidebar... just in case it is affected
411 $codes = array( $code );
412 if ( $code === 'en' ) {
413 // Delete all sidebars, like for example on action=purge on the
414 // sidebar messages
415 $codes = array_keys( Language::getLanguageNames() );
418 global $parserMemc;
419 foreach ( $codes as $code ) {
420 $sidebarKey = wfMemcKey( 'sidebar', $code );
421 $parserMemc->delete( $sidebarKey );
424 wfRunHooks( "MessageCacheReplace", array( $title, $text ) );
426 wfProfileOut( __METHOD__ );
430 * Shortcut to update caches.
432 * @param $cache Array: cached messages with a version.
433 * @param $memc Bool: Wether to update or not memcache.
434 * @param $code String: Language code.
435 * @return False on somekind of error.
437 protected function saveToCaches( $cache, $memc = true, $code = false ) {
438 wfProfileIn( __METHOD__ );
439 global $wgUseLocalMessageCache, $wgLocalMessageCacheSerialized;
441 $cacheKey = wfMemcKey( 'messages', $code );
443 if ( $memc ) {
444 $success = $this->mMemc->set( $cacheKey, $cache, $this->mExpiry );
445 } else {
446 $success = true;
449 # Save to local cache
450 if ( $wgUseLocalMessageCache ) {
451 $serialized = serialize( $cache );
452 $hash = md5( $serialized );
453 $this->mMemc->set( wfMemcKey( 'messages', $code, 'hash' ), $hash, $this->mExpiry );
454 if ($wgLocalMessageCacheSerialized) {
455 $this->saveToLocal( $serialized, $hash, $code );
456 } else {
457 $this->saveToScript( $cache, $hash, $code );
461 wfProfileOut( __METHOD__ );
462 return $success;
466 * Represents a write lock on the messages key
468 * @return Boolean: success
470 function lock($key) {
471 $lockKey = $key . ':lock';
472 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
473 sleep(1);
476 return $i >= MSG_WAIT_TIMEOUT;
479 function unlock($key) {
480 $lockKey = $key . ':lock';
481 $this->mMemc->delete( $lockKey );
485 * Get a message from either the content language or the user language.
487 * @param $key String: the message cache key
488 * @param $useDB Boolean: get the message from the DB, false to use only
489 * the localisation
490 * @param $langcode String: code of the language to get the message for, if
491 * it is a valid code create a language for that language,
492 * if it is a string but not a valid code then make a basic
493 * language object, if it is a false boolean then use the
494 * current users language (as a fallback for the old
495 * parameter functionality), or if it is a true boolean
496 * then use the wikis content language (also as a
497 * fallback).
498 * @param $isFullKey Boolean: specifies whether $key is a two part key
499 * "msg/lang".
501 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
502 global $wgContLanguageCode, $wgContLang;
504 if ( !is_string( $key ) ) {
505 throw new MWException( "Non-string key given" );
508 if ( strval( $key ) === '' ) {
509 # Shortcut: the empty key is always missing
510 return false;
513 $lang = wfGetLangObj( $langcode );
514 $langcode = $lang->getCode();
516 $message = false;
518 # Normalise title-case input (with some inlining)
519 $lckey = str_replace( ' ', '_', $key );
520 if ( ord( $key ) < 128 ) {
521 $lckey[0] = strtolower( $lckey[0] );
522 $uckey = ucfirst( $lckey );
523 } else {
524 $lckey = $wgContLang->lcfirst( $lckey );
525 $uckey = $wgContLang->ucfirst( $lckey );
528 # Try the MediaWiki namespace
529 if( !$this->mDisable && $useDB ) {
530 $title = $uckey;
531 if(!$isFullKey && ( $langcode != $wgContLanguageCode ) ) {
532 $title .= '/' . $langcode;
534 $message = $this->getMsgFromNamespace( $title, $langcode );
537 # Try the array in the language object
538 if ( $message === false ) {
539 $message = $lang->getMessage( $lckey );
540 if ( is_null( $message ) ) {
541 $message = false;
545 # Try the array of another language
546 if( $message === false ) {
547 $parts = explode( '/', $lckey );
548 # We may get calls for things that are http-urls from sidebar
549 # Let's not load nonexistent languages for those
550 # They usually have more than one slash.
551 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
552 $message = Language::getMessageFor( $parts[0], $parts[1] );
553 if ( is_null( $message ) ) {
554 $message = false;
559 # Is this a custom message? Try the default language in the db...
560 if( ($message === false || $message === '-' ) &&
561 !$this->mDisable && $useDB &&
562 !$isFullKey && ($langcode != $wgContLanguageCode) ) {
563 $message = $this->getMsgFromNamespace( $uckey, $wgContLanguageCode );
566 # Final fallback
567 if( $message === false ) {
568 return false;
571 # Fix whitespace
572 $message = strtr( $message,
573 array(
574 # Fix for trailing whitespace, removed by textarea
575 '&#32;' => ' ',
576 # Fix for NBSP, converted to space by firefox
577 '&nbsp;' => "\xc2\xa0",
578 '&#160;' => "\xc2\xa0",
579 ) );
581 return $message;
585 * Get a message from the MediaWiki namespace, with caching. The key must
586 * first be converted to two-part lang/msg form if necessary.
588 * @param $title String: Message cache key with initial uppercase letter.
589 * @param $code String: code denoting the language to try.
591 function getMsgFromNamespace( $title, $code ) {
592 $type = false;
593 $message = false;
595 $this->load( $code );
596 if ( isset( $this->mCache[$code][$title] ) ) {
597 $entry = $this->mCache[$code][$title];
598 if ( substr( $entry, 0, 1 ) === ' ' ) {
599 return substr( $entry, 1 );
600 } elseif ( $entry === '!NONEXISTENT' ) {
601 return false;
605 # Call message hooks, in case they are defined
606 wfRunHooks('MessagesPreLoad', array( $title, &$message ) );
607 if ( $message !== false ) {
608 return $message;
611 # Try the individual message cache
612 $titleKey = wfMemcKey( 'messages', 'individual', $title );
613 $entry = $this->mMemc->get( $titleKey );
614 if ( $entry ) {
615 if ( substr( $entry, 0, 1 ) === ' ' ) {
616 $this->mCache[$code][$title] = $entry;
617 return substr( $entry, 1 );
618 } elseif ( $entry === '!NONEXISTENT' ) {
619 $this->mCache[$code][$title] = '!NONEXISTENT';
620 return false;
621 } else {
622 # Corrupt/obsolete entry, delete it
623 $this->mMemc->delete( $titleKey );
627 # Try loading it from the database
628 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
629 if ( $revision ) {
630 $message = $revision->getText();
631 $this->mCache[$code][$title] = ' ' . $message;
632 $this->mMemc->set( $titleKey, ' ' . $message, $this->mExpiry );
633 } else {
634 $this->mCache[$code][$title] = '!NONEXISTENT';
635 $this->mMemc->set( $titleKey, '!NONEXISTENT', $this->mExpiry );
638 return $message;
641 function transform( $message, $interface = false, $language = null ) {
642 // Avoid creating parser if nothing to transform
643 if( strpos( $message, '{{' ) === false ) {
644 return $message;
647 global $wgParser, $wgParserConf;
648 if ( !$this->mParser && isset( $wgParser ) ) {
649 # Do some initialisation so that we don't have to do it twice
650 $wgParser->firstCallInit();
651 # Clone it and store it
652 $class = $wgParserConf['class'];
653 if ( $class == 'Parser_DiffTest' ) {
654 # Uncloneable
655 $this->mParser = new $class( $wgParserConf );
656 } else {
657 $this->mParser = clone $wgParser;
659 #wfDebug( __METHOD__ . ": following contents triggered transform: $message\n" );
661 if ( $this->mParser ) {
662 $popts = $this->getParserOptions();
663 $popts->setInterfaceMessage( $interface );
664 $popts->setTargetLanguage( $language );
665 $message = $this->mParser->transformMsg( $message, $popts );
667 return $message;
670 function disable() { $this->mDisable = true; }
671 function enable() { $this->mDisable = false; }
673 /** @deprecated */
674 function disableTransform(){
675 wfDeprecated( __METHOD__ );
677 function enableTransform() {
678 wfDeprecated( __METHOD__ );
680 function setTransform( $x ) {
681 wfDeprecated( __METHOD__ );
683 function getTransform() {
684 wfDeprecated( __METHOD__ );
685 return false;
689 * Clear all stored messages. Mainly used after a mass rebuild.
691 function clear() {
692 $langs = Language::getLanguageNames( false );
693 foreach ( array_keys($langs) as $code ) {
694 # Global cache
695 $this->mMemc->delete( wfMemcKey( 'messages', $code ) );
696 # Invalidate all local caches
697 $this->mMemc->delete( wfMemcKey( 'messages', $code, 'hash' ) );
702 * Add a message to the cache
703 * @deprecated Use $wgExtensionMessagesFiles
705 * @param $key Mixed
706 * @param $value Mixed
707 * @param $lang String: the messages language, English by default
709 function addMessage( $key, $value, $lang = 'en' ) {
710 wfDeprecated( __METHOD__ );
711 $lc = Language::getLocalisationCache();
712 $lc->addLegacyMessages( array( $lang => array( $key => $value ) ) );
716 * Add an associative array of message to the cache
717 * @deprecated Use $wgExtensionMessagesFiles
719 * @param $messages Array: an associative array of key => values to be added
720 * @param $lang String: the messages language, English by default
722 function addMessages( $messages, $lang = 'en' ) {
723 wfDeprecated( __METHOD__ );
724 $lc = Language::getLocalisationCache();
725 $lc->addLegacyMessages( array( $lang => $messages ) );
729 * Add a 2-D array of messages by lang. Useful for extensions.
730 * @deprecated Use $wgExtensionMessagesFiles
732 * @param $messages Array: the array to be added
734 function addMessagesByLang( $messages ) {
735 wfDeprecated( __METHOD__ );
736 $lc = Language::getLocalisationCache();
737 $lc->addLegacyMessages( $messages );
741 * Set a hook for addMessagesByLang()
743 function setExtensionMessagesHook( $callback ) {
744 $this->mAddMessagesHook = $callback;
748 * @deprecated
750 function loadAllMessages( $lang = false ) {
754 * @deprecated
756 function loadMessagesFile( $filename, $langcode = false ) {
759 public function figureMessage( $key ) {
760 global $wgContLanguageCode;
761 $pieces = explode( '/', $key );
762 if( count( $pieces ) < 2 )
763 return array( $key, $wgContLanguageCode );
765 $lang = array_pop( $pieces );
766 $validCodes = Language::getLanguageNames();
767 if( !array_key_exists( $lang, $validCodes ) )
768 return array( $key, $wgContLanguageCode );
770 $message = implode( '/', $pieces );
771 return array( $message, $lang );