Improve docs for Title::getInternalURL/getCanonicalURL
[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 Psr\Log\LoggerAwareInterface;
27 use Psr\Log\LoggerInterface;
28 use Psr\Log\NullLogger;
29 use Wikimedia\Rdbms\Database;
30
31 /**
32 * This class generates message blobs for use by ResourceLoader modules.
33 *
34 * A message blob is a JSON object containing the interface messages for a certain module in
35 * a certain language.
36 */
37 class MessageBlobStore implements LoggerAwareInterface {
38
39 /* @var ResourceLoader|null */
40 private $resourceloader;
41
42 /**
43 * @var LoggerInterface
44 */
45 protected $logger;
46
47 /**
48 * @var WANObjectCache
49 */
50 protected $wanCache;
51
52 /**
53 * @param ResourceLoader|null $rl
54 * @param LoggerInterface|null $logger
55 */
56 public function __construct( ResourceLoader $rl = null, LoggerInterface $logger = null ) {
57 $this->resourceloader = $rl;
58 $this->logger = $logger ?: new NullLogger();
59 $this->wanCache = ObjectCache::getMainWANInstance();
60 }
61
62 /**
63 * @since 1.27
64 * @param LoggerInterface $logger
65 */
66 public function setLogger( LoggerInterface $logger ) {
67 $this->logger = $logger;
68 }
69
70 /**
71 * Get the message blob for a module
72 *
73 * @since 1.27
74 * @param ResourceLoaderModule $module
75 * @param string $lang Language code
76 * @return string JSON
77 */
78 public function getBlob( ResourceLoaderModule $module, $lang ) {
79 $blobs = $this->getBlobs( [ $module->getName() => $module ], $lang );
80 return $blobs[$module->getName()];
81 }
82
83 /**
84 * Get the message blobs for a set of modules
85 *
86 * @since 1.27
87 * @param ResourceLoaderModule[] $modules Array of module objects keyed by name
88 * @param string $lang Language code
89 * @return array An array mapping module names to message blobs
90 */
91 public function getBlobs( array $modules, $lang ) {
92 // Each cache key for a message blob by module name and language code also has a generic
93 // check key without language code. This is used to invalidate any and all language subkeys
94 // that exist for a module from the updateMessage() method.
95 $cache = $this->wanCache;
96 $checkKeys = [
97 // Global check key, see clear()
98 $cache->makeKey( __CLASS__ )
99 ];
100 $cacheKeys = [];
101 foreach ( $modules as $name => $module ) {
102 $cacheKey = $this->makeCacheKey( $module, $lang );
103 $cacheKeys[$name] = $cacheKey;
104 // Per-module check key, see updateMessage()
105 $checkKeys[$cacheKey][] = $cache->makeKey( __CLASS__, $name );
106 }
107 $curTTLs = [];
108 $result = $cache->getMulti( array_values( $cacheKeys ), $curTTLs, $checkKeys );
109
110 $blobs = [];
111 foreach ( $modules as $name => $module ) {
112 $key = $cacheKeys[$name];
113 if ( !isset( $result[$key] ) || $curTTLs[$key] === null || $curTTLs[$key] < 0 ) {
114 $blobs[$name] = $this->recacheMessageBlob( $key, $module, $lang );
115 } else {
116 // Use unexpired cache
117 $blobs[$name] = $result[$key];
118 }
119 }
120 return $blobs;
121 }
122
123 /**
124 * @deprecated since 1.27 Use getBlobs() instead
125 * @return array
126 */
127 public function get( ResourceLoader $resourceLoader, $modules, $lang ) {
128 return $this->getBlobs( $modules, $lang );
129 }
130
131 /**
132 * @since 1.27
133 * @param ResourceLoaderModule $module
134 * @param string $lang
135 * @return string Cache key
136 */
137 private function makeCacheKey( ResourceLoaderModule $module, $lang ) {
138 $messages = array_values( array_unique( $module->getMessages() ) );
139 sort( $messages );
140 return $this->wanCache->makeKey( __CLASS__, $module->getName(), $lang,
141 md5( json_encode( $messages ) )
142 );
143 }
144
145 /**
146 * @since 1.27
147 * @param string $cacheKey
148 * @param ResourceLoaderModule $module
149 * @param string $lang
150 * @return string JSON blob
151 */
152 protected function recacheMessageBlob( $cacheKey, ResourceLoaderModule $module, $lang ) {
153 $blob = $this->generateMessageBlob( $module, $lang );
154 $cache = $this->wanCache;
155 $cache->set( $cacheKey, $blob,
156 // Add part of a day to TTL to avoid all modules expiring at once
157 $cache::TTL_WEEK + mt_rand( 0, $cache::TTL_DAY ),
158 Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) )
159 );
160 return $blob;
161 }
162
163 /**
164 * Invalidate cache keys for modules using this message key.
165 * Called by MessageCache when a message has changed.
166 *
167 * @param string $key Message key
168 */
169 public function updateMessage( $key ) {
170 $moduleNames = $this->getResourceLoader()->getModulesByMessage( $key );
171 foreach ( $moduleNames as $moduleName ) {
172 // Uses a holdoff to account for database replica DB lag (for MessageCache)
173 $this->wanCache->touchCheckKey( $this->wanCache->makeKey( __CLASS__, $moduleName ) );
174 }
175 }
176
177 /**
178 * Invalidate cache keys for all known modules.
179 * Called by LocalisationCache after cache is regenerated.
180 */
181 public function clear() {
182 $cache = $this->wanCache;
183 // Disable holdoff because this invalidates all modules and also not needed since
184 // LocalisationCache is stored outside the database and doesn't have lag.
185 $cache->touchCheckKey( $cache->makeKey( __CLASS__ ), $cache::HOLDOFF_NONE );
186 }
187
188 /**
189 * @since 1.27
190 * @return ResourceLoader
191 */
192 protected function getResourceLoader() {
193 // Back-compat: This class supports instantiation without a ResourceLoader object.
194 // Lazy-initialise this property because most callers don't need it.
195 if ( $this->resourceloader === null ) {
196 $this->logger->warning( __CLASS__ . ' created without a ResourceLoader instance' );
197 $this->resourceloader = new ResourceLoader();
198 }
199 return $this->resourceloader;
200 }
201
202 /**
203 * @since 1.27
204 * @param string $key Message key
205 * @param string $lang Language code
206 * @return string
207 */
208 protected function fetchMessage( $key, $lang ) {
209 $message = wfMessage( $key )->inLanguage( $lang );
210 $value = $message->plain();
211 if ( !$message->exists() ) {
212 $this->logger->warning( 'Failed to find {messageKey} ({lang})', [
213 'messageKey' => $key,
214 'lang' => $lang,
215 ] );
216 }
217 return $value;
218 }
219
220 /**
221 * Generate the message blob for a given module in a given language.
222 *
223 * @param ResourceLoaderModule $module
224 * @param string $lang Language code
225 * @return string JSON blob
226 */
227 private function generateMessageBlob( ResourceLoaderModule $module, $lang ) {
228 $messages = [];
229 foreach ( $module->getMessages() as $key ) {
230 $messages[$key] = $this->fetchMessage( $key, $lang );
231 }
232
233 $json = FormatJson::encode( (object)$messages );
234 // @codeCoverageIgnoreStart
235 if ( $json === false ) {
236 $this->logger->warning( 'Failed to encode message blob for {module} ({lang})', [
237 'module' => $module->getName(),
238 'lang' => $lang,
239 ] );
240 $json = '{}';
241 }
242 // codeCoverageIgnoreEnd
243 return $json;
244 }
245 }