Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / includes / resourceloader / MessageBlobStore.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @author Roan Kattouw
20 * @author Trevor Parscal
21 */
22
23 use MediaWiki\MediaWikiServices;
24 use Psr\Log\LoggerAwareInterface;
25 use Psr\Log\LoggerInterface;
26 use Psr\Log\NullLogger;
27 use Wikimedia\Rdbms\Database;
28
29 /**
30 * This class generates message blobs for use by ResourceLoader modules.
31 *
32 * A message blob is a JSON object containing the interface messages for a certain module in
33 * a certain language.
34 */
35 class MessageBlobStore implements LoggerAwareInterface {
36
37 /* @var ResourceLoader */
38 private $resourceloader;
39
40 /**
41 * @var LoggerInterface
42 */
43 protected $logger;
44
45 /**
46 * @var WANObjectCache
47 */
48 protected $wanCache;
49
50 /**
51 * @param ResourceLoader $rl
52 * @param LoggerInterface|null $logger
53 */
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();
58 }
59
60 /**
61 * @since 1.27
62 * @param LoggerInterface $logger
63 */
64 public function setLogger( LoggerInterface $logger ) {
65 $this->logger = $logger;
66 }
67
68 /**
69 * Get the message blob for a module
70 *
71 * @since 1.27
72 * @param ResourceLoaderModule $module
73 * @param string $lang Language code
74 * @return string JSON
75 */
76 public function getBlob( ResourceLoaderModule $module, $lang ) {
77 $blobs = $this->getBlobs( [ $module->getName() => $module ], $lang );
78 return $blobs[$module->getName()];
79 }
80
81 /**
82 * Get the message blobs for a set of modules
83 *
84 * @since 1.27
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
88 */
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;
94 $checkKeys = [
95 // Global check key, see clear()
96 $cache->makeGlobalKey( __CLASS__ )
97 ];
98 $cacheKeys = [];
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 );
104 }
105 $curTTLs = [];
106 $result = $cache->getMulti( array_values( $cacheKeys ), $curTTLs, $checkKeys );
107
108 $blobs = [];
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 );
113 } else {
114 // Use unexpired cache
115 $blobs[$name] = $result[$key];
116 }
117 }
118 return $blobs;
119 }
120
121 /**
122 * @since 1.27
123 * @param ResourceLoaderModule $module
124 * @param string $lang
125 * @return string Cache key
126 */
127 private function makeCacheKey( ResourceLoaderModule $module, $lang ) {
128 $messages = array_values( array_unique( $module->getMessages() ) );
129 sort( $messages );
130 return $this->wanCache->makeKey( __CLASS__, $module->getName(), $lang,
131 md5( json_encode( $messages ) )
132 );
133 }
134
135 /**
136 * @since 1.27
137 * @param string $cacheKey
138 * @param ResourceLoaderModule $module
139 * @param string $lang
140 * @return string JSON blob
141 */
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 ) )
149 );
150 return $blob;
151 }
152
153 /**
154 * Invalidate cache keys for modules using this message key.
155 * Called by MessageCache when a message has changed.
156 *
157 * @param string $key Message key
158 */
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 ) );
164 }
165 }
166
167 /**
168 * Invalidate cache keys for all known modules.
169 * Called by LocalisationCache after cache is regenerated.
170 */
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_TTL_NONE );
181 }
182
183 /**
184 * @since 1.27
185 * @return ResourceLoader
186 */
187 protected function getResourceLoader() {
188 return $this->resourceloader;
189 }
190
191 /**
192 * @since 1.27
193 * @param string $key Message key
194 * @param string $lang Language code
195 * @return string|null
196 */
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,
202 'lang' => $lang,
203 ] );
204 $value = null;
205 } else {
206 $value = $message->plain();
207 }
208 return $value;
209 }
210
211 /**
212 * Generate the message blob for a given module in a given language.
213 *
214 * @param ResourceLoaderModule $module
215 * @param string $lang Language code
216 * @return string JSON blob
217 */
218 private function generateMessageBlob( ResourceLoaderModule $module, $lang ) {
219 $messages = [];
220 foreach ( $module->getMessages() as $key ) {
221 $value = $this->fetchMessage( $key, $lang );
222 if ( $value !== null ) {
223 $messages[$key] = $value;
224 }
225 }
226
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(),
232 'lang' => $lang,
233 ] );
234 $json = '{}';
235 }
236 // codeCoverageIgnoreEnd
237 return $json;
238 }
239 }