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
23 use MediaWiki\MediaWikiServices
;
24 use Wikimedia\ScopedCallback
;
25 use MediaWiki\Logger\LoggerFactory
;
26 use Wikimedia\Rdbms\Database
;
29 * MediaWiki message cache structure version.
30 * Bump this whenever the message cache format has changed.
32 define( 'MSG_CACHE_VERSION', 2 );
35 * Cache of messages that are defined by MediaWiki namespace pages or by hooks
37 * Performs various MediaWiki namespace-related functions
41 const FOR_UPDATE
= 1; // force message reload
43 /** How long to wait for memcached locks */
45 /** How long memcached locks last */
49 * Process cache of loaded messages that are defined in MediaWiki namespace
51 * @var MapCacheLRU Map of (language code => key => " <MESSAGE>" or "!TOO BIG")
56 * @var bool[] Map of (language code => boolean)
58 protected $cacheVolatile = [];
61 * Should mean that database cannot be used, but check
67 * Lifetime for cache, used by object caching.
68 * Set on construction, see __construct().
73 * Message cache has its own parser which it uses to transform messages
76 protected $mParserOptions;
81 * @var bool $mInParser
83 protected $mInParser = false;
85 /** @var WANObjectCache */
88 protected $clusterCache;
97 * @var MessageCache $instance
99 private static $instance;
102 * Get the signleton instance of this class
105 * @return MessageCache
107 public static function singleton() {
108 if ( self
::$instance === null ) {
109 global $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgUseLocalMessageCache;
110 self
::$instance = new self(
111 MediaWikiServices
::getInstance()->getMainWANObjectCache(),
112 wfGetMessageCacheStorage(),
113 $wgUseLocalMessageCache
114 ? MediaWikiServices
::getInstance()->getLocalServerObjectCache()
115 : new EmptyBagOStuff(),
116 $wgUseDatabaseMessages,
118 MediaWikiServices
::getInstance()->getContentLanguage()
122 return self
::$instance;
126 * Destroy the singleton instance
130 public static function destroyInstance() {
131 self
::$instance = null;
135 * Normalize message key input
137 * @param string $key Input message key to be normalized
138 * @return string Normalized message key
140 public static function normalizeKey( $key ) {
141 $lckey = strtr( $key, ' ', '_' );
142 if ( ord( $lckey ) < 128 ) {
143 $lckey[0] = strtolower( $lckey[0] );
145 $lckey = MediaWikiServices
::getInstance()->getContentLanguage()->lcfirst( $lckey );
152 * @param WANObjectCache $wanCache
153 * @param BagOStuff $clusterCache
154 * @param BagOStuff $serverCache
155 * @param bool $useDB Whether to look for message overrides (e.g. MediaWiki: pages)
156 * @param int $expiry Lifetime for cache. @see $mExpiry.
157 * @param Language|null $contLang Content language of site
159 public function __construct(
160 WANObjectCache
$wanCache,
161 BagOStuff
$clusterCache,
162 BagOStuff
$serverCache,
165 Language
$contLang = null
167 $this->wanCache
= $wanCache;
168 $this->clusterCache
= $clusterCache;
169 $this->srvCache
= $serverCache;
171 $this->cache
= new MapCacheLRU( 5 ); // limit size for sanity
173 $this->mDisable
= !$useDB;
174 $this->mExpiry
= $expiry;
175 $this->contLang
= $contLang ?? MediaWikiServices
::getInstance()->getContentLanguage();
179 * ParserOptions is lazy initialised.
181 * @return ParserOptions
183 function getParserOptions() {
186 if ( !$this->mParserOptions
) {
187 if ( !$wgUser->isSafeToLoad() ) {
188 // $wgUser isn't unstubbable yet, so don't try to get a
189 // ParserOptions for it. And don't cache this ParserOptions
191 $po = ParserOptions
::newFromAnon();
192 $po->setAllowUnsafeRawHtml( false );
196 $this->mParserOptions
= new ParserOptions
;
197 // Messages may take parameters that could come
198 // from malicious sources. As a precaution, disable
199 // the <html> parser tag when parsing messages.
200 $this->mParserOptions
->setAllowUnsafeRawHtml( false );
203 return $this->mParserOptions
;
207 * Try to load the cache from APC.
209 * @param string $code Optional language code, see documenation of load().
210 * @return array|bool The cache array, or false if not in cache.
212 protected function getLocalCache( $code ) {
213 $cacheKey = $this->srvCache
->makeKey( __CLASS__
, $code );
215 return $this->srvCache
->get( $cacheKey );
219 * Save the cache to APC.
221 * @param string $code
222 * @param array $cache The cache array
224 protected function saveToLocalCache( $code, $cache ) {
225 $cacheKey = $this->srvCache
->makeKey( __CLASS__
, $code );
226 $this->srvCache
->set( $cacheKey, $cache );
230 * Loads messages from caches or from database in this order:
231 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
233 * (3) from the database.
235 * When successfully loading from (2) or (3), all higher level caches are
236 * updated for the newest version.
238 * Nothing is loaded if member variable mDisable is true, either manually
239 * set by calling code or if message loading fails (is this possible?).
241 * Returns true if cache is already populated or it was successfully populated,
242 * or false if populating empty cache fails. Also returns true if MessageCache
245 * @param string $code Language to which load messages
246 * @param int|null $mode Use MessageCache::FOR_UPDATE to skip process cache [optional]
247 * @throws InvalidArgumentException
250 protected function load( $code, $mode = null ) {
251 if ( !is_string( $code ) ) {
252 throw new InvalidArgumentException( "Missing language code" );
255 # Don't do double loading...
256 if ( $this->cache
->has( $code ) && $mode != self
::FOR_UPDATE
) {
260 # 8 lines of code just to say (once) that message cache is disabled
261 if ( $this->mDisable
) {
262 static $shownDisabled = false;
263 if ( !$shownDisabled ) {
264 wfDebug( __METHOD__
. ": disabled\n" );
265 $shownDisabled = true;
271 # Loading code starts
272 $success = false; # Keep track of success
273 $staleCache = false; # a cache array with expired data, or false if none has been loaded
274 $where = []; # Debug info, delayed to avoid spamming debug log too much
276 # Hash of the contents is stored in memcache, to detect if data-center cache
277 # or local cache goes out of date (e.g. due to replace() on some other server)
278 list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
279 $this->cacheVolatile
[$code] = $hashVolatile;
281 # Try the local cache and check against the cluster hash key...
282 $cache = $this->getLocalCache( $code );
284 $where[] = 'local cache is empty';
285 } elseif ( !isset( $cache['HASH'] ) ||
$cache['HASH'] !== $hash ) {
286 $where[] = 'local cache has the wrong hash';
287 $staleCache = $cache;
288 } elseif ( $this->isCacheExpired( $cache ) ) {
289 $where[] = 'local cache is expired';
290 $staleCache = $cache;
291 } elseif ( $hashVolatile ) {
292 $where[] = 'local cache validation key is expired/volatile';
293 $staleCache = $cache;
295 $where[] = 'got from local cache';
296 $this->cache
->set( $code, $cache );
301 $cacheKey = $this->clusterCache
->makeKey( 'messages', $code );
302 # Try the global cache. If it is empty, try to acquire a lock. If
303 # the lock can't be acquired, wait for the other thread to finish
304 # and then try the global cache a second time.
305 for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++
) {
306 if ( $hashVolatile && $staleCache ) {
307 # Do not bother fetching the whole cache blob to avoid I/O.
308 # Instead, just try to get the non-blocking $statusKey lock
309 # below, and use the local stale value if it was not acquired.
310 $where[] = 'global cache is presumed expired';
312 $cache = $this->clusterCache
->get( $cacheKey );
314 $where[] = 'global cache is empty';
315 } elseif ( $this->isCacheExpired( $cache ) ) {
316 $where[] = 'global cache is expired';
317 $staleCache = $cache;
318 } elseif ( $hashVolatile ) {
319 # DB results are replica DB lag prone until the holdoff TTL passes.
320 # By then, updates should be reflected in loadFromDBWithLock().
321 # One thread renerates the cache while others use old values.
322 $where[] = 'global cache is expired/volatile';
323 $staleCache = $cache;
325 $where[] = 'got from global cache';
326 $this->cache
->set( $code, $cache );
327 $this->saveToCaches( $cache, 'local-only', $code );
333 # Done, no need to retry
337 # We need to call loadFromDB. Limit the concurrency to one process.
338 # This prevents the site from going down when the cache expires.
339 # Note that the DB slam protection lock here is non-blocking.
340 $loadStatus = $this->loadFromDBWithLock( $code, $where, $mode );
341 if ( $loadStatus === true ) {
344 } elseif ( $staleCache ) {
345 # Use the stale cache while some other thread constructs the new one
346 $where[] = 'using stale cache';
347 $this->cache
->set( $code, $staleCache );
350 } elseif ( $failedAttempts > 0 ) {
351 # Already blocked once, so avoid another lock/unlock cycle.
352 # This case will typically be hit if memcached is down, or if
353 # loadFromDB() takes longer than LOCK_WAIT.
354 $where[] = "could not acquire status key.";
356 } elseif ( $loadStatus === 'cantacquire' ) {
357 # Wait for the other thread to finish, then retry. Normally,
358 # the memcached get() will then yeild the other thread's result.
359 $where[] = 'waited for other thread to complete';
360 $this->getReentrantScopedLock( $cacheKey );
362 # Disable cache; $loadStatus is 'disabled'
369 $where[] = 'loading FAILED - cache is disabled';
370 $this->mDisable
= true;
371 $this->cache
->set( $code, [] );
372 wfDebugLog( 'MessageCacheError', __METHOD__
. ": Failed to load $code\n" );
373 # This used to throw an exception, but that led to nasty side effects like
374 # the whole wiki being instantly down if the memcached server died
377 if ( !$this->cache
->has( $code ) ) { // sanity
378 throw new LogicException( "Process cache for '$code' should be set by now." );
381 $info = implode( ', ', $where );
382 wfDebugLog( 'MessageCache', __METHOD__
. ": Loading $code... $info\n" );
388 * @param string $code
389 * @param array &$where List of wfDebug() comments
390 * @param int|null $mode Use MessageCache::FOR_UPDATE to use DB_MASTER
391 * @return bool|string True on success or one of ("cantacquire", "disabled")
393 protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
394 # If cache updates on all levels fail, give up on message overrides.
395 # This is to avoid easy site outages; see $saveSuccess comments below.
396 $statusKey = $this->clusterCache
->makeKey( 'messages', $code, 'status' );
397 $status = $this->clusterCache
->get( $statusKey );
398 if ( $status === 'error' ) {
399 $where[] = "could not load; method is still globally disabled";
403 # Now let's regenerate
404 $where[] = 'loading from database';
406 # Lock the cache to prevent conflicting writes.
407 # This lock is non-blocking so stale cache can quickly be used.
408 # Note that load() will call a blocking getReentrantScopedLock()
409 # after this if it really need to wait for any current thread.
410 $cacheKey = $this->clusterCache
->makeKey( 'messages', $code );
411 $scopedLock = $this->getReentrantScopedLock( $cacheKey, 0 );
412 if ( !$scopedLock ) {
413 $where[] = 'could not acquire main lock';
414 return 'cantacquire';
417 $cache = $this->loadFromDB( $code, $mode );
418 $this->cache
->set( $code, $cache );
419 $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
421 if ( !$saveSuccess ) {
423 * Cache save has failed.
425 * There are two main scenarios where this could be a problem:
426 * - The cache is more than the maximum size (typically 1MB compressed).
427 * - Memcached has no space remaining in the relevant slab class. This is
428 * unlikely with recent versions of memcached.
430 * Either way, if there is a local cache, nothing bad will happen. If there
431 * is no local cache, disabling the message cache for all requests avoids
432 * incurring a loadFromDB() overhead on every request, and thus saves the
433 * wiki from complete downtime under moderate traffic conditions.
435 if ( $this->srvCache
instanceof EmptyBagOStuff
) {
436 $this->clusterCache
->set( $statusKey, 'error', 60 * 5 );
437 $where[] = 'could not save cache, disabled globally for 5 minutes';
439 $where[] = "could not save global cache";
447 * Loads cacheable messages from the database. Messages bigger than
448 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
449 * on-demand from the database later.
451 * @param string $code Language code
452 * @param int|null $mode Use MessageCache::FOR_UPDATE to skip process cache
453 * @return array Loaded messages for storing in caches
455 protected function loadFromDB( $code, $mode = null ) {
456 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
458 // (T164666) The query here performs really poorly on WMF's
459 // contributions replicas. We don't have a way to say "any group except
460 // contributions", so for the moment let's specify 'api'.
461 // @todo: Get rid of this hack.
462 $dbr = wfGetDB( ( $mode == self
::FOR_UPDATE
) ? DB_MASTER
: DB_REPLICA
, 'api' );
468 'page_is_redirect' => 0,
469 'page_namespace' => NS_MEDIAWIKI
,
473 if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
474 if ( !$this->cache
->has( $wgLanguageCode ) ) {
475 $this->load( $wgLanguageCode );
477 $mostused = array_keys( $this->cache
->get( $wgLanguageCode ) );
478 foreach ( $mostused as $key => $value ) {
479 $mostused[$key] = "$value/$code";
483 if ( count( $mostused ) ) {
484 $conds['page_title'] = $mostused;
485 } elseif ( $code !== $wgLanguageCode ) {
486 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
488 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
489 # other than language code.
490 $conds[] = 'page_title NOT' .
491 $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
494 # Conditions to fetch oversized pages to ignore them
496 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
498 # Load titles for all oversized pages in the MediaWiki namespace
501 [ 'page_title', 'page_latest' ],
503 __METHOD__
. "($code)-big"
505 foreach ( $res as $row ) {
506 $cache[$row->page_title
] = '!TOO BIG';
507 // At least include revision ID so page changes are reflected in the hash
508 $cache['EXCESSIVE'][$row->page_title
] = $row->page_latest
;
511 # Conditions to load the remaining pages with their contents
512 $smallConds = $conds;
513 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
516 [ 'page', 'revision', 'text' ],
517 [ 'page_title', 'old_id', 'old_text', 'old_flags' ],
519 __METHOD__
. "($code)-small",
522 'revision' => [ 'JOIN', 'page_latest=rev_id' ],
523 'text' => [ 'JOIN', 'rev_text_id=old_id' ],
527 foreach ( $res as $row ) {
528 $text = Revision
::getRevisionText( $row );
529 if ( $text === false ) {
530 // Failed to fetch data; possible ES errors?
531 // Store a marker to fetch on-demand as a workaround...
532 // TODO Use a differnt marker
537 . ": failed to load message page text for {$row->page_title} ($code)"
540 $entry = ' ' . $text;
542 $cache[$row->page_title
] = $entry;
545 $cache['VERSION'] = MSG_CACHE_VERSION
;
548 # Hash for validating local cache (APC). No need to take into account
549 # messages larger than $wgMaxMsgCacheEntrySize, since those are only
550 # stored and fetched from memcache.
551 $cache['HASH'] = md5( serialize( $cache ) );
552 $cache['EXPIRY'] = wfTimestamp( TS_MW
, time() +
$this->mExpiry
);
558 * Updates cache as necessary when message page is changed
560 * @param string $title Message cache key with initial uppercase letter
561 * @param string|bool $text New contents of the page (false if deleted)
563 public function replace( $title, $text ) {
564 global $wgLanguageCode;
566 if ( $this->mDisable
) {
570 list( $msg, $code ) = $this->figureMessage( $title );
571 if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
572 // Content language overrides do not use the /<code> suffix
576 // (a) Update the process cache with the new message text
577 if ( $text === false ) {
579 $this->cache
->setField( $code, $title, '!NONEXISTENT' );
581 // Ignore $wgMaxMsgCacheEntrySize so the process cache is up to date
582 $this->cache
->setField( $code, $title, ' ' . $text );
585 // (b) Update the shared caches in a deferred update with a fresh DB snapshot
586 DeferredUpdates
::addCallableUpdate(
587 function () use ( $title, $msg, $code ) {
588 global $wgMaxMsgCacheEntrySize;
589 // Allow one caller at a time to avoid race conditions
590 $scopedLock = $this->getReentrantScopedLock(
591 $this->clusterCache
->makeKey( 'messages', $code )
593 if ( !$scopedLock ) {
594 LoggerFactory
::getInstance( 'MessageCache' )->error(
595 __METHOD__
. ': could not acquire lock to update {title} ({code})',
596 [ 'title' => $title, 'code' => $code ] );
599 // Reload messages from the database and pre-populate dc-local caches
600 // as optimisation. Use the master DB to avoid race conditions.
601 $cache = $this->loadFromDB( $code, self
::FOR_UPDATE
);
602 // Check if an individual cache key should exist and update cache accordingly
603 $page = WikiPage
::factory( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
604 $page->loadPageData( $page::READ_LATEST
);
605 $text = $this->getMessageTextFromContent( $page->getContent() );
606 if ( is_string( $text ) && strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
607 // Match logic of loadCachedMessagePageEntry()
608 $this->wanCache
->set(
609 $this->bigMessageCacheKey( $cache['HASH'], $title ),
614 // Mark this cache as definitely being "latest" (non-volatile) so
615 // load() calls do not try to refresh the cache with replica DB data
616 $cache['LATEST'] = time();
617 // Update the process cache
618 $this->cache
->set( $code, $cache );
619 // Pre-emptively update the local datacenter cache so things like edit filter and
620 // blacklist changes are reflected immediately; these often use MediaWiki: pages.
621 // The datacenter handling replace() calls should be the same one handling edits
622 // as they require HTTP POST.
623 $this->saveToCaches( $cache, 'all', $code );
624 // Release the lock now that the cache is saved
625 ScopedCallback
::consume( $scopedLock );
627 // Relay the purge. Touching this check key expires cache contents
628 // and local cache (APC) validation hash across all datacenters.
629 $this->wanCache
->touchCheckKey( $this->getCheckKey( $code ) );
631 // Purge the message in the message blob store
632 $resourceloader = RequestContext
::getMain()->getOutput()->getResourceLoader();
633 $blobStore = $resourceloader->getMessageBlobStore();
634 $blobStore->updateMessage( $this->contLang
->lcfirst( $msg ) );
636 Hooks
::run( 'MessageCacheReplace', [ $title, $text ] );
638 DeferredUpdates
::PRESEND
643 * Is the given cache array expired due to time passing or a version change?
645 * @param array $cache
648 protected function isCacheExpired( $cache ) {
649 if ( !isset( $cache['VERSION'] ) ||
!isset( $cache['EXPIRY'] ) ) {
652 if ( $cache['VERSION'] != MSG_CACHE_VERSION
) {
655 if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
663 * Shortcut to update caches.
665 * @param array $cache Cached messages with a version.
666 * @param string $dest Either "local-only" to save to local caches only
667 * or "all" to save to all caches.
668 * @param string|bool $code Language code (default: false)
671 protected function saveToCaches( array $cache, $dest, $code = false ) {
672 if ( $dest === 'all' ) {
673 $cacheKey = $this->clusterCache
->makeKey( 'messages', $code );
674 $success = $this->clusterCache
->set( $cacheKey, $cache );
675 $this->setValidationHash( $code, $cache );
680 $this->saveToLocalCache( $code, $cache );
686 * Get the md5 used to validate the local APC cache
688 * @param string $code
689 * @return array (hash or false, bool expiry/volatility status)
691 protected function getValidationHash( $code ) {
693 $value = $this->wanCache
->get(
694 $this->wanCache
->makeKey( 'messages', $code, 'hash', 'v1' ),
696 [ $this->getCheckKey( $code ) ]
700 $hash = $value['hash'];
701 if ( ( time() - $value['latest'] ) < WANObjectCache
::TTL_MINUTE
) {
702 // Cache was recently updated via replace() and should be up-to-date.
703 // That method is only called in the primary datacenter and uses FOR_UPDATE.
704 // Also, it is unlikely that the current datacenter is *now* secondary one.
707 // See if the "check" key was bumped after the hash was generated
708 $expired = ( $curTTL < 0 );
711 // No hash found at all; cache must regenerate to be safe
716 return [ $hash, $expired ];
720 * Set the md5 used to validate the local disk cache
722 * If $cache has a 'LATEST' UNIX timestamp key, then the hash will not
723 * be treated as "volatile" by getValidationHash() for the next few seconds.
724 * This is triggered when $cache is generated using FOR_UPDATE mode.
726 * @param string $code
727 * @param array $cache Cached messages with a version
729 protected function setValidationHash( $code, array $cache ) {
730 $this->wanCache
->set(
731 $this->wanCache
->makeKey( 'messages', $code, 'hash', 'v1' ),
733 'hash' => $cache['HASH'],
734 'latest' => $cache['LATEST'] ??
0
736 WANObjectCache
::TTL_INDEFINITE
741 * @param string $key A language message cache key that stores blobs
742 * @param int $timeout Wait timeout in seconds
743 * @return null|ScopedCallback
745 protected function getReentrantScopedLock( $key, $timeout = self
::WAIT_SEC
) {
746 return $this->clusterCache
->getScopedLock( $key, $timeout, self
::LOCK_TTL
, __METHOD__
);
750 * Get a message from either the content language or the user language.
752 * First, assemble a list of languages to attempt getting the message from. This
753 * chain begins with the requested language and its fallbacks and then continues with
754 * the content language and its fallbacks. For each language in the chain, the following
755 * process will occur (in this order):
756 * 1. If a language-specific override, i.e., [[MW:msg/lang]], is available, use that.
757 * Note: for the content language, there is no /lang subpage.
758 * 2. Fetch from the static CDB cache.
759 * 3. If available, check the database for fallback language overrides.
761 * This process provides a number of guarantees. When changing this code, make sure all
762 * of these guarantees are preserved.
763 * * If the requested language is *not* the content language, then the CDB cache for that
764 * specific language will take precedence over the root database page ([[MW:msg]]).
765 * * Fallbacks will be just that: fallbacks. A fallback language will never be reached if
766 * the message is available *anywhere* in the language for which it is a fallback.
768 * @param string $key The message key
769 * @param bool $useDB If true, look for the message in the DB, false
770 * to use only the compiled l10n cache.
771 * @param bool|string|object $langcode Code of the language to get the message for.
772 * - If string and a valid code, will create a standard language object
773 * - If string but not a valid code, will create a basic language object
774 * - If boolean and false, create object from the current users language
775 * - If boolean and true, create object from the wikis content language
776 * - If language object, use it as given
777 * @param bool $isFullKey Specifies whether $key is a two part key "msg/lang".
779 * @throws MWException When given an invalid key
780 * @return string|bool False if the message doesn't exist, otherwise the
781 * message (which can be empty)
783 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
784 if ( is_int( $key ) ) {
785 // Fix numerical strings that somehow become ints
788 } elseif ( !is_string( $key ) ) {
789 throw new MWException( 'Non-string key given' );
790 } elseif ( $key === '' ) {
791 // Shortcut: the empty key is always missing
795 // For full keys, get the language code from the key
796 $pos = strrpos( $key, '/' );
797 if ( $isFullKey && $pos !== false ) {
798 $langcode = substr( $key, $pos +
1 );
799 $key = substr( $key, 0, $pos );
802 // Normalise title-case input (with some inlining)
803 $lckey = self
::normalizeKey( $key );
805 Hooks
::run( 'MessageCache::get', [ &$lckey ] );
807 // Loop through each language in the fallback list until we find something useful
808 $lang = wfGetLangObj( $langcode );
809 $message = $this->getMessageFromFallbackChain(
812 !$this->mDisable
&& $useDB
815 // If we still have no message, maybe the key was in fact a full key so try that
816 if ( $message === false ) {
817 $parts = explode( '/', $lckey );
818 // We may get calls for things that are http-urls from sidebar
819 // Let's not load nonexistent languages for those
820 // They usually have more than one slash.
821 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
822 $message = Language
::getMessageFor( $parts[0], $parts[1] );
823 if ( $message === null ) {
829 // Post-processing if the message exists
830 if ( $message !== false ) {
832 $message = str_replace(
834 # Fix for trailing whitespace, removed by textarea
836 # Fix for NBSP, converted to space by firefox
855 * Given a language, try and fetch messages from that language.
857 * Will also consider fallbacks of that language, the site language, and fallbacks for
860 * @see MessageCache::get
861 * @param Language|StubObject $lang Preferred language
862 * @param string $lckey Lowercase key for the message (as for localisation cache)
863 * @param bool $useDB Whether to include messages from the wiki database
864 * @return string|bool The message, or false if not found
866 protected function getMessageFromFallbackChain( $lang, $lckey, $useDB ) {
869 // First try the requested language.
870 $message = $this->getMessageForLang( $lang, $lckey, $useDB, $alreadyTried );
871 if ( $message !== false ) {
875 // Now try checking the site language.
876 $message = $this->getMessageForLang( $this->contLang
, $lckey, $useDB, $alreadyTried );
881 * Given a language, try and fetch messages from that language and its fallbacks.
883 * @see MessageCache::get
884 * @param Language|StubObject $lang Preferred language
885 * @param string $lckey Lowercase key for the message (as for localisation cache)
886 * @param bool $useDB Whether to include messages from the wiki database
887 * @param bool[] $alreadyTried Contains true for each language that has been tried already
888 * @return string|bool The message, or false if not found
890 private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
891 $langcode = $lang->getCode();
893 // Try checking the database for the requested language
895 $uckey = $this->contLang
->ucfirst( $lckey );
897 if ( !isset( $alreadyTried[$langcode] ) ) {
898 $message = $this->getMsgFromNamespace(
899 $this->getMessagePageName( $langcode, $uckey ),
903 if ( $message !== false ) {
906 $alreadyTried[$langcode] = true;
912 // Check the CDB cache
913 $message = $lang->getMessage( $lckey );
914 if ( $message !== null ) {
918 // Try checking the database for all of the fallback languages
920 $fallbackChain = Language
::getFallbacksFor( $langcode );
922 foreach ( $fallbackChain as $code ) {
923 if ( isset( $alreadyTried[$code] ) ) {
927 $message = $this->getMsgFromNamespace(
928 $this->getMessagePageName( $code, $uckey ), $code );
930 if ( $message !== false ) {
933 $alreadyTried[$code] = true;
941 * Get the message page name for a given language
943 * @param string $langcode
944 * @param string $uckey Uppercase key for the message
945 * @return string The page name
947 private function getMessagePageName( $langcode, $uckey ) {
948 global $wgLanguageCode;
950 if ( $langcode === $wgLanguageCode ) {
951 // Messages created in the content language will not have the /lang extension
954 return "$uckey/$langcode";
959 * Get a message from the MediaWiki namespace, with caching. The key must
960 * first be converted to two-part lang/msg form if necessary.
962 * Unlike self::get(), this function doesn't resolve fallback chains, and
963 * some callers require this behavior. LanguageConverter::parseCachedTable()
964 * and self::get() are some examples in core.
966 * @param string $title Message cache key with initial uppercase letter
967 * @param string $code Code denoting the language to try
968 * @return string|bool The message, or false if it does not exist or on error
970 public function getMsgFromNamespace( $title, $code ) {
971 // Load all MediaWiki page definitions into cache. Note that individual keys
972 // already loaded into cache during this request remain in the cache, which
973 // includes the value of hook-defined messages.
974 $this->load( $code );
976 $entry = $this->cache
->getField( $code, $title );
977 if ( $entry !== null ) {
978 if ( substr( $entry, 0, 1 ) === ' ' ) {
979 // The message exists and is not '!TOO BIG'
980 return (string)substr( $entry, 1 );
981 } elseif ( $entry === '!NONEXISTENT' ) {
984 // Fall through and try invididual message cache below
986 // Message does not have a MediaWiki page definition
988 Hooks
::run( 'MessagesPreLoad', [ $title, &$message, $code ] );
989 if ( $message !== false ) {
990 $this->cache
->setField( $code, $title, ' ' . $message );
992 $this->cache
->setField( $code, $title, '!NONEXISTENT' );
998 if ( $this->cacheVolatile
[$code] ) {
1000 // Make sure that individual keys respect the WAN cache holdoff period too
1001 LoggerFactory
::getInstance( 'MessageCache' )->debug(
1002 __METHOD__
. ': loading volatile key \'{titleKey}\'',
1003 [ 'titleKey' => $title, 'code' => $code ] );
1005 // Try the individual message cache
1006 $entry = $this->loadCachedMessagePageEntry(
1009 $this->cache
->getField( $code, 'HASH' )
1013 if ( $entry !== false && substr( $entry, 0, 1 ) === ' ' ) {
1014 $this->cache
->setField( $code, $title, $entry );
1015 // The message exists, so make sure a string is returned
1016 return (string)substr( $entry, 1 );
1019 $this->cache
->setField( $code, $title, '!NONEXISTENT' );
1025 * @param string $dbKey
1026 * @param string $code
1027 * @param string $hash
1028 * @return string Either " <MESSAGE>" or "!NONEXISTANT"
1030 private function loadCachedMessagePageEntry( $dbKey, $code, $hash ) {
1031 return $this->srvCache
->getWithSetCallback(
1032 $this->srvCache
->makeKey( 'messages-big', $hash, $dbKey ),
1033 IExpiringStore
::TTL_MINUTE
,
1034 function () use ( $code, $dbKey, $hash ) {
1035 return $this->wanCache
->getWithSetCallback(
1036 $this->bigMessageCacheKey( $hash, $dbKey ),
1038 function ( $oldValue, &$ttl, &$setOpts ) use ( $dbKey, $code ) {
1039 // Try loading the message from the database
1040 $dbr = wfGetDB( DB_REPLICA
);
1041 $setOpts +
= Database
::getCacheSetOptions( $dbr );
1042 // Use newKnownCurrent() to avoid querying revision/user tables
1043 $title = Title
::makeTitle( NS_MEDIAWIKI
, $dbKey );
1044 $revision = Revision
::newKnownCurrent( $dbr, $title );
1046 // The wiki doesn't have a local override page. Cache absence with normal TTL.
1047 // When overrides are created, self::replace() takes care of the cache.
1048 return '!NONEXISTENT';
1050 $content = $revision->getContent();
1052 $message = $this->getMessageTextFromContent( $content );
1054 LoggerFactory
::getInstance( 'MessageCache' )->warning(
1055 __METHOD__
. ': failed to load page text for \'{titleKey}\'',
1056 [ 'titleKey' => $dbKey, 'code' => $code ]
1061 if ( !is_string( $message ) ) {
1062 // Revision failed to load Content, or Content is incompatible with wikitext.
1063 // Possibly a temporary loading failure.
1066 return '!NONEXISTENT';
1069 return ' ' . $message;
1077 * @param string $message
1078 * @param bool $interface
1079 * @param Language|null $language
1080 * @param Title|null $title
1083 public function transform( $message, $interface = false, $language = null, $title = null ) {
1084 // Avoid creating parser if nothing to transform
1085 if ( strpos( $message, '{{' ) === false ) {
1089 if ( $this->mInParser
) {
1093 $parser = $this->getParser();
1095 $popts = $this->getParserOptions();
1096 $popts->setInterfaceMessage( $interface );
1097 $popts->setTargetLanguage( $language );
1099 $userlang = $popts->setUserLang( $language );
1100 $this->mInParser
= true;
1101 $message = $parser->transformMsg( $message, $popts, $title );
1102 $this->mInParser
= false;
1103 $popts->setUserLang( $userlang );
1112 public function getParser() {
1113 global $wgParser, $wgParserConf;
1115 if ( !$this->mParser
&& isset( $wgParser ) ) {
1116 # Do some initialisation so that we don't have to do it twice
1117 $wgParser->firstCallInit();
1118 # Clone it and store it
1119 $class = $wgParserConf['class'];
1120 if ( $class == ParserDiffTest
::class ) {
1122 $this->mParser
= new $class( $wgParserConf );
1124 $this->mParser
= clone $wgParser;
1128 return $this->mParser
;
1132 * @param string $text
1133 * @param Title|null $title
1134 * @param bool $linestart Whether or not this is at the start of a line
1135 * @param bool $interface Whether this is an interface message
1136 * @param Language|string|null $language Language code
1137 * @return ParserOutput|string
1139 public function parse( $text, $title = null, $linestart = true,
1140 $interface = false, $language = null
1144 if ( $this->mInParser
) {
1145 return htmlspecialchars( $text );
1148 $parser = $this->getParser();
1149 $popts = $this->getParserOptions();
1150 $popts->setInterfaceMessage( $interface );
1152 if ( is_string( $language ) ) {
1153 $language = Language
::factory( $language );
1155 $popts->setTargetLanguage( $language );
1157 if ( !$title ||
!$title instanceof Title
) {
1158 wfDebugLog( 'GlobalTitleFail', __METHOD__
. ' called by ' .
1159 wfGetAllCallers( 6 ) . ' with no title set.' );
1162 // Sometimes $wgTitle isn't set either...
1164 # It's not uncommon having a null $wgTitle in scripts. See r80898
1165 # Create a ghost title in such case
1166 $title = Title
::makeTitle( NS_SPECIAL
, 'Badtitle/title not set in ' . __METHOD__
);
1169 $this->mInParser
= true;
1170 $res = $parser->parse( $text, $title, $popts, $linestart );
1171 $this->mInParser
= false;
1176 public function disable() {
1177 $this->mDisable
= true;
1180 public function enable() {
1181 $this->mDisable
= false;
1185 * Whether DB/cache usage is disabled for determining messages
1187 * If so, this typically indicates either:
1188 * - a) load() failed to find a cached copy nor query the DB
1189 * - b) we are in a special context or error mode that cannot use the DB
1190 * If the DB is ignored, any derived HTML output or cached objects may be wrong.
1191 * To avoid long-term cache pollution, TTLs can be adjusted accordingly.
1196 public function isDisabled() {
1197 return $this->mDisable
;
1201 * Clear all stored messages in global and local cache
1203 * Mainly used after a mass rebuild
1205 public function clear() {
1206 $langs = Language
::fetchLanguageNames( null, 'mw' );
1207 foreach ( array_keys( $langs ) as $code ) {
1208 $this->wanCache
->touchCheckKey( $this->getCheckKey( $code ) );
1210 $this->cache
->clear();
1214 * @param string $key
1217 public function figureMessage( $key ) {
1218 global $wgLanguageCode;
1220 $pieces = explode( '/', $key );
1221 if ( count( $pieces ) < 2 ) {
1222 return [ $key, $wgLanguageCode ];
1225 $lang = array_pop( $pieces );
1226 if ( !Language
::fetchLanguageName( $lang, null, 'mw' ) ) {
1227 return [ $key, $wgLanguageCode ];
1230 $message = implode( '/', $pieces );
1232 return [ $message, $lang ];
1236 * Get all message keys stored in the message cache for a given language.
1237 * If $code is the content language code, this will return all message keys
1238 * for which MediaWiki:msgkey exists. If $code is another language code, this
1239 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
1240 * @param string $code Language code
1241 * @return array Array of message keys (strings)
1243 public function getAllMessageKeys( $code ) {
1244 $this->load( $code );
1245 if ( !$this->cache
->has( $code ) ) {
1246 // Apparently load() failed
1249 // Remove administrative keys
1250 $cache = $this->cache
->get( $code );
1251 unset( $cache['VERSION'] );
1252 unset( $cache['EXPIRY'] );
1253 unset( $cache['EXCESSIVE'] );
1254 // Remove any !NONEXISTENT keys
1255 $cache = array_diff( $cache, [ '!NONEXISTENT' ] );
1257 // Keys may appear with a capital first letter. lcfirst them.
1258 return array_map( [ $this->contLang
, 'lcfirst' ], array_keys( $cache ) );
1262 * Purge message caches when a MediaWiki: page is created, updated, or deleted
1264 * @param Title $title Message page title
1265 * @param Content|null $content New content for edit/create, null on deletion
1268 public function updateMessageOverride( Title
$title, Content
$content = null ) {
1269 $msgText = $this->getMessageTextFromContent( $content );
1270 if ( $msgText === null ) {
1271 $msgText = false; // treat as not existing
1274 $this->replace( $title->getDBkey(), $msgText );
1276 if ( $this->contLang
->hasVariants() ) {
1277 $this->contLang
->updateConversionTable( $title );
1282 * @param string $code Language code
1283 * @return string WAN cache key usable as a "check key" against language page edits
1285 public function getCheckKey( $code ) {
1286 return $this->wanCache
->makeKey( 'messages', $code );
1290 * @param Content|null $content Content or null if the message page does not exist
1291 * @return string|bool|null Returns false if $content is null and null on error
1293 private function getMessageTextFromContent( Content
$content = null ) {
1294 // @TODO: could skip pseudo-messages like js/css here, based on content model
1296 // Message page exists...
1297 // XXX: Is this the right way to turn a Content object into a message?
1298 // NOTE: $content is typically either WikitextContent, JavaScriptContent or
1299 // CssContent. MessageContent is *not* used for storing messages, it's
1300 // only used for wrapping them when needed.
1301 $msgText = $content->getWikitextForTransclusion();
1302 if ( $msgText === false ||
$msgText === null ) {
1303 // This might be due to some kind of misconfiguration...
1305 LoggerFactory
::getInstance( 'MessageCache' )->warning(
1306 __METHOD__
. ": message content doesn't provide wikitext "
1307 . "(content model: " . $content->getModel() . ")" );
1310 // Message page does not exist...
1318 * @param string $hash Hash for this version of the entire key/value overrides map
1319 * @param string $title Message cache key with initial uppercase letter
1322 private function bigMessageCacheKey( $hash, $title ) {
1323 return $this->wanCache
->makeKey( 'messages-big', $hash, $title );