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
{
33 * Get the message blobs for a set of modules
34 * @param $modules array Array of module objects keyed by module name
35 * @param $lang string Language code
36 * @return array An array mapping module names to message blobs
38 public static function get( ResourceLoader
$resourceLoader, $modules, $lang ) {
39 wfProfileIn( __METHOD__
);
40 if ( !count( $modules ) ) {
41 wfProfileOut( __METHOD__
);
44 // Try getting from the DB first
45 $blobs = self
::getFromDB( $resourceLoader, array_keys( $modules ), $lang );
47 // Generate blobs for any missing modules and store them in the DB
48 $missing = array_diff( array_keys( $modules ), array_keys( $blobs ) );
49 foreach ( $missing as $name ) {
50 $blob = self
::insertMessageBlob( $name, $modules[$name], $lang );
52 $blobs[$name] = $blob;
56 wfProfileOut( __METHOD__
);
61 * Generate and insert a new message blob. If the blob was already
62 * present, it is not regenerated; instead, the preexisting blob
63 * is fetched and returned.
64 * @param $module string Module name
65 * @param $lang string Language code
66 * @return mixed Message blob or false if the module has no messages
68 public static function insertMessageBlob( $name, ResourceLoaderModule
$module, $lang ) {
69 $blob = self
::generateMessageBlob( $module, $lang );
75 $dbw = wfGetDB( DB_MASTER
);
76 $success = $dbw->insert( 'msg_resource', array(
78 'mr_resource' => $name,
80 'mr_timestamp' => $dbw->timestamp()
87 if ( $dbw->affectedRows() == 0 ) {
88 // Blob was already present, fetch it
89 $blob = $dbw->selectField( 'msg_resource', 'mr_blob', array(
90 'mr_resource' => $name,
96 // Update msg_resource_links
99 foreach ( $module->getMessages() as $key ) {
101 'mrl_resource' => $name,
102 'mrl_message' => $key
105 $dbw->insert( 'msg_resource_links', $rows,
106 __METHOD__
, array( 'IGNORE' )
115 * Update all message blobs for a given module.
116 * @param $module string Module name
117 * @param $lang string Language code (optional)
118 * @return mixed If $lang is set, the new message blob for that language is
119 * returned if present. Otherwise, null is returned.
121 public static function updateModule( $name, ResourceLoaderModule
$module, $lang = null ) {
124 // Find all existing blobs for this module
125 $dbw = wfGetDB( DB_MASTER
);
126 $res = $dbw->select( 'msg_resource',
127 array( 'mr_lang', 'mr_blob' ),
128 array( 'mr_resource' => $name ),
132 // Build the new msg_resource rows
134 $now = $dbw->timestamp();
135 // Save the last-processed old and new blobs for later
136 $oldBlob = $newBlob = null;
138 foreach ( $res as $row ) {
139 $oldBlob = $row->mr_blob
;
140 $newBlob = self
::generateMessageBlob( $module, $row->mr_lang
);
142 if ( $row->mr_lang
=== $lang ) {
146 'mr_resource' => $name,
147 'mr_lang' => $row->mr_lang
,
148 'mr_blob' => $newBlob,
149 'mr_timestamp' => $now
153 $dbw->replace( 'msg_resource',
154 array( array( 'mr_resource', 'mr_lang' ) ),
158 // Figure out which messages were added and removed
159 $oldMessages = array_keys( FormatJson
::decode( $oldBlob, true ) );
160 $newMessages = array_keys( FormatJson
::decode( $newBlob, true ) );
161 $added = array_diff( $newMessages, $oldMessages );
162 $removed = array_diff( $oldMessages, $newMessages );
164 // Delete removed messages, insert added ones
166 $dbw->delete( 'msg_resource_links', array(
167 'mrl_resource' => $name,
168 'mrl_message' => $removed
173 $newLinksRows = array();
175 foreach ( $added as $message ) {
176 $newLinksRows[] = array(
177 'mrl_resource' => $name,
178 'mrl_message' => $message
182 if ( $newLinksRows ) {
183 $dbw->insert( 'msg_resource_links', $newLinksRows, __METHOD__
,
184 array( 'IGNORE' ) // just in case
192 * Update a single message in all message blobs it occurs in.
193 * @param $key string Message key
195 public static function updateMessage( $key ) {
196 $dbw = wfGetDB( DB_MASTER
);
198 // Keep running until the updates queue is empty.
199 // Due to update conflicts, the queue might not be emptied
203 $updates = self
::getUpdatesForMessage( $key, $updates );
205 foreach ( $updates as $key => $update ) {
206 // Update the row on the condition that it
207 // didn't change since we fetched it by putting
208 // the timestamp in the WHERE clause.
209 $success = $dbw->update( 'msg_resource',
211 'mr_blob' => $update['newBlob'],
212 'mr_timestamp' => $dbw->timestamp() ),
214 'mr_resource' => $update['resource'],
215 'mr_lang' => $update['lang'],
216 'mr_timestamp' => $update['timestamp'] ),
220 // Only requeue conflicted updates.
221 // If update() returned false, don't retry, for
222 // fear of getting into an infinite loop
223 if ( !( $success && $dbw->affectedRows() == 0 ) ) {
225 unset( $updates[$key] );
228 } while ( count( $updates ) );
230 // No need to update msg_resource_links because we didn't add
231 // or remove any messages, we just changed their contents.
234 public static function clear() {
235 // TODO: Give this some more thought
236 // TODO: Is TRUNCATE better?
237 $dbw = wfGetDB( DB_MASTER
);
238 $dbw->delete( 'msg_resource', '*', __METHOD__
);
239 $dbw->delete( 'msg_resource_links', '*', __METHOD__
);
243 * 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
294 * @param $blob string Message blob (JSON object)
295 * @param $key string Message key
296 * @param $lang string Language code
297 * @return Message blob with $key replaced with its new value
299 private static function reencodeBlob( $blob, $key, $lang ) {
300 $decoded = FormatJson
::decode( $blob, true );
301 $decoded[$key] = wfMsgExt( $key, array( 'language' => $lang ) );
303 return FormatJson
::encode( $decoded );
307 * Get the message blobs for a set of modules from the database.
308 * Modules whose blobs are not in the database are silently dropped.
309 * @param $modules array Array of module names
310 * @param $lang string Language code
311 * @return array Array mapping module names to blobs
313 private static function getFromDB( ResourceLoader
$resourceLoader, $modules, $lang ) {
314 global $wgCacheEpoch;
316 $dbr = wfGetDB( DB_SLAVE
);
317 $res = $dbr->select( 'msg_resource',
318 array( 'mr_blob', 'mr_resource', 'mr_timestamp' ),
319 array( 'mr_resource' => $modules, 'mr_lang' => $lang ),
323 foreach ( $res as $row ) {
324 $module = $resourceLoader->getModule( $row->mr_resource
);
326 // This shouldn't be possible
327 throw new MWException( __METHOD__
. ' passed an invalid module name' );
329 // Update the module's blobs if the set of messages changed or if the blob is
330 // older than $wgCacheEpoch
331 if ( array_keys( FormatJson
::decode( $row->mr_blob
, true ) ) !== $module->getMessages() ||
332 wfTimestamp( TS_MW
, $row->mr_timestamp
) <= $wgCacheEpoch ) {
333 $retval[$row->mr_resource
] = self
::updateModule( $module, $lang );
335 $retval[$row->mr_resource
] = $row->mr_blob
;
343 * Generate the message blob for a given module in a given language.
344 * @param $module string Module name
345 * @param $lang string Language code
346 * @return string JSON object
348 private static function generateMessageBlob( ResourceLoaderModule
$module, $lang ) {
351 foreach ( $module->getMessages() as $key ) {
352 $messages[$key] = wfMsgExt( $key, array( 'language' => $lang ) );
355 return FormatJson
::encode( $messages );