ceb51f2d3fd4f42cd90e782b6701601af763eaec
[lhc/web/wiklou.git] / includes / cache / MessageBlobStore.php
1 <?php
2 /**
3 * Message blobs storage used by ResourceLoader.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Roan Kattouw
22 * @author Trevor Parscal
23 * @author Timo Tijhof
24 */
25
26 use MediaWiki\MediaWikiServices;
27 use Psr\Log\LoggerAwareInterface;
28 use Psr\Log\LoggerInterface;
29 use Psr\Log\NullLogger;
30 use Wikimedia\Rdbms\Database;
31
32 /**
33 * This class generates message blobs for use by ResourceLoader modules.
34 *
35 * A message blob is a JSON object containing the interface messages for a certain module in
36 * a certain language.
37 */
38 class MessageBlobStore implements LoggerAwareInterface {
39
40 /* @var ResourceLoader */
41 private $resourceloader;
42
43 /**
44 * @var LoggerInterface
45 */
46 protected $logger;
47
48 /**
49 * @var WANObjectCache
50 */
51 protected $wanCache;
52
53 /**
54 * @param ResourceLoader $rl
55 * @param LoggerInterface|null $logger
56 */
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();
61 }
62
63 /**
64 * @since 1.27
65 * @param LoggerInterface $logger
66 */
67 public function setLogger( LoggerInterface $logger ) {
68 $this->logger = $logger;
69 }
70
71 /**
72 * Get the message blob for a module
73 *
74 * @since 1.27
75 * @param ResourceLoaderModule $module
76 * @param string $lang Language code
77 * @return string JSON
78 */
79 public function getBlob( ResourceLoaderModule $module, $lang ) {
80 $blobs = $this->getBlobs( [ $module->getName() => $module ], $lang );
81 return $blobs[$module->getName()];
82 }
83
84 /**
85 * Get the message blobs for a set of modules
86 *
87 * @since 1.27
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
91 */
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;
97 $checkKeys = [
98 // Global check key, see clear()
99 $cache->makeKey( __CLASS__ )
100 ];
101 $cacheKeys = [];
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 );
107 }
108 $curTTLs = [];
109 $result = $cache->getMulti( array_values( $cacheKeys ), $curTTLs, $checkKeys );
110
111 $blobs = [];
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 );
116 } else {
117 // Use unexpired cache
118 $blobs[$name] = $result[$key];
119 }
120 }
121 return $blobs;
122 }
123
124 /**
125 * @deprecated since 1.27 Use getBlobs() instead
126 * @return array
127 */
128 public function get( ResourceLoader $resourceLoader, $modules, $lang ) {
129 return $this->getBlobs( $modules, $lang );
130 }
131
132 /**
133 * @since 1.27
134 * @param ResourceLoaderModule $module
135 * @param string $lang
136 * @return string Cache key
137 */
138 private function makeCacheKey( ResourceLoaderModule $module, $lang ) {
139 $messages = array_values( array_unique( $module->getMessages() ) );
140 sort( $messages );
141 return $this->wanCache->makeKey( __CLASS__, $module->getName(), $lang,
142 md5( json_encode( $messages ) )
143 );
144 }
145
146 /**
147 * @since 1.27
148 * @param string $cacheKey
149 * @param ResourceLoaderModule $module
150 * @param string $lang
151 * @return string JSON blob
152 */
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 ) )
160 );
161 return $blob;
162 }
163
164 /**
165 * Invalidate cache keys for modules using this message key.
166 * Called by MessageCache when a message has changed.
167 *
168 * @param string $key Message key
169 */
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 ) );
175 }
176 }
177
178 /**
179 * Invalidate cache keys for all known modules.
180 * Called by LocalisationCache after cache is regenerated.
181 */
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 );
187 }
188
189 /**
190 * @since 1.27
191 * @return ResourceLoader
192 */
193 protected function getResourceLoader() {
194 return $this->resourceloader;
195 }
196
197 /**
198 * @since 1.27
199 * @param string $key Message key
200 * @param string $lang Language code
201 * @return string
202 */
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,
209 'lang' => $lang,
210 ] );
211 }
212 return $value;
213 }
214
215 /**
216 * Generate the message blob for a given module in a given language.
217 *
218 * @param ResourceLoaderModule $module
219 * @param string $lang Language code
220 * @return string JSON blob
221 */
222 private function generateMessageBlob( ResourceLoaderModule $module, $lang ) {
223 $messages = [];
224 foreach ( $module->getMessages() as $key ) {
225 $messages[$key] = $this->fetchMessage( $key, $lang );
226 }
227
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(),
233 'lang' => $lang,
234 ] );
235 $json = '{}';
236 }
237 // codeCoverageIgnoreEnd
238 return $json;
239 }
240 }