3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @author Roan Kattouw
20 * @author Trevor Parscal
23 use MediaWiki\MediaWikiServices
;
24 use Psr\Log\LoggerAwareInterface
;
25 use Psr\Log\LoggerInterface
;
26 use Psr\Log\NullLogger
;
27 use Wikimedia\Rdbms\Database
;
30 * This class generates message blobs for use by ResourceLoader.
32 * A message blob is a JSON object containing the interface messages for a
33 * certain module in a certain language.
35 * @ingroup ResourceLoader
38 class MessageBlobStore
implements LoggerAwareInterface
{
40 /* @var ResourceLoader */
41 private $resourceloader;
44 * @var LoggerInterface
54 * @param ResourceLoader $rl
55 * @param LoggerInterface|null $logger
57 public function __construct( ResourceLoader
$rl, LoggerInterface
$logger = null ) {
58 $this->resourceloader
= $rl;
59 $this->logger
= $logger ?
: new NullLogger();
61 // NOTE: when changing this assignment, make sure the code in the instantiator for
62 // LocalisationCache which calls MessageBlobStore::clearGlobalCacheEntry() uses the
64 $this->wanCache
= MediaWikiServices
::getInstance()->getMainWANObjectCache();
69 * @param LoggerInterface $logger
71 public function setLogger( LoggerInterface
$logger ) {
72 $this->logger
= $logger;
76 * Get the message blob for a module
79 * @param ResourceLoaderModule $module
80 * @param string $lang Language code
83 public function getBlob( ResourceLoaderModule
$module, $lang ) {
84 $blobs = $this->getBlobs( [ $module->getName() => $module ], $lang );
85 return $blobs[$module->getName()];
89 * Get the message blobs for a set of modules
92 * @param ResourceLoaderModule[] $modules Array of module objects keyed by name
93 * @param string $lang Language code
94 * @return array An array mapping module names to message blobs
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.
100 $cache = $this->wanCache
;
102 // Global check key, see clear()
103 $cache->makeGlobalKey( __CLASS__
)
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 );
113 $result = $cache->getMulti( array_values( $cacheKeys ), $curTTLs, $checkKeys );
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 );
121 // Use unexpired cache
122 $blobs[$name] = $result[$key];
130 * @param ResourceLoaderModule $module
131 * @param string $lang
132 * @return string Cache key
134 private function makeCacheKey( ResourceLoaderModule
$module, $lang ) {
135 $messages = array_values( array_unique( $module->getMessages() ) );
137 return $this->wanCache
->makeKey( __CLASS__
, $module->getName(), $lang,
138 md5( json_encode( $messages ) )
144 * @param string $cacheKey
145 * @param ResourceLoaderModule $module
146 * @param string $lang
147 * @return string JSON blob
149 protected function recacheMessageBlob( $cacheKey, ResourceLoaderModule
$module, $lang ) {
150 $blob = $this->generateMessageBlob( $module, $lang );
151 $cache = $this->wanCache
;
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
) )
161 * Invalidate cache keys for modules using this message key.
162 * Called by MessageCache when a message has changed.
164 * @param string $key Message key
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 ) );
175 * Invalidate cache keys for all known modules.
177 public function clear() {
178 self
::clearGlobalCacheEntry( $this->wanCache
);
182 * Invalidate cache keys for all known modules.
184 * Called by LocalisationCache after cache is regenerated.
186 * @param WANObjectCache $cache
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
);
201 * @return ResourceLoader
203 protected function getResourceLoader() {
204 return $this->resourceloader
;
209 * @param string $key Message key
210 * @param string $lang Language code
211 * @return string|null
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,
222 $value = $message->plain();
228 * Generate the message blob for a given module in a given language.
230 * @param ResourceLoaderModule $module
231 * @param string $lang Language code
232 * @return string JSON blob
234 private function generateMessageBlob( ResourceLoaderModule
$module, $lang ) {
236 foreach ( $module->getMessages() as $key ) {
237 $value = $this->fetchMessage( $key, $lang );
238 if ( $value !== null ) {
239 $messages[$key] = $value;
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(),
252 // codeCoverageIgnoreEnd