PHPSessionHandler: Implement SessionHandlerInterface
[mediawiki.git] / includes / cache / MessageCache.php
blob2fae4e3f2b00235813cb01cf06faf6e0067a5762
1 <?php
2 /**
3 * Localisation messages cache.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Cache
24 /**
25 * MediaWiki message cache structure version.
26 * Bump this whenever the message cache format has changed.
28 define( 'MSG_CACHE_VERSION', 2 );
30 /**
31 * Message cache
32 * Performs various MediaWiki namespace-related functions
33 * @ingroup Cache
35 class MessageCache {
36 const FOR_UPDATE = 1; // force message reload
38 /** How long to wait for memcached locks */
39 const WAIT_SEC = 15;
40 /** How long memcached locks last */
41 const LOCK_TTL = 30;
43 /**
44 * Process local cache of loaded messages that are defined in
45 * MediaWiki namespace. First array level is a language code,
46 * second level is message key and the values are either message
47 * content prefixed with space, or !NONEXISTENT for negative
48 * caching.
49 * @var array $mCache
51 protected $mCache;
53 /**
54 * Should mean that database cannot be used, but check
55 * @var bool $mDisable
57 protected $mDisable;
59 /**
60 * Lifetime for cache, used by object caching.
61 * Set on construction, see __construct().
63 protected $mExpiry;
65 /**
66 * Message cache has its own parser which it uses to transform
67 * messages.
69 protected $mParserOptions, $mParser;
71 /**
72 * Variable for tracking which variables are already loaded
73 * @var array $mLoadedLanguages
75 protected $mLoadedLanguages = array();
77 /**
78 * @var bool $mInParser
80 protected $mInParser = false;
82 /** @var BagOStuff */
83 protected $mMemc;
84 /** @var WANObjectCache */
85 protected $wanCache;
87 /**
88 * Singleton instance
90 * @var MessageCache $instance
92 private static $instance;
94 /**
95 * Get the signleton instance of this class
97 * @since 1.18
98 * @return MessageCache
100 public static function singleton() {
101 if ( self::$instance === null ) {
102 global $wgUseDatabaseMessages, $wgMsgCacheExpiry;
103 self::$instance = new self(
104 wfGetMessageCacheStorage(),
105 $wgUseDatabaseMessages,
106 $wgMsgCacheExpiry
110 return self::$instance;
114 * Destroy the singleton instance
116 * @since 1.18
118 public static function destroyInstance() {
119 self::$instance = null;
123 * Normalize message key input
125 * @param string $key Input message key to be normalized
126 * @return string Normalized message key
128 public static function normalizeKey( $key ) {
129 global $wgContLang;
130 $lckey = strtr( $key, ' ', '_' );
131 if ( ord( $lckey ) < 128 ) {
132 $lckey[0] = strtolower( $lckey[0] );
133 } else {
134 $lckey = $wgContLang->lcfirst( $lckey );
137 return $lckey;
141 * @param BagOStuff $memCached A cache instance. If none, fall back to CACHE_NONE.
142 * @param bool $useDB
143 * @param int $expiry Lifetime for cache. @see $mExpiry.
145 function __construct( $memCached, $useDB, $expiry ) {
146 global $wgUseLocalMessageCache;
148 if ( !$memCached ) {
149 $memCached = wfGetCache( CACHE_NONE );
152 $this->mMemc = $memCached;
153 $this->mDisable = !$useDB;
154 $this->mExpiry = $expiry;
156 if ( $wgUseLocalMessageCache ) {
157 $this->localCache = ObjectCache::getLocalServerInstance( CACHE_NONE );
158 } else {
159 $this->localCache = wfGetCache( CACHE_NONE );
162 $this->wanCache = ObjectCache::getMainWANInstance();
166 * ParserOptions is lazy initialised.
168 * @return ParserOptions
170 function getParserOptions() {
171 global $wgUser;
173 if ( !$this->mParserOptions ) {
174 if ( !$wgUser->isSafeToLoad() ) {
175 // $wgUser isn't unstubbable yet, so don't try to get a
176 // ParserOptions for it. And don't cache this ParserOptions
177 // either.
178 $po = ParserOptions::newFromAnon();
179 $po->setEditSection( false );
180 return $po;
183 $this->mParserOptions = new ParserOptions;
184 $this->mParserOptions->setEditSection( false );
187 return $this->mParserOptions;
191 * Try to load the cache from APC.
193 * @param string $code Optional language code, see documenation of load().
194 * @return array|bool The cache array, or false if not in cache.
196 protected function getLocalCache( $code ) {
197 $cacheKey = wfMemcKey( __CLASS__, $code );
199 return $this->localCache->get( $cacheKey );
203 * Save the cache to APC.
205 * @param string $code
206 * @param array $cache The cache array
208 protected function saveToLocalCache( $code, $cache ) {
209 $cacheKey = wfMemcKey( __CLASS__, $code );
210 $this->localCache->set( $cacheKey, $cache );
214 * Loads messages from caches or from database in this order:
215 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
216 * (2) memcached
217 * (3) from the database.
219 * When succesfully loading from (2) or (3), all higher level caches are
220 * updated for the newest version.
222 * Nothing is loaded if member variable mDisable is true, either manually
223 * set by calling code or if message loading fails (is this possible?).
225 * Returns true if cache is already populated or it was succesfully populated,
226 * or false if populating empty cache fails. Also returns true if MessageCache
227 * is disabled.
229 * @param bool|string $code Language to which load messages
230 * @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache
231 * @throws MWException
232 * @return bool
234 function load( $code = false, $mode = null ) {
235 if ( !is_string( $code ) ) {
236 # This isn't really nice, so at least make a note about it and try to
237 # fall back
238 wfDebug( __METHOD__ . " called without providing a language code\n" );
239 $code = 'en';
242 # Don't do double loading...
243 if ( isset( $this->mLoadedLanguages[$code] ) && $mode != self::FOR_UPDATE ) {
244 return true;
247 # 8 lines of code just to say (once) that message cache is disabled
248 if ( $this->mDisable ) {
249 static $shownDisabled = false;
250 if ( !$shownDisabled ) {
251 wfDebug( __METHOD__ . ": disabled\n" );
252 $shownDisabled = true;
255 return true;
258 # Loading code starts
259 $success = false; # Keep track of success
260 $staleCache = false; # a cache array with expired data, or false if none has been loaded
261 $where = array(); # Debug info, delayed to avoid spamming debug log too much
263 # Hash of the contents is stored in memcache, to detect if data-center cache
264 # or local cache goes out of date (e.g. due to replace() on some other server)
265 list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
267 # Try the local cache and check against the cluster hash key...
268 $cache = $this->getLocalCache( $code );
269 if ( !$cache ) {
270 $where[] = 'local cache is empty';
271 } elseif ( !isset( $cache['HASH'] ) || $cache['HASH'] !== $hash ) {
272 $where[] = 'local cache has the wrong hash';
273 $staleCache = $cache;
274 } elseif ( $this->isCacheExpired( $cache ) ) {
275 $where[] = 'local cache is expired';
276 $staleCache = $cache;
277 } elseif ( $hashVolatile ) {
278 $where[] = 'local cache validation key is expired/volatile';
279 $staleCache = $cache;
280 } else {
281 $where[] = 'got from local cache';
282 $success = true;
283 $this->mCache[$code] = $cache;
286 if ( !$success ) {
287 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
288 # Try the global cache. If it is empty, try to acquire a lock. If
289 # the lock can't be acquired, wait for the other thread to finish
290 # and then try the global cache a second time.
291 for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++ ) {
292 if ( $hashVolatile && $staleCache ) {
293 # Do not bother fetching the whole cache blob to avoid I/O.
294 # Instead, just try to get the non-blocking $statusKey lock
295 # below, and use the local stale value if it was not acquired.
296 $where[] = 'global cache is presumed expired';
297 } else {
298 $cache = $this->mMemc->get( $cacheKey );
299 if ( !$cache ) {
300 $where[] = 'global cache is empty';
301 } elseif ( $this->isCacheExpired( $cache ) ) {
302 $where[] = 'global cache is expired';
303 $staleCache = $cache;
304 } elseif ( $hashVolatile ) {
305 # DB results are slave lag prone until the holdoff TTL passes.
306 # By then, updates should be reflected in loadFromDBWithLock().
307 # One thread renerates the cache while others use old values.
308 $where[] = 'global cache is expired/volatile';
309 $staleCache = $cache;
310 } else {
311 $where[] = 'got from global cache';
312 $this->mCache[$code] = $cache;
313 $this->saveToCaches( $cache, 'local-only', $code );
314 $success = true;
318 if ( $success ) {
319 # Done, no need to retry
320 break;
323 # We need to call loadFromDB. Limit the concurrency to one process.
324 # This prevents the site from going down when the cache expires.
325 # Note that the DB slam protection lock here is non-blocking.
326 $loadStatus = $this->loadFromDBWithLock( $code, $where, $mode );
327 if ( $loadStatus === true ) {
328 $success = true;
329 break;
330 } elseif ( $staleCache ) {
331 # Use the stale cache while some other thread constructs the new one
332 $where[] = 'using stale cache';
333 $this->mCache[$code] = $staleCache;
334 $success = true;
335 break;
336 } elseif ( $failedAttempts > 0 ) {
337 # Already blocked once, so avoid another lock/unlock cycle.
338 # This case will typically be hit if memcached is down, or if
339 # loadFromDB() takes longer than LOCK_WAIT.
340 $where[] = "could not acquire status key.";
341 break;
342 } elseif ( $loadStatus === 'cantacquire' ) {
343 # Wait for the other thread to finish, then retry. Normally,
344 # the memcached get() will then yeild the other thread's result.
345 $where[] = 'waited for other thread to complete';
346 $this->getReentrantScopedLock( $cacheKey );
347 } else {
348 # Disable cache; $loadStatus is 'disabled'
349 break;
354 if ( !$success ) {
355 $where[] = 'loading FAILED - cache is disabled';
356 $this->mDisable = true;
357 $this->mCache = false;
358 # This used to throw an exception, but that led to nasty side effects like
359 # the whole wiki being instantly down if the memcached server died
360 } else {
361 # All good, just record the success
362 $this->mLoadedLanguages[$code] = true;
365 $info = implode( ', ', $where );
366 wfDebugLog( 'MessageCache', __METHOD__ . ": Loading $code... $info\n" );
368 return $success;
372 * @param string $code
373 * @param array $where List of wfDebug() comments
374 * @param integer $mode Use MessageCache::FOR_UPDATE to use DB_MASTER
375 * @return bool|string True on success or one of ("cantacquire", "disabled")
377 protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
378 global $wgUseLocalMessageCache;
380 # If cache updates on all levels fail, give up on message overrides.
381 # This is to avoid easy site outages; see $saveSuccess comments below.
382 $statusKey = wfMemcKey( 'messages', $code, 'status' );
383 $status = $this->mMemc->get( $statusKey );
384 if ( $status === 'error' ) {
385 $where[] = "could not load; method is still globally disabled";
386 return 'disabled';
389 # Now let's regenerate
390 $where[] = 'loading from database';
392 # Lock the cache to prevent conflicting writes.
393 # This lock is non-blocking so stale cache can quickly be used.
394 # Note that load() will call a blocking getReentrantScopedLock()
395 # after this if it really need to wait for any current thread.
396 $cacheKey = wfMemcKey( 'messages', $code );
397 $scopedLock = $this->getReentrantScopedLock( $cacheKey, 0 );
398 if ( !$scopedLock ) {
399 $where[] = 'could not acquire main lock';
400 return 'cantacquire';
403 $cache = $this->loadFromDB( $code, $mode );
404 $this->mCache[$code] = $cache;
405 $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
407 if ( !$saveSuccess ) {
409 * Cache save has failed.
411 * There are two main scenarios where this could be a problem:
412 * - The cache is more than the maximum size (typically 1MB compressed).
413 * - Memcached has no space remaining in the relevant slab class. This is
414 * unlikely with recent versions of memcached.
416 * Either way, if there is a local cache, nothing bad will happen. If there
417 * is no local cache, disabling the message cache for all requests avoids
418 * incurring a loadFromDB() overhead on every request, and thus saves the
419 * wiki from complete downtime under moderate traffic conditions.
421 if ( !$wgUseLocalMessageCache ) {
422 $this->mMemc->set( $statusKey, 'error', 60 * 5 );
423 $where[] = 'could not save cache, disabled globally for 5 minutes';
424 } else {
425 $where[] = "could not save global cache";
429 return true;
433 * Loads cacheable messages from the database. Messages bigger than
434 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
435 * on-demand from the database later.
437 * @param string $code Language code
438 * @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache
439 * @return array Loaded messages for storing in caches
441 function loadFromDB( $code, $mode = null ) {
442 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
444 $dbr = wfGetDB( ( $mode == self::FOR_UPDATE ) ? DB_MASTER : DB_SLAVE );
446 $cache = array();
448 # Common conditions
449 $conds = array(
450 'page_is_redirect' => 0,
451 'page_namespace' => NS_MEDIAWIKI,
454 $mostused = array();
455 if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
456 if ( !isset( $this->mCache[$wgLanguageCode] ) ) {
457 $this->load( $wgLanguageCode );
459 $mostused = array_keys( $this->mCache[$wgLanguageCode] );
460 foreach ( $mostused as $key => $value ) {
461 $mostused[$key] = "$value/$code";
465 if ( count( $mostused ) ) {
466 $conds['page_title'] = $mostused;
467 } elseif ( $code !== $wgLanguageCode ) {
468 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
469 } else {
470 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
471 # other than language code.
472 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
475 # Conditions to fetch oversized pages to ignore them
476 $bigConds = $conds;
477 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
479 # Load titles for all oversized pages in the MediaWiki namespace
480 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__ . "($code)-big" );
481 foreach ( $res as $row ) {
482 $cache[$row->page_title] = '!TOO BIG';
485 # Conditions to load the remaining pages with their contents
486 $smallConds = $conds;
487 $smallConds[] = 'page_latest=rev_id';
488 $smallConds[] = 'rev_text_id=old_id';
489 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
491 $res = $dbr->select(
492 array( 'page', 'revision', 'text' ),
493 array( 'page_title', 'old_text', 'old_flags' ),
494 $smallConds,
495 __METHOD__ . "($code)-small"
498 foreach ( $res as $row ) {
499 $text = Revision::getRevisionText( $row );
500 if ( $text === false ) {
501 // Failed to fetch data; possible ES errors?
502 // Store a marker to fetch on-demand as a workaround...
503 $entry = '!TOO BIG';
504 wfDebugLog(
505 'MessageCache',
506 __METHOD__
507 . ": failed to load message page text for {$row->page_title} ($code)"
509 } else {
510 $entry = ' ' . $text;
512 $cache[$row->page_title] = $entry;
515 $cache['VERSION'] = MSG_CACHE_VERSION;
516 ksort( $cache );
517 $cache['HASH'] = md5( serialize( $cache ) );
518 $cache['EXPIRY'] = wfTimestamp( TS_MW, time() + $this->mExpiry );
520 return $cache;
524 * Updates cache as necessary when message page is changed
526 * @param string|bool $title Name of the page changed (false if deleted)
527 * @param mixed $text New contents of the page.
529 public function replace( $title, $text ) {
530 global $wgMaxMsgCacheEntrySize, $wgContLang, $wgLanguageCode;
532 if ( $this->mDisable ) {
533 return;
536 list( $msg, $code ) = $this->figureMessage( $title );
537 if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
538 // Content language overrides do not use the /<code> suffix
539 return;
542 // Note that if the cache is volatile, load() may trigger a DB fetch.
543 // In that case we reenter/reuse the existing cache key lock to avoid
544 // a self-deadlock. This is safe as no reads happen *directly* in this
545 // method between getReentrantScopedLock() and load() below. There is
546 // no risk of data "changing under our feet" for replace().
547 $cacheKey = wfMemcKey( 'messages', $code );
548 $scopedLock = $this->getReentrantScopedLock( $cacheKey );
549 $this->load( $code, self::FOR_UPDATE );
551 $titleKey = wfMemcKey( 'messages', 'individual', $title );
552 if ( $text === false ) {
553 // Article was deleted
554 $this->mCache[$code][$title] = '!NONEXISTENT';
555 $this->wanCache->delete( $titleKey );
556 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
557 // Check for size
558 $this->mCache[$code][$title] = '!TOO BIG';
559 $this->wanCache->set( $titleKey, ' ' . $text, $this->mExpiry );
560 } else {
561 $this->mCache[$code][$title] = ' ' . $text;
562 $this->wanCache->delete( $titleKey );
565 // Mark this cache as definitely "latest" (non-volatile) so
566 // load() calls do try to refresh the cache with slave data
567 $this->mCache[$code]['LATEST'] = time();
569 // Update caches if the lock was acquired
570 if ( $scopedLock ) {
571 $this->saveToCaches( $this->mCache[$code], 'all', $code );
574 ScopedCallback::consume( $scopedLock );
575 // Relay the purge to APC and other DCs
576 $this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
578 // Also delete cached sidebar... just in case it is affected
579 $codes = array( $code );
580 if ( $code === 'en' ) {
581 // Delete all sidebars, like for example on action=purge on the
582 // sidebar messages
583 $codes = array_keys( Language::fetchLanguageNames() );
586 foreach ( $codes as $code ) {
587 $sidebarKey = wfMemcKey( 'sidebar', $code );
588 $this->wanCache->delete( $sidebarKey );
591 // Update the message in the message blob store
592 $resourceloader = RequestContext::getMain()->getOutput()->getResourceLoader();
593 $blobStore = $resourceloader->getMessageBlobStore();
594 $blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
596 Hooks::run( 'MessageCacheReplace', array( $title, $text ) );
600 * Is the given cache array expired due to time passing or a version change?
602 * @param array $cache
603 * @return bool
605 protected function isCacheExpired( $cache ) {
606 if ( !isset( $cache['VERSION'] ) || !isset( $cache['EXPIRY'] ) ) {
607 return true;
609 if ( $cache['VERSION'] != MSG_CACHE_VERSION ) {
610 return true;
612 if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
613 return true;
616 return false;
620 * Shortcut to update caches.
622 * @param array $cache Cached messages with a version.
623 * @param string $dest Either "local-only" to save to local caches only
624 * or "all" to save to all caches.
625 * @param string|bool $code Language code (default: false)
626 * @return bool
628 protected function saveToCaches( array $cache, $dest, $code = false ) {
629 if ( $dest === 'all' ) {
630 $cacheKey = wfMemcKey( 'messages', $code );
631 $success = $this->mMemc->set( $cacheKey, $cache );
632 } else {
633 $success = true;
636 $this->setValidationHash( $code, $cache );
637 $this->saveToLocalCache( $code, $cache );
639 return $success;
643 * Get the md5 used to validate the local APC cache
645 * @param string $code
646 * @return array (hash or false, bool expiry/volatility status)
648 protected function getValidationHash( $code ) {
649 $curTTL = null;
650 $value = $this->wanCache->get(
651 wfMemcKey( 'messages', $code, 'hash', 'v1' ),
652 $curTTL,
653 array( wfMemcKey( 'messages', $code ) )
656 if ( !$value ) {
657 // No hash found at all; cache must regenerate to be safe
658 $hash = false;
659 $expired = true;
660 } else {
661 $hash = $value['hash'];
662 if ( ( time() - $value['latest'] ) < WANObjectCache::HOLDOFF_TTL ) {
663 // Cache was recently updated via replace() and should be up-to-date
664 $expired = false;
665 } else {
666 // See if the "check" key was bumped after the hash was generated
667 $expired = ( $curTTL < 0 );
671 return array( $hash, $expired );
675 * Set the md5 used to validate the local disk cache
677 * If $cache has a 'LATEST' UNIX timestamp key, then the hash will not
678 * be treated as "volatile" by getValidationHash() for the next few seconds
680 * @param string $code
681 * @param array $cache Cached messages with a version
683 protected function setValidationHash( $code, array $cache ) {
684 $this->wanCache->set(
685 wfMemcKey( 'messages', $code, 'hash', 'v1' ),
686 array(
687 'hash' => $cache['HASH'],
688 'latest' => isset( $cache['LATEST'] ) ? $cache['LATEST'] : 0
690 WANObjectCache::TTL_INDEFINITE
695 * @param string $key A language message cache key that stores blobs
696 * @param integer $timeout Wait timeout in seconds
697 * @return null|ScopedCallback
699 protected function getReentrantScopedLock( $key, $timeout = self::WAIT_SEC ) {
700 return $this->mMemc->getScopedLock( $key, $timeout, self::LOCK_TTL, __METHOD__ );
704 * Get a message from either the content language or the user language.
706 * First, assemble a list of languages to attempt getting the message from. This
707 * chain begins with the requested language and its fallbacks and then continues with
708 * the content language and its fallbacks. For each language in the chain, the following
709 * process will occur (in this order):
710 * 1. If a language-specific override, i.e., [[MW:msg/lang]], is available, use that.
711 * Note: for the content language, there is no /lang subpage.
712 * 2. Fetch from the static CDB cache.
713 * 3. If available, check the database for fallback language overrides.
715 * This process provides a number of guarantees. When changing this code, make sure all
716 * of these guarantees are preserved.
717 * * If the requested language is *not* the content language, then the CDB cache for that
718 * specific language will take precedence over the root database page ([[MW:msg]]).
719 * * Fallbacks will be just that: fallbacks. A fallback language will never be reached if
720 * the message is available *anywhere* in the language for which it is a fallback.
722 * @param string $key The message key
723 * @param bool $useDB If true, look for the message in the DB, false
724 * to use only the compiled l10n cache.
725 * @param bool|string|object $langcode Code of the language to get the message for.
726 * - If string and a valid code, will create a standard language object
727 * - If string but not a valid code, will create a basic language object
728 * - If boolean and false, create object from the current users language
729 * - If boolean and true, create object from the wikis content language
730 * - If language object, use it as given
731 * @param bool $isFullKey Specifies whether $key is a two part key "msg/lang".
733 * @throws MWException When given an invalid key
734 * @return string|bool False if the message doesn't exist, otherwise the
735 * message (which can be empty)
737 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
738 if ( is_int( $key ) ) {
739 // Fix numerical strings that somehow become ints
740 // on their way here
741 $key = (string)$key;
742 } elseif ( !is_string( $key ) ) {
743 throw new MWException( 'Non-string key given' );
744 } elseif ( $key === '' ) {
745 // Shortcut: the empty key is always missing
746 return false;
749 // For full keys, get the language code from the key
750 $pos = strrpos( $key, '/' );
751 if ( $isFullKey && $pos !== false ) {
752 $langcode = substr( $key, $pos + 1 );
753 $key = substr( $key, 0, $pos );
756 // Normalise title-case input (with some inlining)
757 $lckey = MessageCache::normalizeKey( $key );
759 Hooks::run( 'MessageCache::get', array( &$lckey ) );
761 // Loop through each language in the fallback list until we find something useful
762 $lang = wfGetLangObj( $langcode );
763 $message = $this->getMessageFromFallbackChain(
764 $lang,
765 $lckey,
766 !$this->mDisable && $useDB
769 // If we still have no message, maybe the key was in fact a full key so try that
770 if ( $message === false ) {
771 $parts = explode( '/', $lckey );
772 // We may get calls for things that are http-urls from sidebar
773 // Let's not load nonexistent languages for those
774 // They usually have more than one slash.
775 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
776 $message = Language::getMessageFor( $parts[0], $parts[1] );
777 if ( $message === null ) {
778 $message = false;
783 // Post-processing if the message exists
784 if ( $message !== false ) {
785 // Fix whitespace
786 $message = str_replace(
787 array(
788 # Fix for trailing whitespace, removed by textarea
789 '&#32;',
790 # Fix for NBSP, converted to space by firefox
791 '&nbsp;',
792 '&#160;',
794 array(
795 ' ',
796 "\xc2\xa0",
797 "\xc2\xa0"
799 $message
803 return $message;
807 * Given a language, try and fetch messages from that language.
809 * Will also consider fallbacks of that language, the site language, and fallbacks for
810 * the site language.
812 * @see MessageCache::get
813 * @param Language|StubObject $lang Preferred language
814 * @param string $lckey Lowercase key for the message (as for localisation cache)
815 * @param bool $useDB Whether to include messages from the wiki database
816 * @return string|bool The message, or false if not found
818 protected function getMessageFromFallbackChain( $lang, $lckey, $useDB ) {
819 global $wgLanguageCode, $wgContLang;
821 $uckey = $wgContLang->ucfirst( $lckey );
822 $langcode = $lang->getCode();
823 $message = false;
825 // First try the requested language.
826 if ( $useDB ) {
827 if ( $langcode === $wgLanguageCode ) {
828 // Messages created in the content language will not have the /lang extension
829 $message = $this->getMsgFromNamespace( $uckey, $langcode );
830 } else {
831 $message = $this->getMsgFromNamespace( "$uckey/$langcode", $langcode );
835 if ( $message !== false ) {
836 return $message;
839 // Check the CDB cache
840 $message = $lang->getMessage( $lckey );
841 if ( $message !== null ) {
842 return $message;
845 list( $fallbackChain, $siteFallbackChain ) =
846 Language::getFallbacksIncludingSiteLanguage( $langcode );
848 // Next try checking the database for all of the fallback languages of the requested language.
849 if ( $useDB ) {
850 foreach ( $fallbackChain as $code ) {
851 if ( $code === $wgLanguageCode ) {
852 // Messages created in the content language will not have the /lang extension
853 $message = $this->getMsgFromNamespace( $uckey, $code );
854 } else {
855 $message = $this->getMsgFromNamespace( "$uckey/$code", $code );
858 if ( $message !== false ) {
859 // Found the message.
860 return $message;
865 // Now try checking the site language.
866 if ( $useDB ) {
867 $message = $this->getMsgFromNamespace( $uckey, $wgLanguageCode );
868 if ( $message !== false ) {
869 return $message;
873 $message = $wgContLang->getMessage( $lckey );
874 if ( $message !== null ) {
875 return $message;
878 // Finally try the DB for the site language's fallbacks.
879 if ( $useDB ) {
880 foreach ( $siteFallbackChain as $code ) {
881 $message = $this->getMsgFromNamespace( "$uckey/$code", $code );
882 if ( $message === false && $code === $wgLanguageCode ) {
883 // Messages created in the content language will not have the /lang extension
884 $message = $this->getMsgFromNamespace( $uckey, $code );
887 if ( $message !== false ) {
888 // Found the message.
889 return $message;
894 return false;
898 * Get a message from the MediaWiki namespace, with caching. The key must
899 * first be converted to two-part lang/msg form if necessary.
901 * Unlike self::get(), this function doesn't resolve fallback chains, and
902 * some callers require this behavior. LanguageConverter::parseCachedTable()
903 * and self::get() are some examples in core.
905 * @param string $title Message cache key with initial uppercase letter.
906 * @param string $code Code denoting the language to try.
907 * @return string|bool The message, or false if it does not exist or on error
909 public function getMsgFromNamespace( $title, $code ) {
910 $this->load( $code );
911 if ( isset( $this->mCache[$code][$title] ) ) {
912 $entry = $this->mCache[$code][$title];
913 if ( substr( $entry, 0, 1 ) === ' ' ) {
914 // The message exists, so make sure a string
915 // is returned.
916 return (string)substr( $entry, 1 );
917 } elseif ( $entry === '!NONEXISTENT' ) {
918 return false;
919 } elseif ( $entry === '!TOO BIG' ) {
920 // Fall through and try invididual message cache below
922 } else {
923 // XXX: This is not cached in process cache, should it?
924 $message = false;
925 Hooks::run( 'MessagesPreLoad', array( $title, &$message ) );
926 if ( $message !== false ) {
927 return $message;
930 return false;
933 # Try the individual message cache
934 $titleKey = wfMemcKey( 'messages', 'individual', $title );
935 $entry = $this->wanCache->get( $titleKey );
936 if ( $entry ) {
937 if ( substr( $entry, 0, 1 ) === ' ' ) {
938 $this->mCache[$code][$title] = $entry;
940 // The message exists, so make sure a string
941 // is returned.
942 return (string)substr( $entry, 1 );
943 } elseif ( $entry === '!NONEXISTENT' ) {
944 $this->mCache[$code][$title] = '!NONEXISTENT';
946 return false;
947 } else {
948 # Corrupt/obsolete entry, delete it
949 $this->wanCache->delete( $titleKey );
953 # Try loading it from the database
954 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
955 if ( $revision ) {
956 $content = $revision->getContent();
957 if ( !$content ) {
958 // A possibly temporary loading failure.
959 wfDebugLog(
960 'MessageCache',
961 __METHOD__ . ": failed to load message page text for {$title} ($code)"
963 $message = null; // no negative caching
964 } else {
965 // XXX: Is this the right way to turn a Content object into a message?
966 // NOTE: $content is typically either WikitextContent, JavaScriptContent or
967 // CssContent. MessageContent is *not* used for storing messages, it's
968 // only used for wrapping them when needed.
969 $message = $content->getWikitextForTransclusion();
971 if ( $message === false || $message === null ) {
972 wfDebugLog(
973 'MessageCache',
974 __METHOD__ . ": message content doesn't provide wikitext "
975 . "(content model: " . $content->getModel() . ")"
978 $message = false; // negative caching
979 } else {
980 $this->mCache[$code][$title] = ' ' . $message;
981 $this->wanCache->set( $titleKey, ' ' . $message, $this->mExpiry );
984 } else {
985 $message = false; // negative caching
988 if ( $message === false ) { // negative caching
989 $this->mCache[$code][$title] = '!NONEXISTENT';
990 $this->wanCache->set( $titleKey, '!NONEXISTENT', $this->mExpiry );
993 return $message;
997 * @param string $message
998 * @param bool $interface
999 * @param string $language Language code
1000 * @param Title $title
1001 * @return string
1003 function transform( $message, $interface = false, $language = null, $title = null ) {
1004 // Avoid creating parser if nothing to transform
1005 if ( strpos( $message, '{{' ) === false ) {
1006 return $message;
1009 if ( $this->mInParser ) {
1010 return $message;
1013 $parser = $this->getParser();
1014 if ( $parser ) {
1015 $popts = $this->getParserOptions();
1016 $popts->setInterfaceMessage( $interface );
1017 $popts->setTargetLanguage( $language );
1019 $userlang = $popts->setUserLang( $language );
1020 $this->mInParser = true;
1021 $message = $parser->transformMsg( $message, $popts, $title );
1022 $this->mInParser = false;
1023 $popts->setUserLang( $userlang );
1026 return $message;
1030 * @return Parser
1032 function getParser() {
1033 global $wgParser, $wgParserConf;
1034 if ( !$this->mParser && isset( $wgParser ) ) {
1035 # Do some initialisation so that we don't have to do it twice
1036 $wgParser->firstCallInit();
1037 # Clone it and store it
1038 $class = $wgParserConf['class'];
1039 if ( $class == 'ParserDiffTest' ) {
1040 # Uncloneable
1041 $this->mParser = new $class( $wgParserConf );
1042 } else {
1043 $this->mParser = clone $wgParser;
1047 return $this->mParser;
1051 * @param string $text
1052 * @param Title $title
1053 * @param bool $linestart Whether or not this is at the start of a line
1054 * @param bool $interface Whether this is an interface message
1055 * @param string $language Language code
1056 * @return ParserOutput|string
1058 public function parse( $text, $title = null, $linestart = true,
1059 $interface = false, $language = null
1061 if ( $this->mInParser ) {
1062 return htmlspecialchars( $text );
1065 $parser = $this->getParser();
1066 $popts = $this->getParserOptions();
1067 $popts->setInterfaceMessage( $interface );
1068 $popts->setTargetLanguage( $language );
1070 if ( !$title || !$title instanceof Title ) {
1071 global $wgTitle;
1072 wfDebugLog( 'GlobalTitleFail', __METHOD__ . ' called by ' .
1073 wfGetAllCallers( 5 ) . ' with no title set.' );
1074 $title = $wgTitle;
1076 // Sometimes $wgTitle isn't set either...
1077 if ( !$title ) {
1078 # It's not uncommon having a null $wgTitle in scripts. See r80898
1079 # Create a ghost title in such case
1080 $title = Title::makeTitle( NS_SPECIAL, 'Badtitle/title not set in ' . __METHOD__ );
1083 $this->mInParser = true;
1084 $res = $parser->parse( $text, $title, $popts, $linestart );
1085 $this->mInParser = false;
1087 return $res;
1090 function disable() {
1091 $this->mDisable = true;
1094 function enable() {
1095 $this->mDisable = false;
1099 * Clear all stored messages. Mainly used after a mass rebuild.
1101 function clear() {
1102 $langs = Language::fetchLanguageNames( null, 'mw' );
1103 foreach ( array_keys( $langs ) as $code ) {
1104 # Global and local caches
1105 $this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
1108 $this->mLoadedLanguages = array();
1112 * @param string $key
1113 * @return array
1115 public function figureMessage( $key ) {
1116 global $wgLanguageCode;
1118 $pieces = explode( '/', $key );
1119 if ( count( $pieces ) < 2 ) {
1120 return array( $key, $wgLanguageCode );
1123 $lang = array_pop( $pieces );
1124 if ( !Language::fetchLanguageName( $lang, null, 'mw' ) ) {
1125 return array( $key, $wgLanguageCode );
1128 $message = implode( '/', $pieces );
1130 return array( $message, $lang );
1134 * Get all message keys stored in the message cache for a given language.
1135 * If $code is the content language code, this will return all message keys
1136 * for which MediaWiki:msgkey exists. If $code is another language code, this
1137 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
1138 * @param string $code Language code
1139 * @return array Array of message keys (strings)
1141 public function getAllMessageKeys( $code ) {
1142 global $wgContLang;
1143 $this->load( $code );
1144 if ( !isset( $this->mCache[$code] ) ) {
1145 // Apparently load() failed
1146 return null;
1148 // Remove administrative keys
1149 $cache = $this->mCache[$code];
1150 unset( $cache['VERSION'] );
1151 unset( $cache['EXPIRY'] );
1152 // Remove any !NONEXISTENT keys
1153 $cache = array_diff( $cache, array( '!NONEXISTENT' ) );
1155 // Keys may appear with a capital first letter. lcfirst them.
1156 return array_map( array( $wgContLang, 'lcfirst' ), array_keys( $cache ) );