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;
94 /** @var WANObjectCache */
100 * @var MessageCache $instance
102 private static $instance;
105 * Get the signleton instance of this class
108 * @return MessageCache
110 public static function singleton() {
111 if ( self
::$instance === null ) {
112 global $wgUseDatabaseMessages, $wgMsgCacheExpiry;
113 self
::$instance = new self(
114 wfGetMessageCacheStorage(),
115 $wgUseDatabaseMessages,
120 return self
::$instance;
124 * Destroy the singleton instance
128 public static function destroyInstance() {
129 self
::$instance = null;
133 * Normalize message key input
135 * @param string $key Input message key to be normalized
136 * @return string Normalized message key
138 public static function normalizeKey( $key ) {
141 $lckey = strtr( $key, ' ', '_' );
142 if ( ord( $lckey ) < 128 ) {
143 $lckey[0] = strtolower( $lckey[0] );
145 $lckey = $wgContLang->lcfirst( $lckey );
152 * @param BagOStuff $memCached A cache instance. If none, fall back to CACHE_NONE.
154 * @param int $expiry Lifetime for cache. @see $mExpiry.
156 function __construct( BagOStuff
$memCached, $useDB, $expiry ) {
157 global $wgUseLocalMessageCache;
159 $this->mMemc
= $memCached;
160 $this->mDisable
= !$useDB;
161 $this->mExpiry
= $expiry;
163 if ( $wgUseLocalMessageCache ) {
164 $this->localCache
= MediaWikiServices
::getInstance()->getLocalServerObjectCache();
166 $this->localCache
= new EmptyBagOStuff();
169 $this->wanCache
= ObjectCache
::getMainWANInstance();
173 * ParserOptions is lazy initialised.
175 * @return ParserOptions
177 function getParserOptions() {
180 if ( !$this->mParserOptions
) {
181 if ( !$wgUser->isSafeToLoad() ) {
182 // $wgUser isn't unstubbable yet, so don't try to get a
183 // ParserOptions for it. And don't cache this ParserOptions
185 $po = ParserOptions
::newFromAnon();
186 $po->setEditSection( false );
190 $this->mParserOptions
= new ParserOptions
;
191 $this->mParserOptions
->setEditSection( false );
194 return $this->mParserOptions
;
198 * Try to load the cache from APC.
200 * @param string $code Optional language code, see documenation of load().
201 * @return array|bool The cache array, or false if not in cache.
203 protected function getLocalCache( $code ) {
204 $cacheKey = wfMemcKey( __CLASS__
, $code );
206 return $this->localCache
->get( $cacheKey );
210 * Save the cache to APC.
212 * @param string $code
213 * @param array $cache The cache array
215 protected function saveToLocalCache( $code, $cache ) {
216 $cacheKey = wfMemcKey( __CLASS__
, $code );
217 $this->localCache
->set( $cacheKey, $cache );
221 * Loads messages from caches or from database in this order:
222 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
224 * (3) from the database.
226 * When succesfully loading from (2) or (3), all higher level caches are
227 * updated for the newest version.
229 * Nothing is loaded if member variable mDisable is true, either manually
230 * set by calling code or if message loading fails (is this possible?).
232 * Returns true if cache is already populated or it was succesfully populated,
233 * or false if populating empty cache fails. Also returns true if MessageCache
236 * @param string $code Language to which load messages
237 * @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache [optional]
238 * @throws MWException
241 protected function load( $code, $mode = null ) {
242 if ( !is_string( $code ) ) {
243 throw new InvalidArgumentException( "Missing language code" );
246 # Don't do double loading...
247 if ( isset( $this->mLoadedLanguages
[$code] ) && $mode != self
::FOR_UPDATE
) {
251 # 8 lines of code just to say (once) that message cache is disabled
252 if ( $this->mDisable
) {
253 static $shownDisabled = false;
254 if ( !$shownDisabled ) {
255 wfDebug( __METHOD__
. ": disabled\n" );
256 $shownDisabled = true;
262 # Loading code starts
263 $success = false; # Keep track of success
264 $staleCache = false; # a cache array with expired data, or false if none has been loaded
265 $where = []; # Debug info, delayed to avoid spamming debug log too much
267 # Hash of the contents is stored in memcache, to detect if data-center cache
268 # or local cache goes out of date (e.g. due to replace() on some other server)
269 list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
270 $this->mCacheVolatile
[$code] = $hashVolatile;
272 # Try the local cache and check against the cluster hash key...
273 $cache = $this->getLocalCache( $code );
275 $where[] = 'local cache is empty';
276 } elseif ( !isset( $cache['HASH'] ) ||
$cache['HASH'] !== $hash ) {
277 $where[] = 'local cache has the wrong hash';
278 $staleCache = $cache;
279 } elseif ( $this->isCacheExpired( $cache ) ) {
280 $where[] = 'local cache is expired';
281 $staleCache = $cache;
282 } elseif ( $hashVolatile ) {
283 $where[] = 'local cache validation key is expired/volatile';
284 $staleCache = $cache;
286 $where[] = 'got from local cache';
288 $this->mCache
[$code] = $cache;
292 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
293 # Try the global cache. If it is empty, try to acquire a lock. If
294 # the lock can't be acquired, wait for the other thread to finish
295 # and then try the global cache a second time.
296 for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++
) {
297 if ( $hashVolatile && $staleCache ) {
298 # Do not bother fetching the whole cache blob to avoid I/O.
299 # Instead, just try to get the non-blocking $statusKey lock
300 # below, and use the local stale value if it was not acquired.
301 $where[] = 'global cache is presumed expired';
303 $cache = $this->mMemc
->get( $cacheKey );
305 $where[] = 'global cache is empty';
306 } elseif ( $this->isCacheExpired( $cache ) ) {
307 $where[] = 'global cache is expired';
308 $staleCache = $cache;
309 } elseif ( $hashVolatile ) {
310 # DB results are replica DB lag prone until the holdoff TTL passes.
311 # By then, updates should be reflected in loadFromDBWithLock().
312 # One thread renerates the cache while others use old values.
313 $where[] = 'global cache is expired/volatile';
314 $staleCache = $cache;
316 $where[] = 'got from global cache';
317 $this->mCache
[$code] = $cache;
318 $this->saveToCaches( $cache, 'local-only', $code );
324 # Done, no need to retry
328 # We need to call loadFromDB. Limit the concurrency to one process.
329 # This prevents the site from going down when the cache expires.
330 # Note that the DB slam protection lock here is non-blocking.
331 $loadStatus = $this->loadFromDBWithLock( $code, $where, $mode );
332 if ( $loadStatus === true ) {
335 } elseif ( $staleCache ) {
336 # Use the stale cache while some other thread constructs the new one
337 $where[] = 'using stale cache';
338 $this->mCache
[$code] = $staleCache;
341 } elseif ( $failedAttempts > 0 ) {
342 # Already blocked once, so avoid another lock/unlock cycle.
343 # This case will typically be hit if memcached is down, or if
344 # loadFromDB() takes longer than LOCK_WAIT.
345 $where[] = "could not acquire status key.";
347 } elseif ( $loadStatus === 'cantacquire' ) {
348 # Wait for the other thread to finish, then retry. Normally,
349 # the memcached get() will then yeild the other thread's result.
350 $where[] = 'waited for other thread to complete';
351 $this->getReentrantScopedLock( $cacheKey );
353 # Disable cache; $loadStatus is 'disabled'
360 $where[] = 'loading FAILED - cache is disabled';
361 $this->mDisable
= true;
362 $this->mCache
= false;
363 wfDebugLog( 'MessageCacheError', __METHOD__
. ": Failed to load $code\n" );
364 # This used to throw an exception, but that led to nasty side effects like
365 # the whole wiki being instantly down if the memcached server died
367 # All good, just record the success
368 $this->mLoadedLanguages
[$code] = true;
371 $info = implode( ', ', $where );
372 wfDebugLog( 'MessageCache', __METHOD__
. ": Loading $code... $info\n" );
378 * @param string $code
379 * @param array $where List of wfDebug() comments
380 * @param integer $mode Use MessageCache::FOR_UPDATE to use DB_MASTER
381 * @return bool|string True on success or one of ("cantacquire", "disabled")
383 protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
384 global $wgUseLocalMessageCache;
386 # If cache updates on all levels fail, give up on message overrides.
387 # This is to avoid easy site outages; see $saveSuccess comments below.
388 $statusKey = wfMemcKey( 'messages', $code, 'status' );
389 $status = $this->mMemc
->get( $statusKey );
390 if ( $status === 'error' ) {
391 $where[] = "could not load; method is still globally disabled";
395 # Now let's regenerate
396 $where[] = 'loading from database';
398 # Lock the cache to prevent conflicting writes.
399 # This lock is non-blocking so stale cache can quickly be used.
400 # Note that load() will call a blocking getReentrantScopedLock()
401 # after this if it really need to wait for any current thread.
402 $cacheKey = wfMemcKey( 'messages', $code );
403 $scopedLock = $this->getReentrantScopedLock( $cacheKey, 0 );
404 if ( !$scopedLock ) {
405 $where[] = 'could not acquire main lock';
406 return 'cantacquire';
409 $cache = $this->loadFromDB( $code, $mode );
410 $this->mCache
[$code] = $cache;
411 $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
413 if ( !$saveSuccess ) {
415 * Cache save has failed.
417 * There are two main scenarios where this could be a problem:
418 * - The cache is more than the maximum size (typically 1MB compressed).
419 * - Memcached has no space remaining in the relevant slab class. This is
420 * unlikely with recent versions of memcached.
422 * Either way, if there is a local cache, nothing bad will happen. If there
423 * is no local cache, disabling the message cache for all requests avoids
424 * incurring a loadFromDB() overhead on every request, and thus saves the
425 * wiki from complete downtime under moderate traffic conditions.
427 if ( !$wgUseLocalMessageCache ) {
428 $this->mMemc
->set( $statusKey, 'error', 60 * 5 );
429 $where[] = 'could not save cache, disabled globally for 5 minutes';
431 $where[] = "could not save global cache";
439 * Loads cacheable messages from the database. Messages bigger than
440 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
441 * on-demand from the database later.
443 * @param string $code Language code
444 * @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache
445 * @return array Loaded messages for storing in caches
447 function loadFromDB( $code, $mode = null ) {
448 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
450 $dbr = wfGetDB( ( $mode == self
::FOR_UPDATE
) ? DB_MASTER
: DB_REPLICA
);
456 'page_is_redirect' => 0,
457 'page_namespace' => NS_MEDIAWIKI
,
461 if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
462 if ( !isset( $this->mCache
[$wgLanguageCode] ) ) {
463 $this->load( $wgLanguageCode );
465 $mostused = array_keys( $this->mCache
[$wgLanguageCode] );
466 foreach ( $mostused as $key => $value ) {
467 $mostused[$key] = "$value/$code";
471 if ( count( $mostused ) ) {
472 $conds['page_title'] = $mostused;
473 } elseif ( $code !== $wgLanguageCode ) {
474 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
476 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
477 # other than language code.
478 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
481 # Conditions to fetch oversized pages to ignore them
483 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
485 # Load titles for all oversized pages in the MediaWiki namespace
488 [ 'page_title', 'page_latest' ],
490 __METHOD__
. "($code)-big"
492 foreach ( $res as $row ) {
493 $cache[$row->page_title
] = '!TOO BIG';
494 // At least include revision ID so page changes are reflected in the hash
495 $cache['EXCESSIVE'][$row->page_title
] = $row->page_latest
;
498 # Conditions to load the remaining pages with their contents
499 $smallConds = $conds;
500 $smallConds[] = 'page_latest=rev_id';
501 $smallConds[] = 'rev_text_id=old_id';
502 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
505 [ 'page', 'revision', 'text' ],
506 [ 'page_title', 'old_text', 'old_flags' ],
508 __METHOD__
. "($code)-small"
511 foreach ( $res as $row ) {
512 $text = Revision
::getRevisionText( $row );
513 if ( $text === false ) {
514 // Failed to fetch data; possible ES errors?
515 // Store a marker to fetch on-demand as a workaround...
520 . ": failed to load message page text for {$row->page_title} ($code)"
523 $entry = ' ' . $text;
525 $cache[$row->page_title
] = $entry;
528 $cache['VERSION'] = MSG_CACHE_VERSION
;
530 $cache['HASH'] = md5( serialize( $cache ) );
531 $cache['EXPIRY'] = wfTimestamp( TS_MW
, time() +
$this->mExpiry
);
537 * Updates cache as necessary when message page is changed
539 * @param string|bool $title Name of the page changed (false if deleted)
540 * @param string|bool $text New contents of the page (false if deleted)
542 public function replace( $title, $text ) {
543 global $wgMaxMsgCacheEntrySize, $wgContLang, $wgLanguageCode;
545 if ( $this->mDisable
) {
549 list( $msg, $code ) = $this->figureMessage( $title );
550 if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
551 // Content language overrides do not use the /<code> suffix
555 // Note that if the cache is volatile, load() may trigger a DB fetch.
556 // In that case we reenter/reuse the existing cache key lock to avoid
557 // a self-deadlock. This is safe as no reads happen *directly* in this
558 // method between getReentrantScopedLock() and load() below. There is
559 // no risk of data "changing under our feet" for replace().
560 $scopedLock = $this->getReentrantScopedLock( wfMemcKey( 'messages', $code ) );
561 // Load the messages from the master DB to avoid race conditions
562 $this->load( $code, self
::FOR_UPDATE
);
564 // Load the new value into the process cache...
565 if ( $text === false ) {
566 $this->mCache
[$code][$title] = '!NONEXISTENT';
567 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
568 $this->mCache
[$code][$title] = '!TOO BIG';
569 // Pre-fill the individual key cache with the known latest message text
570 $key = $this->wanCache
->makeKey( 'messages-big', $this->mCache
[$code]['HASH'], $title );
571 $this->wanCache
->set( $key, " $text", $this->mExpiry
);
573 $this->mCache
[$code][$title] = ' ' . $text;
575 // Mark this cache as definitely being "latest" (non-volatile) so
576 // load() calls do not try to refresh the cache with replica DB data
577 $this->mCache
[$code]['LATEST'] = time();
579 // Update caches if the lock was acquired
581 $this->saveToCaches( $this->mCache
[$code], 'all', $code );
583 LoggerFactory
::getInstance( 'MessageCache' )->error(
584 __METHOD__
. ': could not acquire lock to update {title} ({code})',
585 [ 'title' => $title, 'code' => $code ] );
588 ScopedCallback
::consume( $scopedLock );
589 // Relay the purge to APC and other DCs
590 $this->wanCache
->touchCheckKey( wfMemcKey( 'messages', $code ) );
592 // Also delete cached sidebar... just in case it is affected
594 if ( $code === 'en' ) {
595 // Delete all sidebars, like for example on action=purge on the
597 $codes = array_keys( Language
::fetchLanguageNames() );
600 foreach ( $codes as $code ) {
601 $sidebarKey = wfMemcKey( 'sidebar', $code );
602 $this->wanCache
->delete( $sidebarKey );
605 // Update the message in the message blob store
606 $resourceloader = RequestContext
::getMain()->getOutput()->getResourceLoader();
607 $blobStore = $resourceloader->getMessageBlobStore();
608 $blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
610 Hooks
::run( 'MessageCacheReplace', [ $title, $text ] );
614 * Is the given cache array expired due to time passing or a version change?
616 * @param array $cache
619 protected function isCacheExpired( $cache ) {
620 if ( !isset( $cache['VERSION'] ) ||
!isset( $cache['EXPIRY'] ) ) {
623 if ( $cache['VERSION'] != MSG_CACHE_VERSION
) {
626 if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
634 * Shortcut to update caches.
636 * @param array $cache Cached messages with a version.
637 * @param string $dest Either "local-only" to save to local caches only
638 * or "all" to save to all caches.
639 * @param string|bool $code Language code (default: false)
642 protected function saveToCaches( array $cache, $dest, $code = false ) {
643 if ( $dest === 'all' ) {
644 $cacheKey = wfMemcKey( 'messages', $code );
645 $success = $this->mMemc
->set( $cacheKey, $cache );
646 $this->setValidationHash( $code, $cache );
651 $this->saveToLocalCache( $code, $cache );
657 * Get the md5 used to validate the local APC cache
659 * @param string $code
660 * @return array (hash or false, bool expiry/volatility status)
662 protected function getValidationHash( $code ) {
664 $value = $this->wanCache
->get(
665 $this->wanCache
->makeKey( 'messages', $code, 'hash', 'v1' ),
667 [ wfMemcKey( 'messages', $code ) ]
671 $hash = $value['hash'];
672 if ( ( time() - $value['latest'] ) < WANObjectCache
::TTL_MINUTE
) {
673 // Cache was recently updated via replace() and should be up-to-date.
674 // That method is only called in the primary datacenter and uses FOR_UPDATE.
675 // Also, it is unlikely that the current datacenter is *now* secondary one.
678 // See if the "check" key was bumped after the hash was generated
679 $expired = ( $curTTL < 0 );
682 // No hash found at all; cache must regenerate to be safe
687 return [ $hash, $expired ];
691 * Set the md5 used to validate the local disk cache
693 * If $cache has a 'LATEST' UNIX timestamp key, then the hash will not
694 * be treated as "volatile" by getValidationHash() for the next few seconds.
695 * This is triggered when $cache is generated using FOR_UPDATE mode.
697 * @param string $code
698 * @param array $cache Cached messages with a version
700 protected function setValidationHash( $code, array $cache ) {
701 $this->wanCache
->set(
702 $this->wanCache
->makeKey( 'messages', $code, 'hash', 'v1' ),
704 'hash' => $cache['HASH'],
705 'latest' => isset( $cache['LATEST'] ) ?
$cache['LATEST'] : 0
707 WANObjectCache
::TTL_INDEFINITE
712 * @param string $key A language message cache key that stores blobs
713 * @param integer $timeout Wait timeout in seconds
714 * @return null|ScopedCallback
716 protected function getReentrantScopedLock( $key, $timeout = self
::WAIT_SEC
) {
717 return $this->mMemc
->getScopedLock( $key, $timeout, self
::LOCK_TTL
, __METHOD__
);
721 * Get a message from either the content language or the user language.
723 * First, assemble a list of languages to attempt getting the message from. This
724 * chain begins with the requested language and its fallbacks and then continues with
725 * the content language and its fallbacks. For each language in the chain, the following
726 * process will occur (in this order):
727 * 1. If a language-specific override, i.e., [[MW:msg/lang]], is available, use that.
728 * Note: for the content language, there is no /lang subpage.
729 * 2. Fetch from the static CDB cache.
730 * 3. If available, check the database for fallback language overrides.
732 * This process provides a number of guarantees. When changing this code, make sure all
733 * of these guarantees are preserved.
734 * * If the requested language is *not* the content language, then the CDB cache for that
735 * specific language will take precedence over the root database page ([[MW:msg]]).
736 * * Fallbacks will be just that: fallbacks. A fallback language will never be reached if
737 * the message is available *anywhere* in the language for which it is a fallback.
739 * @param string $key The message key
740 * @param bool $useDB If true, look for the message in the DB, false
741 * to use only the compiled l10n cache.
742 * @param bool|string|object $langcode Code of the language to get the message for.
743 * - If string and a valid code, will create a standard language object
744 * - If string but not a valid code, will create a basic language object
745 * - If boolean and false, create object from the current users language
746 * - If boolean and true, create object from the wikis content language
747 * - If language object, use it as given
748 * @param bool $isFullKey Specifies whether $key is a two part key "msg/lang".
750 * @throws MWException When given an invalid key
751 * @return string|bool False if the message doesn't exist, otherwise the
752 * message (which can be empty)
754 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
755 if ( is_int( $key ) ) {
756 // Fix numerical strings that somehow become ints
759 } elseif ( !is_string( $key ) ) {
760 throw new MWException( 'Non-string key given' );
761 } elseif ( $key === '' ) {
762 // Shortcut: the empty key is always missing
766 // For full keys, get the language code from the key
767 $pos = strrpos( $key, '/' );
768 if ( $isFullKey && $pos !== false ) {
769 $langcode = substr( $key, $pos +
1 );
770 $key = substr( $key, 0, $pos );
773 // Normalise title-case input (with some inlining)
774 $lckey = MessageCache
::normalizeKey( $key );
776 Hooks
::run( 'MessageCache::get', [ &$lckey ] );
778 // Loop through each language in the fallback list until we find something useful
779 $lang = wfGetLangObj( $langcode );
780 $message = $this->getMessageFromFallbackChain(
783 !$this->mDisable
&& $useDB
786 // If we still have no message, maybe the key was in fact a full key so try that
787 if ( $message === false ) {
788 $parts = explode( '/', $lckey );
789 // We may get calls for things that are http-urls from sidebar
790 // Let's not load nonexistent languages for those
791 // They usually have more than one slash.
792 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
793 $message = Language
::getMessageFor( $parts[0], $parts[1] );
794 if ( $message === null ) {
800 // Post-processing if the message exists
801 if ( $message !== false ) {
803 $message = str_replace(
805 # Fix for trailing whitespace, removed by textarea
807 # Fix for NBSP, converted to space by firefox
826 * Given a language, try and fetch messages from that language.
828 * Will also consider fallbacks of that language, the site language, and fallbacks for
831 * @see MessageCache::get
832 * @param Language|StubObject $lang Preferred language
833 * @param string $lckey Lowercase key for the message (as for localisation cache)
834 * @param bool $useDB Whether to include messages from the wiki database
835 * @return string|bool The message, or false if not found
837 protected function getMessageFromFallbackChain( $lang, $lckey, $useDB ) {
842 // First try the requested language.
843 $message = $this->getMessageForLang( $lang, $lckey, $useDB, $alreadyTried );
844 if ( $message !== false ) {
848 // Now try checking the site language.
849 $message = $this->getMessageForLang( $wgContLang, $lckey, $useDB, $alreadyTried );
854 * Given a language, try and fetch messages from that language and its fallbacks.
856 * @see MessageCache::get
857 * @param Language|StubObject $lang Preferred language
858 * @param string $lckey Lowercase key for the message (as for localisation cache)
859 * @param bool $useDB Whether to include messages from the wiki database
860 * @param bool[] $alreadyTried Contains true for each language that has been tried already
861 * @return string|bool The message, or false if not found
863 private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
866 $langcode = $lang->getCode();
868 // Try checking the database for the requested language
870 $uckey = $wgContLang->ucfirst( $lckey );
872 if ( !isset( $alreadyTried[ $langcode ] ) ) {
873 $message = $this->getMsgFromNamespace(
874 $this->getMessagePageName( $langcode, $uckey ),
878 if ( $message !== false ) {
881 $alreadyTried[ $langcode ] = true;
887 // Check the CDB cache
888 $message = $lang->getMessage( $lckey );
889 if ( $message !== null ) {
893 // Try checking the database for all of the fallback languages
895 $fallbackChain = Language
::getFallbacksFor( $langcode );
897 foreach ( $fallbackChain as $code ) {
898 if ( isset( $alreadyTried[ $code ] ) ) {
902 $message = $this->getMsgFromNamespace(
903 $this->getMessagePageName( $code, $uckey ), $code );
905 if ( $message !== false ) {
908 $alreadyTried[ $code ] = true;
916 * Get the message page name for a given language
918 * @param string $langcode
919 * @param string $uckey Uppercase key for the message
920 * @return string The page name
922 private function getMessagePageName( $langcode, $uckey ) {
923 global $wgLanguageCode;
925 if ( $langcode === $wgLanguageCode ) {
926 // Messages created in the content language will not have the /lang extension
929 return "$uckey/$langcode";
934 * Get a message from the MediaWiki namespace, with caching. The key must
935 * first be converted to two-part lang/msg form if necessary.
937 * Unlike self::get(), this function doesn't resolve fallback chains, and
938 * some callers require this behavior. LanguageConverter::parseCachedTable()
939 * and self::get() are some examples in core.
941 * @param string $title Message cache key with initial uppercase letter.
942 * @param string $code Code denoting the language to try.
943 * @return string|bool The message, or false if it does not exist or on error
945 public function getMsgFromNamespace( $title, $code ) {
946 $this->load( $code );
947 if ( isset( $this->mCache
[$code][$title] ) ) {
948 $entry = $this->mCache
[$code][$title];
949 if ( substr( $entry, 0, 1 ) === ' ' ) {
950 // The message exists, so make sure a string is returned.
951 return (string)substr( $entry, 1 );
952 } elseif ( $entry === '!NONEXISTENT' ) {
954 } elseif ( $entry === '!TOO BIG' ) {
955 // Fall through and try invididual message cache below
958 // XXX: This is not cached in process cache, should it?
960 Hooks
::run( 'MessagesPreLoad', [ $title, &$message ] );
961 if ( $message !== false ) {
968 // Try the individual message cache
969 $titleKey = $this->wanCache
->makeKey( 'messages-big', $this->mCache
[$code]['HASH'], $title );
971 if ( $this->mCacheVolatile
[$code] ) {
973 // Make sure that individual keys respect the WAN cache holdoff period too
974 LoggerFactory
::getInstance( 'MessageCache' )->debug(
975 __METHOD__
. ': loading volatile key \'{titleKey}\'',
976 [ 'titleKey' => $titleKey, 'code' => $code ] );
978 $entry = $this->wanCache
->get( $titleKey );
981 if ( $entry !== false ) {
982 if ( substr( $entry, 0, 1 ) === ' ' ) {
983 $this->mCache
[$code][$title] = $entry;
984 // The message exists, so make sure a string is returned
985 return (string)substr( $entry, 1 );
986 } elseif ( $entry === '!NONEXISTENT' ) {
987 $this->mCache
[$code][$title] = '!NONEXISTENT';
991 // Corrupt/obsolete entry, delete it
992 $this->wanCache
->delete( $titleKey );
996 // Try loading the message from the database
997 $dbr = wfGetDB( DB_REPLICA
);
998 $cacheOpts = Database
::getCacheSetOptions( $dbr );
999 // Use newKnownCurrent() to avoid querying revision/user tables
1000 $titleObj = Title
::makeTitle( NS_MEDIAWIKI
, $title );
1001 if ( $titleObj->getLatestRevID() ) {
1002 $revision = Revision
::newKnownCurrent(
1004 $titleObj->getArticleID(),
1005 $titleObj->getLatestRevID()
1012 $content = $revision->getContent();
1014 $message = $this->getMessageTextFromContent( $content );
1015 if ( is_string( $message ) ) {
1016 $this->mCache
[$code][$title] = ' ' . $message;
1017 $this->wanCache
->set( $titleKey, ' ' . $message, $this->mExpiry
, $cacheOpts );
1020 // A possibly temporary loading failure
1021 LoggerFactory
::getInstance( 'MessageCache' )->warning(
1022 __METHOD__
. ': failed to load message page text for \'{titleKey}\'',
1023 [ 'titleKey' => $titleKey, 'code' => $code ] );
1024 $message = null; // no negative caching
1027 $message = false; // negative caching
1030 if ( $message === false ) { // negative caching
1031 $this->mCache
[$code][$title] = '!NONEXISTENT';
1032 $this->wanCache
->set( $titleKey, '!NONEXISTENT', $this->mExpiry
, $cacheOpts );
1039 * @param string $message
1040 * @param bool $interface
1041 * @param string $language Language code
1042 * @param Title $title
1045 function transform( $message, $interface = false, $language = null, $title = null ) {
1046 // Avoid creating parser if nothing to transform
1047 if ( strpos( $message, '{{' ) === false ) {
1051 if ( $this->mInParser
) {
1055 $parser = $this->getParser();
1057 $popts = $this->getParserOptions();
1058 $popts->setInterfaceMessage( $interface );
1059 $popts->setTargetLanguage( $language );
1061 $userlang = $popts->setUserLang( $language );
1062 $this->mInParser
= true;
1063 $message = $parser->transformMsg( $message, $popts, $title );
1064 $this->mInParser
= false;
1065 $popts->setUserLang( $userlang );
1074 function getParser() {
1075 global $wgParser, $wgParserConf;
1077 if ( !$this->mParser
&& isset( $wgParser ) ) {
1078 # Do some initialisation so that we don't have to do it twice
1079 $wgParser->firstCallInit();
1080 # Clone it and store it
1081 $class = $wgParserConf['class'];
1082 if ( $class == 'ParserDiffTest' ) {
1084 $this->mParser
= new $class( $wgParserConf );
1086 $this->mParser
= clone $wgParser;
1090 return $this->mParser
;
1094 * @param string $text
1095 * @param Title $title
1096 * @param bool $linestart Whether or not this is at the start of a line
1097 * @param bool $interface Whether this is an interface message
1098 * @param Language|string $language Language code
1099 * @return ParserOutput|string
1101 public function parse( $text, $title = null, $linestart = true,
1102 $interface = false, $language = null
1106 if ( $this->mInParser
) {
1107 return htmlspecialchars( $text );
1110 $parser = $this->getParser();
1111 $popts = $this->getParserOptions();
1112 $popts->setInterfaceMessage( $interface );
1114 if ( is_string( $language ) ) {
1115 $language = Language
::factory( $language );
1117 $popts->setTargetLanguage( $language );
1119 if ( !$title ||
!$title instanceof Title
) {
1120 wfDebugLog( 'GlobalTitleFail', __METHOD__
. ' called by ' .
1121 wfGetAllCallers( 6 ) . ' with no title set.' );
1124 // Sometimes $wgTitle isn't set either...
1126 # It's not uncommon having a null $wgTitle in scripts. See r80898
1127 # Create a ghost title in such case
1128 $title = Title
::makeTitle( NS_SPECIAL
, 'Badtitle/title not set in ' . __METHOD__
);
1131 $this->mInParser
= true;
1132 $res = $parser->parse( $text, $title, $popts, $linestart );
1133 $this->mInParser
= false;
1138 function disable() {
1139 $this->mDisable
= true;
1143 $this->mDisable
= false;
1147 * Whether DB/cache usage is disabled for determining messages
1149 * If so, this typically indicates either:
1150 * - a) load() failed to find a cached copy nor query the DB
1151 * - b) we are in a special context or error mode that cannot use the DB
1152 * If the DB is ignored, any derived HTML output or cached objects may be wrong.
1153 * To avoid long-term cache pollution, TTLs can be adjusted accordingly.
1158 public function isDisabled() {
1159 return $this->mDisable
;
1163 * Clear all stored messages. Mainly used after a mass rebuild.
1166 $langs = Language
::fetchLanguageNames( null, 'mw' );
1167 foreach ( array_keys( $langs ) as $code ) {
1168 # Global and local caches
1169 $this->wanCache
->touchCheckKey( wfMemcKey( 'messages', $code ) );
1172 $this->mLoadedLanguages
= [];
1176 * @param string $key
1179 public function figureMessage( $key ) {
1180 global $wgLanguageCode;
1182 $pieces = explode( '/', $key );
1183 if ( count( $pieces ) < 2 ) {
1184 return [ $key, $wgLanguageCode ];
1187 $lang = array_pop( $pieces );
1188 if ( !Language
::fetchLanguageName( $lang, null, 'mw' ) ) {
1189 return [ $key, $wgLanguageCode ];
1192 $message = implode( '/', $pieces );
1194 return [ $message, $lang ];
1198 * Get all message keys stored in the message cache for a given language.
1199 * If $code is the content language code, this will return all message keys
1200 * for which MediaWiki:msgkey exists. If $code is another language code, this
1201 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
1202 * @param string $code Language code
1203 * @return array Array of message keys (strings)
1205 public function getAllMessageKeys( $code ) {
1208 $this->load( $code );
1209 if ( !isset( $this->mCache
[$code] ) ) {
1210 // Apparently load() failed
1213 // Remove administrative keys
1214 $cache = $this->mCache
[$code];
1215 unset( $cache['VERSION'] );
1216 unset( $cache['EXPIRY'] );
1217 unset( $cache['EXCESSIVE'] );
1218 // Remove any !NONEXISTENT keys
1219 $cache = array_diff( $cache, [ '!NONEXISTENT' ] );
1221 // Keys may appear with a capital first letter. lcfirst them.
1222 return array_map( [ $wgContLang, 'lcfirst' ], array_keys( $cache ) );
1226 * Purge message caches when a MediaWiki: page is created, updated, or deleted
1228 * @param Title $title Message page title
1229 * @param Content|null $content New content for edit/create, null on deletion
1232 public function updateMessageOverride( Title
$title, Content
$content = null ) {
1235 $msgText = $this->getMessageTextFromContent( $content );
1236 if ( $msgText === null ) {
1237 $msgText = false; // treat as not existing
1240 $this->replace( $title->getDBkey(), $msgText );
1242 if ( $wgContLang->hasVariants() ) {
1243 $wgContLang->updateConversionTable( $title );
1248 * @param Content|null $content Content or null if the message page does not exist
1249 * @return string|bool|null Returns false if $content is null and null on error
1251 private function getMessageTextFromContent( Content
$content = null ) {
1252 // @TODO: could skip pseudo-messages like js/css here, based on content model
1254 // Message page exists...
1255 // XXX: Is this the right way to turn a Content object into a message?
1256 // NOTE: $content is typically either WikitextContent, JavaScriptContent or
1257 // CssContent. MessageContent is *not* used for storing messages, it's
1258 // only used for wrapping them when needed.
1259 $msgText = $content->getWikitextForTransclusion();
1260 if ( $msgText === false ||
$msgText === null ) {
1261 // This might be due to some kind of misconfiguration...
1263 LoggerFactory
::getInstance( 'MessageCache' )->warning(
1264 __METHOD__
. ": message content doesn't provide wikitext "
1265 . "(content model: " . $content->getModel() . ")" );
1268 // Message page does not exist...