3 * Resource message blobs storage used by the resource loader.
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 by
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();
45 * Get the singleton instance
48 * @deprecated since 1.25
49 * @return MessageBlobStore
51 public static function getInstance() {
52 wfDeprecated( __METHOD__
, '1.25' );
57 * Get the message blobs for a set of modules
59 * @param ResourceLoader $resourceLoader
60 * @param array $modules Array of module objects keyed by module name
61 * @param string $lang Language code
62 * @return array An array mapping module names to message blobs
64 public function get( ResourceLoader
$resourceLoader, $modules, $lang ) {
65 if ( !count( $modules ) ) {
71 // Try in-process cache
72 $missingFromCache = array();
73 foreach ( $modules as $name => $module ) {
74 if ( isset( $this->blobCache
[$lang][$name] ) ) {
75 $blobs[$name] = $this->blobCache
[$lang][$name];
77 $missingFromCache[] = $name;
82 if ( $missingFromCache ) {
83 $blobs +
= $this->getFromDB( $resourceLoader, $missingFromCache, $lang );
86 // Generate new blobs for any remaining modules and store in DB
87 $missingFromDb = array_diff( array_keys( $modules ), array_keys( $blobs ) );
88 foreach ( $missingFromDb as $name ) {
89 $blob = $this->insertMessageBlob( $name, $modules[$name], $lang );
91 $blobs[$name] = $blob;
95 // Update in-process cache
96 if ( isset( $this->blobCache
[$lang] ) ) {
97 $this->blobCache
[$lang] +
= $blobs;
99 $this->blobCache
[$lang] = $blobs;
106 * Generate and insert a new message blob. If the blob was already
107 * present, it is not regenerated; instead, the preexisting blob
108 * is fetched and returned.
110 * @param string $name Module name
111 * @param ResourceLoaderModule $module
112 * @param string $lang Language code
113 * @return mixed Message blob or false if the module has no messages
115 public function insertMessageBlob( $name, ResourceLoaderModule
$module, $lang ) {
116 $blob = $this->generateMessageBlob( $module, $lang );
123 $dbw = wfGetDB( DB_MASTER
);
124 $success = $dbw->insert( 'msg_resource', array(
126 'mr_resource' => $name,
128 'mr_timestamp' => $dbw->timestamp()
135 if ( $dbw->affectedRows() == 0 ) {
136 // Blob was already present, fetch it
137 $blob = $dbw->selectField( 'msg_resource', 'mr_blob', array(
138 'mr_resource' => $name,
144 // Update msg_resource_links
147 foreach ( $module->getMessages() as $key ) {
149 'mrl_resource' => $name,
150 'mrl_message' => $key
153 $dbw->insert( 'msg_resource_links', $rows,
154 __METHOD__
, array( 'IGNORE' )
158 } catch ( DBError
$e ) {
159 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
165 * Update the message blob for a given module in a given language
167 * @param string $name Module name
168 * @param ResourceLoaderModule $module
169 * @param string $lang Language code
170 * @return string Regenerated message blob, or null if there was no blob for
171 * the given module/language pair.
173 public function updateModule( $name, ResourceLoaderModule
$module, $lang ) {
174 $dbw = wfGetDB( DB_MASTER
);
175 $row = $dbw->selectRow( 'msg_resource', 'mr_blob',
176 array( 'mr_resource' => $name, 'mr_lang' => $lang ),
183 // Save the old and new blobs for later
184 $oldBlob = $row->mr_blob
;
185 $newBlob = $this->generateMessageBlob( $module, $lang );
189 'mr_resource' => $name,
191 'mr_blob' => $newBlob,
192 'mr_timestamp' => $dbw->timestamp()
195 $dbw->replace( 'msg_resource',
196 array( array( 'mr_resource', 'mr_lang' ) ),
200 // Figure out which messages were added and removed
201 $oldMessages = array_keys( FormatJson
::decode( $oldBlob, true ) );
202 $newMessages = array_keys( FormatJson
::decode( $newBlob, true ) );
203 $added = array_diff( $newMessages, $oldMessages );
204 $removed = array_diff( $oldMessages, $newMessages );
206 // Delete removed messages, insert added ones
208 $dbw->delete( 'msg_resource_links', array(
209 'mrl_resource' => $name,
210 'mrl_message' => $removed
215 $newLinksRows = array();
217 foreach ( $added as $message ) {
218 $newLinksRows[] = array(
219 'mrl_resource' => $name,
220 'mrl_message' => $message
224 if ( $newLinksRows ) {
225 $dbw->insert( 'msg_resource_links', $newLinksRows, __METHOD__
,
226 array( 'IGNORE' ) // just in case
229 } catch ( Exception
$e ) {
230 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
236 * Update a single message in all message blobs it occurs in.
238 * @param string $key Message key
240 public function updateMessage( $key ) {
242 $dbw = wfGetDB( DB_MASTER
);
244 // Keep running until the updates queue is empty.
245 // Due to update conflicts, the queue might not be emptied
249 $updates = $this->getUpdatesForMessage( $key, $updates );
251 foreach ( $updates as $k => $update ) {
252 // Update the row on the condition that it
253 // didn't change since we fetched it by putting
254 // the timestamp in the WHERE clause.
255 $success = $dbw->update( 'msg_resource',
257 'mr_blob' => $update['newBlob'],
258 'mr_timestamp' => $dbw->timestamp() ),
260 'mr_resource' => $update['resource'],
261 'mr_lang' => $update['lang'],
262 'mr_timestamp' => $update['timestamp'] ),
266 // Only requeue conflicted updates.
267 // If update() returned false, don't retry, for
268 // fear of getting into an infinite loop
269 if ( !( $success && $dbw->affectedRows() == 0 ) ) {
271 unset( $updates[$k] );
274 } while ( count( $updates ) );
276 // No need to update msg_resource_links because we didn't add
277 // or remove any messages, we just changed their contents.
278 } catch ( Exception
$e ) {
279 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
283 public function clear() {
284 // TODO: Give this some more thought
286 // Not using TRUNCATE, because that needs extra permissions,
287 // which maybe not granted to the database user.
288 $dbw = wfGetDB( DB_MASTER
);
289 $dbw->delete( 'msg_resource', '*', __METHOD__
);
290 $dbw->delete( 'msg_resource_links', '*', __METHOD__
);
291 } catch ( Exception
$e ) {
292 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
297 * Create an update queue for updateMessage()
299 * @param string $key Message key
300 * @param array $prevUpdates Updates queue to refresh or null to build a fresh update queue
301 * @return array Updates queue
303 private function getUpdatesForMessage( $key, $prevUpdates = null ) {
304 $dbw = wfGetDB( DB_MASTER
);
306 if ( is_null( $prevUpdates ) ) {
307 // Fetch all blobs referencing $key
309 array( 'msg_resource', 'msg_resource_links' ),
310 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
311 array( 'mrl_message' => $key, 'mr_resource=mrl_resource' ),
315 // Refetch the blobs referenced by $prevUpdates
317 // Reorganize the (resource, lang) pairs in the format
318 // expected by makeWhereFrom2d()
321 foreach ( $prevUpdates as $update ) {
322 $twoD[$update['resource']][$update['lang']] = true;
325 $res = $dbw->select( 'msg_resource',
326 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
327 $dbw->makeWhereFrom2d( $twoD, 'mr_resource', 'mr_lang' ),
332 // Build the new updates queue
335 foreach ( $res as $row ) {
337 'resource' => $row->mr_resource
,
338 'lang' => $row->mr_lang
,
339 'timestamp' => $row->mr_timestamp
,
340 'newBlob' => $this->reencodeBlob( $row->mr_blob
, $key, $row->mr_lang
)
348 * Reencode a message blob with the updated value for a message
350 * @param string $blob Message blob (JSON object)
351 * @param string $key Message key
352 * @param string $lang Language code
353 * @return string Message blob with $key replaced with its new value
355 private function reencodeBlob( $blob, $key, $lang ) {
356 $decoded = FormatJson
::decode( $blob, true );
357 $decoded[$key] = wfMessage( $key )->inLanguage( $lang )->plain();
359 return FormatJson
::encode( (object)$decoded );
363 * Get the message blobs for a set of modules from the database.
364 * Modules whose blobs are not in the database are silently dropped.
366 * @param ResourceLoader $resourceLoader
367 * @param array $modules Array of module names
368 * @param string $lang Language code
369 * @throws MWException
370 * @return array Array mapping module names to blobs
372 private function getFromDB( ResourceLoader
$resourceLoader, $modules, $lang ) {
373 if ( !count( $modules ) ) {
377 $config = $resourceLoader->getConfig();
379 $dbr = wfGetDB( DB_SLAVE
);
380 $res = $dbr->select( 'msg_resource',
381 array( 'mr_blob', 'mr_resource', 'mr_timestamp' ),
382 array( 'mr_resource' => $modules, 'mr_lang' => $lang ),
386 foreach ( $res as $row ) {
387 $module = $resourceLoader->getModule( $row->mr_resource
);
389 // This shouldn't be possible
390 throw new MWException( __METHOD__
. ' passed an invalid module name' );
393 // Update the module's blobs if the set of messages changed or if the blob is
394 // older than the CacheEpoch setting
395 $keys = array_keys( FormatJson
::decode( $row->mr_blob
, true ) );
396 $values = array_values( array_unique( $module->getMessages() ) );
397 if ( $keys !== $values
398 ||
wfTimestamp( TS_MW
, $row->mr_timestamp
) <= $config->get( 'CacheEpoch' )
400 $retval[$row->mr_resource
] = $this->updateModule( $row->mr_resource
, $module, $lang );
402 $retval[$row->mr_resource
] = $row->mr_blob
;
410 * Generate the message blob for a given module in a given language.
412 * @param ResourceLoaderModule $module
413 * @param string $lang Language code
414 * @return string JSON object
416 private function generateMessageBlob( ResourceLoaderModule
$module, $lang ) {
419 foreach ( $module->getMessages() as $key ) {
420 $messages[$key] = wfMessage( $key )->inLanguage( $lang )->plain();
423 return FormatJson
::encode( (object)$messages );