3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @author Roan Kattouw
19 * @author Trevor Parscal
23 * This class provides access to the resource message blobs storage used by
26 * A message blob is a JSON object containing the interface messages for a
27 * certain resource in a certain language. These message blobs are cached
28 * in the msg_resource table and automatically invalidated when one of their
29 * consistuent messages or the resource itself is changed.
31 class MessageBlobStore
{
34 * Get the message blobs for a set of modules
36 * @param $resourceLoader ResourceLoader object
37 * @param $modules array Array of module objects keyed by module name
38 * @param $lang string Language code
39 * @return array An array mapping module names to message blobs
41 public static function get( ResourceLoader
$resourceLoader, $modules, $lang ) {
42 wfProfileIn( __METHOD__
);
43 if ( !count( $modules ) ) {
44 wfProfileOut( __METHOD__
);
47 // Try getting from the DB first
48 $blobs = self
::getFromDB( $resourceLoader, array_keys( $modules ), $lang );
50 // Generate blobs for any missing modules and store them in the DB
51 $missing = array_diff( array_keys( $modules ), array_keys( $blobs ) );
52 foreach ( $missing as $name ) {
53 $blob = self
::insertMessageBlob( $name, $modules[$name], $lang );
55 $blobs[$name] = $blob;
59 wfProfileOut( __METHOD__
);
64 * Generate and insert a new message blob. If the blob was already
65 * present, it is not regenerated; instead, the preexisting blob
66 * is fetched and returned.
68 * @param $name String: module name
69 * @param $module ResourceLoaderModule object
70 * @param $lang String: language code
71 * @return mixed Message blob or false if the module has no messages
73 public static function insertMessageBlob( $name, ResourceLoaderModule
$module, $lang ) {
74 $blob = self
::generateMessageBlob( $module, $lang );
80 $dbw = wfGetDB( DB_MASTER
);
81 $success = $dbw->insert( 'msg_resource', array(
83 'mr_resource' => $name,
85 'mr_timestamp' => $dbw->timestamp()
92 if ( $dbw->affectedRows() == 0 ) {
93 // Blob was already present, fetch it
94 $blob = $dbw->selectField( 'msg_resource', 'mr_blob', array(
95 'mr_resource' => $name,
101 // Update msg_resource_links
104 foreach ( $module->getMessages() as $key ) {
106 'mrl_resource' => $name,
107 'mrl_message' => $key
110 $dbw->insert( 'msg_resource_links', $rows,
111 __METHOD__
, array( 'IGNORE' )
120 * Update all message blobs for a given module.
122 * @param $name String: module name
123 * @param $module ResourceLoaderModule object
124 * @param $lang String: language code (optional)
125 * @return Mixed: if $lang is set, the new message blob for that language is
126 * returned if present. Otherwise, null is returned.
128 public static function updateModule( $name, ResourceLoaderModule
$module, $lang = null ) {
131 // Find all existing blobs for this module
132 $dbw = wfGetDB( DB_MASTER
);
133 $res = $dbw->select( 'msg_resource',
134 array( 'mr_lang', 'mr_blob' ),
135 array( 'mr_resource' => $name ),
139 // Build the new msg_resource rows
141 $now = $dbw->timestamp();
142 // Save the last-processed old and new blobs for later
143 $oldBlob = $newBlob = null;
145 foreach ( $res as $row ) {
146 $oldBlob = $row->mr_blob
;
147 $newBlob = self
::generateMessageBlob( $module, $row->mr_lang
);
149 if ( $row->mr_lang
=== $lang ) {
153 'mr_resource' => $name,
154 'mr_lang' => $row->mr_lang
,
155 'mr_blob' => $newBlob,
156 'mr_timestamp' => $now
160 $dbw->replace( 'msg_resource',
161 array( array( 'mr_resource', 'mr_lang' ) ),
165 // Figure out which messages were added and removed
166 $oldMessages = array_keys( FormatJson
::decode( $oldBlob, true ) );
167 $newMessages = array_keys( FormatJson
::decode( $newBlob, true ) );
168 $added = array_diff( $newMessages, $oldMessages );
169 $removed = array_diff( $oldMessages, $newMessages );
171 // Delete removed messages, insert added ones
173 $dbw->delete( 'msg_resource_links', array(
174 'mrl_resource' => $name,
175 'mrl_message' => $removed
180 $newLinksRows = array();
182 foreach ( $added as $message ) {
183 $newLinksRows[] = array(
184 'mrl_resource' => $name,
185 'mrl_message' => $message
189 if ( $newLinksRows ) {
190 $dbw->insert( 'msg_resource_links', $newLinksRows, __METHOD__
,
191 array( 'IGNORE' ) // just in case
199 * Update a single message in all message blobs it occurs in.
201 * @param $key String: message key
203 public static function updateMessage( $key ) {
204 $dbw = wfGetDB( DB_MASTER
);
206 // Keep running until the updates queue is empty.
207 // Due to update conflicts, the queue might not be emptied
211 $updates = self
::getUpdatesForMessage( $key, $updates );
213 foreach ( $updates as $key => $update ) {
214 // Update the row on the condition that it
215 // didn't change since we fetched it by putting
216 // the timestamp in the WHERE clause.
217 $success = $dbw->update( 'msg_resource',
219 'mr_blob' => $update['newBlob'],
220 'mr_timestamp' => $dbw->timestamp() ),
222 'mr_resource' => $update['resource'],
223 'mr_lang' => $update['lang'],
224 'mr_timestamp' => $update['timestamp'] ),
228 // Only requeue conflicted updates.
229 // If update() returned false, don't retry, for
230 // fear of getting into an infinite loop
231 if ( !( $success && $dbw->affectedRows() == 0 ) ) {
233 unset( $updates[$key] );
236 } while ( count( $updates ) );
238 // No need to update msg_resource_links because we didn't add
239 // or remove any messages, we just changed their contents.
242 public static function clear() {
243 // TODO: Give this some more thought
244 // TODO: Is TRUNCATE better?
245 $dbw = wfGetDB( DB_MASTER
);
246 $dbw->delete( 'msg_resource', '*', __METHOD__
);
247 $dbw->delete( 'msg_resource_links', '*', __METHOD__
);
251 * Create an update queue for updateMessage()
253 * @param $key String: message key
254 * @param $prevUpdates Array: updates queue to refresh or null to build a fresh update queue
255 * @return Array: updates queue
257 private static function getUpdatesForMessage( $key, $prevUpdates = null ) {
258 $dbw = wfGetDB( DB_MASTER
);
260 if ( is_null( $prevUpdates ) ) {
261 // Fetch all blobs referencing $key
263 array( 'msg_resource', 'msg_resource_links' ),
264 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
265 array( 'mrl_message' => $key, 'mr_resource=mrl_resource' ),
269 // Refetch the blobs referenced by $prevUpdates
271 // Reorganize the (resource, lang) pairs in the format
272 // expected by makeWhereFrom2d()
275 foreach ( $prevUpdates as $update ) {
276 $twoD[$update['resource']][$update['lang']] = true;
279 $res = $dbw->select( 'msg_resource',
280 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
281 $dbw->makeWhereFrom2d( $twoD, 'mr_resource', 'mr_lang' ),
286 // Build the new updates queue
289 foreach ( $res as $row ) {
291 'resource' => $row->mr_resource
,
292 'lang' => $row->mr_lang
,
293 'timestamp' => $row->mr_timestamp
,
294 'newBlob' => self
::reencodeBlob( $row->mr_blob
, $key, $row->mr_lang
)
302 * Reencode a message blob with the updated value for a message
304 * @param $blob String: message blob (JSON object)
305 * @param $key String: message key
306 * @param $lang String: language code
307 * @return Message blob with $key replaced with its new value
309 private static function reencodeBlob( $blob, $key, $lang ) {
310 $decoded = FormatJson
::decode( $blob, true );
311 $decoded[$key] = wfMsgExt( $key, array( 'language' => $lang ) );
313 return FormatJson
::encode( $decoded );
317 * Get the message blobs for a set of modules from the database.
318 * Modules whose blobs are not in the database are silently dropped.
320 * @param $resourceLoader ResourceLoader object
321 * @param $modules Array of module names
322 * @param $lang String: language code
323 * @return array Array mapping module names to blobs
325 private static function getFromDB( ResourceLoader
$resourceLoader, $modules, $lang ) {
326 global $wgCacheEpoch;
328 $dbr = wfGetDB( DB_SLAVE
);
329 $res = $dbr->select( 'msg_resource',
330 array( 'mr_blob', 'mr_resource', 'mr_timestamp' ),
331 array( 'mr_resource' => $modules, 'mr_lang' => $lang ),
335 foreach ( $res as $row ) {
336 $module = $resourceLoader->getModule( $row->mr_resource
);
338 // This shouldn't be possible
339 throw new MWException( __METHOD__
. ' passed an invalid module name' );
341 // Update the module's blobs if the set of messages changed or if the blob is
342 // older than $wgCacheEpoch
343 if ( array_keys( FormatJson
::decode( $row->mr_blob
, true ) ) !== $module->getMessages() ||
344 wfTimestamp( TS_MW
, $row->mr_timestamp
) <= $wgCacheEpoch ) {
345 $retval[$row->mr_resource
] = self
::updateModule( $row->mr_resource
, $module, $lang );
347 $retval[$row->mr_resource
] = $row->mr_blob
;
355 * Generate the message blob for a given module in a given language.
357 * @param $module ResourceLoaderModule object
358 * @param $lang String: language code
359 * @return String: JSON object
361 private static function generateMessageBlob( ResourceLoaderModule
$module, $lang ) {
364 foreach ( $module->getMessages() as $key ) {
365 $messages[$key] = wfMsgExt( $key, array( 'language' => $lang ) );
368 return FormatJson
::encode( $messages );