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
;
28 * MediaWiki message cache structure version.
29 * Bump this whenever the message cache format has changed.
31 define( 'MSG_CACHE_VERSION', 2 );
35 * Performs various MediaWiki namespace-related functions
39 const FOR_UPDATE
= 1; // force message reload
41 /** How long to wait for memcached locks */
43 /** How long memcached locks last */
47 * Process local cache of loaded messages that are defined in
48 * MediaWiki namespace. First array level is a language code,
49 * second level is message key and the values are either message
50 * content prefixed with space, or !NONEXISTENT for negative
57 * @var bool[] Map of (language code => boolean)
59 protected $mCacheVolatile = [];
62 * Should mean that database cannot be used, but check
68 * Lifetime for cache, used by object caching.
69 * Set on construction, see __construct().
74 * Message cache has its own parser which it uses to transform messages
77 protected $mParserOptions;
82 * Variable for tracking which variables are already loaded
83 * @var array $mLoadedLanguages
85 protected $mLoadedLanguages = [];
88 * @var bool $mInParser
90 protected $mInParser = false;
92 /** @var WANObjectCache */
95 protected $clusterCache;
102 * @var MessageCache $instance
104 private static $instance;
107 * Get the signleton instance of this class
110 * @return MessageCache
112 public static function singleton() {
113 if ( self
::$instance === null ) {
114 global $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgUseLocalMessageCache;
115 self
::$instance = new self(
116 MediaWikiServices
::getInstance()->getMainWANObjectCache(),
117 wfGetMessageCacheStorage(),
118 $wgUseLocalMessageCache
119 ? MediaWikiServices
::getInstance()->getLocalServerObjectCache()
120 : new EmptyBagOStuff(),
121 $wgUseDatabaseMessages,
126 return self
::$instance;
130 * Destroy the singleton instance
134 public static function destroyInstance() {
135 self
::$instance = null;
139 * Normalize message key input
141 * @param string $key Input message key to be normalized
142 * @return string Normalized message key
144 public static function normalizeKey( $key ) {
147 $lckey = strtr( $key, ' ', '_' );
148 if ( ord( $lckey ) < 128 ) {
149 $lckey[0] = strtolower( $lckey[0] );
151 $lckey = $wgContLang->lcfirst( $lckey );
158 * @param WANObjectCache $wanCache WAN cache instance
159 * @param BagOStuff $clusterCache Cluster cache instance
160 * @param BagOStuff $srvCache Server cache instance
161 * @param bool $useDB Whether to look for message overrides (e.g. MediaWiki: pages)
162 * @param int $expiry Lifetime for cache. @see $mExpiry.
164 public function __construct(
165 WANObjectCache
$wanCache,
166 BagOStuff
$clusterCache,
171 $this->wanCache
= $wanCache;
172 $this->clusterCache
= $clusterCache;
173 $this->srvCache
= $srvCache;
175 $this->mDisable
= !$useDB;
176 $this->mExpiry
= $expiry;
180 * ParserOptions is lazy initialised.
182 * @return ParserOptions
184 function getParserOptions() {
187 if ( !$this->mParserOptions
) {
188 if ( !$wgUser->isSafeToLoad() ) {
189 // $wgUser isn't unstubbable yet, so don't try to get a
190 // ParserOptions for it. And don't cache this ParserOptions
192 $po = ParserOptions
::newFromAnon();
193 $po->setEditSection( false );
194 $po->setAllowUnsafeRawHtml( false );
198 $this->mParserOptions
= new ParserOptions
;
199 $this->mParserOptions
->setEditSection( false );
200 // Messages may take parameters that could come
201 // from malicious sources. As a precaution, disable
202 // the <html> parser tag when parsing messages.
203 $this->mParserOptions
->setAllowUnsafeRawHtml( false );
206 return $this->mParserOptions
;
210 * Try to load the cache from APC.
212 * @param string $code Optional language code, see documenation of load().
213 * @return array|bool The cache array, or false if not in cache.
215 protected function getLocalCache( $code ) {
216 $cacheKey = wfMemcKey( __CLASS__
, $code );
218 return $this->srvCache
->get( $cacheKey );
222 * Save the cache to APC.
224 * @param string $code
225 * @param array $cache The cache array
227 protected function saveToLocalCache( $code, $cache ) {
228 $cacheKey = wfMemcKey( __CLASS__
, $code );
229 $this->srvCache
->set( $cacheKey, $cache );
233 * Loads messages from caches or from database in this order:
234 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
236 * (3) from the database.
238 * When succesfully loading from (2) or (3), all higher level caches are
239 * updated for the newest version.
241 * Nothing is loaded if member variable mDisable is true, either manually
242 * set by calling code or if message loading fails (is this possible?).
244 * Returns true if cache is already populated or it was succesfully populated,
245 * or false if populating empty cache fails. Also returns true if MessageCache
248 * @param string $code Language to which load messages
249 * @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache [optional]
250 * @throws MWException
253 protected function load( $code, $mode = null ) {
254 if ( !is_string( $code ) ) {
255 throw new InvalidArgumentException( "Missing language code" );
258 # Don't do double loading...
259 if ( isset( $this->mLoadedLanguages
[$code] ) && $mode != self
::FOR_UPDATE
) {
263 # 8 lines of code just to say (once) that message cache is disabled
264 if ( $this->mDisable
) {
265 static $shownDisabled = false;
266 if ( !$shownDisabled ) {
267 wfDebug( __METHOD__
. ": disabled\n" );
268 $shownDisabled = true;
274 # Loading code starts
275 $success = false; # Keep track of success
276 $staleCache = false; # a cache array with expired data, or false if none has been loaded
277 $where = []; # Debug info, delayed to avoid spamming debug log too much
279 # Hash of the contents is stored in memcache, to detect if data-center cache
280 # or local cache goes out of date (e.g. due to replace() on some other server)
281 list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
282 $this->mCacheVolatile
[$code] = $hashVolatile;
284 # Try the local cache and check against the cluster hash key...
285 $cache = $this->getLocalCache( $code );
287 $where[] = 'local cache is empty';
288 } elseif ( !isset( $cache['HASH'] ) ||
$cache['HASH'] !== $hash ) {
289 $where[] = 'local cache has the wrong hash';
290 $staleCache = $cache;
291 } elseif ( $this->isCacheExpired( $cache ) ) {
292 $where[] = 'local cache is expired';
293 $staleCache = $cache;
294 } elseif ( $hashVolatile ) {
295 $where[] = 'local cache validation key is expired/volatile';
296 $staleCache = $cache;
298 $where[] = 'got from local cache';
300 $this->mCache
[$code] = $cache;
304 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
305 # Try the global cache. If it is empty, try to acquire a lock. If
306 # the lock can't be acquired, wait for the other thread to finish
307 # and then try the global cache a second time.
308 for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++
) {
309 if ( $hashVolatile && $staleCache ) {
310 # Do not bother fetching the whole cache blob to avoid I/O.
311 # Instead, just try to get the non-blocking $statusKey lock
312 # below, and use the local stale value if it was not acquired.
313 $where[] = 'global cache is presumed expired';
315 $cache = $this->clusterCache
->get( $cacheKey );
317 $where[] = 'global cache is empty';
318 } elseif ( $this->isCacheExpired( $cache ) ) {
319 $where[] = 'global cache is expired';
320 $staleCache = $cache;
321 } elseif ( $hashVolatile ) {
322 # DB results are replica DB lag prone until the holdoff TTL passes.
323 # By then, updates should be reflected in loadFromDBWithLock().
324 # One thread renerates the cache while others use old values.
325 $where[] = 'global cache is expired/volatile';
326 $staleCache = $cache;
328 $where[] = 'got from global cache';
329 $this->mCache
[$code] = $cache;
330 $this->saveToCaches( $cache, 'local-only', $code );
336 # Done, no need to retry
340 # We need to call loadFromDB. Limit the concurrency to one process.
341 # This prevents the site from going down when the cache expires.
342 # Note that the DB slam protection lock here is non-blocking.
343 $loadStatus = $this->loadFromDBWithLock( $code, $where, $mode );
344 if ( $loadStatus === true ) {
347 } elseif ( $staleCache ) {
348 # Use the stale cache while some other thread constructs the new one
349 $where[] = 'using stale cache';
350 $this->mCache
[$code] = $staleCache;
353 } elseif ( $failedAttempts > 0 ) {
354 # Already blocked once, so avoid another lock/unlock cycle.
355 # This case will typically be hit if memcached is down, or if
356 # loadFromDB() takes longer than LOCK_WAIT.
357 $where[] = "could not acquire status key.";
359 } elseif ( $loadStatus === 'cantacquire' ) {
360 # Wait for the other thread to finish, then retry. Normally,
361 # the memcached get() will then yeild the other thread's result.
362 $where[] = 'waited for other thread to complete';
363 $this->getReentrantScopedLock( $cacheKey );
365 # Disable cache; $loadStatus is 'disabled'
372 $where[] = 'loading FAILED - cache is disabled';
373 $this->mDisable
= true;
374 $this->mCache
= false;
375 wfDebugLog( 'MessageCacheError', __METHOD__
. ": Failed to load $code\n" );
376 # This used to throw an exception, but that led to nasty side effects like
377 # the whole wiki being instantly down if the memcached server died
379 # All good, just record the success
380 $this->mLoadedLanguages
[$code] = true;
383 $info = implode( ', ', $where );
384 wfDebugLog( 'MessageCache', __METHOD__
. ": Loading $code... $info\n" );
390 * @param string $code
391 * @param array $where List of wfDebug() comments
392 * @param integer $mode Use MessageCache::FOR_UPDATE to use DB_MASTER
393 * @return bool|string True on success or one of ("cantacquire", "disabled")
395 protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
396 # If cache updates on all levels fail, give up on message overrides.
397 # This is to avoid easy site outages; see $saveSuccess comments below.
398 $statusKey = wfMemcKey( 'messages', $code, 'status' );
399 $status = $this->clusterCache
->get( $statusKey );
400 if ( $status === 'error' ) {
401 $where[] = "could not load; method is still globally disabled";
405 # Now let's regenerate
406 $where[] = 'loading from database';
408 # Lock the cache to prevent conflicting writes.
409 # This lock is non-blocking so stale cache can quickly be used.
410 # Note that load() will call a blocking getReentrantScopedLock()
411 # after this if it really need to wait for any current thread.
412 $cacheKey = wfMemcKey( 'messages', $code );
413 $scopedLock = $this->getReentrantScopedLock( $cacheKey, 0 );
414 if ( !$scopedLock ) {
415 $where[] = 'could not acquire main lock';
416 return 'cantacquire';
419 $cache = $this->loadFromDB( $code, $mode );
420 $this->mCache
[$code] = $cache;
421 $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
423 if ( !$saveSuccess ) {
425 * Cache save has failed.
427 * There are two main scenarios where this could be a problem:
428 * - The cache is more than the maximum size (typically 1MB compressed).
429 * - Memcached has no space remaining in the relevant slab class. This is
430 * unlikely with recent versions of memcached.
432 * Either way, if there is a local cache, nothing bad will happen. If there
433 * is no local cache, disabling the message cache for all requests avoids
434 * incurring a loadFromDB() overhead on every request, and thus saves the
435 * wiki from complete downtime under moderate traffic conditions.
437 if ( $this->srvCache
instanceof EmptyBagOStuff
) {
438 $this->clusterCache
->set( $statusKey, 'error', 60 * 5 );
439 $where[] = 'could not save cache, disabled globally for 5 minutes';
441 $where[] = "could not save global cache";
449 * Loads cacheable messages from the database. Messages bigger than
450 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
451 * on-demand from the database later.
453 * @param string $code Language code
454 * @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache
455 * @return array Loaded messages for storing in caches
457 protected function loadFromDB( $code, $mode = null ) {
458 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
460 $dbr = wfGetDB( ( $mode == self
::FOR_UPDATE
) ? DB_MASTER
: DB_REPLICA
);
466 'page_is_redirect' => 0,
467 'page_namespace' => NS_MEDIAWIKI
,
471 if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
472 if ( !isset( $this->mCache
[$wgLanguageCode] ) ) {
473 $this->load( $wgLanguageCode );
475 $mostused = array_keys( $this->mCache
[$wgLanguageCode] );
476 foreach ( $mostused as $key => $value ) {
477 $mostused[$key] = "$value/$code";
481 if ( count( $mostused ) ) {
482 $conds['page_title'] = $mostused;
483 } elseif ( $code !== $wgLanguageCode ) {
484 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
486 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
487 # other than language code.
488 $conds[] = 'page_title NOT' .
489 $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
492 # Conditions to fetch oversized pages to ignore them
494 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
496 # Load titles for all oversized pages in the MediaWiki namespace
499 [ 'page_title', 'page_latest' ],
501 __METHOD__
. "($code)-big"
503 foreach ( $res as $row ) {
504 $cache[$row->page_title
] = '!TOO BIG';
505 // At least include revision ID so page changes are reflected in the hash
506 $cache['EXCESSIVE'][$row->page_title
] = $row->page_latest
;
509 # Conditions to load the remaining pages with their contents
510 $smallConds = $conds;
511 $smallConds[] = 'page_latest=rev_id';
512 $smallConds[] = 'rev_text_id=old_id';
513 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
516 [ 'page', 'revision', 'text' ],
517 [ 'page_title', 'old_id', 'old_text', 'old_flags' ],
519 __METHOD__
. "($code)-small"
522 foreach ( $res as $row ) {
523 $text = Revision
::getRevisionText( $row );
524 if ( $text === false ) {
525 // Failed to fetch data; possible ES errors?
526 // Store a marker to fetch on-demand as a workaround...
527 // TODO Use a differnt marker
532 . ": failed to load message page text for {$row->page_title} ($code)"
535 $entry = ' ' . $text;
537 $cache[$row->page_title
] = $entry;
540 $cache['VERSION'] = MSG_CACHE_VERSION
;
543 # Hash for validating local cache (APC). No need to take into account
544 # messages larger than $wgMaxMsgCacheEntrySize, since those are only
545 # stored and fetched from memcache.
546 $cache['HASH'] = md5( serialize( $cache ) );
547 $cache['EXPIRY'] = wfTimestamp( TS_MW
, time() +
$this->mExpiry
);
553 * Updates cache as necessary when message page is changed
555 * @param string $title Message cache key with initial uppercase letter
556 * @param string|bool $text New contents of the page (false if deleted)
558 public function replace( $title, $text ) {
559 global $wgLanguageCode;
561 if ( $this->mDisable
) {
565 list( $msg, $code ) = $this->figureMessage( $title );
566 if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
567 // Content language overrides do not use the /<code> suffix
571 // (a) Update the process cache with the new message text
572 if ( $text === false ) {
574 $this->mCache
[$code][$title] = '!NONEXISTENT';
576 // Ignore $wgMaxMsgCacheEntrySize so the process cache is up to date
577 $this->mCache
[$code][$title] = ' ' . $text;
580 // (b) Update the shared caches in a deferred update with a fresh DB snapshot
581 DeferredUpdates
::addCallableUpdate(
582 function () use ( $title, $msg, $code ) {
583 global $wgContLang, $wgMaxMsgCacheEntrySize;
584 // Allow one caller at a time to avoid race conditions
585 $scopedLock = $this->getReentrantScopedLock( wfMemcKey( 'messages', $code ) );
586 if ( !$scopedLock ) {
587 LoggerFactory
::getInstance( 'MessageCache' )->error(
588 __METHOD__
. ': could not acquire lock to update {title} ({code})',
589 [ 'title' => $title, 'code' => $code ] );
592 // Load the messages from the master DB to avoid race conditions
593 $cache = $this->loadFromDB( $code, self
::FOR_UPDATE
);
594 $this->mCache
[$code] = $cache;
595 // Load the process cache values and set the per-title cache keys
596 $page = WikiPage
::factory( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
597 $page->loadPageData( $page::READ_LATEST
);
598 $text = $this->getMessageTextFromContent( $page->getContent() );
599 // Check if an individual cache key should exist and update cache accordingly
600 if ( is_string( $text ) && strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
601 $titleKey = $this->bigMessageCacheKey( $this->mCache
[$code]['HASH'], $title );
602 $this->wanCache
->set( $titleKey, ' ' . $text, $this->mExpiry
);
604 // Mark this cache as definitely being "latest" (non-volatile) so
605 // load() calls do try to refresh the cache with replica DB data
606 $this->mCache
[$code]['LATEST'] = time();
607 // Pre-emptively update the local datacenter cache so things like edit filter and
608 // blacklist changes are reflect immediately, as these often use MediaWiki: pages.
609 // The datacenter handling replace() calls should be the same one handling edits
610 // as they require HTTP POST.
611 $this->saveToCaches( $this->mCache
[$code], 'all', $code );
612 // Release the lock now that the cache is saved
613 ScopedCallback
::consume( $scopedLock );
615 // Relay the purge. Touching this check key expires cache contents
616 // and local cache (APC) validation hash across all datacenters.
617 $this->wanCache
->touchCheckKey( wfMemcKey( 'messages', $code ) );
618 // Also delete cached sidebar... just in case it is affected
619 // @TODO: shouldn't this be $code === $wgLanguageCode?
620 if ( $code === 'en' ) {
621 // Purge all language sidebars, e.g. on ?action=purge to the sidebar messages
622 $codes = array_keys( Language
::fetchLanguageNames() );
624 // Purge only the sidebar for this language
627 foreach ( $codes as $code ) {
628 $this->wanCache
->delete( wfMemcKey( 'sidebar', $code ) );
631 // Purge the message in the message blob store
632 $resourceloader = RequestContext
::getMain()->getOutput()->getResourceLoader();
633 $blobStore = $resourceloader->getMessageBlobStore();
634 $blobStore->updateMessage( $wgContLang->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 = wfMemcKey( '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 [ wfMemcKey( 'messages', $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' => isset( $cache['LATEST'] ) ?
$cache['LATEST'] : 0
736 WANObjectCache
::TTL_INDEFINITE
741 * @param string $key A language message cache key that stores blobs
742 * @param integer $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 = MessageCache
::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 ) {
871 // First try the requested language.
872 $message = $this->getMessageForLang( $lang, $lckey, $useDB, $alreadyTried );
873 if ( $message !== false ) {
877 // Now try checking the site language.
878 $message = $this->getMessageForLang( $wgContLang, $lckey, $useDB, $alreadyTried );
883 * Given a language, try and fetch messages from that language and its fallbacks.
885 * @see MessageCache::get
886 * @param Language|StubObject $lang Preferred language
887 * @param string $lckey Lowercase key for the message (as for localisation cache)
888 * @param bool $useDB Whether to include messages from the wiki database
889 * @param bool[] $alreadyTried Contains true for each language that has been tried already
890 * @return string|bool The message, or false if not found
892 private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
895 $langcode = $lang->getCode();
897 // Try checking the database for the requested language
899 $uckey = $wgContLang->ucfirst( $lckey );
901 if ( !isset( $alreadyTried[ $langcode ] ) ) {
902 $message = $this->getMsgFromNamespace(
903 $this->getMessagePageName( $langcode, $uckey ),
907 if ( $message !== false ) {
910 $alreadyTried[ $langcode ] = true;
916 // Check the CDB cache
917 $message = $lang->getMessage( $lckey );
918 if ( $message !== null ) {
922 // Try checking the database for all of the fallback languages
924 $fallbackChain = Language
::getFallbacksFor( $langcode );
926 foreach ( $fallbackChain as $code ) {
927 if ( isset( $alreadyTried[ $code ] ) ) {
931 $message = $this->getMsgFromNamespace(
932 $this->getMessagePageName( $code, $uckey ), $code );
934 if ( $message !== false ) {
937 $alreadyTried[ $code ] = true;
945 * Get the message page name for a given language
947 * @param string $langcode
948 * @param string $uckey Uppercase key for the message
949 * @return string The page name
951 private function getMessagePageName( $langcode, $uckey ) {
952 global $wgLanguageCode;
954 if ( $langcode === $wgLanguageCode ) {
955 // Messages created in the content language will not have the /lang extension
958 return "$uckey/$langcode";
963 * Get a message from the MediaWiki namespace, with caching. The key must
964 * first be converted to two-part lang/msg form if necessary.
966 * Unlike self::get(), this function doesn't resolve fallback chains, and
967 * some callers require this behavior. LanguageConverter::parseCachedTable()
968 * and self::get() are some examples in core.
970 * @param string $title Message cache key with initial uppercase letter
971 * @param string $code Code denoting the language to try
972 * @return string|bool The message, or false if it does not exist or on error
974 public function getMsgFromNamespace( $title, $code ) {
975 $this->load( $code );
977 if ( isset( $this->mCache
[$code][$title] ) ) {
978 $entry = $this->mCache
[$code][$title];
979 if ( substr( $entry, 0, 1 ) === ' ' ) {
980 // The message exists, so make sure a string is returned.
981 return (string)substr( $entry, 1 );
982 } elseif ( $entry === '!NONEXISTENT' ) {
984 } elseif ( $entry === '!TOO BIG' ) {
985 // Fall through and try invididual message cache below
988 // XXX: This is not cached in process cache, should it?
990 Hooks
::run( 'MessagesPreLoad', [ $title, &$message, $code ] );
991 if ( $message !== false ) {
998 // Individual message cache key
999 $titleKey = $this->bigMessageCacheKey( $this->mCache
[$code]['HASH'], $title );
1001 if ( $this->mCacheVolatile
[$code] ) {
1003 // Make sure that individual keys respect the WAN cache holdoff period too
1004 LoggerFactory
::getInstance( 'MessageCache' )->debug(
1005 __METHOD__
. ': loading volatile key \'{titleKey}\'',
1006 [ 'titleKey' => $titleKey, 'code' => $code ] );
1008 // Try the individual message cache
1009 $entry = $this->wanCache
->get( $titleKey );
1012 if ( $entry !== false ) {
1013 if ( substr( $entry, 0, 1 ) === ' ' ) {
1014 $this->mCache
[$code][$title] = $entry;
1015 // The message exists, so make sure a string is returned
1016 return (string)substr( $entry, 1 );
1017 } elseif ( $entry === '!NONEXISTENT' ) {
1018 $this->mCache
[$code][$title] = '!NONEXISTENT';
1022 // Corrupt/obsolete entry, delete it
1023 $this->wanCache
->delete( $titleKey );
1027 // Try loading the message from the database
1028 $dbr = wfGetDB( DB_REPLICA
);
1029 $cacheOpts = Database
::getCacheSetOptions( $dbr );
1030 // Use newKnownCurrent() to avoid querying revision/user tables
1031 $titleObj = Title
::makeTitle( NS_MEDIAWIKI
, $title );
1032 if ( $titleObj->getLatestRevID() ) {
1033 $revision = Revision
::newKnownCurrent(
1035 $titleObj->getArticleID(),
1036 $titleObj->getLatestRevID()
1043 $content = $revision->getContent();
1045 $message = $this->getMessageTextFromContent( $content );
1046 if ( is_string( $message ) ) {
1047 $this->mCache
[$code][$title] = ' ' . $message;
1048 $this->wanCache
->set( $titleKey, ' ' . $message, $this->mExpiry
, $cacheOpts );
1051 // A possibly temporary loading failure
1052 LoggerFactory
::getInstance( 'MessageCache' )->warning(
1053 __METHOD__
. ': failed to load message page text for \'{titleKey}\'',
1054 [ 'titleKey' => $titleKey, 'code' => $code ] );
1055 $message = null; // no negative caching
1058 $message = false; // negative caching
1061 if ( $message === false ) {
1062 // Negative caching in case a "too big" message is no longer available (deleted)
1063 $this->mCache
[$code][$title] = '!NONEXISTENT';
1064 $this->wanCache
->set( $titleKey, '!NONEXISTENT', $this->mExpiry
, $cacheOpts );
1071 * @param string $message
1072 * @param bool $interface
1073 * @param string $language Language code
1074 * @param Title $title
1077 function transform( $message, $interface = false, $language = null, $title = null ) {
1078 // Avoid creating parser if nothing to transform
1079 if ( strpos( $message, '{{' ) === false ) {
1083 if ( $this->mInParser
) {
1087 $parser = $this->getParser();
1089 $popts = $this->getParserOptions();
1090 $popts->setInterfaceMessage( $interface );
1091 $popts->setTargetLanguage( $language );
1093 $userlang = $popts->setUserLang( $language );
1094 $this->mInParser
= true;
1095 $message = $parser->transformMsg( $message, $popts, $title );
1096 $this->mInParser
= false;
1097 $popts->setUserLang( $userlang );
1106 function getParser() {
1107 global $wgParser, $wgParserConf;
1109 if ( !$this->mParser
&& isset( $wgParser ) ) {
1110 # Do some initialisation so that we don't have to do it twice
1111 $wgParser->firstCallInit();
1112 # Clone it and store it
1113 $class = $wgParserConf['class'];
1114 if ( $class == 'ParserDiffTest' ) {
1116 $this->mParser
= new $class( $wgParserConf );
1118 $this->mParser
= clone $wgParser;
1122 return $this->mParser
;
1126 * @param string $text
1127 * @param Title $title
1128 * @param bool $linestart Whether or not this is at the start of a line
1129 * @param bool $interface Whether this is an interface message
1130 * @param Language|string $language Language code
1131 * @return ParserOutput|string
1133 public function parse( $text, $title = null, $linestart = true,
1134 $interface = false, $language = null
1138 if ( $this->mInParser
) {
1139 return htmlspecialchars( $text );
1142 $parser = $this->getParser();
1143 $popts = $this->getParserOptions();
1144 $popts->setInterfaceMessage( $interface );
1146 if ( is_string( $language ) ) {
1147 $language = Language
::factory( $language );
1149 $popts->setTargetLanguage( $language );
1151 if ( !$title ||
!$title instanceof Title
) {
1152 wfDebugLog( 'GlobalTitleFail', __METHOD__
. ' called by ' .
1153 wfGetAllCallers( 6 ) . ' with no title set.' );
1156 // Sometimes $wgTitle isn't set either...
1158 # It's not uncommon having a null $wgTitle in scripts. See r80898
1159 # Create a ghost title in such case
1160 $title = Title
::makeTitle( NS_SPECIAL
, 'Badtitle/title not set in ' . __METHOD__
);
1163 $this->mInParser
= true;
1164 $res = $parser->parse( $text, $title, $popts, $linestart );
1165 $this->mInParser
= false;
1170 function disable() {
1171 $this->mDisable
= true;
1175 $this->mDisable
= false;
1179 * Whether DB/cache usage is disabled for determining messages
1181 * If so, this typically indicates either:
1182 * - a) load() failed to find a cached copy nor query the DB
1183 * - b) we are in a special context or error mode that cannot use the DB
1184 * If the DB is ignored, any derived HTML output or cached objects may be wrong.
1185 * To avoid long-term cache pollution, TTLs can be adjusted accordingly.
1190 public function isDisabled() {
1191 return $this->mDisable
;
1195 * Clear all stored messages. Mainly used after a mass rebuild.
1198 $langs = Language
::fetchLanguageNames( null, 'mw' );
1199 foreach ( array_keys( $langs ) as $code ) {
1200 # Global and local caches
1201 $this->wanCache
->touchCheckKey( wfMemcKey( 'messages', $code ) );
1204 $this->mLoadedLanguages
= [];
1208 * @param string $key
1211 public function figureMessage( $key ) {
1212 global $wgLanguageCode;
1214 $pieces = explode( '/', $key );
1215 if ( count( $pieces ) < 2 ) {
1216 return [ $key, $wgLanguageCode ];
1219 $lang = array_pop( $pieces );
1220 if ( !Language
::fetchLanguageName( $lang, null, 'mw' ) ) {
1221 return [ $key, $wgLanguageCode ];
1224 $message = implode( '/', $pieces );
1226 return [ $message, $lang ];
1230 * Get all message keys stored in the message cache for a given language.
1231 * If $code is the content language code, this will return all message keys
1232 * for which MediaWiki:msgkey exists. If $code is another language code, this
1233 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
1234 * @param string $code Language code
1235 * @return array Array of message keys (strings)
1237 public function getAllMessageKeys( $code ) {
1240 $this->load( $code );
1241 if ( !isset( $this->mCache
[$code] ) ) {
1242 // Apparently load() failed
1245 // Remove administrative keys
1246 $cache = $this->mCache
[$code];
1247 unset( $cache['VERSION'] );
1248 unset( $cache['EXPIRY'] );
1249 unset( $cache['EXCESSIVE'] );
1250 // Remove any !NONEXISTENT keys
1251 $cache = array_diff( $cache, [ '!NONEXISTENT' ] );
1253 // Keys may appear with a capital first letter. lcfirst them.
1254 return array_map( [ $wgContLang, 'lcfirst' ], array_keys( $cache ) );
1258 * Purge message caches when a MediaWiki: page is created, updated, or deleted
1260 * @param Title $title Message page title
1261 * @param Content|null $content New content for edit/create, null on deletion
1264 public function updateMessageOverride( Title
$title, Content
$content = null ) {
1267 $msgText = $this->getMessageTextFromContent( $content );
1268 if ( $msgText === null ) {
1269 $msgText = false; // treat as not existing
1272 $this->replace( $title->getDBkey(), $msgText );
1274 if ( $wgContLang->hasVariants() ) {
1275 $wgContLang->updateConversionTable( $title );
1280 * @param Content|null $content Content or null if the message page does not exist
1281 * @return string|bool|null Returns false if $content is null and null on error
1283 private function getMessageTextFromContent( Content
$content = null ) {
1284 // @TODO: could skip pseudo-messages like js/css here, based on content model
1286 // Message page exists...
1287 // XXX: Is this the right way to turn a Content object into a message?
1288 // NOTE: $content is typically either WikitextContent, JavaScriptContent or
1289 // CssContent. MessageContent is *not* used for storing messages, it's
1290 // only used for wrapping them when needed.
1291 $msgText = $content->getWikitextForTransclusion();
1292 if ( $msgText === false ||
$msgText === null ) {
1293 // This might be due to some kind of misconfiguration...
1295 LoggerFactory
::getInstance( 'MessageCache' )->warning(
1296 __METHOD__
. ": message content doesn't provide wikitext "
1297 . "(content model: " . $content->getModel() . ")" );
1300 // Message page does not exist...
1308 * @param string $hash Hash for this version of the entire key/value overrides map
1309 * @param string $title Message cache key with initial uppercase letter
1312 private function bigMessageCacheKey( $hash, $title ) {
1313 return $this->wanCache
->makeKey( 'messages-big', $hash, $title );