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 names
35 * @param $lang string Language code
36 * @return array An array mapping module names to message blobs
38 public static function get( $modules, $lang ) {
39 // TODO: Invalidate blob when module touched
40 wfProfileIn( __METHOD__
);
41 if ( !count( $modules ) ) {
42 wfProfileOut( __METHOD__
);
45 // Try getting from the DB first
46 $blobs = self
::getFromDB( $modules, $lang );
48 // Generate blobs for any missing modules and store them in the DB
49 $missing = array_diff( $modules, array_keys( $blobs ) );
50 foreach ( $missing as $module ) {
51 $blob = self
::insertMessageBlob( $module, $lang );
53 $blobs[$module] = $blob;
57 wfProfileOut( __METHOD__
);
62 * Generate and insert a new message blob. If the blob was already
63 * present, it is not regenerated; instead, the preexisting blob
64 * is fetched and returned.
65 * @param $module string Module name
66 * @param $lang string Language code
67 * @return mixed Message blob or false if the module has no messages
69 public static function insertMessageBlob( $module, $lang ) {
70 $blob = self
::generateMessageBlob( $module, $lang );
76 $dbw = wfGetDB( DB_MASTER
);
77 $success = $dbw->insert( 'msg_resource', array(
79 'mr_resource' => $module,
81 'mr_timestamp' => $dbw->timestamp()
88 if ( $dbw->affectedRows() == 0 ) {
89 // Blob was already present, fetch it
90 $dbr = wfGetDB( DB_SLAVE
);
91 $blob = $dbr->selectField( 'msg_resource', 'mr_blob', array(
92 'mr_resource' => $module,
98 // Update msg_resource_links
100 $mod = ResourceLoader
::getModule( $module );
102 foreach ( $mod->getMessages() as $key ) {
104 'mrl_resource' => $module,
105 'mrl_message' => $key
108 $dbw->insert( 'msg_resource_links', $rows,
109 __METHOD__
, array( 'IGNORE' )
118 * Update all message blobs for a given module.
119 * @param $module string Module name
120 * @param $lang string Language code (optional)
121 * @return mixed If $lang is set, the new message blob for that language is
122 * returned if present. Otherwise, null is returned.
124 public static function updateModule( $module, $lang = null ) {
127 // Find all existing blobs for this module
128 $dbw = wfGetDB( DB_MASTER
);
129 $res = $dbw->select( 'msg_resource',
130 array( 'mr_lang', 'mr_blob' ),
131 array( 'mr_resource' => $module ),
135 // Build the new msg_resource rows
137 $now = $dbw->timestamp();
138 // Save the last-processed old and new blobs for later
139 $oldBlob = $newBlob = null;
141 foreach ( $res as $row ) {
142 $oldBlob = $row->mr_blob
;
143 $newBlob = self
::generateMessageBlob( $module, $row->mr_lang
);
145 if ( $row->mr_lang
=== $lang ) {
149 'mr_resource' => $module,
150 'mr_lang' => $row->mr_lang
,
151 'mr_blob' => $newBlob,
152 'mr_timestamp' => $now
156 $dbw->replace( 'msg_resource',
157 array( array( 'mr_resource', 'mr_lang' ) ),
161 // Figure out which messages were added and removed
162 $oldMessages = array_keys( FormatJson
::decode( $oldBlob, true ) );
163 $newMessages = array_keys( FormatJson
::decode( $newBlob, true ) );
164 $added = array_diff( $newMessages, $oldMessages );
165 $removed = array_diff( $oldMessages, $newMessages );
167 // Delete removed messages, insert added ones
169 $dbw->delete( 'msg_resource_links', array(
170 'mrl_resource' => $module,
171 'mrl_message' => $removed
176 $newLinksRows = array();
178 foreach ( $added as $message ) {
179 $newLinksRows[] = array(
180 'mrl_resource' => $module,
181 'mrl_message' => $message
185 if ( $newLinksRows ) {
186 $dbw->insert( 'msg_resource_links', $newLinksRows, __METHOD__
,
187 array( 'IGNORE' ) // just in case
195 * Update a single message in all message blobs it occurs in.
196 * @param $key string Message key
198 public static function updateMessage( $key ) {
199 $dbw = wfGetDB( DB_MASTER
);
201 // Keep running until the updates queue is empty.
202 // Due to update conflicts, the queue might not be emptied
206 $updates = self
::getUpdatesForMessage( $key, $updates );
208 foreach ( $updates as $key => $update ) {
209 // Update the row on the condition that it
210 // didn't change since we fetched it by putting
211 // the timestamp in the WHERE clause.
212 $success = $dbw->update( 'msg_resource',
214 'mr_blob' => $update['newBlob'],
215 'mr_timestamp' => $dbw->timestamp() ),
217 'mr_resource' => $update['resource'],
218 'mr_lang' => $update['lang'],
219 'mr_timestamp' => $update['timestamp'] ),
223 // Only requeue conflicted updates.
224 // If update() returned false, don't retry, for
225 // fear of getting into an infinite loop
226 if ( !( $success && $dbw->affectedRows() == 0 ) ) {
228 unset( $updates[$key] );
231 } while ( count( $updates ) );
233 // No need to update msg_resource_links because we didn't add
234 // or remove any messages, we just changed their contents.
237 public static function clear() {
238 // TODO: Give this some more thought
239 // TODO: Is TRUNCATE better?
240 $dbw = wfGetDB( DB_MASTER
);
241 $dbw->delete( 'msg_resource', '*', __METHOD__
);
242 $dbw->delete( 'msg_resource_links', '*', __METHOD__
);
246 * 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
297 * @param $blob string Message blob (JSON object)
298 * @param $key string Message key
299 * @param $lang string Language code
300 * @return Message blob with $key replaced with its new value
302 private static function reencodeBlob( $blob, $key, $lang ) {
303 $decoded = FormatJson
::decode( $blob, true );
304 $decoded[$key] = wfMsgExt( $key, array( 'language' => $lang ) );
306 return FormatJson
::encode( $decoded );
310 * Get the message blobs for a set of modules from the database.
311 * Modules whose blobs are not in the database are silently dropped.
312 * @param $modules array Array of module names
313 * @param $lang string Language code
314 * @return array Array mapping module names to blobs
316 private static function getFromDB( $modules, $lang ) {
318 $dbr = wfGetDB( DB_SLAVE
);
319 $res = $dbr->select( 'msg_resource',
320 array( 'mr_blob', 'mr_resource', 'mr_timestamp' ),
321 array( 'mr_resource' => $modules, 'mr_lang' => $lang ),
325 foreach ( $res as $row ) {
326 $module = ResourceLoader
::getModule( $row->mr_resource
);
328 // This shouldn't be possible
329 throw new MWException( __METHOD__
. ' passed an invalid module name' );
331 if ( array_keys( FormatJson
::decode( $row->mr_blob
, true ) ) !== $module->getMessages() ) {
332 $retval[$row->mr_resource
] = self
::updateModule( $row->mr_resource
, $lang );
334 $retval[$row->mr_resource
] = $row->mr_blob
;
342 * Generate the message blob for a given module in a given language.
343 * @param $module string Module name
344 * @param $lang string Language code
345 * @return string JSON object
347 private static function generateMessageBlob( $module, $lang ) {
348 $mod = ResourceLoader
::getModule( $module );
351 foreach ( $mod->getMessages() as $key ) {
352 $messages[$key] = wfMsgExt( $key, array( 'language' => $lang ) );
355 return FormatJson
::encode( $messages );