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  $this->wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
61  }
62 
67  public function setLogger( LoggerInterface $logger ) {
68  $this->logger = $logger;
69  }
70 
79  public function getBlob( ResourceLoaderModule $module, $lang ) {
80  $blobs = $this->getBlobs( [ $module->getName() => $module ], $lang );
81  return $blobs[$module->getName()];
82  }
83 
92  public function getBlobs( array $modules, $lang ) {
93  // Each cache key for a message blob by module name and language code also has a generic
94  // check key without language code. This is used to invalidate any and all language subkeys
95  // that exist for a module from the updateMessage() method.
97  $checkKeys = [
98  // Global check key, see clear()
99  $cache->makeGlobalKey( __CLASS__ )
100  ];
101  $cacheKeys = [];
102  foreach ( $modules as $name => $module ) {
103  $cacheKey = $this->makeCacheKey( $module, $lang );
104  $cacheKeys[$name] = $cacheKey;
105  // Per-module check key, see updateMessage()
106  $checkKeys[$cacheKey][] = $cache->makeKey( __CLASS__, $name );
107  }
108  $curTTLs = [];
109  $result = $cache->getMulti( array_values( $cacheKeys ), $curTTLs, $checkKeys );
110 
111  $blobs = [];
112  foreach ( $modules as $name => $module ) {
113  $key = $cacheKeys[$name];
114  if ( !isset( $result[$key] ) || $curTTLs[$key] === null || $curTTLs[$key] < 0 ) {
115  $blobs[$name] = $this->recacheMessageBlob( $key, $module, $lang );
116  } else {
117  // Use unexpired cache
118  $blobs[$name] = $result[$key];
119  }
120  }
121  return $blobs;
122  }
123 
130  private function makeCacheKey( ResourceLoaderModule $module, $lang ) {
131  $messages = array_values( array_unique( $module->getMessages() ) );
132  sort( $messages );
133  return $this->wanCache->makeKey( __CLASS__, $module->getName(), $lang,
134  md5( json_encode( $messages ) )
135  );
136  }
137 
145  protected function recacheMessageBlob( $cacheKey, ResourceLoaderModule $module, $lang ) {
146  $blob = $this->generateMessageBlob( $module, $lang );
148  $cache->set( $cacheKey, $blob,
149  // Add part of a day to TTL to avoid all modules expiring at once
150  $cache::TTL_WEEK + mt_rand( 0, $cache::TTL_DAY ),
151  Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) )
152  );
153  return $blob;
154  }
155 
162  public function updateMessage( $key ) {
163  $moduleNames = $this->getResourceLoader()->getModulesByMessage( $key );
164  foreach ( $moduleNames as $moduleName ) {
165  // Uses a holdoff to account for database replica DB lag (for MessageCache)
166  $this->wanCache->touchCheckKey( $this->wanCache->makeKey( __CLASS__, $moduleName ) );
167  }
168  }
169 
174  public function clear() {
176  // Disable hold-off because:
177  // - LocalisationCache is populated by messages on-disk and don't have DB lag,
178  // thus there is no need for hold off. We only clear it after new localisation
179  // updates are known to be deployed to all servers.
180  // - This global check key invalidates message blobs for all modules for all wikis
181  // in cache contexts (e.g. languages, skins). Setting a hold-off on this key could
182  // cause a cache stampede since no values would be stored for several seconds.
183  $cache->touchCheckKey( $cache->makeGlobalKey( __CLASS__ ), $cache::HOLDOFF_TTL_NONE );
184  }
185 
190  protected function getResourceLoader() {
191  return $this->resourceloader;
192  }
193 
200  protected function fetchMessage( $key, $lang ) {
201  $message = wfMessage( $key )->inLanguage( $lang );
202  if ( !$message->exists() ) {
203  $this->logger->warning( 'Failed to find {messageKey} ({lang})', [
204  'messageKey' => $key,
205  'lang' => $lang,
206  ] );
207  $value = null;
208  } else {
209  $value = $message->plain();
210  }
211  return $value;
212  }
213 
221  private function generateMessageBlob( ResourceLoaderModule $module, $lang ) {
222  $messages = [];
223  foreach ( $module->getMessages() as $key ) {
224  $value = $this->fetchMessage( $key, $lang );
225  if ( $value !== null ) {
226  $messages[$key] = $value;
227  }
228  }
229 
230  $json = FormatJson::encode( (object)$messages, false, FormatJson::UTF8_OK );
231  // @codeCoverageIgnoreStart
232  if ( $json === false ) {
233  $this->logger->warning( 'Failed to encode message blob for {module} ({lang})', [
234  'module' => $module->getName(),
235  'lang' => $lang,
236  ] );
237  $json = '{}';
238  }
239  // codeCoverageIgnoreEnd
240  return $json;
241  }
242 }
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)
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.
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.