3 * Message blobs storage used by ResourceLoader.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Roan Kattouw
22 * @author Trevor Parscal
26 use MediaWiki\MediaWikiServices
;
27 use Psr\Log\LoggerAwareInterface
;
28 use Psr\Log\LoggerInterface
;
29 use Psr\Log\NullLogger
;
30 use Wikimedia\Rdbms\Database
;
33 * This class generates message blobs for use by ResourceLoader modules.
35 * A message blob is a JSON object containing the interface messages for a certain module in
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();
60 $this->wanCache
= MediaWikiServices
::getInstance()->getMainWANObjectCache();
65 * @param LoggerInterface $logger
67 public function setLogger( LoggerInterface
$logger ) {
68 $this->logger
= $logger;
72 * Get the message blob for a module
75 * @param ResourceLoaderModule $module
76 * @param string $lang Language code
79 public function getBlob( ResourceLoaderModule
$module, $lang ) {
80 $blobs = $this->getBlobs( [ $module->getName() => $module ], $lang );
81 return $blobs[$module->getName()];
85 * Get the message blobs for a set of modules
88 * @param ResourceLoaderModule[] $modules Array of module objects keyed by name
89 * @param string $lang Language code
90 * @return array An array mapping module names to message blobs
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.
96 $cache = $this->wanCache
;
98 // Global check key, see clear()
99 $cache->makeGlobalKey( __CLASS__
)
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 );
109 $result = $cache->getMulti( array_values( $cacheKeys ), $curTTLs, $checkKeys );
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 );
117 // Use unexpired cache
118 $blobs[$name] = $result[$key];
126 * @param ResourceLoaderModule $module
127 * @param string $lang
128 * @return string Cache key
130 private function makeCacheKey( ResourceLoaderModule
$module, $lang ) {
131 $messages = array_values( array_unique( $module->getMessages() ) );
133 return $this->wanCache
->makeKey( __CLASS__
, $module->getName(), $lang,
134 md5( json_encode( $messages ) )
140 * @param string $cacheKey
141 * @param ResourceLoaderModule $module
142 * @param string $lang
143 * @return string JSON blob
145 protected function recacheMessageBlob( $cacheKey, ResourceLoaderModule
$module, $lang ) {
146 $blob = $this->generateMessageBlob( $module, $lang );
147 $cache = $this->wanCache
;
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
) )
157 * Invalidate cache keys for modules using this message key.
158 * Called by MessageCache when a message has changed.
160 * @param string $key Message key
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 ) );
171 * Invalidate cache keys for all known modules.
172 * Called by LocalisationCache after cache is regenerated.
174 public function clear() {
175 $cache = $this->wanCache
;
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_NONE
);
188 * @return ResourceLoader
190 protected function getResourceLoader() {
191 return $this->resourceloader
;
196 * @param string $key Message key
197 * @param string $lang Language code
198 * @return string|null
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,
209 $value = $message->plain();
215 * Generate the message blob for a given module in a given language.
217 * @param ResourceLoaderModule $module
218 * @param string $lang Language code
219 * @return string JSON blob
221 private function generateMessageBlob( ResourceLoaderModule
$module, $lang ) {
223 foreach ( $module->getMessages() as $key ) {
224 $value = $this->fetchMessage( $key, $lang );
225 if ( $value !== null ) {
226 $messages[$key] = $value;
230 $json = FormatJson
::encode( (object)$messages );
231 // @codeCoverageIgnoreStart
232 if ( $json === false ) {
233 $this->logger
->warning( 'Failed to encode message blob for {module} ({lang})', [
234 'module' => $module->getName(),
239 // codeCoverageIgnoreEnd