MediaWiki REL1_37
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 {
41
43 protected $logger;
44
46 protected $wanCache;
47
53 public function __construct(
55 ?LoggerInterface $logger,
56 ?WANObjectCache $wanObjectCache
57 ) {
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 = $wanObjectCache ?: MediaWikiServices::getInstance()
65 ->getMainWANObjectCache();
66 }
67
72 public function setLogger( LoggerInterface $logger ) {
73 $this->logger = $logger;
74 }
75
84 public function getBlob( ResourceLoaderModule $module, $lang ) {
85 $blobs = $this->getBlobs( [ $module->getName() => $module ], $lang );
86 return $blobs[$module->getName()];
87 }
88
97 public function getBlobs( array $modules, $lang ) {
98 // Each cache key for a message blob by module name and language code also has a generic
99 // check key without language code. This is used to invalidate any and all language subkeys
100 // that exist for a module from the updateMessage() method.
102 $checkKeys = [
103 // Global check key, see clear()
104 $cache->makeGlobalKey( __CLASS__ )
105 ];
106 $cacheKeys = [];
107 foreach ( $modules as $name => $module ) {
108 $cacheKey = $this->makeCacheKey( $module, $lang );
109 $cacheKeys[$name] = $cacheKey;
110 // Per-module check key, see updateMessage()
111 $checkKeys[$cacheKey][] = $cache->makeKey( __CLASS__, $name );
112 }
113 $curTTLs = [];
114 $result = $cache->getMulti( array_values( $cacheKeys ), $curTTLs, $checkKeys );
115
116 $blobs = [];
117 foreach ( $modules as $name => $module ) {
118 $key = $cacheKeys[$name];
119 if ( !isset( $result[$key] ) || $curTTLs[$key] === null || $curTTLs[$key] < 0 ) {
120 $blobs[$name] = $this->recacheMessageBlob( $key, $module, $lang );
121 } else {
122 // Use unexpired cache
123 $blobs[$name] = $result[$key];
124 }
125 }
126 return $blobs;
127 }
128
135 private function makeCacheKey( ResourceLoaderModule $module, $lang ) {
136 $messages = array_values( array_unique( $module->getMessages() ) );
137 sort( $messages );
138 return $this->wanCache->makeKey( __CLASS__, $module->getName(), $lang,
139 md5( json_encode( $messages ) )
140 );
141 }
142
150 protected function recacheMessageBlob( $cacheKey, ResourceLoaderModule $module, $lang ) {
151 $blob = $this->generateMessageBlob( $module, $lang );
153 $cache->set( $cacheKey, $blob,
154 // Add part of a day to TTL to avoid all modules expiring at once
155 $cache::TTL_WEEK + mt_rand( 0, $cache::TTL_DAY ),
156 Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) )
157 );
158 return $blob;
159 }
160
167 public function updateMessage( $key ): void {
168 $moduleNames = $this->resourceloader->getModulesByMessage( $key );
169 foreach ( $moduleNames as $moduleName ) {
170 // Uses a holdoff to account for database replica DB lag (for MessageCache)
171 $this->wanCache->touchCheckKey( $this->wanCache->makeKey( __CLASS__, $moduleName ) );
172 }
173 }
174
178 public function clear() {
179 self::clearGlobalCacheEntry( $this->wanCache );
180 }
181
189 public static function clearGlobalCacheEntry( WANObjectCache $cache ) {
190 // Disable hold-off because:
191 // - LocalisationCache is populated by messages on-disk and don't have DB lag,
192 // thus there is no need for hold off. We only clear it after new localisation
193 // updates are known to be deployed to all servers.
194 // - This global check key invalidates message blobs for all modules for all wikis
195 // in cache contexts (e.g. languages, skins). Setting a hold-off on this key could
196 // cause a cache stampede since no values would be stored for several seconds.
197 $cache->touchCheckKey( $cache->makeGlobalKey( __CLASS__ ), $cache::HOLDOFF_TTL_NONE );
198 }
199
206 protected function fetchMessage( $key, $lang ) {
207 $message = wfMessage( $key )->inLanguage( $lang );
208 if ( !$message->exists() ) {
209 $this->logger->warning( 'Failed to find {messageKey} ({lang})', [
210 'messageKey' => $key,
211 'lang' => $lang,
212 ] );
213 $value = null;
214 } else {
215 $value = $message->plain();
216 }
217 return $value;
218 }
219
227 private function generateMessageBlob( ResourceLoaderModule $module, $lang ) {
228 $messages = [];
229 foreach ( $module->getMessages() as $key ) {
230 $value = $this->fetchMessage( $key, $lang );
231 // If the message does not exist, omit it from the blob so that
232 // client-side mw.message may do its own existence handling.
233 if ( $value !== null ) {
234 $messages[$key] = $value;
235 }
236 }
237
238 $json = FormatJson::encode( (object)$messages, false, FormatJson::UTF8_OK );
239 // @codeCoverageIgnoreStart
240 if ( $json === false ) {
241 $this->logger->warning( 'Failed to encode message blob for {module} ({lang})', [
242 'module' => $module->getName(),
243 'lang' => $lang,
244 ] );
245 $json = '{}';
246 }
247 // codeCoverageIgnoreEnd
248 return $json;
249 }
250}
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
static clearGlobalCacheEntry(WANObjectCache $cache)
Invalidate cache keys for all known modules.
ResourceLoader $resourceloader
__construct(ResourceLoader $rl, ?LoggerInterface $logger, ?WANObjectCache $wanObjectCache)
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:52
$cache
Definition mcc.php:33
const DB_REPLICA
Definition defines.php:25
if(!isset( $args[0])) $lang