MediaWiki  1.34.0
MessageBlobStore.php
Go to the documentation of this file.
1 <?php
24 use Psr\Log\LoggerAwareInterface;
25 use Psr\Log\LoggerInterface;
26 use Psr\Log\NullLogger;
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 }
MessageBlobStore\getResourceLoader
getResourceLoader()
Definition: MessageBlobStore.php:203
MessageBlobStore\__construct
__construct(ResourceLoader $rl, LoggerInterface $logger=null)
Definition: MessageBlobStore.php:57
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:49
MessageBlobStore\clearGlobalCacheEntry
static clearGlobalCacheEntry(WANObjectCache $cache)
Invalidate cache keys for all known modules.
Definition: MessageBlobStore.php:188
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
MessageBlobStore\fetchMessage
fetchMessage( $key, $lang)
Definition: MessageBlobStore.php:213
MessageBlobStore\clear
clear()
Invalidate cache keys for all known modules.
Definition: MessageBlobStore.php:177
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
FormatJson\UTF8_OK
const UTF8_OK
Skip escaping most characters above U+007F for readability and compactness.
Definition: FormatJson.php:34
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
MessageBlobStore\updateMessage
updateMessage( $key)
Invalidate cache keys for modules using this message key.
Definition: MessageBlobStore.php:166
$blob
$blob
Definition: testCompression.php:65
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
MessageBlobStore\$resourceloader
$resourceloader
Definition: MessageBlobStore.php:41
$modules
$modules
Definition: HTMLFormElement.php:13
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
MessageBlobStore\$logger
LoggerInterface $logger
Definition: MessageBlobStore.php:46
MessageBlobStore\setLogger
setLogger(LoggerInterface $logger)
Definition: MessageBlobStore.php:71
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:116
ResourceLoaderModule\getMessages
getMessages()
Get the messages needed for this module.
Definition: ResourceLoaderModule.php:319
MessageBlobStore\makeCacheKey
makeCacheKey(ResourceLoaderModule $module, $lang)
Definition: MessageBlobStore.php:134
ResourceLoaderModule
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
Definition: ResourceLoaderModule.php:37
$cache
$cache
Definition: mcc.php:33
MessageBlobStore\generateMessageBlob
generateMessageBlob(ResourceLoaderModule $module, $lang)
Generate the message blob for a given module in a given language.
Definition: MessageBlobStore.php:234
MessageBlobStore\getBlobs
getBlobs(array $modules, $lang)
Get the message blobs for a set of modules.
Definition: MessageBlobStore.php:96
MessageBlobStore
This class generates message blobs for use by ResourceLoader.
Definition: MessageBlobStore.php:38
MessageBlobStore\$wanCache
WANObjectCache $wanCache
Definition: MessageBlobStore.php:51
MessageBlobStore\getBlob
getBlob(ResourceLoaderModule $module, $lang)
Get the message blob for a module.
Definition: MessageBlobStore.php:83
ResourceLoaderModule\getName
getName()
Get this module's name.
Definition: ResourceLoaderModule.php:102
MessageBlobStore\recacheMessageBlob
recacheMessageBlob( $cacheKey, ResourceLoaderModule $module, $lang)
Definition: MessageBlobStore.php:149