Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / cache / MessageBlobStore.php
blob279898c7f8c40ab023fe6066f5f3ed784bc09c72
1 <?php
2 /**
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
20 * @file
21 * @author Roan Kattouw
22 * @author Trevor Parscal
23 * @author Timo Tijhof
26 use Psr\Log\LoggerAwareInterface;
27 use Psr\Log\LoggerInterface;
28 use Psr\Log\NullLogger;
30 /**
31 * This class generates message blobs for use by ResourceLoader modules.
33 * A message blob is a JSON object containing the interface messages for a certain module in
34 * a certain language.
36 class MessageBlobStore implements LoggerAwareInterface {
38 /* @var ResourceLoader|null */
39 private $resourceloader;
41 /**
42 * @var LoggerInterface
44 protected $logger;
46 /**
47 * @var WANObjectCache
49 protected $wanCache;
51 /**
52 * @param ResourceLoader $rl
53 * @param LoggerInterface $logger
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();
61 /**
62 * @since 1.27
63 * @param LoggerInterface $logger
65 public function setLogger( LoggerInterface $logger ) {
66 $this->logger = $logger;
69 /**
70 * Get the message blob for a module
72 * @since 1.27
73 * @param ResourceLoaderModule $module
74 * @param string $lang Language code
75 * @return string JSON
77 public function getBlob( ResourceLoaderModule $module, $lang ) {
78 $blobs = $this->getBlobs( [ $module->getName() => $module ], $lang );
79 return $blobs[$module->getName()];
82 /**
83 * Get the message blobs for a set of modules
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
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__ )
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 );
106 $curTTLs = [];
107 $result = $cache->getMulti( array_values( $cacheKeys ), $curTTLs, $checkKeys );
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 ]
116 $blobs[$name] = $this->recacheMessageBlob( $key, $module, $lang );
117 } else {
118 // Use unexpired cache
119 $blobs[$name] = $result[$key];
122 return $blobs;
126 * @deprecated since 1.27 Use getBlobs() instead
127 * @return array
129 public function get( ResourceLoader $resourceLoader, $modules, $lang ) {
130 return $this->getBlobs( $modules, $lang );
134 * @deprecated since 1.27 Obsolete. Used to populate a cache table in the database.
135 * @return bool
137 public function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
138 return false;
142 * @since 1.27
143 * @param ResourceLoaderModule $module
144 * @param string $lang
145 * @return string Cache key
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 ) )
156 * @since 1.27
157 * @param string $cacheKey
158 * @param ResourceLoaderModule $module
159 * @param string $lang
160 * @return string JSON blob
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_SLAVE ) )
170 return $blob;
174 * Invalidate cache keys for modules using this message key.
175 * Called by MessageCache when a message has changed.
177 * @param string $key Message key
179 public function updateMessage( $key ) {
180 $moduleNames = $this->getResourceLoader()->getModulesByMessage( $key );
181 foreach ( $moduleNames as $moduleName ) {
182 // Uses a holdoff to account for database slave lag (for MessageCache)
183 $this->wanCache->touchCheckKey( $this->wanCache->makeKey( __CLASS__, $moduleName ) );
188 * Invalidate cache keys for all known modules.
189 * Called by LocalisationCache after cache is regenerated.
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 );
199 * @since 1.27
200 * @return ResourceLoader
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();
209 return $this->resourceloader;
213 * @since 1.27
214 * @param string $key Message key
215 * @param string $lang Language code
216 * @return string
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 ] );
227 return $value;
231 * Generate the message blob for a given module in a given language.
233 * @param ResourceLoaderModule $module
234 * @param string $lang Language code
235 * @return string JSON blob
237 private function generateMessageBlob( ResourceLoaderModule $module, $lang ) {
238 $messages = [];
239 foreach ( $module->getMessages() as $key ) {
240 $messages[$key] = $this->fetchMessage( $key, $lang );
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 = '{}';
251 return $json;