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 modules.
32 * A message blob is a JSON object containing the interface messages for a certain module in
35 class MessageBlobStore
implements LoggerAwareInterface
{
37 /* @var ResourceLoader */
38 private $resourceloader;
41 * @var LoggerInterface
51 * @param ResourceLoader $rl
52 * @param LoggerInterface|null $logger
54 public function __construct( ResourceLoader
$rl, LoggerInterface
$logger = null ) {
55 $this->resourceloader
= $rl;
56 $this->logger
= $logger ?
: new NullLogger();
57 $this->wanCache
= MediaWikiServices
::getInstance()->getMainWANObjectCache();
62 * @param LoggerInterface $logger
64 public function setLogger( LoggerInterface
$logger ) {
65 $this->logger
= $logger;
69 * Get the message blob for a module
72 * @param ResourceLoaderModule $module
73 * @param string $lang Language code
76 public function getBlob( ResourceLoaderModule
$module, $lang ) {
77 $blobs = $this->getBlobs( [ $module->getName() => $module ], $lang );
78 return $blobs[$module->getName()];
82 * Get the message blobs for a set of modules
85 * @param ResourceLoaderModule[] $modules Array of module objects keyed by name
86 * @param string $lang Language code
87 * @return array An array mapping module names to message blobs
89 public function getBlobs( array $modules, $lang ) {
90 // Each cache key for a message blob by module name and language code also has a generic
91 // check key without language code. This is used to invalidate any and all language subkeys
92 // that exist for a module from the updateMessage() method.
93 $cache = $this->wanCache
;
95 // Global check key, see clear()
96 $cache->makeGlobalKey( __CLASS__
)
99 foreach ( $modules as $name => $module ) {
100 $cacheKey = $this->makeCacheKey( $module, $lang );
101 $cacheKeys[$name] = $cacheKey;
102 // Per-module check key, see updateMessage()
103 $checkKeys[$cacheKey][] = $cache->makeKey( __CLASS__
, $name );
106 $result = $cache->getMulti( array_values( $cacheKeys ), $curTTLs, $checkKeys );
109 foreach ( $modules as $name => $module ) {
110 $key = $cacheKeys[$name];
111 if ( !isset( $result[$key] ) ||
$curTTLs[$key] === null ||
$curTTLs[$key] < 0 ) {
112 $blobs[$name] = $this->recacheMessageBlob( $key, $module, $lang );
114 // Use unexpired cache
115 $blobs[$name] = $result[$key];
123 * @param ResourceLoaderModule $module
124 * @param string $lang
125 * @return string Cache key
127 private function makeCacheKey( ResourceLoaderModule
$module, $lang ) {
128 $messages = array_values( array_unique( $module->getMessages() ) );
130 return $this->wanCache
->makeKey( __CLASS__
, $module->getName(), $lang,
131 md5( json_encode( $messages ) )
137 * @param string $cacheKey
138 * @param ResourceLoaderModule $module
139 * @param string $lang
140 * @return string JSON blob
142 protected function recacheMessageBlob( $cacheKey, ResourceLoaderModule
$module, $lang ) {
143 $blob = $this->generateMessageBlob( $module, $lang );
144 $cache = $this->wanCache
;
145 $cache->set( $cacheKey, $blob,
146 // Add part of a day to TTL to avoid all modules expiring at once
147 $cache::TTL_WEEK +
mt_rand( 0, $cache::TTL_DAY
),
148 Database
::getCacheSetOptions( wfGetDB( DB_REPLICA
) )
154 * Invalidate cache keys for modules using this message key.
155 * Called by MessageCache when a message has changed.
157 * @param string $key Message key
159 public function updateMessage( $key ) {
160 $moduleNames = $this->getResourceLoader()->getModulesByMessage( $key );
161 foreach ( $moduleNames as $moduleName ) {
162 // Uses a holdoff to account for database replica DB lag (for MessageCache)
163 $this->wanCache
->touchCheckKey( $this->wanCache
->makeKey( __CLASS__
, $moduleName ) );
168 * Invalidate cache keys for all known modules.
169 * Called by LocalisationCache after cache is regenerated.
171 public function clear() {
172 $cache = $this->wanCache
;
173 // Disable hold-off because:
174 // - LocalisationCache is populated by messages on-disk and don't have DB lag,
175 // thus there is no need for hold off. We only clear it after new localisation
176 // updates are known to be deployed to all servers.
177 // - This global check key invalidates message blobs for all modules for all wikis
178 // in cache contexts (e.g. languages, skins). Setting a hold-off on this key could
179 // cause a cache stampede since no values would be stored for several seconds.
180 $cache->touchCheckKey( $cache->makeGlobalKey( __CLASS__
), $cache::HOLDOFF_NONE
);
185 * @return ResourceLoader
187 protected function getResourceLoader() {
188 return $this->resourceloader
;
193 * @param string $key Message key
194 * @param string $lang Language code
195 * @return string|null
197 protected function fetchMessage( $key, $lang ) {
198 $message = wfMessage( $key )->inLanguage( $lang );
199 if ( !$message->exists() ) {
200 $this->logger
->warning( 'Failed to find {messageKey} ({lang})', [
201 'messageKey' => $key,
206 $value = $message->plain();
212 * Generate the message blob for a given module in a given language.
214 * @param ResourceLoaderModule $module
215 * @param string $lang Language code
216 * @return string JSON blob
218 private function generateMessageBlob( ResourceLoaderModule
$module, $lang ) {
220 foreach ( $module->getMessages() as $key ) {
221 $value = $this->fetchMessage( $key, $lang );
222 if ( $value !== null ) {
223 $messages[$key] = $value;
227 $json = FormatJson
::encode( (object)$messages );
228 // @codeCoverageIgnoreStart
229 if ( $json === false ) {
230 $this->logger
->warning( 'Failed to encode message blob for {module} ({lang})', [
231 'module' => $module->getName(),
236 // codeCoverageIgnoreEnd