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 * consistuent messages or the resource itself is changed.
34 class MessageBlobStore
{
37 * Get the message blobs for a set of modules
39 * @param $resourceLoader ResourceLoader object
40 * @param $modules array Array of module objects keyed by module name
41 * @param $lang string Language code
42 * @return array An array mapping module names to message blobs
44 public static function get( ResourceLoader
$resourceLoader, $modules, $lang ) {
45 wfProfileIn( __METHOD__
);
46 if ( !count( $modules ) ) {
47 wfProfileOut( __METHOD__
);
50 // Try getting from the DB first
51 $blobs = self
::getFromDB( $resourceLoader, array_keys( $modules ), $lang );
53 // Generate blobs for any missing modules and store them in the DB
54 $missing = array_diff( array_keys( $modules ), array_keys( $blobs ) );
55 foreach ( $missing as $name ) {
56 $blob = self
::insertMessageBlob( $name, $modules[$name], $lang );
58 $blobs[$name] = $blob;
62 wfProfileOut( __METHOD__
);
67 * Generate and insert a new message blob. If the blob was already
68 * present, it is not regenerated; instead, the preexisting blob
69 * is fetched and returned.
71 * @param $name String: module name
72 * @param $module ResourceLoaderModule object
73 * @param $lang String: language code
74 * @return mixed Message blob or false if the module has no messages
76 public static function insertMessageBlob( $name, ResourceLoaderModule
$module, $lang ) {
77 $blob = self
::generateMessageBlob( $module, $lang );
83 $dbw = wfGetDB( DB_MASTER
);
84 $success = $dbw->insert( 'msg_resource', array(
86 'mr_resource' => $name,
88 'mr_timestamp' => $dbw->timestamp()
95 if ( $dbw->affectedRows() == 0 ) {
96 // Blob was already present, fetch it
97 $blob = $dbw->selectField( 'msg_resource', 'mr_blob', array(
98 'mr_resource' => $name,
104 // Update msg_resource_links
107 foreach ( $module->getMessages() as $key ) {
109 'mrl_resource' => $name,
110 'mrl_message' => $key
113 $dbw->insert( 'msg_resource_links', $rows,
114 __METHOD__
, array( 'IGNORE' )
123 * Update the message blob for a given module in a given language
125 * @param $name String: module name
126 * @param $module ResourceLoaderModule object
127 * @param $lang String: language code
128 * @return String Regenerated message blob, or null if there was no blob for the given module/language pair
130 public static function updateModule( $name, ResourceLoaderModule
$module, $lang ) {
131 $dbw = wfGetDB( DB_MASTER
);
132 $row = $dbw->selectRow( 'msg_resource', 'mr_blob',
133 array( 'mr_resource' => $name, 'mr_lang' => $lang ),
140 // Save the old and new blobs for later
141 $oldBlob = $row->mr_blob
;
142 $newBlob = self
::generateMessageBlob( $module, $lang );
145 'mr_resource' => $name,
147 'mr_blob' => $newBlob,
148 'mr_timestamp' => $dbw->timestamp()
151 $dbw->replace( 'msg_resource',
152 array( array( 'mr_resource', 'mr_lang' ) ),
156 // Figure out which messages were added and removed
157 $oldMessages = array_keys( FormatJson
::decode( $oldBlob, true ) );
158 $newMessages = array_keys( FormatJson
::decode( $newBlob, true ) );
159 $added = array_diff( $newMessages, $oldMessages );
160 $removed = array_diff( $oldMessages, $newMessages );
162 // Delete removed messages, insert added ones
164 $dbw->delete( 'msg_resource_links', array(
165 'mrl_resource' => $name,
166 'mrl_message' => $removed
171 $newLinksRows = array();
173 foreach ( $added as $message ) {
174 $newLinksRows[] = array(
175 'mrl_resource' => $name,
176 'mrl_message' => $message
180 if ( $newLinksRows ) {
181 $dbw->insert( 'msg_resource_links', $newLinksRows, __METHOD__
,
182 array( 'IGNORE' ) // just in case
190 * Update a single message in all message blobs it occurs in.
192 * @param $key String: message key
194 public static function updateMessage( $key ) {
195 $dbw = wfGetDB( DB_MASTER
);
197 // Keep running until the updates queue is empty.
198 // Due to update conflicts, the queue might not be emptied
202 $updates = self
::getUpdatesForMessage( $key, $updates );
204 foreach ( $updates as $k => $update ) {
205 // Update the row on the condition that it
206 // didn't change since we fetched it by putting
207 // the timestamp in the WHERE clause.
208 $success = $dbw->update( 'msg_resource',
210 'mr_blob' => $update['newBlob'],
211 'mr_timestamp' => $dbw->timestamp() ),
213 'mr_resource' => $update['resource'],
214 'mr_lang' => $update['lang'],
215 'mr_timestamp' => $update['timestamp'] ),
219 // Only requeue conflicted updates.
220 // If update() returned false, don't retry, for
221 // fear of getting into an infinite loop
222 if ( !( $success && $dbw->affectedRows() == 0 ) ) {
224 unset( $updates[$k] );
227 } while ( count( $updates ) );
229 // No need to update msg_resource_links because we didn't add
230 // or remove any messages, we just changed their contents.
233 public static function clear() {
234 // TODO: Give this some more thought
235 // TODO: Is TRUNCATE better?
236 $dbw = wfGetDB( DB_MASTER
);
237 $dbw->delete( 'msg_resource', '*', __METHOD__
);
238 $dbw->delete( 'msg_resource_links', '*', __METHOD__
);
242 * Create an update queue for updateMessage()
244 * @param $key String: message key
245 * @param $prevUpdates Array: updates queue to refresh or null to build a fresh update queue
246 * @return Array: updates queue
248 private static function getUpdatesForMessage( $key, $prevUpdates = null ) {
249 $dbw = wfGetDB( DB_MASTER
);
251 if ( is_null( $prevUpdates ) ) {
252 // Fetch all blobs referencing $key
254 array( 'msg_resource', 'msg_resource_links' ),
255 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
256 array( 'mrl_message' => $key, 'mr_resource=mrl_resource' ),
260 // Refetch the blobs referenced by $prevUpdates
262 // Reorganize the (resource, lang) pairs in the format
263 // expected by makeWhereFrom2d()
266 foreach ( $prevUpdates as $update ) {
267 $twoD[$update['resource']][$update['lang']] = true;
270 $res = $dbw->select( 'msg_resource',
271 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
272 $dbw->makeWhereFrom2d( $twoD, 'mr_resource', 'mr_lang' ),
277 // Build the new updates queue
280 foreach ( $res as $row ) {
282 'resource' => $row->mr_resource
,
283 'lang' => $row->mr_lang
,
284 'timestamp' => $row->mr_timestamp
,
285 'newBlob' => self
::reencodeBlob( $row->mr_blob
, $key, $row->mr_lang
)
293 * Reencode a message blob with the updated value for a message
295 * @param $blob String: message blob (JSON object)
296 * @param $key String: message key
297 * @param $lang String: language code
298 * @return Message blob with $key replaced with its new value
300 private static function reencodeBlob( $blob, $key, $lang ) {
301 $decoded = FormatJson
::decode( $blob, true );
302 $decoded[$key] = wfMsgExt( $key, array( 'language' => $lang ) );
304 return FormatJson
::encode( (object)$decoded );
308 * Get the message blobs for a set of modules from the database.
309 * Modules whose blobs are not in the database are silently dropped.
311 * @param $resourceLoader ResourceLoader object
312 * @param $modules Array of module names
313 * @param $lang String: language code
314 * @return array Array mapping module names to blobs
316 private static function getFromDB( ResourceLoader
$resourceLoader, $modules, $lang ) {
317 global $wgCacheEpoch;
319 $dbr = wfGetDB( DB_SLAVE
);
320 $res = $dbr->select( 'msg_resource',
321 array( 'mr_blob', 'mr_resource', 'mr_timestamp' ),
322 array( 'mr_resource' => $modules, 'mr_lang' => $lang ),
326 foreach ( $res as $row ) {
327 $module = $resourceLoader->getModule( $row->mr_resource
);
329 // This shouldn't be possible
330 throw new MWException( __METHOD__
. ' passed an invalid module name' );
332 // Update the module's blobs if the set of messages changed or if the blob is
333 // older than $wgCacheEpoch
334 if ( array_keys( FormatJson
::decode( $row->mr_blob
, true ) ) !== array_values( array_unique( $module->getMessages() ) ) ||
335 wfTimestamp( TS_MW
, $row->mr_timestamp
) <= $wgCacheEpoch ) {
336 $retval[$row->mr_resource
] = self
::updateModule( $row->mr_resource
, $module, $lang );
338 $retval[$row->mr_resource
] = $row->mr_blob
;
346 * Generate the message blob for a given module in a given language.
348 * @param $module ResourceLoaderModule object
349 * @param $lang String: language code
350 * @return String: JSON object
352 private static function generateMessageBlob( ResourceLoaderModule
$module, $lang ) {
355 foreach ( $module->getMessages() as $key ) {
356 $messages[$key] = wfMsgExt( $key, array( 'language' => $lang ) );
359 return FormatJson
::encode( (object)$messages );