MediaWiki  master
MessageCache.php
Go to the documentation of this file.
1 <?php
34 use Psr\Log\LoggerAwareInterface;
35 use Psr\Log\LoggerInterface;
37 use Wikimedia\RequestTimeout\TimeoutException;
38 use Wikimedia\ScopedCallback;
39 
44 define( 'MSG_CACHE_VERSION', 2 );
45 
51 class MessageCache implements LoggerAwareInterface {
56  public const MAX_REQUEST_LANGUAGES = 10;
57 
58  private const FOR_UPDATE = 1; // force message reload
59 
61  private const WAIT_SEC = 15;
63  private const LOCK_TTL = 30;
64 
69  private const WAN_TTL = IExpiringStore::TTL_DAY;
70 
72  private $logger;
73 
79  protected $cache;
80 
87 
91  protected $cacheVolatile = [];
92 
97  protected $mDisable;
98 
103  protected $mParserOptions;
105  protected $mParser;
106 
110  protected $mInParser = false;
111 
113  protected $wanCache;
115  protected $clusterCache;
117  protected $srvCache;
119  protected $contLang;
121  protected $contLangCode;
125  protected $langFactory;
131  protected $languageFallback;
133  private $hookRunner;
134 
141  public static function normalizeKey( $key ) {
142  $lckey = strtr( $key, ' ', '_' );
143  if ( $lckey === '' ) {
144  // T300792
145  return $lckey;
146  }
147 
148  if ( ord( $lckey ) < 128 ) {
149  $lckey[0] = strtolower( $lckey[0] );
150  } else {
151  $lckey = MediaWikiServices::getInstance()->getContentLanguage()->lcfirst( $lckey );
152  }
153 
154  return $lckey;
155  }
156 
174  public function __construct(
177  BagOStuff $serverCache,
179  LanguageConverterFactory $langConverterFactory,
180  LoggerInterface $logger,
181  array $options,
186  HookContainer $hookContainer
187  ) {
188  $this->wanCache = $wanCache;
189  $this->clusterCache = $clusterCache;
190  $this->srvCache = $serverCache;
191  $this->contLang = $contLang;
192  $this->contLangConverter = $langConverterFactory->getLanguageConverter( $contLang );
193  $this->contLangCode = $contLang->getCode();
194  $this->logger = $logger;
195  $this->langFactory = $langFactory;
196  $this->localisationCache = $localisationCache;
197  $this->languageNameUtils = $languageNameUtils;
198  $this->languageFallback = $languageFallback;
199  $this->hookRunner = new HookRunner( $hookContainer );
200 
201  // limit size
202  $this->cache = new MapCacheLRU( self::MAX_REQUEST_LANGUAGES );
203 
204  $this->mDisable = !( $options['useDB'] ?? true );
205  }
206 
207  public function setLogger( LoggerInterface $logger ) {
208  $this->logger = $logger;
209  }
210 
216  private function getParserOptions() {
217  if ( !$this->mParserOptions ) {
218  $context = RequestContext::getMain();
219  $user = $context->getUser();
220  if ( !$user->isSafeToLoad() ) {
221  // It isn't safe to use the context user yet, so don't try to get a
222  // ParserOptions for it. And don't cache this ParserOptions
223  // either.
225  $po->setAllowUnsafeRawHtml( false );
226  return $po;
227  }
228 
229  $this->mParserOptions = ParserOptions::newFromContext( $context );
230  // Messages may take parameters that could come
231  // from malicious sources. As a precaution, disable
232  // the <html> parser tag when parsing messages.
233  $this->mParserOptions->setAllowUnsafeRawHtml( false );
234  }
235 
236  return $this->mParserOptions;
237  }
238 
245  protected function getLocalCache( $code ) {
246  $cacheKey = $this->srvCache->makeKey( __CLASS__, $code );
247 
248  return $this->srvCache->get( $cacheKey );
249  }
250 
257  protected function saveToLocalCache( $code, $cache ) {
258  $cacheKey = $this->srvCache->makeKey( __CLASS__, $code );
259  $this->srvCache->set( $cacheKey, $cache );
260  }
261 
283  protected function load( $code, $mode = null ) {
284  if ( !is_string( $code ) ) {
285  throw new InvalidArgumentException( "Missing language code" );
286  }
287 
288  # Don't do double loading...
289  if ( $this->isLanguageLoaded( $code ) && $mode != self::FOR_UPDATE ) {
290  return true;
291  }
292 
293  # 8 lines of code just to say (once) that message cache is disabled
294  if ( $this->mDisable ) {
295  static $shownDisabled = false;
296  if ( !$shownDisabled ) {
297  $this->logger->debug( __METHOD__ . ': disabled' );
298  $shownDisabled = true;
299  }
300 
301  return true;
302  }
303 
304  # Loading code starts
305  $success = false; # Keep track of success
306  $staleCache = false; # a cache array with expired data, or false if none has been loaded
307  $where = []; # Debug info, delayed to avoid spamming debug log too much
308 
309  # Hash of the contents is stored in memcache, to detect if data-center cache
310  # or local cache goes out of date (e.g. due to replace() on some other server)
311  list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
312  $this->cacheVolatile[$code] = $hashVolatile;
313 
314  # Try the local cache and check against the cluster hash key...
315  $cache = $this->getLocalCache( $code );
316  if ( !$cache ) {
317  $where[] = 'local cache is empty';
318  } elseif ( !isset( $cache['HASH'] ) || $cache['HASH'] !== $hash ) {
319  $where[] = 'local cache has the wrong hash';
320  $staleCache = $cache;
321  } elseif ( $this->isCacheExpired( $cache ) ) {
322  $where[] = 'local cache is expired';
323  $staleCache = $cache;
324  } elseif ( $hashVolatile ) {
325  $where[] = 'local cache validation key is expired/volatile';
326  $staleCache = $cache;
327  } else {
328  $where[] = 'got from local cache';
329  $this->cache->set( $code, $cache );
330  $success = true;
331  }
332 
333  if ( !$success ) {
334  $cacheKey = $this->clusterCache->makeKey( 'messages', $code );
335  # Try the global cache. If it is empty, try to acquire a lock. If
336  # the lock can't be acquired, wait for the other thread to finish
337  # and then try the global cache a second time.
338  for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++ ) {
339  if ( $hashVolatile && $staleCache ) {
340  # Do not bother fetching the whole cache blob to avoid I/O.
341  # Instead, just try to get the non-blocking $statusKey lock
342  # below, and use the local stale value if it was not acquired.
343  $where[] = 'global cache is presumed expired';
344  } else {
345  $cache = $this->clusterCache->get( $cacheKey );
346  if ( !$cache ) {
347  $where[] = 'global cache is empty';
348  } elseif ( $this->isCacheExpired( $cache ) ) {
349  $where[] = 'global cache is expired';
350  $staleCache = $cache;
351  } elseif ( $hashVolatile ) {
352  # DB results are replica DB lag prone until the holdoff TTL passes.
353  # By then, updates should be reflected in loadFromDBWithLock().
354  # One thread regenerates the cache while others use old values.
355  $where[] = 'global cache is expired/volatile';
356  $staleCache = $cache;
357  } else {
358  $where[] = 'got from global cache';
359  $this->cache->set( $code, $cache );
360  $this->saveToCaches( $cache, 'local-only', $code );
361  $success = true;
362  }
363  }
364 
365  if ( $success ) {
366  # Done, no need to retry
367  break;
368  }
369 
370  # We need to call loadFromDB. Limit the concurrency to one process.
371  # This prevents the site from going down when the cache expires.
372  # Note that the DB slam protection lock here is non-blocking.
373  $loadStatus = $this->loadFromDBWithLock( $code, $where, $mode );
374  if ( $loadStatus === true ) {
375  $success = true;
376  break;
377  } elseif ( $staleCache ) {
378  # Use the stale cache while some other thread constructs the new one
379  $where[] = 'using stale cache';
380  $this->cache->set( $code, $staleCache );
381  $success = true;
382  break;
383  } elseif ( $failedAttempts > 0 ) {
384  # Already blocked once, so avoid another lock/unlock cycle.
385  # This case will typically be hit if memcached is down, or if
386  # loadFromDB() takes longer than LOCK_WAIT.
387  $where[] = "could not acquire status key.";
388  break;
389  } elseif ( $loadStatus === 'cantacquire' ) {
390  # Wait for the other thread to finish, then retry. Normally,
391  # the memcached get() will then yield the other thread's result.
392  $where[] = 'waited for other thread to complete';
393  $this->getReentrantScopedLock( $cacheKey );
394  } else {
395  # Disable cache; $loadStatus is 'disabled'
396  break;
397  }
398  }
399  }
400 
401  if ( !$success ) {
402  $where[] = 'loading FAILED - cache is disabled';
403  $this->mDisable = true;
404  $this->cache->set( $code, [] );
405  $this->logger->error( __METHOD__ . ": Failed to load $code" );
406  # This used to throw an exception, but that led to nasty side effects like
407  # the whole wiki being instantly down if the memcached server died
408  }
409 
410  if ( !$this->isLanguageLoaded( $code ) ) {
411  throw new LogicException( "Process cache for '$code' should be set by now." );
412  }
413 
414  $info = implode( ', ', $where );
415  $this->logger->debug( __METHOD__ . ": Loading $code... $info" );
416 
417  return $success;
418  }
419 
426  protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
427  # If cache updates on all levels fail, give up on message overrides.
428  # This is to avoid easy site outages; see $saveSuccess comments below.
429  $statusKey = $this->clusterCache->makeKey( 'messages', $code, 'status' );
430  $status = $this->clusterCache->get( $statusKey );
431  if ( $status === 'error' ) {
432  $where[] = "could not load; method is still globally disabled";
433  return 'disabled';
434  }
435 
436  # Now let's regenerate
437  $where[] = 'loading from database';
438 
439  # Lock the cache to prevent conflicting writes.
440  # This lock is non-blocking so stale cache can quickly be used.
441  # Note that load() will call a blocking getReentrantScopedLock()
442  # after this if it really need to wait for any current thread.
443  $cacheKey = $this->clusterCache->makeKey( 'messages', $code );
444  $scopedLock = $this->getReentrantScopedLock( $cacheKey, 0 );
445  if ( !$scopedLock ) {
446  $where[] = 'could not acquire main lock';
447  return 'cantacquire';
448  }
449 
450  $cache = $this->loadFromDB( $code, $mode );
451  $this->cache->set( $code, $cache );
452  $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
453 
454  if ( !$saveSuccess ) {
468  if ( $this->srvCache instanceof EmptyBagOStuff ) {
469  $this->clusterCache->set( $statusKey, 'error', 60 * 5 );
470  $where[] = 'could not save cache, disabled globally for 5 minutes';
471  } else {
472  $where[] = "could not save global cache";
473  }
474  }
475 
476  return true;
477  }
478 
488  protected function loadFromDB( $code, $mode = null ) {
489  $maxMsgCacheEntrySize = MediaWikiServices::getInstance()->getMainConfig()
490  ->get( MainConfigNames::MaxMsgCacheEntrySize );
491  $adaptiveMessageCache = MediaWikiServices::getInstance()->getMainConfig()
492  ->get( MainConfigNames::AdaptiveMessageCache );
493  // (T164666) The query here performs really poorly on WMF's
494  // contributions replicas. We don't have a way to say "any group except
495  // contributions", so for the moment let's specify 'api'.
496  // @todo: Get rid of this hack.
497  $dbr = wfGetDB( ( $mode == self::FOR_UPDATE ) ? DB_PRIMARY : DB_REPLICA, 'api' );
498 
499  $cache = [];
500 
501  $mostused = []; // list of "<cased message key>/<code>"
502  if ( $adaptiveMessageCache && $code !== $this->contLangCode ) {
503  if ( !$this->cache->has( $this->contLangCode ) ) {
504  $this->load( $this->contLangCode );
505  }
506  $mostused = array_keys( $this->cache->get( $this->contLangCode ) );
507  foreach ( $mostused as $key => $value ) {
508  $mostused[$key] = "$value/$code";
509  }
510  }
511 
512  // Common conditions
513  $conds = [
514  'page_is_redirect' => 0,
515  'page_namespace' => NS_MEDIAWIKI,
516  ];
517  if ( count( $mostused ) ) {
518  $conds['page_title'] = $mostused;
519  } elseif ( $code !== $this->contLangCode ) {
520  $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
521  } else {
522  # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
523  # other than language code.
524  $conds[] = 'page_title NOT' .
525  $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
526  }
527 
528  // Set the stubs for oversized software-defined messages in the main cache map
529  $res = $dbr->select(
530  'page',
531  [ 'page_title', 'page_latest' ],
532  array_merge( $conds, [ 'page_len > ' . intval( $maxMsgCacheEntrySize ) ] ),
533  __METHOD__ . "($code)-big"
534  );
535  foreach ( $res as $row ) {
536  // Include entries/stubs for all keys in $mostused in adaptive mode
537  if ( $adaptiveMessageCache || $this->isMainCacheable( $row->page_title )
538  ) {
539  $cache[$row->page_title] = '!TOO BIG';
540  }
541  // At least include revision ID so page changes are reflected in the hash
542  $cache['EXCESSIVE'][$row->page_title] = $row->page_latest;
543  }
544 
545  // Can not inject the RevisionStore as it would break the installer since
546  // it instantiates MessageCache before the DB.
547  $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
548  // Set the text for small software-defined messages in the main cache map
549  $revQuery = $revisionStore->getQueryInfo( [ 'page' ] );
550 
551  // T231196: MySQL/MariaDB (10.1.37) can sometimes irrationally decide that querying `actor` then
552  // `revision` then `page` is somehow better than starting with `page`. Tell it not to reorder the
553  // query (and also reorder it ourselves because as generated by RevisionStore it'll have
554  // `revision` first rather than `page`).
555  $revQuery['joins']['revision'] = $revQuery['joins']['page'];
556  unset( $revQuery['joins']['page'] );
557  // It isn't actually necessary to reorder $revQuery['tables'] as Database does the right thing
558  // when join conditions are given for all joins, but GergÅ‘ is wary of relying on that so pull
559  // `page` to the start.
560  $revQuery['tables'] = array_merge(
561  [ 'page' ],
562  array_diff( $revQuery['tables'], [ 'page' ] )
563  );
564 
565  $res = $dbr->select(
566  $revQuery['tables'],
567  $revQuery['fields'],
568  array_merge( $conds, [
569  'page_len <= ' . intval( $maxMsgCacheEntrySize ),
570  'page_latest = rev_id' // get the latest revision only
571  ] ),
572  __METHOD__ . "($code)-small",
573  [ 'STRAIGHT_JOIN' ],
574  $revQuery['joins']
575  );
576  $result = $revisionStore->newRevisionsFromBatch( $res, [
577  'slots' => [ SlotRecord::MAIN ],
578  'content' => true
579  ] );
580  $revisions = $result->isOK() ? $result->getValue() : [];
581  foreach ( $res as $row ) {
582  // Include entries/stubs for all keys in $mostused in adaptive mode
583  if ( $adaptiveMessageCache || $this->isMainCacheable( $row->page_title )
584  ) {
585  try {
586  $rev = $revisions[$row->rev_id] ?? null;
587  $content = $rev ? $rev->getContent( SlotRecord::MAIN ) : null;
588  $text = $this->getMessageTextFromContent( $content );
589  } catch ( TimeoutException $e ) {
590  throw $e;
591  } catch ( Exception $ex ) {
592  $text = false;
593  }
594 
595  if ( !is_string( $text ) ) {
596  $entry = '!ERROR';
597  $this->logger->error(
598  __METHOD__
599  . ": failed to load message page text for {$row->page_title} ($code)"
600  );
601  } else {
602  $entry = ' ' . $text;
603  }
604  $cache[$row->page_title] = $entry;
605  } else {
606  // T193271: cache object gets too big and slow to generate.
607  // At least include revision ID so page changes are reflected in the hash.
608  $cache['EXCESSIVE'][$row->page_title] = $row->page_latest;
609  }
610  }
611 
612  $cache['VERSION'] = MSG_CACHE_VERSION;
613  ksort( $cache );
614 
615  # Hash for validating local cache (APC). No need to take into account
616  # messages larger than $wgMaxMsgCacheEntrySize, since those are only
617  # stored and fetched from memcache.
618  $cache['HASH'] = md5( serialize( $cache ) );
619  $cache['EXPIRY'] = wfTimestamp( TS_MW, time() + self::WAN_TTL );
620  unset( $cache['EXCESSIVE'] ); // only needed for hash
621 
622  return $cache;
623  }
624 
631  private function isLanguageLoaded( $lang ) {
632  // It is important that this only returns true if the cache was fully
633  // populated by load(), so that callers can assume all cache keys exist.
634  // It is possible for $this->cache to be only partially populated through
635  // methods like MessageCache::replace(), which must not make this method
636  // return true (T208897). And this method must cease to return true
637  // if the language was evicted by MapCacheLRU (T230690).
638  return $this->cache->hasField( $lang, 'VERSION' );
639  }
640 
652  private function isMainCacheable( $name, $code = null ) {
653  // Convert first letter to lowercase, and strip /code suffix
654  $name = $this->contLang->lcfirst( $name );
655  // Include common conversion table pages. This also avoids problems with
656  // Installer::parse() bailing out due to disallowed DB queries (T207979).
657  if ( strpos( $name, 'conversiontable/' ) === 0 ) {
658  return true;
659  }
660  $msg = preg_replace( '/\/[a-z0-9-]{2,}$/', '', $name );
661 
662  if ( $code === null ) {
663  // Bulk load
664  if ( $this->systemMessageNames === null ) {
665  $this->systemMessageNames = array_fill_keys(
666  $this->localisationCache->getSubitemList( $this->contLangCode, 'messages' ),
667  true );
668  }
669  return isset( $this->systemMessageNames[$msg] );
670  } else {
671  // Use individual subitem
672  return $this->localisationCache->getSubitem( $code, 'messages', $msg ) !== null;
673  }
674  }
675 
682  public function replace( $title, $text ) {
683  if ( $this->mDisable ) {
684  return;
685  }
686 
687  list( $msg, $code ) = $this->figureMessage( $title );
688  if ( strpos( $title, '/' ) !== false && $code === $this->contLangCode ) {
689  // Content language overrides do not use the /<code> suffix
690  return;
691  }
692 
693  // (a) Update the process cache with the new message text
694  if ( $text === false ) {
695  // Page deleted
696  $this->cache->setField( $code, $title, '!NONEXISTENT' );
697  } else {
698  // Ignore $wgMaxMsgCacheEntrySize so the process cache is up to date
699  $this->cache->setField( $code, $title, ' ' . $text );
700  }
701 
702  // (b) Update the shared caches in a deferred update with a fresh DB snapshot
704  new MessageCacheUpdate( $code, $title, $msg ),
705  DeferredUpdates::PRESEND
706  );
707  }
708 
714  public function refreshAndReplaceInternal( $code, array $replacements ) {
715  $maxMsgCacheEntrySize = MediaWikiServices::getInstance()->getMainConfig()
716  ->get( MainConfigNames::MaxMsgCacheEntrySize );
717 
718  // Allow one caller at a time to avoid race conditions
719  $scopedLock = $this->getReentrantScopedLock(
720  $this->clusterCache->makeKey( 'messages', $code )
721  );
722  if ( !$scopedLock ) {
723  foreach ( $replacements as list( $title ) ) {
724  $this->logger->error(
725  __METHOD__ . ': could not acquire lock to update {title} ({code})',
726  [ 'title' => $title, 'code' => $code ] );
727  }
728 
729  return;
730  }
731 
732  // Load the existing cache to update it in the local DC cache.
733  // The other DCs will see a hash mismatch.
734  if ( $this->load( $code, self::FOR_UPDATE ) ) {
735  $cache = $this->cache->get( $code );
736  } else {
737  // Err? Fall back to loading from the database.
738  $cache = $this->loadFromDB( $code, self::FOR_UPDATE );
739  }
740  // Check if individual cache keys should exist and update cache accordingly
741  $newTextByTitle = []; // map of (title => content)
742  $newBigTitles = []; // map of (title => latest revision ID), like EXCESSIVE in loadFromDB()
743  // Can not inject the WikiPageFactory as it would break the installer since
744  // it instantiates MessageCache before the DB.
745  $wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory();
746  foreach ( $replacements as list( $title ) ) {
747  $page = $wikiPageFactory->newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
748  $page->loadPageData( $page::READ_LATEST );
749  $text = $this->getMessageTextFromContent( $page->getContent() );
750  // Remember the text for the blob store update later on
751  $newTextByTitle[$title] = $text ?? '';
752  // Note that if $text is false, then $cache should have a !NONEXISTANT entry
753  if ( !is_string( $text ) ) {
754  $cache[$title] = '!NONEXISTENT';
755  } elseif ( strlen( $text ) > $maxMsgCacheEntrySize ) {
756  $cache[$title] = '!TOO BIG';
757  $newBigTitles[$title] = $page->getLatest();
758  } else {
759  $cache[$title] = ' ' . $text;
760  }
761  }
762  // Update HASH for the new key. Incorporates various administrative keys,
763  // including the old HASH (and thereby the EXCESSIVE value from loadFromDB()
764  // and previous replace() calls), but that doesn't really matter since we
765  // only ever compare it for equality with a copy saved by saveToCaches().
766  $cache['HASH'] = md5( serialize( $cache + [ 'EXCESSIVE' => $newBigTitles ] ) );
767  // Update the too-big WAN cache entries now that we have the new HASH
768  foreach ( $newBigTitles as $title => $id ) {
769  // Match logic of loadCachedMessagePageEntry()
770  $this->wanCache->set(
771  $this->bigMessageCacheKey( $cache['HASH'], $title ),
772  ' ' . $newTextByTitle[$title],
773  self::WAN_TTL
774  );
775  }
776  // Mark this cache as definitely being "latest" (non-volatile) so
777  // load() calls do not try to refresh the cache with replica DB data
778  $cache['LATEST'] = time();
779  // Update the process cache
780  $this->cache->set( $code, $cache );
781  // Pre-emptively update the local datacenter cache so things like edit filter and
782  // prevented changes are reflected immediately; these often use MediaWiki: pages.
783  // The datacenter handling replace() calls should be the same one handling edits
784  // as they require HTTP POST.
785  $this->saveToCaches( $cache, 'all', $code );
786  // Release the lock now that the cache is saved
787  ScopedCallback::consume( $scopedLock );
788 
789  // Relay the purge. Touching this check key expires cache contents
790  // and local cache (APC) validation hash across all datacenters.
791  $this->wanCache->touchCheckKey( $this->getCheckKey( $code ) );
792 
793  // Purge the messages in the message blob store and fire any hook handlers
794  $blobStore = MediaWikiServices::getInstance()->getResourceLoader()->getMessageBlobStore();
795  foreach ( $replacements as list( $title, $msg ) ) {
796  $blobStore->updateMessage( $this->contLang->lcfirst( $msg ) );
797  $this->hookRunner->onMessageCacheReplace( $title, $newTextByTitle[$title] );
798  }
799  }
800 
807  protected function isCacheExpired( $cache ) {
808  if ( !isset( $cache['VERSION'] ) || !isset( $cache['EXPIRY'] ) ) {
809  return true;
810  }
811  if ( $cache['VERSION'] != MSG_CACHE_VERSION ) {
812  return true;
813  }
814  if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
815  return true;
816  }
817 
818  return false;
819  }
820 
830  protected function saveToCaches( array $cache, $dest, $code = false ) {
831  if ( $dest === 'all' ) {
832  $cacheKey = $this->clusterCache->makeKey( 'messages', $code );
833  $success = $this->clusterCache->set( $cacheKey, $cache );
834  $this->setValidationHash( $code, $cache );
835  } else {
836  $success = true;
837  }
838 
839  $this->saveToLocalCache( $code, $cache );
840 
841  return $success;
842  }
843 
850  protected function getValidationHash( $code ) {
851  $curTTL = null;
852  $value = $this->wanCache->get(
853  $this->wanCache->makeKey( 'messages', $code, 'hash', 'v1' ),
854  $curTTL,
855  [ $this->getCheckKey( $code ) ]
856  );
857 
858  if ( $value ) {
859  $hash = $value['hash'];
860  if ( ( time() - $value['latest'] ) < WANObjectCache::TTL_MINUTE ) {
861  // Cache was recently updated via replace() and should be up-to-date.
862  // That method is only called in the primary datacenter and uses FOR_UPDATE.
863  // Also, it is unlikely that the current datacenter is *now* secondary one.
864  $expired = false;
865  } else {
866  // See if the "check" key was bumped after the hash was generated
867  $expired = ( $curTTL < 0 );
868  }
869  } else {
870  // No hash found at all; cache must regenerate to be safe
871  $hash = false;
872  $expired = true;
873  }
874 
875  return [ $hash, $expired ];
876  }
877 
888  protected function setValidationHash( $code, array $cache ) {
889  $this->wanCache->set(
890  $this->wanCache->makeKey( 'messages', $code, 'hash', 'v1' ),
891  [
892  'hash' => $cache['HASH'],
893  'latest' => $cache['LATEST'] ?? 0
894  ],
895  WANObjectCache::TTL_INDEFINITE
896  );
897  }
898 
904  protected function getReentrantScopedLock( $key, $timeout = self::WAIT_SEC ) {
905  return $this->clusterCache->getScopedLock( $key, $timeout, self::LOCK_TTL, __METHOD__ );
906  }
907 
941  public function get( $key, $useDB = true, $langcode = true ) {
942  if ( is_int( $key ) ) {
943  // Fix numerical strings that somehow become ints
944  // on their way here
945  $key = (string)$key;
946  } elseif ( !is_string( $key ) ) {
947  throw new MWException( 'Non-string key given' );
948  } elseif ( $key === '' ) {
949  // Shortcut: the empty key is always missing
950  return false;
951  }
952 
953  // Normalise title-case input (with some inlining)
954  $lckey = self::normalizeKey( $key );
955 
956  $this->hookRunner->onMessageCache__get( $lckey );
957 
958  // Loop through each language in the fallback list until we find something useful
959  $message = $this->getMessageFromFallbackChain(
960  wfGetLangObj( $langcode ),
961  $lckey,
962  !$this->mDisable && $useDB
963  );
964 
965  // If we still have no message, maybe the key was in fact a full key so try that
966  if ( $message === false ) {
967  $parts = explode( '/', $lckey );
968  // We may get calls for things that are http-urls from sidebar
969  // Let's not load nonexistent languages for those
970  // They usually have more than one slash.
971  if ( count( $parts ) == 2 && $parts[1] !== '' ) {
972  $message = $this->localisationCache->getSubitem( $parts[1], 'messages', $parts[0] );
973  if ( $message === null ) {
974  $message = false;
975  }
976  }
977  }
978 
979  // Post-processing if the message exists
980  if ( $message !== false ) {
981  // Fix whitespace
982  $message = str_replace(
983  [
984  # Fix for trailing whitespace, removed by textarea
985  '&#32;',
986  # Fix for NBSP, converted to space by firefox
987  '&nbsp;',
988  '&#160;',
989  '&shy;'
990  ],
991  [
992  ' ',
993  "\u{00A0}",
994  "\u{00A0}",
995  "\u{00AD}"
996  ],
997  $message
998  );
999  }
1000 
1001  return $message;
1002  }
1003 
1016  protected function getMessageFromFallbackChain( $lang, $lckey, $useDB ) {
1017  $alreadyTried = [];
1018 
1019  // First try the requested language.
1020  $message = $this->getMessageForLang( $lang, $lckey, $useDB, $alreadyTried );
1021  if ( $message !== false ) {
1022  return $message;
1023  }
1024 
1025  // Now try checking the site language.
1026  $message = $this->getMessageForLang( $this->contLang, $lckey, $useDB, $alreadyTried );
1027  return $message;
1028  }
1029 
1040  private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
1041  $langcode = $lang->getCode();
1042 
1043  // Try checking the database for the requested language
1044  if ( $useDB ) {
1045  $uckey = $this->contLang->ucfirst( $lckey );
1046 
1047  if ( !isset( $alreadyTried[$langcode] ) ) {
1048  $message = $this->getMsgFromNamespace(
1049  $this->getMessagePageName( $langcode, $uckey ),
1050  $langcode
1051  );
1052  if ( $message !== false ) {
1053  return $message;
1054  }
1055  $alreadyTried[$langcode] = true;
1056  }
1057  } else {
1058  $uckey = null;
1059  }
1060 
1061  // Check the CDB cache
1062  $message = $lang->getMessage( $lckey );
1063  if ( $message !== null ) {
1064  return $message;
1065  }
1066 
1067  // Try checking the database for all of the fallback languages
1068  if ( $useDB ) {
1069  $fallbackChain = $this->languageFallback->getAll( $langcode );
1070 
1071  foreach ( $fallbackChain as $code ) {
1072  if ( isset( $alreadyTried[$code] ) ) {
1073  continue;
1074  }
1075 
1076  $message = $this->getMsgFromNamespace(
1077  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable uckey is set when used
1078  $this->getMessagePageName( $code, $uckey ), $code );
1079 
1080  if ( $message !== false ) {
1081  return $message;
1082  }
1083  $alreadyTried[$code] = true;
1084  }
1085  }
1086 
1087  return false;
1088  }
1089 
1097  private function getMessagePageName( $langcode, $uckey ) {
1098  if ( $langcode === $this->contLangCode ) {
1099  // Messages created in the content language will not have the /lang extension
1100  return $uckey;
1101  } else {
1102  return "$uckey/$langcode";
1103  }
1104  }
1105 
1118  public function getMsgFromNamespace( $title, $code ) {
1119  // Load all MediaWiki page definitions into cache. Note that individual keys
1120  // already loaded into cache during this request remain in the cache, which
1121  // includes the value of hook-defined messages.
1122  $this->load( $code );
1123 
1124  $entry = $this->cache->getField( $code, $title );
1125 
1126  if ( $entry !== null ) {
1127  // Message page exists as an override of a software messages
1128  if ( substr( $entry, 0, 1 ) === ' ' ) {
1129  // The message exists and is not '!TOO BIG' or '!ERROR'
1130  return (string)substr( $entry, 1 );
1131  } elseif ( $entry === '!NONEXISTENT' ) {
1132  // The text might be '-' or missing due to some data loss
1133  return false;
1134  }
1135  // Load the message page, utilizing the individual message cache.
1136  // If the page does not exist, there will be no hook handler fallbacks.
1137  $entry = $this->loadCachedMessagePageEntry(
1138  $title,
1139  $code,
1140  $this->cache->getField( $code, 'HASH' )
1141  );
1142  } else {
1143  // Message page either does not exist or does not override a software message
1144  if ( !$this->isMainCacheable( $title, $code ) ) {
1145  // Message page does not override any software-defined message. A custom
1146  // message might be defined to have content or settings specific to the wiki.
1147  // Load the message page, utilizing the individual message cache as needed.
1148  $entry = $this->loadCachedMessagePageEntry(
1149  $title,
1150  $code,
1151  $this->cache->getField( $code, 'HASH' )
1152  );
1153  }
1154  if ( $entry === null || substr( $entry, 0, 1 ) !== ' ' ) {
1155  // Message does not have a MediaWiki page definition; try hook handlers
1156  $message = false;
1157  // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args
1158  $this->hookRunner->onMessagesPreLoad( $title, $message, $code );
1159  if ( $message !== false ) {
1160  $this->cache->setField( $code, $title, ' ' . $message );
1161  } else {
1162  $this->cache->setField( $code, $title, '!NONEXISTENT' );
1163  }
1164 
1165  return $message;
1166  }
1167  }
1168 
1169  if ( $entry !== false && substr( $entry, 0, 1 ) === ' ' ) {
1170  if ( $this->cacheVolatile[$code] ) {
1171  // Make sure that individual keys respect the WAN cache holdoff period too
1172  $this->logger->debug(
1173  __METHOD__ . ': loading volatile key \'{titleKey}\'',
1174  [ 'titleKey' => $title, 'code' => $code ] );
1175  } else {
1176  $this->cache->setField( $code, $title, $entry );
1177  }
1178  // The message exists, so make sure a string is returned
1179  return (string)substr( $entry, 1 );
1180  }
1181 
1182  $this->cache->setField( $code, $title, '!NONEXISTENT' );
1183 
1184  return false;
1185  }
1186 
1193  private function loadCachedMessagePageEntry( $dbKey, $code, $hash ) {
1194  $fname = __METHOD__;
1195  return $this->srvCache->getWithSetCallback(
1196  $this->srvCache->makeKey( 'messages-big', $hash, $dbKey ),
1197  BagOStuff::TTL_HOUR,
1198  function () use ( $code, $dbKey, $hash, $fname ) {
1199  return $this->wanCache->getWithSetCallback(
1200  $this->bigMessageCacheKey( $hash, $dbKey ),
1201  self::WAN_TTL,
1202  function ( $oldValue, &$ttl, &$setOpts ) use ( $dbKey, $code, $fname ) {
1203  // Try loading the message from the database
1204  $setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) );
1205  // Use newKnownCurrent() to avoid querying revision/user tables
1206  $title = Title::makeTitle( NS_MEDIAWIKI, $dbKey );
1207  // Injecting RevisionStore breaks installer since it
1208  // instantiates MessageCache before DB.
1209  $revision = MediaWikiServices::getInstance()
1210  ->getRevisionLookup()
1211  ->getKnownCurrentRevision( $title );
1212  if ( !$revision ) {
1213  // The wiki doesn't have a local override page. Cache absence with normal TTL.
1214  // When overrides are created, self::replace() takes care of the cache.
1215  return '!NONEXISTENT';
1216  }
1217  $content = $revision->getContent( SlotRecord::MAIN );
1218  if ( $content ) {
1219  $message = $this->getMessageTextFromContent( $content );
1220  } else {
1221  $this->logger->warning(
1222  $fname . ': failed to load page text for \'{titleKey}\'',
1223  [ 'titleKey' => $dbKey, 'code' => $code ]
1224  );
1225  $message = null;
1226  }
1227 
1228  if ( !is_string( $message ) ) {
1229  // Revision failed to load Content, or Content is incompatible with wikitext.
1230  // Possibly a temporary loading failure.
1231  $ttl = 5;
1232 
1233  return '!NONEXISTENT';
1234  }
1235 
1236  return ' ' . $message;
1237  }
1238  );
1239  }
1240  );
1241  }
1242 
1250  public function transform( $message, $interface = false, $language = null, PageReference $page = null ) {
1251  // Avoid creating parser if nothing to transform
1252  if ( strpos( $message, '{{' ) === false ) {
1253  return $message;
1254  }
1255 
1256  if ( $this->mInParser ) {
1257  return $message;
1258  }
1259 
1260  $parser = $this->getParser();
1261  if ( $parser ) {
1262  $popts = $this->getParserOptions();
1263  $popts->setInterfaceMessage( $interface );
1264  $popts->setTargetLanguage( $language );
1265 
1266  $userlang = $popts->setUserLang( $language );
1267  $this->mInParser = true;
1268  $message = $parser->transformMsg( $message, $popts, $page );
1269  $this->mInParser = false;
1270  $popts->setUserLang( $userlang );
1271  }
1272 
1273  return $message;
1274  }
1275 
1279  public function getParser() {
1280  if ( !$this->mParser ) {
1281  $parser = MediaWikiServices::getInstance()->getParser();
1282  # Clone it and store it
1283  $this->mParser = clone $parser;
1284  }
1285 
1286  return $this->mParser;
1287  }
1288 
1297  public function parse( $text, PageReference $page = null, $linestart = true,
1298  $interface = false, $language = null
1299  ) {
1300  global $wgTitle;
1301 
1302  if ( $this->mInParser ) {
1303  return htmlspecialchars( $text );
1304  }
1305 
1306  $parser = $this->getParser();
1307  $popts = $this->getParserOptions();
1308  $popts->setInterfaceMessage( $interface );
1309 
1310  if ( is_string( $language ) ) {
1311  $language = $this->langFactory->getLanguage( $language );
1312  }
1313  $popts->setTargetLanguage( $language );
1314 
1315  if ( !$page ) {
1316  $logger = LoggerFactory::getInstance( 'GlobalTitleFail' );
1317  $logger->info(
1318  __METHOD__ . ' called with no title set.',
1319  [ 'exception' => new Exception ]
1320  );
1321  $page = $wgTitle;
1322  }
1323  // Sometimes $wgTitle isn't set either...
1324  if ( !$page ) {
1325  # It's not uncommon having a null $wgTitle in scripts. See r80898
1326  # Create a ghost title in such case
1327  $page = PageReferenceValue::localReference(
1328  NS_SPECIAL,
1329  'Badtitle/title not set in ' . __METHOD__
1330  );
1331  }
1332 
1333  $this->mInParser = true;
1334  $res = $parser->parse( $text, $page, $popts, $linestart );
1335  $this->mInParser = false;
1336 
1337  return $res;
1338  }
1339 
1340  public function disable() {
1341  $this->mDisable = true;
1342  }
1343 
1344  public function enable() {
1345  $this->mDisable = false;
1346  }
1347 
1360  public function isDisabled() {
1361  return $this->mDisable;
1362  }
1363 
1369  public function clear() {
1370  $langs = $this->languageNameUtils->getLanguageNames();
1371  foreach ( array_keys( $langs ) as $code ) {
1372  $this->wanCache->touchCheckKey( $this->getCheckKey( $code ) );
1373  }
1374  $this->cache->clear();
1375  }
1376 
1381  public function figureMessage( $key ) {
1382  $pieces = explode( '/', $key );
1383  if ( count( $pieces ) < 2 ) {
1384  return [ $key, $this->contLangCode ];
1385  }
1386 
1387  $lang = array_pop( $pieces );
1388  if ( !$this->languageNameUtils->getLanguageName(
1389  $lang,
1390  LanguageNameUtils::AUTONYMS,
1391  LanguageNameUtils::DEFINED
1392  ) ) {
1393  return [ $key, $this->contLangCode ];
1394  }
1395 
1396  $message = implode( '/', $pieces );
1397 
1398  return [ $message, $lang ];
1399  }
1400 
1409  public function getAllMessageKeys( $code ) {
1410  $this->load( $code );
1411  if ( !$this->cache->has( $code ) ) {
1412  // Apparently load() failed
1413  return null;
1414  }
1415  // Remove administrative keys
1416  $cache = $this->cache->get( $code );
1417  unset( $cache['VERSION'] );
1418  unset( $cache['EXPIRY'] );
1419  unset( $cache['EXCESSIVE'] );
1420  // Remove any !NONEXISTENT keys
1421  $cache = array_diff( $cache, [ '!NONEXISTENT' ] );
1422 
1423  // Keys may appear with a capital first letter. lcfirst them.
1424  return array_map( [ $this->contLang, 'lcfirst' ], array_keys( $cache ) );
1425  }
1426 
1434  public function updateMessageOverride( LinkTarget $linkTarget, Content $content = null ) {
1435  $msgText = $this->getMessageTextFromContent( $content );
1436  if ( $msgText === null ) {
1437  $msgText = false; // treat as not existing
1438  }
1439 
1440  $this->replace( $linkTarget->getDBkey(), $msgText );
1441 
1442  if ( $this->contLangConverter->hasVariants() ) {
1443  $this->contLangConverter->updateConversionTable( $linkTarget );
1444  }
1445  }
1446 
1451  public function getCheckKey( $code ) {
1452  return $this->wanCache->makeKey( 'messages', $code );
1453  }
1454 
1459  private function getMessageTextFromContent( Content $content = null ) {
1460  // @TODO: could skip pseudo-messages like js/css here, based on content model
1461  if ( $content ) {
1462  // Message page exists...
1463  // XXX: Is this the right way to turn a Content object into a message?
1464  // NOTE: $content is typically either WikitextContent, JavaScriptContent or
1465  // CssContent. MessageContent is *not* used for storing messages, it's
1466  // only used for wrapping them when needed.
1467  $msgText = $content->getWikitextForTransclusion();
1468  if ( $msgText === false || $msgText === null ) {
1469  // This might be due to some kind of misconfiguration...
1470  $msgText = null;
1471  $this->logger->warning(
1472  __METHOD__ . ": message content doesn't provide wikitext "
1473  . "(content model: " . $content->getModel() . ")" );
1474  }
1475  } else {
1476  // Message page does not exist...
1477  $msgText = false;
1478  }
1479 
1480  return $msgText;
1481  }
1482 
1488  private function bigMessageCacheKey( $hash, $title ) {
1489  return $this->wanCache->makeKey( 'messages-big', $hash, $title );
1490  }
1491 }
serialize()
const NS_MEDIAWIKI
Definition: Defines.php:72
const NS_SPECIAL
Definition: Defines.php:53
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.
$success
if(!defined( 'MW_NO_SESSION') &&! $wgCommandLineMode) $wgTitle
Definition: Setup.php:486
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:87
static addUpdate(DeferrableUpdate $update, $stage=self::POSTSEND)
Add an update to the pending update queue for execution at the appropriate time.
A BagOStuff object with no objects in it.
Base class for language-specific code.
Definition: Language.php:53
getCode()
Get the internal language code for this language object.
Definition: Language.php:4375
Caching for the contents of localisation files.
MediaWiki exception.
Definition: MWException.php:29
Handles a simple LRU key/value map with a maximum number of entries.
Definition: MapCacheLRU.php:36
set( $key, $value, $rank=self::RANK_TOP)
Set a key/value pair.
get( $key, $maxAge=INF, $default=null)
Get the value for a key.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:561
An interface for creating language converters.
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
A service that provides utilities to do with language names and codes.
PSR-3 logger instance factory.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Immutable value object representing a page reference.
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
Message cache purging and in-place update handler for specific message page changes.
Cache 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.
Language $contLang
const MAX_REQUEST_LANGUAGES
The size of the MapCacheLRU which stores message data.
getCheckKey( $code)
saveToCaches(array $cache, $dest, $code=false)
Shortcut to update caches.
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.
parse( $text, PageReference $page=null, $linestart=true, $interface=false, $language=null)
transform( $message, $interface=false, $language=null, PageReference $page=null)
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 ...
string $contLangCode
LanguageFallback $languageFallback
load( $code, $mode=null)
Loads messages from caches or from database in this order: (1) local message cache (if $wgUseLocalMes...
__construct(WANObjectCache $wanCache, BagOStuff $clusterCache, BagOStuff $serverCache, Language $contLang, LanguageConverterFactory $langConverterFactory, LoggerInterface $logger, array $options, LanguageFactory $langFactory, LocalisationCache $localisationCache, LanguageNameUtils $languageNameUtils, LanguageFallback $languageFallback, HookContainer $hookContainer)
updateMessageOverride(LinkTarget $linkTarget, Content $content=null)
Purge message caches when a MediaWiki: page is created, updated, or deleted.
BagOStuff $srvCache
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)
BagOStuff $clusterCache
loadCachedMessagePageEntry( $dbKey, $code, $hash)
getMessageTextFromContent(Content $content=null)
clear()
Clear all stored messages in global and local cache.
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.
LoggerInterface $logger
WANObjectCache $wanCache
static normalizeKey( $key)
Normalize message key input.
const FOR_UPDATE
figureMessage( $key)
bool[] $cacheVolatile
Map of (language code => boolean)
array $systemMessageNames
Map of (lowercase message key => unused) for all software defined messages.
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.
HookRunner $hookRunner
bigMessageCacheKey( $hash, $title)
static newFromContext(IContextSource $context)
Get a ParserOptions object from a IContextSource object.
static newFromAnon()
Get a ParserOptions object for an anonymous user.
static getMain()
Get the RequestContext object associated with the main request.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:638
Multi-datacenter aware caching interface.
Base interface for content objects.
Definition: Content.php:35
getDBkey()
Get the main part of the link target, in canonical database form.
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
const DB_REPLICA
Definition: defines.php:26
const DB_PRIMARY
Definition: defines.php:28
$content
Definition: router.php:76
return true
Definition: router.php:90
if(!isset( $args[0])) $lang
$revQuery