3 * Resource 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
21 * @author Roan Kattouw
22 * @author Trevor Parscal
26 * This class provides access to the resource message blobs storage used
29 * A message blob is a JSON object containing the interface messages for a
30 * certain resource in a certain language. These message blobs are cached
31 * in the msg_resource table and automatically invalidated when one of their
32 * constituent messages or the resource itself is changed.
34 class MessageBlobStore
{
36 * In-process cache for message blobs.
38 * Keyed by language code, then module name.
42 protected $blobCache = array();
44 /* @var ResourceLoader */
45 protected $resourceloader;
48 * @param ResourceLoader $resourceloader
50 public function __construct( ResourceLoader
$resourceloader = null ) {
51 $this->resourceloader
= $resourceloader;
55 * Get the singleton instance
58 * @deprecated since 1.25
59 * @return MessageBlobStore
61 public static function getInstance() {
62 wfDeprecated( __METHOD__
, '1.25' );
67 * Get the message blobs for a set of modules
69 * @param ResourceLoader $resourceLoader
70 * @param array $modules Array of module objects keyed by module name
71 * @param string $lang Language code
72 * @return array An array mapping module names to message blobs
74 public function get( ResourceLoader
$resourceLoader, $modules, $lang ) {
75 if ( !count( $modules ) ) {
81 // Try in-process cache
82 $missingFromCache = array();
83 foreach ( $modules as $name => $module ) {
84 if ( isset( $this->blobCache
[$lang][$name] ) ) {
85 $blobs[$name] = $this->blobCache
[$lang][$name];
87 $missingFromCache[] = $name;
92 if ( $missingFromCache ) {
93 $blobs +
= $this->getFromDB( $resourceLoader, $missingFromCache, $lang );
96 // Generate new blobs for any remaining modules and store in DB
97 $missingFromDb = array_diff( array_keys( $modules ), array_keys( $blobs ) );
98 foreach ( $missingFromDb as $name ) {
99 $blob = $this->insertMessageBlob( $name, $modules[$name], $lang );
101 $blobs[$name] = $blob;
105 // Update in-process cache
106 if ( isset( $this->blobCache
[$lang] ) ) {
107 $this->blobCache
[$lang] +
= $blobs;
109 $this->blobCache
[$lang] = $blobs;
116 * Generate and insert a new message blob. If the blob was already
117 * present, it is not regenerated; instead, the preexisting blob
118 * is fetched and returned.
120 * @param string $name Module name
121 * @param ResourceLoaderModule $module
122 * @param string $lang Language code
123 * @return mixed Message blob or false if the module has no messages
125 public function insertMessageBlob( $name, ResourceLoaderModule
$module, $lang ) {
126 $blob = $this->generateMessageBlob( $module, $lang );
133 $dbw = wfGetDB( DB_MASTER
);
134 $success = $dbw->insert( 'msg_resource', array(
136 'mr_resource' => $name,
138 'mr_timestamp' => $dbw->timestamp()
144 if ( $success && $dbw->affectedRows() == 0 ) {
145 // Blob was already present, fetch it
146 $blob = $dbw->selectField( 'msg_resource', 'mr_blob', array(
147 'mr_resource' => $name,
153 } catch ( DBError
$e ) {
154 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
160 * Update the message blob for a given module in a given language
162 * @param string $name Module name
163 * @param ResourceLoaderModule $module
164 * @param string $lang Language code
165 * @return string|null Regenerated message blob, or null if there was no blob for
166 * the given module/language pair.
168 public function updateModule( $name, ResourceLoaderModule
$module, $lang ) {
169 $dbw = wfGetDB( DB_MASTER
);
170 $row = $dbw->selectRow( 'msg_resource', 'mr_blob',
171 array( 'mr_resource' => $name, 'mr_lang' => $lang ),
178 $newBlob = $this->generateMessageBlob( $module, $lang );
182 'mr_resource' => $name,
184 'mr_blob' => $newBlob,
185 'mr_timestamp' => $dbw->timestamp()
188 $dbw->replace( 'msg_resource',
189 array( array( 'mr_resource', 'mr_lang' ) ),
192 } catch ( Exception
$e ) {
193 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
199 * Update a single message in all message blobs it occurs in.
201 * @param string $key Message key
203 public function updateMessage( $key ) {
205 $dbw = wfGetDB( DB_MASTER
);
207 // Keep running until the updates queue is empty.
208 // Due to update conflicts, the queue might not be emptied
212 $updates = $this->getUpdatesForMessage( $key, $updates );
214 foreach ( $updates as $k => $update ) {
215 // Update the row on the condition that it
216 // didn't change since we fetched it by putting
217 // the timestamp in the WHERE clause.
218 $success = $dbw->update( 'msg_resource',
220 'mr_blob' => $update['newBlob'],
221 'mr_timestamp' => $dbw->timestamp() ),
223 'mr_resource' => $update['resource'],
224 'mr_lang' => $update['lang'],
225 'mr_timestamp' => $update['timestamp'] ),
229 // Only requeue conflicted updates.
230 // If update() returned false, don't retry, for
231 // fear of getting into an infinite loop
232 if ( !( $success && $dbw->affectedRows() == 0 ) ) {
234 unset( $updates[$k] );
237 } while ( count( $updates ) );
239 } catch ( Exception
$e ) {
240 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
244 public function clear() {
246 // Not using TRUNCATE, because that needs extra permissions,
247 // which maybe not granted to the database user.
248 $dbw = wfGetDB( DB_MASTER
);
249 $dbw->delete( 'msg_resource', '*', __METHOD__
);
250 } catch ( Exception
$e ) {
251 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
256 * @return ResourceLoader
258 protected function getResourceLoader() {
259 // For back-compat this class supports instantiation without passing ResourceLoader
260 // Lazy-initialise this property because most callers don't need it.
261 if ( $this->resourceloader
=== null ) {
262 wfDebug( __CLASS__
. ' created without a ResourceLoader instance' );
263 $this->resourceloader
= new ResourceLoader();
266 return $this->resourceloader
;
270 * Create an update queue for updateMessage()
272 * @param string $key Message key
273 * @param array $prevUpdates Updates queue to refresh or null to build a fresh update queue
274 * @return array Updates queue
276 private function getUpdatesForMessage( $key, $prevUpdates = null ) {
277 $dbw = wfGetDB( DB_MASTER
);
279 if ( is_null( $prevUpdates ) ) {
280 $rl = $this->getResourceLoader();
281 $moduleNames = $rl->getModulesByMessage( $key );
282 // Fetch all blobs referencing $key
284 array( 'msg_resource' ),
285 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
287 'mr_resource' => $moduleNames,
292 // Refetch the blobs referenced by $prevUpdates
294 // Reorganize the (resource, lang) pairs in the format
295 // expected by makeWhereFrom2d()
298 foreach ( $prevUpdates as $update ) {
299 $twoD[$update['resource']][$update['lang']] = true;
302 $res = $dbw->select( 'msg_resource',
303 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
304 $dbw->makeWhereFrom2d( $twoD, 'mr_resource', 'mr_lang' ),
309 // Build the new updates queue
312 foreach ( $res as $row ) {
314 'resource' => $row->mr_resource
,
315 'lang' => $row->mr_lang
,
316 'timestamp' => $row->mr_timestamp
,
317 'newBlob' => $this->reencodeBlob( $row->mr_blob
, $key, $row->mr_lang
)
325 * Reencode a message blob with the updated value for a message
327 * @param string $blob Message blob (JSON object)
328 * @param string $key Message key
329 * @param string $lang Language code
330 * @return string Message blob with $key replaced with its new value
332 private function reencodeBlob( $blob, $key, $lang ) {
333 $decoded = FormatJson
::decode( $blob, true );
334 $decoded[$key] = wfMessage( $key )->inLanguage( $lang )->plain();
336 return FormatJson
::encode( (object)$decoded );
340 * Get the message blobs for a set of modules from the database.
341 * Modules whose blobs are not in the database are silently dropped.
343 * @param ResourceLoader $resourceLoader
344 * @param array $modules Array of module names
345 * @param string $lang Language code
346 * @throws MWException
347 * @return array Array mapping module names to blobs
349 private function getFromDB( ResourceLoader
$resourceLoader, $modules, $lang ) {
350 if ( !count( $modules ) ) {
355 $dbr = wfGetDB( DB_SLAVE
);
356 $res = $dbr->select( 'msg_resource',
357 array( 'mr_blob', 'mr_resource', 'mr_timestamp' ),
358 array( 'mr_resource' => $modules, 'mr_lang' => $lang ),
362 foreach ( $res as $row ) {
363 $module = $resourceLoader->getModule( $row->mr_resource
);
365 // This shouldn't be possible
366 throw new MWException( __METHOD__
. ' passed an invalid module name' );
369 // Update the module's blob if the list of messages changed
370 $blobKeys = array_keys( FormatJson
::decode( $row->mr_blob
, true ) );
371 $moduleMsgs = array_values( array_unique( $module->getMessages() ) );
372 if ( $blobKeys !== $moduleMsgs ) {
373 $retval[$row->mr_resource
] = $this->updateModule( $row->mr_resource
, $module, $lang );
375 $retval[$row->mr_resource
] = $row->mr_blob
;
383 * Generate the message blob for a given module in a given language.
385 * @param ResourceLoaderModule $module
386 * @param string $lang Language code
387 * @return string JSON object
389 private function generateMessageBlob( ResourceLoaderModule
$module, $lang ) {
392 foreach ( $module->getMessages() as $key ) {
393 $messages[$key] = wfMessage( $key )->inLanguage( $lang )->plain();
396 return FormatJson
::encode( (object)$messages );