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 // HACK: disable clear() on WMF servers
237 // TODO: Give this some more thought
238 // TODO: Is TRUNCATE better?
239 $dbw = wfGetDB( DB_MASTER
);
240 $dbw->delete( 'msg_resource', '*', __METHOD__
);
241 $dbw->delete( 'msg_resource_links', '*', __METHOD__
);
245 * Create an update queue for updateMessage()
247 * @param $key String: message key
248 * @param $prevUpdates Array: updates queue to refresh or null to build a fresh update queue
249 * @return Array: updates queue
251 private static function getUpdatesForMessage( $key, $prevUpdates = null ) {
252 $dbw = wfGetDB( DB_MASTER
);
254 if ( is_null( $prevUpdates ) ) {
255 // Fetch all blobs referencing $key
257 array( 'msg_resource', 'msg_resource_links' ),
258 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
259 array( 'mrl_message' => $key, 'mr_resource=mrl_resource' ),
263 // Refetch the blobs referenced by $prevUpdates
265 // Reorganize the (resource, lang) pairs in the format
266 // expected by makeWhereFrom2d()
269 foreach ( $prevUpdates as $update ) {
270 $twoD[$update['resource']][$update['lang']] = true;
273 $res = $dbw->select( 'msg_resource',
274 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
275 $dbw->makeWhereFrom2d( $twoD, 'mr_resource', 'mr_lang' ),
280 // Build the new updates queue
283 foreach ( $res as $row ) {
285 'resource' => $row->mr_resource
,
286 'lang' => $row->mr_lang
,
287 'timestamp' => $row->mr_timestamp
,
288 'newBlob' => self
::reencodeBlob( $row->mr_blob
, $key, $row->mr_lang
)
296 * Reencode a message blob with the updated value for a message
298 * @param $blob String: message blob (JSON object)
299 * @param $key String: message key
300 * @param $lang String: language code
301 * @return Message blob with $key replaced with its new value
303 private static function reencodeBlob( $blob, $key, $lang ) {
304 $decoded = FormatJson
::decode( $blob, true );
305 $decoded[$key] = wfMessage( $key )->inLanguage( $lang )->plain();
307 return FormatJson
::encode( (object)$decoded );
311 * Get the message blobs for a set of modules from the database.
312 * Modules whose blobs are not in the database are silently dropped.
314 * @param $resourceLoader ResourceLoader object
315 * @param $modules Array of module names
316 * @param $lang String: language code
317 * @throws MWException
318 * @return array Array mapping module names to blobs
320 private static function getFromDB( ResourceLoader
$resourceLoader, $modules, $lang ) {
321 global $wgCacheEpoch;
323 $dbr = wfGetDB( DB_SLAVE
);
324 $res = $dbr->select( 'msg_resource',
325 array( 'mr_blob', 'mr_resource', 'mr_timestamp' ),
326 array( 'mr_resource' => $modules, 'mr_lang' => $lang ),
330 foreach ( $res as $row ) {
331 $module = $resourceLoader->getModule( $row->mr_resource
);
333 // This shouldn't be possible
334 throw new MWException( __METHOD__
. ' passed an invalid module name' );
336 // Update the module's blobs if the set of messages changed or if the blob is
337 // older than $wgCacheEpoch
338 if ( array_keys( FormatJson
::decode( $row->mr_blob
, true ) ) !== array_values( array_unique( $module->getMessages() ) ) ||
339 wfTimestamp( TS_MW
, $row->mr_timestamp
) <= $wgCacheEpoch ) {
340 $retval[$row->mr_resource
] = self
::updateModule( $row->mr_resource
, $module, $lang );
342 $retval[$row->mr_resource
] = $row->mr_blob
;
350 * Generate the message blob for a given module in a given language.
352 * @param $module ResourceLoaderModule object
353 * @param $lang String: language code
354 * @return String: JSON object
356 private static function generateMessageBlob( ResourceLoaderModule
$module, $lang ) {
359 foreach ( $module->getMessages() as $key ) {
360 $messages[$key] = wfMessage( $key )->inLanguage( $lang )->plain();
363 return FormatJson
::encode( (object)$messages );