MediaWiki  master
MessageCache.php
Go to the documentation of this file.
1 <?php
28 
33 define( 'MSG_CACHE_VERSION', 2 );
34 
41 class MessageCache implements LoggerAwareInterface {
42  const FOR_UPDATE = 1; // force message reload
43 
45  const WAIT_SEC = 15;
47  const LOCK_TTL = 30;
48 
53  const WAN_TTL = IExpiringStore::TTL_DAY;
54 
56  private $logger;
57 
63  protected $cache;
64 
70  protected $overridable;
71 
75  protected $cacheVolatile = [];
76 
81  protected $mDisable;
82 
87  protected $mParserOptions;
89  protected $mParser;
90 
94  protected $mInParser = false;
95 
97  protected $wanCache;
99  protected $clusterCache;
101  protected $srvCache;
103  protected $contLang;
104 
112  public static function singleton() {
113  return MediaWikiServices::getInstance()->getMessageCache();
114  }
115 
122  public static function normalizeKey( $key ) {
123  $lckey = strtr( $key, ' ', '_' );
124  if ( ord( $lckey ) < 128 ) {
125  $lckey[0] = strtolower( $lckey[0] );
126  } else {
127  $lckey = MediaWikiServices::getInstance()->getContentLanguage()->lcfirst( $lckey );
128  }
129 
130  return $lckey;
131  }
132 
144  public function __construct(
147  BagOStuff $serverCache,
149  LoggerInterface $logger,
150  array $options
151  ) {
152  $this->wanCache = $wanCache;
153  $this->clusterCache = $clusterCache;
154  $this->srvCache = $serverCache;
155  $this->contLang = $contLang;
156  $this->logger = $logger;
157 
158  $this->cache = new MapCacheLRU( 5 ); // limit size for sanity
159 
160  $this->mDisable = !( $options['useDB'] ?? true );
161  }
162 
163  public function setLogger( LoggerInterface $logger ) {
164  $this->logger = $logger;
165  }
166 
172  function getParserOptions() {
173  global $wgUser;
174 
175  if ( !$this->mParserOptions ) {
176  if ( !$wgUser || !$wgUser->isSafeToLoad() ) {
177  // $wgUser isn't available yet, so don't try to get a
178  // ParserOptions for it. And don't cache this ParserOptions
179  // either.
181  $po->setAllowUnsafeRawHtml( false );
182  $po->setTidy( true );
183  return $po;
184  }
185 
186  $this->mParserOptions = new ParserOptions;
187  // Messages may take parameters that could come
188  // from malicious sources. As a precaution, disable
189  // the <html> parser tag when parsing messages.
190  $this->mParserOptions->setAllowUnsafeRawHtml( false );
191  // For the same reason, tidy the output!
192  $this->mParserOptions->setTidy( true );
193  }
194 
195  return $this->mParserOptions;
196  }
197 
204  protected function getLocalCache( $code ) {
205  $cacheKey = $this->srvCache->makeKey( __CLASS__, $code );
206 
207  return $this->srvCache->get( $cacheKey );
208  }
209 
216  protected function saveToLocalCache( $code, $cache ) {
217  $cacheKey = $this->srvCache->makeKey( __CLASS__, $code );
218  $this->srvCache->set( $cacheKey, $cache );
219  }
220 
242  protected function load( $code, $mode = null ) {
243  if ( !is_string( $code ) ) {
244  throw new InvalidArgumentException( "Missing language code" );
245  }
246 
247  # Don't do double loading...
248  if ( $this->isLanguageLoaded( $code ) && $mode != self::FOR_UPDATE ) {
249  return true;
250  }
251 
252  $this->overridable = array_flip( Language::getMessageKeysFor( $code ) );
253 
254  # 8 lines of code just to say (once) that message cache is disabled
255  if ( $this->mDisable ) {
256  static $shownDisabled = false;
257  if ( !$shownDisabled ) {
258  $this->logger->debug( __METHOD__ . ': disabled' );
259  $shownDisabled = true;
260  }
261 
262  return true;
263  }
264 
265  # Loading code starts
266  $success = false; # Keep track of success
267  $staleCache = false; # a cache array with expired data, or false if none has been loaded
268  $where = []; # Debug info, delayed to avoid spamming debug log too much
269 
270  # Hash of the contents is stored in memcache, to detect if data-center cache
271  # or local cache goes out of date (e.g. due to replace() on some other server)
272  list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
273  $this->cacheVolatile[$code] = $hashVolatile;
274 
275  # Try the local cache and check against the cluster hash key...
276  $cache = $this->getLocalCache( $code );
277  if ( !$cache ) {
278  $where[] = 'local cache is empty';
279  } elseif ( !isset( $cache['HASH'] ) || $cache['HASH'] !== $hash ) {
280  $where[] = 'local cache has the wrong hash';
281  $staleCache = $cache;
282  } elseif ( $this->isCacheExpired( $cache ) ) {
283  $where[] = 'local cache is expired';
284  $staleCache = $cache;
285  } elseif ( $hashVolatile ) {
286  $where[] = 'local cache validation key is expired/volatile';
287  $staleCache = $cache;
288  } else {
289  $where[] = 'got from local cache';
290  $this->cache->set( $code, $cache );
291  $success = true;
292  }
293 
294  if ( !$success ) {
295  $cacheKey = $this->clusterCache->makeKey( 'messages', $code );
296  # Try the global cache. If it is empty, try to acquire a lock. If
297  # the lock can't be acquired, wait for the other thread to finish
298  # and then try the global cache a second time.
299  for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++ ) {
300  if ( $hashVolatile && $staleCache ) {
301  # Do not bother fetching the whole cache blob to avoid I/O.
302  # Instead, just try to get the non-blocking $statusKey lock
303  # below, and use the local stale value if it was not acquired.
304  $where[] = 'global cache is presumed expired';
305  } else {
306  $cache = $this->clusterCache->get( $cacheKey );
307  if ( !$cache ) {
308  $where[] = 'global cache is empty';
309  } elseif ( $this->isCacheExpired( $cache ) ) {
310  $where[] = 'global cache is expired';
311  $staleCache = $cache;
312  } elseif ( $hashVolatile ) {
313  # DB results are replica DB lag prone until the holdoff TTL passes.
314  # By then, updates should be reflected in loadFromDBWithLock().
315  # One thread regenerates the cache while others use old values.
316  $where[] = 'global cache is expired/volatile';
317  $staleCache = $cache;
318  } else {
319  $where[] = 'got from global cache';
320  $this->cache->set( $code, $cache );
321  $this->saveToCaches( $cache, 'local-only', $code );
322  $success = true;
323  }
324  }
325 
326  if ( $success ) {
327  # Done, no need to retry
328  break;
329  }
330 
331  # We need to call loadFromDB. Limit the concurrency to one process.
332  # This prevents the site from going down when the cache expires.
333  # Note that the DB slam protection lock here is non-blocking.
334  $loadStatus = $this->loadFromDBWithLock( $code, $where, $mode );
335  if ( $loadStatus === true ) {
336  $success = true;
337  break;
338  } elseif ( $staleCache ) {
339  # Use the stale cache while some other thread constructs the new one
340  $where[] = 'using stale cache';
341  $this->cache->set( $code, $staleCache );
342  $success = true;
343  break;
344  } elseif ( $failedAttempts > 0 ) {
345  # Already blocked once, so avoid another lock/unlock cycle.
346  # This case will typically be hit if memcached is down, or if
347  # loadFromDB() takes longer than LOCK_WAIT.
348  $where[] = "could not acquire status key.";
349  break;
350  } elseif ( $loadStatus === 'cantacquire' ) {
351  # Wait for the other thread to finish, then retry. Normally,
352  # the memcached get() will then yield the other thread's result.
353  $where[] = 'waited for other thread to complete';
354  $this->getReentrantScopedLock( $cacheKey );
355  } else {
356  # Disable cache; $loadStatus is 'disabled'
357  break;
358  }
359  }
360  }
361 
362  if ( !$success ) {
363  $where[] = 'loading FAILED - cache is disabled';
364  $this->mDisable = true;
365  $this->cache->set( $code, [] );
366  $this->logger->error( __METHOD__ . ": Failed to load $code" );
367  # This used to throw an exception, but that led to nasty side effects like
368  # the whole wiki being instantly down if the memcached server died
369  }
370 
371  if ( !$this->isLanguageLoaded( $code ) ) { // sanity
372  throw new LogicException( "Process cache for '$code' should be set by now." );
373  }
374 
375  $info = implode( ', ', $where );
376  $this->logger->debug( __METHOD__ . ": Loading $code... $info" );
377 
378  return $success;
379  }
380 
387  protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
388  # If cache updates on all levels fail, give up on message overrides.
389  # This is to avoid easy site outages; see $saveSuccess comments below.
390  $statusKey = $this->clusterCache->makeKey( 'messages', $code, 'status' );
391  $status = $this->clusterCache->get( $statusKey );
392  if ( $status === 'error' ) {
393  $where[] = "could not load; method is still globally disabled";
394  return 'disabled';
395  }
396 
397  # Now let's regenerate
398  $where[] = 'loading from database';
399 
400  # Lock the cache to prevent conflicting writes.
401  # This lock is non-blocking so stale cache can quickly be used.
402  # Note that load() will call a blocking getReentrantScopedLock()
403  # after this if it really need to wait for any current thread.
404  $cacheKey = $this->clusterCache->makeKey( 'messages', $code );
405  $scopedLock = $this->getReentrantScopedLock( $cacheKey, 0 );
406  if ( !$scopedLock ) {
407  $where[] = 'could not acquire main lock';
408  return 'cantacquire';
409  }
410 
411  $cache = $this->loadFromDB( $code, $mode );
412  $this->cache->set( $code, $cache );
413  $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
414 
415  if ( !$saveSuccess ) {
429  if ( $this->srvCache instanceof EmptyBagOStuff ) {
430  $this->clusterCache->set( $statusKey, 'error', 60 * 5 );
431  $where[] = 'could not save cache, disabled globally for 5 minutes';
432  } else {
433  $where[] = "could not save global cache";
434  }
435  }
436 
437  return true;
438  }
439 
449  protected function loadFromDB( $code, $mode = null ) {
451 
452  // (T164666) The query here performs really poorly on WMF's
453  // contributions replicas. We don't have a way to say "any group except
454  // contributions", so for the moment let's specify 'api'.
455  // @todo: Get rid of this hack.
456  $dbr = wfGetDB( ( $mode == self::FOR_UPDATE ) ? DB_MASTER : DB_REPLICA, 'api' );
457 
458  $cache = [];
459 
460  $mostused = []; // list of "<cased message key>/<code>"
461  if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
462  if ( !$this->cache->has( $wgLanguageCode ) ) {
463  $this->load( $wgLanguageCode );
464  }
465  $mostused = array_keys( $this->cache->get( $wgLanguageCode ) );
466  foreach ( $mostused as $key => $value ) {
467  $mostused[$key] = "$value/$code";
468  }
469  }
470 
471  // Get the list of software-defined messages in core/extensions
472  $overridable = array_flip( Language::getMessageKeysFor( $wgLanguageCode ) );
473 
474  // Common conditions
475  $conds = [
476  'page_is_redirect' => 0,
477  'page_namespace' => NS_MEDIAWIKI,
478  ];
479  if ( count( $mostused ) ) {
480  $conds['page_title'] = $mostused;
481  } elseif ( $code !== $wgLanguageCode ) {
482  $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
483  } else {
484  # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
485  # other than language code.
486  $conds[] = 'page_title NOT' .
487  $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
488  }
489 
490  // Set the stubs for oversized software-defined messages in the main cache map
491  $res = $dbr->select(
492  'page',
493  [ 'page_title', 'page_latest' ],
494  array_merge( $conds, [ 'page_len > ' . intval( $wgMaxMsgCacheEntrySize ) ] ),
495  __METHOD__ . "($code)-big"
496  );
497  foreach ( $res as $row ) {
498  // Include entries/stubs for all keys in $mostused in adaptive mode
499  if ( $wgAdaptiveMessageCache || $this->isMainCacheable( $row->page_title, $overridable ) ) {
500  $cache[$row->page_title] = '!TOO BIG';
501  }
502  // At least include revision ID so page changes are reflected in the hash
503  $cache['EXCESSIVE'][$row->page_title] = $row->page_latest;
504  }
505 
506  // Set the text for small software-defined messages in the main cache map
507  $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
508  $revQuery = $revisionStore->getQueryInfo( [ 'page', 'user' ] );
509 
510  // T231196: MySQL/MariaDB (10.1.37) can sometimes irrationally decide that querying `actor` then
511  // `revision` then `page` is somehow better than starting with `page`. Tell it not to reorder the
512  // query (and also reorder it ourselves because as generated by RevisionStore it'll have
513  // `revision` first rather than `page`).
514  $revQuery['joins']['revision'] = $revQuery['joins']['page'];
515  unset( $revQuery['joins']['page'] );
516  // It isn't actually necesssary to reorder $revQuery['tables'] as Database does the right thing
517  // when join conditions are given for all joins, but GergÅ‘ is wary of relying on that so pull
518  // `page` to the start.
519  $revQuery['tables'] = array_merge(
520  [ 'page' ],
521  array_diff( $revQuery['tables'], [ 'page' ] )
522  );
523 
524  $res = $dbr->select(
525  $revQuery['tables'],
526  $revQuery['fields'],
527  array_merge( $conds, [
528  'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ),
529  'page_latest = rev_id' // get the latest revision only
530  ] ),
531  __METHOD__ . "($code)-small",
532  [ 'STRAIGHT_JOIN' ],
533  $revQuery['joins']
534  );
535  foreach ( $res as $row ) {
536  // Include entries/stubs for all keys in $mostused in adaptive mode
537  if ( $wgAdaptiveMessageCache || $this->isMainCacheable( $row->page_title, $overridable ) ) {
538  try {
539  $rev = $revisionStore->newRevisionFromRow( $row );
540  $content = $rev->getContent( MediaWiki\Revision\SlotRecord::MAIN );
541  $text = $this->getMessageTextFromContent( $content );
542  } catch ( Exception $ex ) {
543  $text = false;
544  }
545 
546  if ( !is_string( $text ) ) {
547  $entry = '!ERROR';
548  $this->logger->error(
549  __METHOD__
550  . ": failed to load message page text for {$row->page_title} ($code)"
551  );
552  } else {
553  $entry = ' ' . $text;
554  }
555  $cache[$row->page_title] = $entry;
556  } else {
557  // T193271: cache object gets too big and slow to generate.
558  // At least include revision ID so page changes are reflected in the hash.
559  $cache['EXCESSIVE'][$row->page_title] = $row->page_latest;
560  }
561  }
562 
563  $cache['VERSION'] = MSG_CACHE_VERSION;
564  ksort( $cache );
565 
566  # Hash for validating local cache (APC). No need to take into account
567  # messages larger than $wgMaxMsgCacheEntrySize, since those are only
568  # stored and fetched from memcache.
569  $cache['HASH'] = md5( serialize( $cache ) );
570  $cache['EXPIRY'] = wfTimestamp( TS_MW, time() + self::WAN_TTL );
571  unset( $cache['EXCESSIVE'] ); // only needed for hash
572 
573  return $cache;
574  }
575 
582  private function isLanguageLoaded( $lang ) {
583  // It is important that this only returns true if the cache was fully
584  // populated by load(), so that callers can assume all cache keys exist.
585  // It is possible for $this->cache to be only patially populated through
586  // methods like MessageCache::replace(), which must not make this method
587  // return true (T208897). And this method must cease to return true
588  // if the language was evicted by MapCacheLRU (T230690).
589  return $this->cache->hasField( $lang, 'VERSION' );
590  }
591 
597  private function isMainCacheable( $name, array $overridable ) {
598  // Convert first letter to lowercase, and strip /code suffix
599  $name = $this->contLang->lcfirst( $name );
600  $msg = preg_replace( '/\/[a-z0-9-]{2,}$/', '', $name );
601  // Include common conversion table pages. This also avoids problems with
602  // Installer::parse() bailing out due to disallowed DB queries (T207979).
603  return ( isset( $overridable[$msg] ) || strpos( $name, 'conversiontable/' ) === 0 );
604  }
605 
612  public function replace( $title, $text ) {
613  global $wgLanguageCode;
614 
615  if ( $this->mDisable ) {
616  return;
617  }
618 
619  list( $msg, $code ) = $this->figureMessage( $title );
620  if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
621  // Content language overrides do not use the /<code> suffix
622  return;
623  }
624 
625  // (a) Update the process cache with the new message text
626  if ( $text === false ) {
627  // Page deleted
628  $this->cache->setField( $code, $title, '!NONEXISTENT' );
629  } else {
630  // Ignore $wgMaxMsgCacheEntrySize so the process cache is up to date
631  $this->cache->setField( $code, $title, ' ' . $text );
632  }
633 
634  // (b) Update the shared caches in a deferred update with a fresh DB snapshot
636  new MessageCacheUpdate( $code, $title, $msg ),
638  );
639  }
640 
646  public function refreshAndReplaceInternal( $code, array $replacements ) {
648 
649  // Allow one caller at a time to avoid race conditions
650  $scopedLock = $this->getReentrantScopedLock(
651  $this->clusterCache->makeKey( 'messages', $code )
652  );
653  if ( !$scopedLock ) {
654  foreach ( $replacements as list( $title ) ) {
655  $this->logger->error(
656  __METHOD__ . ': could not acquire lock to update {title} ({code})',
657  [ 'title' => $title, 'code' => $code ] );
658  }
659 
660  return;
661  }
662 
663  // Load the existing cache to update it in the local DC cache.
664  // The other DCs will see a hash mismatch.
665  if ( $this->load( $code, self::FOR_UPDATE ) ) {
666  $cache = $this->cache->get( $code );
667  } else {
668  // Err? Fall back to loading from the database.
669  $cache = $this->loadFromDB( $code, self::FOR_UPDATE );
670  }
671  // Check if individual cache keys should exist and update cache accordingly
672  $newTextByTitle = []; // map of (title => content)
673  $newBigTitles = []; // map of (title => latest revision ID), like EXCESSIVE in loadFromDB()
674  foreach ( $replacements as list( $title ) ) {
676  $page->loadPageData( $page::READ_LATEST );
677  $text = $this->getMessageTextFromContent( $page->getContent() );
678  // Remember the text for the blob store update later on
679  $newTextByTitle[$title] = $text;
680  // Note that if $text is false, then $cache should have a !NONEXISTANT entry
681  if ( !is_string( $text ) ) {
682  $cache[$title] = '!NONEXISTENT';
683  } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
684  $cache[$title] = '!TOO BIG';
685  $newBigTitles[$title] = $page->getLatest();
686  } else {
687  $cache[$title] = ' ' . $text;
688  }
689  }
690  // Update HASH for the new key. Incorporates various administrative keys,
691  // including the old HASH (and thereby the EXCESSIVE value from loadFromDB()
692  // and previous replace() calls), but that doesn't really matter since we
693  // only ever compare it for equality with a copy saved by saveToCaches().
694  $cache['HASH'] = md5( serialize( $cache + [ 'EXCESSIVE' => $newBigTitles ] ) );
695  // Update the too-big WAN cache entries now that we have the new HASH
696  foreach ( $newBigTitles as $title => $id ) {
697  // Match logic of loadCachedMessagePageEntry()
698  $this->wanCache->set(
699  $this->bigMessageCacheKey( $cache['HASH'], $title ),
700  ' ' . $newTextByTitle[$title],
701  self::WAN_TTL
702  );
703  }
704  // Mark this cache as definitely being "latest" (non-volatile) so
705  // load() calls do not try to refresh the cache with replica DB data
706  $cache['LATEST'] = time();
707  // Update the process cache
708  $this->cache->set( $code, $cache );
709  // Pre-emptively update the local datacenter cache so things like edit filter and
710  // blacklist changes are reflected immediately; these often use MediaWiki: pages.
711  // The datacenter handling replace() calls should be the same one handling edits
712  // as they require HTTP POST.
713  $this->saveToCaches( $cache, 'all', $code );
714  // Release the lock now that the cache is saved
715  ScopedCallback::consume( $scopedLock );
716 
717  // Relay the purge. Touching this check key expires cache contents
718  // and local cache (APC) validation hash across all datacenters.
719  $this->wanCache->touchCheckKey( $this->getCheckKey( $code ) );
720 
721  // Purge the messages in the message blob store and fire any hook handlers
722  $blobStore = MediaWikiServices::getInstance()->getResourceLoader()->getMessageBlobStore();
723  foreach ( $replacements as list( $title, $msg ) ) {
724  $blobStore->updateMessage( $this->contLang->lcfirst( $msg ) );
725  Hooks::run( 'MessageCacheReplace', [ $title, $newTextByTitle[$title] ] );
726  }
727  }
728 
735  protected function isCacheExpired( $cache ) {
736  if ( !isset( $cache['VERSION'] ) || !isset( $cache['EXPIRY'] ) ) {
737  return true;
738  }
739  if ( $cache['VERSION'] != MSG_CACHE_VERSION ) {
740  return true;
741  }
742  if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
743  return true;
744  }
745 
746  return false;
747  }
748 
758  protected function saveToCaches( array $cache, $dest, $code = false ) {
759  if ( $dest === 'all' ) {
760  $cacheKey = $this->clusterCache->makeKey( 'messages', $code );
761  $success = $this->clusterCache->set( $cacheKey, $cache );
762  $this->setValidationHash( $code, $cache );
763  } else {
764  $success = true;
765  }
766 
767  $this->saveToLocalCache( $code, $cache );
768 
769  return $success;
770  }
771 
778  protected function getValidationHash( $code ) {
779  $curTTL = null;
780  $value = $this->wanCache->get(
781  $this->wanCache->makeKey( 'messages', $code, 'hash', 'v1' ),
782  $curTTL,
783  [ $this->getCheckKey( $code ) ]
784  );
785 
786  if ( $value ) {
787  $hash = $value['hash'];
788  if ( ( time() - $value['latest'] ) < WANObjectCache::TTL_MINUTE ) {
789  // Cache was recently updated via replace() and should be up-to-date.
790  // That method is only called in the primary datacenter and uses FOR_UPDATE.
791  // Also, it is unlikely that the current datacenter is *now* secondary one.
792  $expired = false;
793  } else {
794  // See if the "check" key was bumped after the hash was generated
795  $expired = ( $curTTL < 0 );
796  }
797  } else {
798  // No hash found at all; cache must regenerate to be safe
799  $hash = false;
800  $expired = true;
801  }
802 
803  return [ $hash, $expired ];
804  }
805 
816  protected function setValidationHash( $code, array $cache ) {
817  $this->wanCache->set(
818  $this->wanCache->makeKey( 'messages', $code, 'hash', 'v1' ),
819  [
820  'hash' => $cache['HASH'],
821  'latest' => $cache['LATEST'] ?? 0
822  ],
824  );
825  }
826 
832  protected function getReentrantScopedLock( $key, $timeout = self::WAIT_SEC ) {
833  return $this->clusterCache->getScopedLock( $key, $timeout, self::LOCK_TTL, __METHOD__ );
834  }
835 
869  function get( $key, $useDB = true, $langcode = true ) {
870  if ( is_int( $key ) ) {
871  // Fix numerical strings that somehow become ints
872  // on their way here
873  $key = (string)$key;
874  } elseif ( !is_string( $key ) ) {
875  throw new MWException( 'Non-string key given' );
876  } elseif ( $key === '' ) {
877  // Shortcut: the empty key is always missing
878  return false;
879  }
880 
881  // Normalise title-case input (with some inlining)
882  $lckey = self::normalizeKey( $key );
883 
884  Hooks::run( 'MessageCache::get', [ &$lckey ] );
885 
886  // Loop through each language in the fallback list until we find something useful
887  $message = $this->getMessageFromFallbackChain(
888  wfGetLangObj( $langcode ),
889  $lckey,
890  !$this->mDisable && $useDB
891  );
892 
893  // If we still have no message, maybe the key was in fact a full key so try that
894  if ( $message === false ) {
895  $parts = explode( '/', $lckey );
896  // We may get calls for things that are http-urls from sidebar
897  // Let's not load nonexistent languages for those
898  // They usually have more than one slash.
899  if ( count( $parts ) == 2 && $parts[1] !== '' ) {
900  $message = Language::getMessageFor( $parts[0], $parts[1] );
901  if ( $message === null ) {
902  $message = false;
903  }
904  }
905  }
906 
907  // Post-processing if the message exists
908  if ( $message !== false ) {
909  // Fix whitespace
910  $message = str_replace(
911  [
912  # Fix for trailing whitespace, removed by textarea
913  '&#32;',
914  # Fix for NBSP, converted to space by firefox
915  '&nbsp;',
916  '&#160;',
917  '&shy;'
918  ],
919  [
920  ' ',
921  "\u{00A0}",
922  "\u{00A0}",
923  "\u{00AD}"
924  ],
925  $message
926  );
927  }
928 
929  return $message;
930  }
931 
944  protected function getMessageFromFallbackChain( $lang, $lckey, $useDB ) {
945  $alreadyTried = [];
946 
947  // First try the requested language.
948  $message = $this->getMessageForLang( $lang, $lckey, $useDB, $alreadyTried );
949  if ( $message !== false ) {
950  return $message;
951  }
952 
953  // Now try checking the site language.
954  $message = $this->getMessageForLang( $this->contLang, $lckey, $useDB, $alreadyTried );
955  return $message;
956  }
957 
968  private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
969  $langcode = $lang->getCode();
970 
971  // Try checking the database for the requested language
972  if ( $useDB ) {
973  $uckey = $this->contLang->ucfirst( $lckey );
974 
975  if ( !isset( $alreadyTried[$langcode] ) ) {
976  $message = $this->getMsgFromNamespace(
977  $this->getMessagePageName( $langcode, $uckey ),
978  $langcode
979  );
980  if ( $message !== false ) {
981  return $message;
982  }
983  $alreadyTried[$langcode] = true;
984  }
985  } else {
986  $uckey = null;
987  }
988 
989  // Check the CDB cache
990  $message = $lang->getMessage( $lckey );
991  if ( $message !== null ) {
992  return $message;
993  }
994 
995  // Try checking the database for all of the fallback languages
996  if ( $useDB ) {
997  $fallbackChain = Language::getFallbacksFor( $langcode );
998 
999  foreach ( $fallbackChain as $code ) {
1000  if ( isset( $alreadyTried[$code] ) ) {
1001  continue;
1002  }
1003 
1004  $message = $this->getMsgFromNamespace(
1005  $this->getMessagePageName( $code, $uckey ), $code );
1006 
1007  if ( $message !== false ) {
1008  return $message;
1009  }
1010  $alreadyTried[$code] = true;
1011  }
1012  }
1013 
1014  return false;
1015  }
1016 
1024  private function getMessagePageName( $langcode, $uckey ) {
1025  global $wgLanguageCode;
1026 
1027  if ( $langcode === $wgLanguageCode ) {
1028  // Messages created in the content language will not have the /lang extension
1029  return $uckey;
1030  } else {
1031  return "$uckey/$langcode";
1032  }
1033  }
1034 
1047  public function getMsgFromNamespace( $title, $code ) {
1048  // Load all MediaWiki page definitions into cache. Note that individual keys
1049  // already loaded into cache during this request remain in the cache, which
1050  // includes the value of hook-defined messages.
1051  $this->load( $code );
1052 
1053  $entry = $this->cache->getField( $code, $title );
1054 
1055  if ( $entry !== null ) {
1056  // Message page exists as an override of a software messages
1057  if ( substr( $entry, 0, 1 ) === ' ' ) {
1058  // The message exists and is not '!TOO BIG' or '!ERROR'
1059  return (string)substr( $entry, 1 );
1060  } elseif ( $entry === '!NONEXISTENT' ) {
1061  // The text might be '-' or missing due to some data loss
1062  return false;
1063  }
1064  // Load the message page, utilizing the individual message cache.
1065  // If the page does not exist, there will be no hook handler fallbacks.
1066  $entry = $this->loadCachedMessagePageEntry(
1067  $title,
1068  $code,
1069  $this->cache->getField( $code, 'HASH' )
1070  );
1071  } else {
1072  // Message page either does not exist or does not override a software message
1073  if ( !$this->isMainCacheable( $title, $this->overridable ) ) {
1074  // Message page does not override any software-defined message. A custom
1075  // message might be defined to have content or settings specific to the wiki.
1076  // Load the message page, utilizing the individual message cache as needed.
1077  $entry = $this->loadCachedMessagePageEntry(
1078  $title,
1079  $code,
1080  $this->cache->getField( $code, 'HASH' )
1081  );
1082  }
1083  if ( $entry === null || substr( $entry, 0, 1 ) !== ' ' ) {
1084  // Message does not have a MediaWiki page definition; try hook handlers
1085  $message = false;
1086  Hooks::run( 'MessagesPreLoad', [ $title, &$message, $code ] );
1087  if ( $message !== false ) {
1088  $this->cache->setField( $code, $title, ' ' . $message );
1089  } else {
1090  $this->cache->setField( $code, $title, '!NONEXISTENT' );
1091  }
1092 
1093  return $message;
1094  }
1095  }
1096 
1097  if ( $entry !== false && substr( $entry, 0, 1 ) === ' ' ) {
1098  if ( $this->cacheVolatile[$code] ) {
1099  // Make sure that individual keys respect the WAN cache holdoff period too
1100  $this->logger->debug(
1101  __METHOD__ . ': loading volatile key \'{titleKey}\'',
1102  [ 'titleKey' => $title, 'code' => $code ] );
1103  } else {
1104  $this->cache->setField( $code, $title, $entry );
1105  }
1106  // The message exists, so make sure a string is returned
1107  return (string)substr( $entry, 1 );
1108  }
1109 
1110  $this->cache->setField( $code, $title, '!NONEXISTENT' );
1111 
1112  return false;
1113  }
1114 
1121  private function loadCachedMessagePageEntry( $dbKey, $code, $hash ) {
1122  $fname = __METHOD__;
1123  return $this->srvCache->getWithSetCallback(
1124  $this->srvCache->makeKey( 'messages-big', $hash, $dbKey ),
1126  function () use ( $code, $dbKey, $hash, $fname ) {
1127  return $this->wanCache->getWithSetCallback(
1128  $this->bigMessageCacheKey( $hash, $dbKey ),
1129  self::WAN_TTL,
1130  function ( $oldValue, &$ttl, &$setOpts ) use ( $dbKey, $code, $fname ) {
1131  // Try loading the message from the database
1132  $dbr = wfGetDB( DB_REPLICA );
1133  $setOpts += Database::getCacheSetOptions( $dbr );
1134  // Use newKnownCurrent() to avoid querying revision/user tables
1135  $title = Title::makeTitle( NS_MEDIAWIKI, $dbKey );
1136  $revision = Revision::newKnownCurrent( $dbr, $title );
1137  if ( !$revision ) {
1138  // The wiki doesn't have a local override page. Cache absence with normal TTL.
1139  // When overrides are created, self::replace() takes care of the cache.
1140  return '!NONEXISTENT';
1141  }
1142  $content = $revision->getContent();
1143  if ( $content ) {
1144  $message = $this->getMessageTextFromContent( $content );
1145  } else {
1146  $this->logger->warning(
1147  $fname . ': failed to load page text for \'{titleKey}\'',
1148  [ 'titleKey' => $dbKey, 'code' => $code ]
1149  );
1150  $message = null;
1151  }
1152 
1153  if ( !is_string( $message ) ) {
1154  // Revision failed to load Content, or Content is incompatible with wikitext.
1155  // Possibly a temporary loading failure.
1156  $ttl = 5;
1157 
1158  return '!NONEXISTENT';
1159  }
1160 
1161  return ' ' . $message;
1162  }
1163  );
1164  }
1165  );
1166  }
1167 
1175  public function transform( $message, $interface = false, $language = null, $title = null ) {
1176  // Avoid creating parser if nothing to transform
1177  if ( strpos( $message, '{{' ) === false ) {
1178  return $message;
1179  }
1180 
1181  if ( $this->mInParser ) {
1182  return $message;
1183  }
1184 
1185  $parser = $this->getParser();
1186  if ( $parser ) {
1187  $popts = $this->getParserOptions();
1188  $popts->setInterfaceMessage( $interface );
1189  $popts->setTargetLanguage( $language );
1190 
1191  $userlang = $popts->setUserLang( $language );
1192  $this->mInParser = true;
1193  $message = $parser->transformMsg( $message, $popts, $title );
1194  $this->mInParser = false;
1195  $popts->setUserLang( $userlang );
1196  }
1197 
1198  return $message;
1199  }
1200 
1204  public function getParser() {
1205  global $wgParserConf;
1206  if ( !$this->mParser ) {
1207  $parser = MediaWikiServices::getInstance()->getParser();
1208  # Do some initialisation so that we don't have to do it twice
1209  $parser->firstCallInit();
1210  # Clone it and store it
1211  $class = $wgParserConf['class'];
1212  if ( $class == ParserDiffTest::class ) {
1213  # Uncloneable
1214  // @phan-suppress-next-line PhanTypeMismatchProperty
1215  $this->mParser = new $class( $wgParserConf );
1216  } else {
1217  $this->mParser = clone $parser;
1218  }
1219  }
1220 
1221  return $this->mParser;
1222  }
1223 
1232  public function parse( $text, $title = null, $linestart = true,
1233  $interface = false, $language = null
1234  ) {
1235  global $wgTitle;
1236 
1237  if ( $this->mInParser ) {
1238  return htmlspecialchars( $text );
1239  }
1240 
1241  $parser = $this->getParser();
1242  $popts = $this->getParserOptions();
1243  $popts->setInterfaceMessage( $interface );
1244 
1245  if ( is_string( $language ) ) {
1246  $language = Language::factory( $language );
1247  }
1248  $popts->setTargetLanguage( $language );
1249 
1250  if ( !$title || !$title instanceof Title ) {
1251  wfDebugLog( 'GlobalTitleFail', __METHOD__ . ' called by ' .
1252  wfGetAllCallers( 6 ) . ' with no title set.' );
1253  $title = $wgTitle;
1254  }
1255  // Sometimes $wgTitle isn't set either...
1256  if ( !$title ) {
1257  # It's not uncommon having a null $wgTitle in scripts. See r80898
1258  # Create a ghost title in such case
1259  $title = Title::makeTitle( NS_SPECIAL, 'Badtitle/title not set in ' . __METHOD__ );
1260  }
1261 
1262  $this->mInParser = true;
1263  $res = $parser->parse( $text, $title, $popts, $linestart );
1264  $this->mInParser = false;
1265 
1266  return $res;
1267  }
1268 
1269  public function disable() {
1270  $this->mDisable = true;
1271  }
1272 
1273  public function enable() {
1274  $this->mDisable = false;
1275  }
1276 
1289  public function isDisabled() {
1290  return $this->mDisable;
1291  }
1292 
1298  public function clear() {
1299  $langs = Language::fetchLanguageNames( null, 'mw' );
1300  foreach ( array_keys( $langs ) as $code ) {
1301  $this->wanCache->touchCheckKey( $this->getCheckKey( $code ) );
1302  }
1303  $this->cache->clear();
1304  }
1305 
1310  public function figureMessage( $key ) {
1311  global $wgLanguageCode;
1312 
1313  $pieces = explode( '/', $key );
1314  if ( count( $pieces ) < 2 ) {
1315  return [ $key, $wgLanguageCode ];
1316  }
1317 
1318  $lang = array_pop( $pieces );
1319  if ( !Language::fetchLanguageName( $lang, null, 'mw' ) ) {
1320  return [ $key, $wgLanguageCode ];
1321  }
1322 
1323  $message = implode( '/', $pieces );
1324 
1325  return [ $message, $lang ];
1326  }
1327 
1336  public function getAllMessageKeys( $code ) {
1337  $this->load( $code );
1338  if ( !$this->cache->has( $code ) ) {
1339  // Apparently load() failed
1340  return null;
1341  }
1342  // Remove administrative keys
1343  $cache = $this->cache->get( $code );
1344  unset( $cache['VERSION'] );
1345  unset( $cache['EXPIRY'] );
1346  unset( $cache['EXCESSIVE'] );
1347  // Remove any !NONEXISTENT keys
1348  $cache = array_diff( $cache, [ '!NONEXISTENT' ] );
1349 
1350  // Keys may appear with a capital first letter. lcfirst them.
1351  return array_map( [ $this->contLang, 'lcfirst' ], array_keys( $cache ) );
1352  }
1353 
1361  public function updateMessageOverride( Title $title, Content $content = null ) {
1362  $msgText = $this->getMessageTextFromContent( $content );
1363  if ( $msgText === null ) {
1364  $msgText = false; // treat as not existing
1365  }
1366 
1367  $this->replace( $title->getDBkey(), $msgText );
1368 
1369  if ( $this->contLang->hasVariants() ) {
1370  $this->contLang->updateConversionTable( $title );
1371  }
1372  }
1373 
1378  public function getCheckKey( $code ) {
1379  return $this->wanCache->makeKey( 'messages', $code );
1380  }
1381 
1386  private function getMessageTextFromContent( Content $content = null ) {
1387  // @TODO: could skip pseudo-messages like js/css here, based on content model
1388  if ( $content ) {
1389  // Message page exists...
1390  // XXX: Is this the right way to turn a Content object into a message?
1391  // NOTE: $content is typically either WikitextContent, JavaScriptContent or
1392  // CssContent. MessageContent is *not* used for storing messages, it's
1393  // only used for wrapping them when needed.
1394  $msgText = $content->getWikitextForTransclusion();
1395  if ( $msgText === false || $msgText === null ) {
1396  // This might be due to some kind of misconfiguration...
1397  $msgText = null;
1398  $this->logger->warning(
1399  __METHOD__ . ": message content doesn't provide wikitext "
1400  . "(content model: " . $content->getModel() . ")" );
1401  }
1402  } else {
1403  // Message page does not exist...
1404  $msgText = false;
1405  }
1406 
1407  return $msgText;
1408  }
1409 
1415  private function bigMessageCacheKey( $hash, $title ) {
1416  return $this->wanCache->makeKey( 'messages-big', $hash, $title );
1417  }
1418 }
load( $code, $mode=null)
Loads messages from caches or from database in this order: (1) local message cache (if $wgUseLocalMes...
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:142
saveToCaches(array $cache, $dest, $code=false)
Shortcut to update caches.
loadFromDBWithLock( $code, array &$where, $mode=null)
const MSG_CACHE_VERSION
MediaWiki message cache structure version.
static fetchLanguageNames( $inLanguage=self::AS_AUTONYMS, $include='mw')
Get an array of language names, indexed by code.
Definition: Language.php:814
get( $key, $maxAge=INF, $default=null)
Get the value for a key.
figureMessage( $key)
$success
serialize()
transform( $message, $interface=false, $language=null, $title=null)
setValidationHash( $code, array $cache)
Set the md5 used to validate the local disk cache.
if(!isset( $args[0])) $lang
getAllMessageKeys( $code)
Get all message keys stored in the message cache for a given language.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
bigMessageCacheKey( $hash, $title)
saveToLocalCache( $code, $cache)
Save the cache to APC.
getMessageTextFromContent(Content $content=null)
const NS_SPECIAL
Definition: Defines.php:49
isMainCacheable( $name, array $overridable)
BagOStuff $clusterCache
Message cache purging and in-place update handler for specific message page changes.
A helper class for throttling authentication attempts.
loadFromDB( $code, $mode=null)
Loads cacheable messages from the database.
Parser $mParser
const DB_MASTER
Definition: defines.php:26
static normalizeKey( $key)
Normalize message key input.
wfGetLangObj( $langcode=false)
Return a Language object from $langcode.
getParserOptions()
ParserOptions is lazy initialised.
refreshAndReplaceInternal( $code, array $replacements)
getMsgFromNamespace( $title, $code)
Get a message from the MediaWiki namespace, with caching.
wfGetAllCallers( $limit=3)
Return a string consisting of callers in the stack.
$wgLanguageCode
Site language code.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
static getMessageFor( $key, $code)
Get a message for a given language.
Definition: Language.php:4426
MapCacheLRU $cache
Process cache of loaded messages that are defined in MediaWiki namespace.
$wgMaxMsgCacheEntrySize
Maximum entry size in the message cache, in bytes.
loadCachedMessagePageEntry( $dbKey, $code, $hash)
set( $key, $value, $rank=self::RANK_TOP)
Set a key/value pair.
replace( $title, $text)
Updates cache as necessary when message page is changed.
static getMessageKeysFor( $code)
Get all message keys for a given language.
Definition: Language.php:4438
getDBkey()
Get the main part with underscores.
Definition: Title.php:1016
static newKnownCurrent(IDatabase $db, $pageIdOrTitle, $revId=0)
Load a revision based on a known page ID and current revision ID from the DB.
Definition: Revision.php:1123
getValidationHash( $code)
Get the md5 used to validate the local APC cache.
LoggerInterface $logger
$wgAdaptiveMessageCache
Instead of caching everything, only cache those messages which have been customised in the site conte...
array $overridable
Map of (lowercase message key => index) for all software defined messages.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
getLocalCache( $code)
Try to load the cache from APC.
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:212
bool [] $cacheVolatile
Map of (language code => boolean)
$wgParserConf
Parser configuration.
const NS_MEDIAWIKI
Definition: Defines.php:68
static newFromAnon()
Get a ParserOptions object for an anonymous user.
getReentrantScopedLock( $key, $timeout=self::WAIT_SEC)
const WAIT_SEC
How long to wait for memcached locks.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
static fetchLanguageName( $code, $inLanguage=self::AS_AUTONYMS, $include=self::ALL)
Definition: Language.php:828
getMessagePageName( $langcode, $uckey)
Get the message page name for a given language.
getMessageFromFallbackChain( $lang, $lckey, $useDB)
Given a language, try and fetch messages from that language.
const FOR_UPDATE
__construct(WANObjectCache $wanCache, BagOStuff $clusterCache, BagOStuff $serverCache, Language $contLang, LoggerInterface $logger, array $options)
isDisabled()
Whether DB/cache usage is disabled for determining messages.
updateMessageOverride(Title $title, Content $content=null)
Purge message caches when a MediaWiki: page is created, updated, or deleted.
parse( $text, $title=null, $linestart=true, $interface=false, $language=null)
ParserOptions $mParserOptions
Message cache has its own parser which it uses to transform messages.
static addUpdate(DeferrableUpdate $update, $stage=self::POSTSEND)
Add an update to the deferred list to be run later by execute()
BagOStuff $srvCache
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not...
getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried)
Given a language, try and fetch messages from that language and its fallbacks.
static getFallbacksFor( $code, $mode=self::MESSAGES_FALLBACKS)
Get the ordered list of fallback languages.
Definition: Language.php:4386
$revQuery
Language $contLang
clear()
Clear all stored messages in global and local cache.
getCheckKey( $code)
const DB_REPLICA
Definition: defines.php:25
$content
Definition: router.php:78
if(! $wgRequest->checkUrlExtension()) if(isset( $_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] !='') $wgTitle
Definition: api.php:58
$mDisable
Should mean that database cannot be used, but check.
isLanguageLoaded( $lang)
Whether the language was loaded and its data is still in the process cache.
WANObjectCache $wanCache
return true
Definition: router.php:92
const LOCK_TTL
How long memcached locks last.
static singleton()
Get the singleton instance of this class.
isCacheExpired( $cache)
Is the given cache array expired due to time passing or a version change?
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
setLogger(LoggerInterface $logger)