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->makeKey( __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];
125 * @deprecated since 1.27 Use getBlobs() instead
128 public function get( ResourceLoader
$resourceLoader, $modules, $lang ) {
129 return $this->getBlobs( $modules, $lang );
134 * @param ResourceLoaderModule $module
135 * @param string $lang
136 * @return string Cache key
138 private function makeCacheKey( ResourceLoaderModule
$module, $lang ) {
139 $messages = array_values( array_unique( $module->getMessages() ) );
141 return $this->wanCache
->makeKey( __CLASS__
, $module->getName(), $lang,
142 md5( json_encode( $messages ) )
148 * @param string $cacheKey
149 * @param ResourceLoaderModule $module
150 * @param string $lang
151 * @return string JSON blob
153 protected function recacheMessageBlob( $cacheKey, ResourceLoaderModule
$module, $lang ) {
154 $blob = $this->generateMessageBlob( $module, $lang );
155 $cache = $this->wanCache
;
156 $cache->set( $cacheKey, $blob,
157 // Add part of a day to TTL to avoid all modules expiring at once
158 $cache::TTL_WEEK +
mt_rand( 0, $cache::TTL_DAY
),
159 Database
::getCacheSetOptions( wfGetDB( DB_REPLICA
) )
165 * Invalidate cache keys for modules using this message key.
166 * Called by MessageCache when a message has changed.
168 * @param string $key Message key
170 public function updateMessage( $key ) {
171 $moduleNames = $this->getResourceLoader()->getModulesByMessage( $key );
172 foreach ( $moduleNames as $moduleName ) {
173 // Uses a holdoff to account for database replica DB lag (for MessageCache)
174 $this->wanCache
->touchCheckKey( $this->wanCache
->makeKey( __CLASS__
, $moduleName ) );
179 * Invalidate cache keys for all known modules.
180 * Called by LocalisationCache after cache is regenerated.
182 public function clear() {
183 $cache = $this->wanCache
;
184 // Disable holdoff because this invalidates all modules and also not needed since
185 // LocalisationCache is stored outside the database and doesn't have lag.
186 $cache->touchCheckKey( $cache->makeKey( __CLASS__
), $cache::HOLDOFF_NONE
);
191 * @return ResourceLoader
193 protected function getResourceLoader() {
194 return $this->resourceloader
;
199 * @param string $key Message key
200 * @param string $lang Language code
203 protected function fetchMessage( $key, $lang ) {
204 $message = wfMessage( $key )->inLanguage( $lang );
205 $value = $message->plain();
206 if ( !$message->exists() ) {
207 $this->logger
->warning( 'Failed to find {messageKey} ({lang})', [
208 'messageKey' => $key,
216 * Generate the message blob for a given module in a given language.
218 * @param ResourceLoaderModule $module
219 * @param string $lang Language code
220 * @return string JSON blob
222 private function generateMessageBlob( ResourceLoaderModule
$module, $lang ) {
224 foreach ( $module->getMessages() as $key ) {
225 $messages[$key] = $this->fetchMessage( $key, $lang );
228 $json = FormatJson
::encode( (object)$messages );
229 // @codeCoverageIgnoreStart
230 if ( $json === false ) {
231 $this->logger
->warning( 'Failed to encode message blob for {module} ({lang})', [
232 'module' => $module->getName(),
237 // codeCoverageIgnoreEnd