33use Psr\Log\LoggerAwareInterface;
34use Psr\Log\LoggerInterface;
36use Wikimedia\ScopedCallback;
42define(
'MSG_CACHE_VERSION', 2 );
62 private const WAN_TTL = IExpiringStore::TTL_DAY;
134 return MediaWikiServices::getInstance()->getMessageCache();
144 $lckey = strtr( $key,
' ',
'_' );
145 if ( ord( $lckey ) < 128 ) {
146 $lckey[0] = strtolower( $lckey[0] );
148 $lckey = MediaWikiServices::getInstance()->getContentLanguage()->lcfirst( $lckey );
177 LoggerInterface $logger,
185 $this->wanCache = $wanCache;
186 $this->clusterCache = $clusterCache;
187 $this->srvCache = $serverCache;
188 $this->contLang = $contLang;
189 $this->contLangConverter = $contLangConverter;
190 $this->logger = $logger;
191 $this->langFactory = $langFactory;
192 $this->localisationCache = $localisationCache;
193 $this->languageNameUtils = $languageNameUtils;
194 $this->languageFallback = $languageFallback;
195 $this->hookRunner =
new HookRunner( $hookContainer );
199 $this->mDisable = !( $options[
'useDB'] ??
true );
203 $this->logger = $logger;
214 if ( !$this->mParserOptions ) {
215 if ( !$wgUser || !$wgUser->isSafeToLoad() ) {
219 $po = ParserOptions::newFromAnon();
220 $po->setAllowUnsafeRawHtml(
false );
228 $this->mParserOptions->setAllowUnsafeRawHtml(
false );
231 return $this->mParserOptions;
241 $cacheKey = $this->srvCache->makeKey( __CLASS__, $code );
243 return $this->srvCache->get( $cacheKey );
253 $cacheKey = $this->srvCache->makeKey( __CLASS__, $code );
254 $this->srvCache->set( $cacheKey,
$cache );
278 protected function load( $code, $mode =
null ) {
279 if ( !is_string( $code ) ) {
280 throw new InvalidArgumentException(
"Missing language code" );
283 # Don't do double loading...
288 # 8 lines of code just to say (once) that message cache is disabled
289 if ( $this->mDisable ) {
290 static $shownDisabled =
false;
291 if ( !$shownDisabled ) {
292 $this->logger->debug( __METHOD__ .
': disabled' );
293 $shownDisabled =
true;
299 # Loading code starts
300 $success =
false; # Keep track of success
301 $staleCache =
false; # a cache array with expired data, or
false if none has been loaded
302 $where = []; # Debug info, delayed to avoid spamming debug log too much
304 # Hash of the contents is stored in memcache, to detect if data-center cache
305 # or local cache goes out of date (e.g. due to replace() on some other server)
307 $this->cacheVolatile[$code] = $hashVolatile;
309 # Try the local cache and check against the cluster hash key...
312 $where[] =
'local cache is empty';
313 } elseif ( !isset(
$cache[
'HASH'] ) ||
$cache[
'HASH'] !== $hash ) {
314 $where[] =
'local cache has the wrong hash';
317 $where[] =
'local cache is expired';
319 } elseif ( $hashVolatile ) {
320 $where[] =
'local cache validation key is expired/volatile';
323 $where[] =
'got from local cache';
324 $this->cache->set( $code,
$cache );
329 $cacheKey = $this->clusterCache->makeKey(
'messages', $code );
330 # Try the global cache. If it is empty, try to acquire a lock. If
331 # the lock can't be acquired, wait for the other thread to finish
332 # and then try the global cache a second time.
333 for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++ ) {
334 if ( $hashVolatile && $staleCache ) {
335 # Do not bother fetching the whole cache blob to avoid I/O.
336 # Instead, just try to get the non-blocking $statusKey lock
337 # below, and use the local stale value if it was not acquired.
338 $where[] =
'global cache is presumed expired';
340 $cache = $this->clusterCache->get( $cacheKey );
342 $where[] =
'global cache is empty';
344 $where[] =
'global cache is expired';
346 } elseif ( $hashVolatile ) {
347 # DB results are replica DB lag prone until the holdoff TTL passes.
348 # By then, updates should be reflected in loadFromDBWithLock().
349 # One thread regenerates the cache while others use old values.
350 $where[] =
'global cache is expired/volatile';
353 $where[] =
'got from global cache';
354 $this->cache->set( $code,
$cache );
361 # Done, no need to retry
365 # We need to call loadFromDB. Limit the concurrency to one process.
366 # This prevents the site from going down when the cache expires.
367 # Note that the DB slam protection lock here is non-blocking.
369 if ( $loadStatus ===
true ) {
372 } elseif ( $staleCache ) {
373 # Use the stale cache while some other thread constructs the new one
374 $where[] =
'using stale cache';
375 $this->cache->set( $code, $staleCache );
378 } elseif ( $failedAttempts > 0 ) {
379 # Already blocked once, so avoid another lock/unlock cycle.
380 # This case will typically be hit if memcached is down, or if
381 # loadFromDB() takes longer than LOCK_WAIT.
382 $where[] =
"could not acquire status key.";
384 } elseif ( $loadStatus ===
'cantacquire' ) {
385 # Wait for the other thread to finish, then retry. Normally,
386 # the memcached get() will then yield the other thread's result.
387 $where[] =
'waited for other thread to complete';
390 # Disable cache; $loadStatus is 'disabled'
397 $where[] =
'loading FAILED - cache is disabled';
398 $this->mDisable =
true;
399 $this->cache->set( $code, [] );
400 $this->logger->error( __METHOD__ .
": Failed to load $code" );
401 # This used to throw an exception, but that led to nasty side effects like
402 # the whole wiki being instantly down if the memcached server died
406 throw new LogicException(
"Process cache for '$code' should be set by now." );
409 $info = implode(
', ', $where );
410 $this->logger->debug( __METHOD__ .
": Loading $code... $info" );
422 # If cache updates on all levels fail, give up on message overrides.
423 # This is to avoid easy site outages; see $saveSuccess comments below.
424 $statusKey = $this->clusterCache->makeKey(
'messages', $code,
'status' );
425 $status = $this->clusterCache->get( $statusKey );
426 if ( $status ===
'error' ) {
427 $where[] =
"could not load; method is still globally disabled";
431 # Now let's regenerate
432 $where[] =
'loading from database';
434 # Lock the cache to prevent conflicting writes.
435 # This lock is non-blocking so stale cache can quickly be used.
436 # Note that load() will call a blocking getReentrantScopedLock()
437 # after this if it really need to wait for any current thread.
438 $cacheKey = $this->clusterCache->makeKey(
'messages', $code );
440 if ( !$scopedLock ) {
441 $where[] =
'could not acquire main lock';
442 return 'cantacquire';
446 $this->cache->set( $code,
$cache );
449 if ( !$saveSuccess ) {
464 $this->clusterCache->set( $statusKey,
'error', 60 * 5 );
465 $where[] =
'could not save cache, disabled globally for 5 minutes';
467 $where[] =
"could not save global cache";
500 foreach ( $mostused as $key => $value ) {
501 $mostused[$key] =
"$value/$code";
507 'page_is_redirect' => 0,
510 if ( count( $mostused ) ) {
511 $conds[
'page_title'] = $mostused;
513 $conds[] =
'page_title' .
$dbr->buildLike(
$dbr->anyString(),
'/', $code );
515 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
516 # other than language code.
517 $conds[] =
'page_title NOT' .
518 $dbr->buildLike(
$dbr->anyString(),
'/',
$dbr->anyString() );
524 [
'page_title',
'page_latest' ],
526 __METHOD__ .
"($code)-big"
528 foreach (
$res as $row ) {
532 $cache[$row->page_title] =
'!TOO BIG';
535 $cache[
'EXCESSIVE'][$row->page_title] = $row->page_latest;
540 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
542 $revQuery = $revisionStore->getQueryInfo( [
'page',
'user' ] );
555 array_diff(
$revQuery[
'tables'], [
'page' ] )
561 array_merge( $conds, [
563 'page_latest = rev_id'
565 __METHOD__ .
"($code)-small",
569 foreach (
$res as $row ) {
574 $rev = $revisionStore->newRevisionFromRow( $row, 0, Title::newFromRow( $row ) );
577 }
catch ( Exception $ex ) {
581 if ( !is_string( $text ) ) {
583 $this->logger->error(
585 .
": failed to load message page text for {$row->page_title} ($code)"
588 $entry =
' ' . $text;
590 $cache[$row->page_title] = $entry;
594 $cache[
'EXCESSIVE'][$row->page_title] = $row->page_latest;
601 # Hash for validating local cache (APC). No need to take into account
602 # messages larger than $wgMaxMsgCacheEntrySize, since those are only
603 # stored and fetched from memcache.
606 unset(
$cache[
'EXCESSIVE'] );
624 return $this->cache->hasField(
$lang,
'VERSION' );
642 $name = $this->contLang->lcfirst( $name );
645 if ( strpos( $name,
'conversiontable/' ) === 0 ) {
648 $msg = preg_replace(
'/\/[a-z0-9-]{2,}$/',
'', $name );
650 if ( $code ===
null ) {
652 if ( $this->systemMessageNames ===
null ) {
653 $this->systemMessageNames = array_flip(
654 $this->localisationCache->getSubitemList(
$wgLanguageCode,
'messages' ) );
656 return isset( $this->systemMessageNames[$msg] );
659 return $this->localisationCache->getSubitem( $code,
'messages', $msg ) !==
null;
672 if ( $this->mDisable ) {
683 if ( $text ===
false ) {
685 $this->cache->setField( $code,
$title,
'!NONEXISTENT' );
688 $this->cache->setField( $code,
$title,
' ' . $text );
692 DeferredUpdates::addUpdate(
694 DeferredUpdates::PRESEND
708 $this->clusterCache->makeKey(
'messages', $code )
710 if ( !$scopedLock ) {
711 foreach ( $replacements as list(
$title ) ) {
712 $this->logger->error(
713 __METHOD__ .
': could not acquire lock to update {title} ({code})',
714 [
'title' =>
$title,
'code' => $code ] );
722 if ( $this->
load( $code, self::FOR_UPDATE ) ) {
723 $cache = $this->cache->get( $code );
729 $newTextByTitle = [];
731 foreach ( $replacements as list(
$title ) ) {
733 $page->loadPageData( $page::READ_LATEST );
736 $newTextByTitle[
$title] = $text;
738 if ( !is_string( $text ) ) {
742 $newBigTitles[
$title] = $page->getLatest();
753 foreach ( $newBigTitles as
$title => $id ) {
755 $this->wanCache->set(
757 ' ' . $newTextByTitle[
$title],
763 $cache[
'LATEST'] = time();
765 $this->cache->set( $code,
$cache );
772 ScopedCallback::consume( $scopedLock );
776 $this->wanCache->touchCheckKey( $this->
getCheckKey( $code ) );
779 $blobStore = MediaWikiServices::getInstance()->getResourceLoader()->getMessageBlobStore();
780 foreach ( $replacements as list(
$title, $msg ) ) {
781 $blobStore->updateMessage( $this->contLang->lcfirst( $msg ) );
782 $this->hookRunner->onMessageCacheReplace(
$title, $newTextByTitle[
$title] );
793 if ( !isset(
$cache[
'VERSION'] ) || !isset(
$cache[
'EXPIRY'] ) ) {
816 if ( $dest ===
'all' ) {
817 $cacheKey = $this->clusterCache->makeKey(
'messages', $code );
837 $value = $this->wanCache->get(
838 $this->wanCache->makeKey(
'messages', $code,
'hash',
'v1' ),
840 [ $this->getCheckKey( $code ) ]
844 $hash = $value[
'hash'];
845 if ( ( time() - $value[
'latest'] ) < WANObjectCache::TTL_MINUTE ) {
852 $expired = ( $curTTL < 0 );
860 return [ $hash, $expired ];
874 $this->wanCache->set(
875 $this->wanCache->makeKey(
'messages', $code,
'hash',
'v1' ),
878 'latest' =>
$cache[
'LATEST'] ?? 0
880 WANObjectCache::TTL_INDEFINITE
890 return $this->clusterCache->getScopedLock( $key, $timeout, self::LOCK_TTL, __METHOD__ );
926 public function get( $key, $useDB =
true, $langcode =
true ) {
927 if ( is_int( $key ) ) {
931 } elseif ( !is_string( $key ) ) {
933 } elseif ( $key ===
'' ) {
939 $lckey = self::normalizeKey( $key );
941 $this->hookRunner->onMessageCache__get( $lckey );
947 !$this->mDisable && $useDB
951 if ( $message ===
false ) {
952 $parts = explode(
'/', $lckey );
956 if ( count( $parts ) == 2 && $parts[1] !==
'' ) {
957 $message = $this->localisationCache->getSubitem( $parts[1],
'messages', $parts[0] );
958 if ( $message ===
null ) {
965 if ( $message !==
false ) {
967 $message = str_replace(
969 # Fix
for trailing whitespace, removed by textarea
971 # Fix
for NBSP, converted to space by firefox
1006 if ( $message !==
false ) {
1011 $message = $this->
getMessageForLang( $this->contLang, $lckey, $useDB, $alreadyTried );
1026 $langcode =
$lang->getCode();
1030 $uckey = $this->contLang->ucfirst( $lckey );
1032 if ( !isset( $alreadyTried[$langcode] ) ) {
1037 if ( $message !==
false ) {
1040 $alreadyTried[$langcode] =
true;
1047 $message =
$lang->getMessage( $lckey );
1048 if ( $message !==
null ) {
1054 $fallbackChain = $this->languageFallback->getAll( $langcode );
1056 foreach ( $fallbackChain as $code ) {
1057 if ( isset( $alreadyTried[$code] ) ) {
1064 if ( $message !==
false ) {
1067 $alreadyTried[$code] =
true;
1088 return "$uckey/$langcode";
1108 $this->
load( $code );
1110 $entry = $this->cache->getField( $code,
$title );
1112 if ( $entry !==
null ) {
1114 if ( substr( $entry, 0, 1 ) ===
' ' ) {
1116 return (
string)substr( $entry, 1 );
1117 } elseif ( $entry ===
'!NONEXISTENT' ) {
1126 $this->cache->getField( $code,
'HASH' )
1137 $this->cache->getField( $code,
'HASH' )
1140 if ( $entry ===
null || substr( $entry, 0, 1 ) !==
' ' ) {
1143 $this->hookRunner->onMessagesPreLoad(
$title, $message, $code );
1144 if ( $message !==
false ) {
1145 $this->cache->setField( $code,
$title,
' ' . $message );
1147 $this->cache->setField( $code,
$title,
'!NONEXISTENT' );
1154 if ( $entry !==
false && substr( $entry, 0, 1 ) ===
' ' ) {
1155 if ( $this->cacheVolatile[$code] ) {
1157 $this->logger->debug(
1158 __METHOD__ .
': loading volatile key \'{titleKey}\'',
1159 [
'titleKey' =>
$title,
'code' => $code ] );
1161 $this->cache->setField( $code,
$title, $entry );
1164 return (
string)substr( $entry, 1 );
1167 $this->cache->setField( $code,
$title,
'!NONEXISTENT' );
1179 $fname = __METHOD__;
1180 return $this->srvCache->getWithSetCallback(
1181 $this->srvCache->makeKey(
'messages-big', $hash, $dbKey ),
1182 BagOStuff::TTL_HOUR,
1183 function () use ( $code, $dbKey, $hash, $fname ) {
1184 return $this->wanCache->getWithSetCallback(
1187 function ( $oldValue, &$ttl, &$setOpts ) use ( $dbKey, $code, $fname ) {
1194 $revision = MediaWikiServices::getInstance()
1195 ->getRevisionLookup()
1196 ->getKnownCurrentRevision(
$title );
1200 return '!NONEXISTENT';
1202 $content = $revision->getContent( SlotRecord::MAIN );
1206 $this->logger->warning(
1207 $fname .
': failed to load page text for \'{titleKey}\'',
1208 [
'titleKey' => $dbKey,
'code' => $code ]
1213 if ( !is_string( $message ) ) {
1218 return '!NONEXISTENT';
1221 return ' ' . $message;
1235 public function transform( $message, $interface =
false, $language =
null,
$title =
null ) {
1237 if ( strpos( $message,
'{{' ) ===
false ) {
1241 if ( $this->mInParser ) {
1248 $popts->setInterfaceMessage( $interface );
1249 $popts->setTargetLanguage( $language );
1251 $userlang = $popts->setUserLang( $language );
1252 $this->mInParser =
true;
1253 $message = $parser->transformMsg( $message, $popts,
$title );
1254 $this->mInParser =
false;
1255 $popts->setUserLang( $userlang );
1265 if ( !$this->mParser ) {
1266 $parser = MediaWikiServices::getInstance()->getParser();
1267 # Do some initialisation so that we don't have to do it twice
1268 $parser->firstCallInit();
1269 # Clone it and store it
1270 $this->mParser = clone $parser;
1273 return $this->mParser;
1285 $interface =
false, $language =
null
1289 if ( $this->mInParser ) {
1290 return htmlspecialchars( $text );
1295 $popts->setInterfaceMessage( $interface );
1297 if ( is_string( $language ) ) {
1298 $language = $this->langFactory->getLanguage( $language );
1300 $popts->setTargetLanguage( $language );
1303 $logger = LoggerFactory::getInstance(
'GlobalTitleFail' );
1305 __METHOD__ .
' called with no title set.',
1306 [
'exception' =>
new Exception ]
1312 # It's not uncommon having a null $wgTitle in scripts. See r80898
1313 # Create a ghost title in such case
1314 $title = Title::makeTitle(
NS_SPECIAL,
'Badtitle/title not set in ' . __METHOD__ );
1317 $this->mInParser =
true;
1318 $res = $parser->parse( $text,
$title, $popts, $linestart );
1319 $this->mInParser =
false;
1325 $this->mDisable =
true;
1329 $this->mDisable =
false;
1345 return $this->mDisable;
1354 $langs = $this->languageNameUtils->getLanguageNames(
null,
'mw' );
1355 foreach ( array_keys( $langs ) as $code ) {
1356 $this->wanCache->touchCheckKey( $this->
getCheckKey( $code ) );
1358 $this->cache->clear();
1368 $pieces = explode(
'/', $key );
1369 if ( count( $pieces ) < 2 ) {
1373 $lang = array_pop( $pieces );
1374 if ( !$this->languageNameUtils->getLanguageName(
$lang,
null,
'mw' ) ) {
1378 $message = implode(
'/', $pieces );
1380 return [ $message,
$lang ];
1392 $this->
load( $code );
1393 if ( !$this->cache->has( $code ) ) {
1398 $cache = $this->cache->get( $code );
1399 unset(
$cache[
'VERSION'] );
1400 unset(
$cache[
'EXPIRY'] );
1401 unset(
$cache[
'EXCESSIVE'] );
1406 return array_map( [ $this->contLang,
'lcfirst' ], array_keys(
$cache ) );
1418 if ( $msgText ===
null ) {
1424 if ( $this->contLangConverter->hasVariants() ) {
1425 $this->contLangConverter->updateConversionTable( $linkTarget );
1434 return $this->wanCache->makeKey(
'messages', $code );
1449 $msgText =
$content->getWikitextForTransclusion();
1450 if ( $msgText ===
false || $msgText ===
null ) {
1453 $this->logger->warning(
1454 __METHOD__ .
": message content doesn't provide wikitext "
1455 .
"(content model: " .
$content->getModel() .
")" );
1471 return $this->wanCache->makeKey(
'messages-big', $hash,
$title );
$wgLanguageCode
Site language code.
$wgAdaptiveMessageCache
Instead of caching everything, only cache those messages which have been customised in the site conte...
$wgMaxMsgCacheEntrySize
Maximum entry size in the message cache, in bytes.
wfGetLangObj( $langcode=false)
Return a Language object from $langcode.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
const MSG_CACHE_VERSION
MediaWiki message cache structure version.
Class representing a cache/ephemeral data store.
A BagOStuff object with no objects in it.
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Class for caching the contents of localisation files, Messages*.php and *.i18n.php.
Handles a simple LRU key/value map with a maximum number of entries.
Message cache purging and in-place update handler for specific message page changes.
Cache of messages that are defined by MediaWiki namespace pages or by hooks.
getValidationHash( $code)
Get the md5 used to validate the local APC cache.
isLanguageLoaded( $lang)
Whether the language was loaded and its data is still in the process cache.
loadFromDBWithLock( $code, array &$where, $mode=null)
const LOCK_TTL
How long memcached locks last.
loadFromDB( $code, $mode=null)
Loads cacheable messages from the database.
getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried)
Given a language, try and fetch messages from that language and its fallbacks.
saveToLocalCache( $code, $cache)
Save the cache to APC.
LanguageFactory $langFactory
getMessagePageName( $langcode, $uckey)
Get the message page name for a given language.
saveToCaches(array $cache, $dest, $code=false)
Shortcut to update caches.
__construct(WANObjectCache $wanCache, BagOStuff $clusterCache, BagOStuff $serverCache, Language $contLang, ILanguageConverter $contLangConverter, LoggerInterface $logger, array $options, LanguageFactory $langFactory, LocalisationCache $localisationCache, LanguageNameUtils $languageNameUtils, LanguageFallback $languageFallback, HookContainer $hookContainer)
refreshAndReplaceInternal( $code, array $replacements)
setValidationHash( $code, array $cache)
Set the md5 used to validate the local disk cache.
isCacheExpired( $cache)
Is the given cache array expired due to time passing or a version change?
getMsgFromNamespace( $title, $code)
Get a message from the MediaWiki namespace, with caching.
getReentrantScopedLock( $key, $timeout=self::WAIT_SEC)
const WAIT_SEC
How long to wait for memcached locks.
bool $mDisable
Should mean that database cannot be used, but check.
getLocalCache( $code)
Try to load the cache from APC.
LocalisationCache $localisationCache
LanguageNameUtils $languageNameUtils
isMainCacheable( $name, $code=null)
Can the given DB key be added to the main cache blob? To reduce the impact of abuse of the MediaWiki ...
LanguageFallback $languageFallback
load( $code, $mode=null)
Loads messages from caches or from database in this order: (1) local message cache (if $wgUseLocalMes...
updateMessageOverride(LinkTarget $linkTarget, Content $content=null)
Purge message caches when a MediaWiki: page is created, updated, or deleted.
transform( $message, $interface=false, $language=null, $title=null)
getMessageFromFallbackChain( $lang, $lckey, $useDB)
Given a language, try and fetch messages from that language.
isDisabled()
Whether DB/cache usage is disabled for determining messages.
setLogger(LoggerInterface $logger)
loadCachedMessagePageEntry( $dbKey, $code, $hash)
getMessageTextFromContent(Content $content=null)
clear()
Clear all stored messages in global and local cache.
static singleton()
Get the singleton instance of this class.
getAllMessageKeys( $code)
Get all message keys stored in the message cache for a given language.
MapCacheLRU $cache
Process cache of loaded messages that are defined in MediaWiki namespace.
static normalizeKey( $key)
Normalize message key input.
bool[] $cacheVolatile
Map of (language code => boolean)
array $systemMessageNames
Map of (lowercase message key => unused) for all software defined messages.
parse( $text, $title=null, $linestart=true, $interface=false, $language=null)
ParserOptions $mParserOptions
Message cache has its own parser which it uses to transform messages.
ILanguageConverter $contLangConverter
replace( $title, $text)
Updates cache as necessary when message page is changed.
getParserOptions()
ParserOptions is lazy initialised.
bigMessageCacheKey( $hash, $title)
Set options of the Parser.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Represents a title within MediaWiki.
Multi-datacenter aware caching interface.
Base interface for content objects.
The shared interface for all language converters.
if(!isset( $args[0])) $lang