MediaWiki  master
MessageBlobStore.php
Go to the documentation of this file.
1 <?php
28 
38 class MessageBlobStore implements LoggerAwareInterface {
39 
40  /* @var ResourceLoader */
41  private $resourceloader;
42 
46  protected $logger;
47 
51  protected $wanCache;
52 
57  public function __construct( ResourceLoader $rl, LoggerInterface $logger = null ) {
58  $this->resourceloader = $rl;
59  $this->logger = $logger ?: new NullLogger();
60 
61  // NOTE: when changing this assignment, make sure the code in the instantiator for
62  // LocalisationCache which calls MessageBlobStore::clearGlobalCacheEntry() uses the
63  // same cache object.
64  $this->wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
65  }
66 
71  public function setLogger( LoggerInterface $logger ) {
72  $this->logger = $logger;
73  }
74 
83  public function getBlob( ResourceLoaderModule $module, $lang ) {
84  $blobs = $this->getBlobs( [ $module->getName() => $module ], $lang );
85  return $blobs[$module->getName()];
86  }
87 
96  public function getBlobs( array $modules, $lang ) {
97  // Each cache key for a message blob by module name and language code also has a generic
98  // check key without language code. This is used to invalidate any and all language subkeys
99  // that exist for a module from the updateMessage() method.
101  $checkKeys = [
102  // Global check key, see clear()
103  $cache->makeGlobalKey( __CLASS__ )
104  ];
105  $cacheKeys = [];
106  foreach ( $modules as $name => $module ) {
107  $cacheKey = $this->makeCacheKey( $module, $lang );
108  $cacheKeys[$name] = $cacheKey;
109  // Per-module check key, see updateMessage()
110  $checkKeys[$cacheKey][] = $cache->makeKey( __CLASS__, $name );
111  }
112  $curTTLs = [];
113  $result = $cache->getMulti( array_values( $cacheKeys ), $curTTLs, $checkKeys );
114 
115  $blobs = [];
116  foreach ( $modules as $name => $module ) {
117  $key = $cacheKeys[$name];
118  if ( !isset( $result[$key] ) || $curTTLs[$key] === null || $curTTLs[$key] < 0 ) {
119  $blobs[$name] = $this->recacheMessageBlob( $key, $module, $lang );
120  } else {
121  // Use unexpired cache
122  $blobs[$name] = $result[$key];
123  }
124  }
125  return $blobs;
126  }
127 
134  private function makeCacheKey( ResourceLoaderModule $module, $lang ) {
135  $messages = array_values( array_unique( $module->getMessages() ) );
136  sort( $messages );
137  return $this->wanCache->makeKey( __CLASS__, $module->getName(), $lang,
138  md5( json_encode( $messages ) )
139  );
140  }
141 
149  protected function recacheMessageBlob( $cacheKey, ResourceLoaderModule $module, $lang ) {
150  $blob = $this->generateMessageBlob( $module, $lang );
152  $cache->set( $cacheKey, $blob,
153  // Add part of a day to TTL to avoid all modules expiring at once
154  $cache::TTL_WEEK + mt_rand( 0, $cache::TTL_DAY ),
155  Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) )
156  );
157  return $blob;
158  }
159 
166  public function updateMessage( $key ) {
167  $moduleNames = $this->getResourceLoader()->getModulesByMessage( $key );
168  foreach ( $moduleNames as $moduleName ) {
169  // Uses a holdoff to account for database replica DB lag (for MessageCache)
170  $this->wanCache->touchCheckKey( $this->wanCache->makeKey( __CLASS__, $moduleName ) );
171  }
172  }
173 
177  public function clear() {
178  self::clearGlobalCacheEntry( $this->wanCache );
179  }
180 
188  public static function clearGlobalCacheEntry( WANObjectCache $cache ) {
189  // Disable hold-off because:
190  // - LocalisationCache is populated by messages on-disk and don't have DB lag,
191  // thus there is no need for hold off. We only clear it after new localisation
192  // updates are known to be deployed to all servers.
193  // - This global check key invalidates message blobs for all modules for all wikis
194  // in cache contexts (e.g. languages, skins). Setting a hold-off on this key could
195  // cause a cache stampede since no values would be stored for several seconds.
196  $cache->touchCheckKey( $cache->makeGlobalKey( __CLASS__ ), $cache::HOLDOFF_TTL_NONE );
197  }
198 
203  protected function getResourceLoader() {
204  return $this->resourceloader;
205  }
206 
213  protected function fetchMessage( $key, $lang ) {
214  $message = wfMessage( $key )->inLanguage( $lang );
215  if ( !$message->exists() ) {
216  $this->logger->warning( 'Failed to find {messageKey} ({lang})', [
217  'messageKey' => $key,
218  'lang' => $lang,
219  ] );
220  $value = null;
221  } else {
222  $value = $message->plain();
223  }
224  return $value;
225  }
226 
234  private function generateMessageBlob( ResourceLoaderModule $module, $lang ) {
235  $messages = [];
236  foreach ( $module->getMessages() as $key ) {
237  $value = $this->fetchMessage( $key, $lang );
238  if ( $value !== null ) {
239  $messages[$key] = $value;
240  }
241  }
242 
243  $json = FormatJson::encode( (object)$messages, false, FormatJson::UTF8_OK );
244  // @codeCoverageIgnoreStart
245  if ( $json === false ) {
246  $this->logger->warning( 'Failed to encode message blob for {module} ({lang})', [
247  'module' => $module->getName(),
248  'lang' => $lang,
249  ] );
250  $json = '{}';
251  }
252  // codeCoverageIgnoreEnd
253  return $json;
254  }
255 }
This class generates message blobs for use by ResourceLoader.
getMessages()
Get the messages needed for this module.
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
fetchMessage( $key, $lang)
if(!isset( $args[0])) $lang
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
setLogger(LoggerInterface $logger)
WANObjectCache $wanCache
recacheMessageBlob( $cacheKey, ResourceLoaderModule $module, $lang)
LoggerInterface $logger
const UTF8_OK
Skip escaping most characters above U+007F for readability and compactness.
Definition: FormatJson.php:34
$modules
getBlobs(array $modules, $lang)
Get the message blobs for a set of modules.
$cache
Definition: mcc.php:33
clear()
Invalidate cache keys for all known modules.
makeCacheKey(ResourceLoaderModule $module, $lang)
touchCheckKey( $key, $holdoff=self::HOLDOFF_TTL)
Purge a "check" key from all datacenters, invalidating keys that use it.
updateMessage( $key)
Invalidate cache keys for modules using this message key.
generateMessageBlob(ResourceLoaderModule $module, $lang)
Generate the message blob for a given module in a given language.
getBlob(ResourceLoaderModule $module, $lang)
Get the message blob for a module.
static clearGlobalCacheEntry(WANObjectCache $cache)
Invalidate cache keys for all known modules.
makeGlobalKey( $class,... $components)
const DB_REPLICA
Definition: defines.php:25
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
__construct(ResourceLoader $rl, LoggerInterface $logger=null)
getName()
Get this module&#39;s name.