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