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 * Get the singleton instance
39 * @deprecated since 1.25
40 * @return MessageBlobStore
42 public static function getInstance() {
43 wfDeprecated( __METHOD__
, '1.25' );
48 * Get the message blobs for a set of modules
50 * @param ResourceLoader $resourceLoader
51 * @param array $modules Array of module objects keyed by module name
52 * @param string $lang Language code
53 * @return array An array mapping module names to message blobs
55 public function get( ResourceLoader
$resourceLoader, $modules, $lang ) {
56 if ( !count( $modules ) ) {
59 // Try getting from the DB first
60 $blobs = $this->getFromDB( $resourceLoader, array_keys( $modules ), $lang );
62 // Generate blobs for any missing modules and store them in the DB
63 $missing = array_diff( array_keys( $modules ), array_keys( $blobs ) );
64 foreach ( $missing as $name ) {
65 $blob = $this->insertMessageBlob( $name, $modules[$name], $lang );
67 $blobs[$name] = $blob;
75 * Generate and insert a new message blob. If the blob was already
76 * present, it is not regenerated; instead, the preexisting blob
77 * is fetched and returned.
79 * @param string $name Module name
80 * @param ResourceLoaderModule $module
81 * @param string $lang Language code
82 * @return mixed Message blob or false if the module has no messages
84 public function insertMessageBlob( $name, ResourceLoaderModule
$module, $lang ) {
85 $blob = $this->generateMessageBlob( $module, $lang );
92 $dbw = wfGetDB( DB_MASTER
);
93 $success = $dbw->insert( 'msg_resource', array(
95 'mr_resource' => $name,
97 'mr_timestamp' => $dbw->timestamp()
104 if ( $dbw->affectedRows() == 0 ) {
105 // Blob was already present, fetch it
106 $blob = $dbw->selectField( 'msg_resource', 'mr_blob', array(
107 'mr_resource' => $name,
113 // Update msg_resource_links
116 foreach ( $module->getMessages() as $key ) {
118 'mrl_resource' => $name,
119 'mrl_message' => $key
122 $dbw->insert( 'msg_resource_links', $rows,
123 __METHOD__
, array( 'IGNORE' )
127 } catch ( DBError
$e ) {
128 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
134 * Update the message blob for a given module in a given language
136 * @param string $name Module name
137 * @param ResourceLoaderModule $module
138 * @param string $lang Language code
139 * @return string Regenerated message blob, or null if there was no blob for
140 * the given module/language pair.
142 public function updateModule( $name, ResourceLoaderModule
$module, $lang ) {
143 $dbw = wfGetDB( DB_MASTER
);
144 $row = $dbw->selectRow( 'msg_resource', 'mr_blob',
145 array( 'mr_resource' => $name, 'mr_lang' => $lang ),
152 // Save the old and new blobs for later
153 $oldBlob = $row->mr_blob
;
154 $newBlob = $this->generateMessageBlob( $module, $lang );
158 'mr_resource' => $name,
160 'mr_blob' => $newBlob,
161 'mr_timestamp' => $dbw->timestamp()
164 $dbw->replace( 'msg_resource',
165 array( array( 'mr_resource', 'mr_lang' ) ),
169 // Figure out which messages were added and removed
170 $oldMessages = array_keys( FormatJson
::decode( $oldBlob, true ) );
171 $newMessages = array_keys( FormatJson
::decode( $newBlob, true ) );
172 $added = array_diff( $newMessages, $oldMessages );
173 $removed = array_diff( $oldMessages, $newMessages );
175 // Delete removed messages, insert added ones
177 $dbw->delete( 'msg_resource_links', array(
178 'mrl_resource' => $name,
179 'mrl_message' => $removed
184 $newLinksRows = array();
186 foreach ( $added as $message ) {
187 $newLinksRows[] = array(
188 'mrl_resource' => $name,
189 'mrl_message' => $message
193 if ( $newLinksRows ) {
194 $dbw->insert( 'msg_resource_links', $newLinksRows, __METHOD__
,
195 array( 'IGNORE' ) // just in case
198 } catch ( Exception
$e ) {
199 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
205 * Update a single message in all message blobs it occurs in.
207 * @param string $key Message key
209 public function updateMessage( $key ) {
211 $dbw = wfGetDB( DB_MASTER
);
213 // Keep running until the updates queue is empty.
214 // Due to update conflicts, the queue might not be emptied
218 $updates = $this->getUpdatesForMessage( $key, $updates );
220 foreach ( $updates as $k => $update ) {
221 // Update the row on the condition that it
222 // didn't change since we fetched it by putting
223 // the timestamp in the WHERE clause.
224 $success = $dbw->update( 'msg_resource',
226 'mr_blob' => $update['newBlob'],
227 'mr_timestamp' => $dbw->timestamp() ),
229 'mr_resource' => $update['resource'],
230 'mr_lang' => $update['lang'],
231 'mr_timestamp' => $update['timestamp'] ),
235 // Only requeue conflicted updates.
236 // If update() returned false, don't retry, for
237 // fear of getting into an infinite loop
238 if ( !( $success && $dbw->affectedRows() == 0 ) ) {
240 unset( $updates[$k] );
243 } while ( count( $updates ) );
245 // No need to update msg_resource_links because we didn't add
246 // or remove any messages, we just changed their contents.
247 } catch ( Exception
$e ) {
248 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
252 public function clear() {
253 // TODO: Give this some more thought
255 // Not using TRUNCATE, because that needs extra permissions,
256 // which maybe not granted to the database user.
257 $dbw = wfGetDB( DB_MASTER
);
258 $dbw->delete( 'msg_resource', '*', __METHOD__
);
259 $dbw->delete( 'msg_resource_links', '*', __METHOD__
);
260 } catch ( Exception
$e ) {
261 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
266 * Create an update queue for updateMessage()
268 * @param string $key Message key
269 * @param array $prevUpdates Updates queue to refresh or null to build a fresh update queue
270 * @return array Updates queue
272 private function getUpdatesForMessage( $key, $prevUpdates = null ) {
273 $dbw = wfGetDB( DB_MASTER
);
275 if ( is_null( $prevUpdates ) ) {
276 // Fetch all blobs referencing $key
278 array( 'msg_resource', 'msg_resource_links' ),
279 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
280 array( 'mrl_message' => $key, 'mr_resource=mrl_resource' ),
284 // Refetch the blobs referenced by $prevUpdates
286 // Reorganize the (resource, lang) pairs in the format
287 // expected by makeWhereFrom2d()
290 foreach ( $prevUpdates as $update ) {
291 $twoD[$update['resource']][$update['lang']] = true;
294 $res = $dbw->select( 'msg_resource',
295 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
296 $dbw->makeWhereFrom2d( $twoD, 'mr_resource', 'mr_lang' ),
301 // Build the new updates queue
304 foreach ( $res as $row ) {
306 'resource' => $row->mr_resource
,
307 'lang' => $row->mr_lang
,
308 'timestamp' => $row->mr_timestamp
,
309 'newBlob' => $this->reencodeBlob( $row->mr_blob
, $key, $row->mr_lang
)
317 * Reencode a message blob with the updated value for a message
319 * @param string $blob Message blob (JSON object)
320 * @param string $key Message key
321 * @param string $lang Language code
322 * @return string Message blob with $key replaced with its new value
324 private function reencodeBlob( $blob, $key, $lang ) {
325 $decoded = FormatJson
::decode( $blob, true );
326 $decoded[$key] = wfMessage( $key )->inLanguage( $lang )->plain();
328 return FormatJson
::encode( (object)$decoded );
332 * Get the message blobs for a set of modules from the database.
333 * Modules whose blobs are not in the database are silently dropped.
335 * @param ResourceLoader $resourceLoader
336 * @param array $modules Array of module names
337 * @param string $lang Language code
338 * @throws MWException
339 * @return array Array mapping module names to blobs
341 private function getFromDB( ResourceLoader
$resourceLoader, $modules, $lang ) {
342 $config = $resourceLoader->getConfig();
344 $dbr = wfGetDB( DB_SLAVE
);
345 $res = $dbr->select( 'msg_resource',
346 array( 'mr_blob', 'mr_resource', 'mr_timestamp' ),
347 array( 'mr_resource' => $modules, 'mr_lang' => $lang ),
351 foreach ( $res as $row ) {
352 $module = $resourceLoader->getModule( $row->mr_resource
);
354 // This shouldn't be possible
355 throw new MWException( __METHOD__
. ' passed an invalid module name' );
358 // Update the module's blobs if the set of messages changed or if the blob is
359 // older than the CacheEpoch setting
360 $keys = array_keys( FormatJson
::decode( $row->mr_blob
, true ) );
361 $values = array_values( array_unique( $module->getMessages() ) );
362 if ( $keys !== $values
363 ||
wfTimestamp( TS_MW
, $row->mr_timestamp
) <= $config->get( 'CacheEpoch' )
365 $retval[$row->mr_resource
] = $this->updateModule( $row->mr_resource
, $module, $lang );
367 $retval[$row->mr_resource
] = $row->mr_blob
;
375 * Generate the message blob for a given module in a given language.
377 * @param ResourceLoaderModule $module
378 * @param string $lang Language code
379 * @return string JSON object
381 private function generateMessageBlob( ResourceLoaderModule
$module, $lang ) {
384 foreach ( $module->getMessages() as $key ) {
385 $messages[$key] = wfMessage( $key )->inLanguage( $lang )->plain();
388 return FormatJson
::encode( (object)$messages );