MediaWiki REL1_34
MessageBlobStore.php
Go to the documentation of this file.
1<?php
24use Psr\Log\LoggerAwareInterface;
25use Psr\Log\LoggerInterface;
26use Psr\Log\NullLogger;
28
38class MessageBlobStore implements LoggerAwareInterface {
39
40 /* @var 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() {
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}
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
MediaWikiServices is the service locator for the application scope of MediaWiki.
This class generates message blobs for use by ResourceLoader.
getBlobs(array $modules, $lang)
Get the message blobs for a set of modules.
fetchMessage( $key, $lang)
generateMessageBlob(ResourceLoaderModule $module, $lang)
Generate the message blob for a given module in a given language.
WANObjectCache $wanCache
__construct(ResourceLoader $rl, LoggerInterface $logger=null)
static clearGlobalCacheEntry(WANObjectCache $cache)
Invalidate cache keys for all known modules.
recacheMessageBlob( $cacheKey, ResourceLoaderModule $module, $lang)
getBlob(ResourceLoaderModule $module, $lang)
Get the message blob for a module.
LoggerInterface $logger
setLogger(LoggerInterface $logger)
clear()
Invalidate cache keys for all known modules.
updateMessage( $key)
Invalidate cache keys for modules using this message key.
makeCacheKey(ResourceLoaderModule $module, $lang)
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
getMessages()
Get the messages needed for this module.
getName()
Get this module's name.
ResourceLoader is a loading system for JavaScript and CSS resources.
Multi-datacenter aware caching interface.
set( $key, $value, $ttl=self::TTL_INDEFINITE, array $opts=[])
Set the value of a key in cache.
Relational database abstraction object.
Definition Database.php:49
$cache
Definition mcc.php:33
const DB_REPLICA
Definition defines.php:25
if(!isset( $args[0])) $lang