Minor resource loader changes:
[lhc/web/wiklou.git] / includes / MessageBlobStore.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @author Roan Kattouw
19 * @author Trevor Parscal
20 */
21
22 /**
23 * This class provides access to the resource message blobs storage used by
24 * the ResourceLoader.
25 *
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.
30 */
31 class MessageBlobStore {
32 /**
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
37 */
38 public static function get( $modules, $lang ) {
39 // TODO: Invalidate blob when module touched
40 wfProfileIn( __METHOD__ );
41 if ( !count( $modules ) ) {
42 wfProfileOut( __METHOD__ );
43 return array();
44 }
45 // Try getting from the DB first
46 $blobs = self::getFromDB( $modules, $lang );
47
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 );
52 if ( $blob ) {
53 $blobs[$module] = $blob;
54 }
55 }
56
57 wfProfileOut( __METHOD__ );
58 return $blobs;
59 }
60
61 /**
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
68 */
69 public static function insertMessageBlob( $module, $lang ) {
70 $blob = self::generateMessageBlob( $module, $lang );
71
72 if ( !$blob ) {
73 return false;
74 }
75
76 $dbw = wfGetDB( DB_MASTER );
77 $success = $dbw->insert( 'msg_resource', array(
78 'mr_lang' => $lang,
79 'mr_resource' => $module,
80 'mr_blob' => $blob,
81 'mr_timestamp' => $dbw->timestamp()
82 ),
83 __METHOD__,
84 array( 'IGNORE' )
85 );
86
87 if ( $success ) {
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,
93 'mr_lang' => $lang,
94 ),
95 __METHOD__
96 );
97 } else {
98 // Update msg_resource_links
99 $rows = array();
100 $mod = ResourceLoader::getModule( $module );
101
102 foreach ( $mod->getMessages() as $key ) {
103 $rows[] = array(
104 'mrl_resource' => $module,
105 'mrl_message' => $key
106 );
107 }
108 $dbw->insert( 'msg_resource_links', $rows,
109 __METHOD__, array( 'IGNORE' )
110 );
111 }
112 }
113
114 return $blob;
115 }
116
117 /**
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.
123 */
124 public static function updateModule( $module, $lang = null ) {
125 $retval = null;
126
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 ),
132 __METHOD__
133 );
134
135 // Build the new msg_resource rows
136 $newRows = array();
137 $now = $dbw->timestamp();
138 // Save the last-processed old and new blobs for later
139 $oldBlob = $newBlob = null;
140
141 foreach ( $res as $row ) {
142 $oldBlob = $row->mr_blob;
143 $newBlob = self::generateMessageBlob( $module, $row->mr_lang );
144
145 if ( $row->mr_lang === $lang ) {
146 $retval = $newBlob;
147 }
148 $newRows[] = array(
149 'mr_resource' => $module,
150 'mr_lang' => $row->mr_lang,
151 'mr_blob' => $newBlob,
152 'mr_timestamp' => $now
153 );
154 }
155
156 $dbw->replace( 'msg_resource',
157 array( array( 'mr_resource', 'mr_lang' ) ),
158 $newRows, __METHOD__
159 );
160
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 );
166
167 // Delete removed messages, insert added ones
168 if ( $removed ) {
169 $dbw->delete( 'msg_resource_links', array(
170 'mrl_resource' => $module,
171 'mrl_message' => $removed
172 ), __METHOD__
173 );
174 }
175
176 $newLinksRows = array();
177
178 foreach ( $added as $message ) {
179 $newLinksRows[] = array(
180 'mrl_resource' => $module,
181 'mrl_message' => $message
182 );
183 }
184
185 if ( $newLinksRows ) {
186 $dbw->insert( 'msg_resource_links', $newLinksRows, __METHOD__,
187 array( 'IGNORE' ) // just in case
188 );
189 }
190
191 return $retval;
192 }
193
194 /**
195 * Update a single message in all message blobs it occurs in.
196 * @param $key string Message key
197 */
198 public static function updateMessage( $key ) {
199 $dbw = wfGetDB( DB_MASTER );
200
201 // Keep running until the updates queue is empty.
202 // Due to update conflicts, the queue might not be emptied
203 // in one iteration.
204 $updates = null;
205 do {
206 $updates = self::getUpdatesForMessage( $key, $updates );
207
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',
213 array(
214 'mr_blob' => $update['newBlob'],
215 'mr_timestamp' => $dbw->timestamp() ),
216 array(
217 'mr_resource' => $update['resource'],
218 'mr_lang' => $update['lang'],
219 'mr_timestamp' => $update['timestamp'] ),
220 __METHOD__
221 );
222
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 ) ) {
227 // Not conflicted
228 unset( $updates[$key] );
229 }
230 }
231 } while ( count( $updates ) );
232
233 // No need to update msg_resource_links because we didn't add
234 // or remove any messages, we just changed their contents.
235 }
236
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__ );
243 }
244
245 /**
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
250 */
251 private static function getUpdatesForMessage( $key, $prevUpdates = null ) {
252 $dbw = wfGetDB( DB_MASTER );
253
254 if ( is_null( $prevUpdates ) ) {
255 // Fetch all blobs referencing $key
256 $res = $dbw->select(
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' ),
260 __METHOD__
261 );
262 } else {
263 // Refetch the blobs referenced by $prevUpdates
264
265 // Reorganize the (resource, lang) pairs in the format
266 // expected by makeWhereFrom2d()
267 $twoD = array();
268
269 foreach ( $prevUpdates as $update ) {
270 $twoD[$update['resource']][$update['lang']] = true;
271 }
272
273 $res = $dbw->select( 'msg_resource',
274 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
275 $dbw->makeWhereFrom2d( $twoD, 'mr_resource', 'mr_lang' ),
276 __METHOD__
277 );
278 }
279
280 // Build the new updates queue
281 $updates = array();
282
283 foreach ( $res as $row ) {
284 $updates[] = array(
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 )
289 );
290 }
291
292 return $updates;
293 }
294
295 /**
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
301 */
302 private static function reencodeBlob( $blob, $key, $lang ) {
303 $decoded = FormatJson::decode( $blob, true );
304 $decoded[$key] = wfMsgExt( $key, array( 'language' => $lang ) );
305
306 return FormatJson::encode( $decoded );
307 }
308
309 /**
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
315 */
316 private static function getFromDB( $modules, $lang ) {
317 $retval = array();
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 ),
322 __METHOD__
323 );
324
325 foreach ( $res as $row ) {
326 $module = ResourceLoader::getModule( $row->mr_resource );
327 if ( !$module ) {
328 // This shouldn't be possible
329 throw new MWException( __METHOD__ . ' passed an invalid module name' );
330 }
331 if ( array_keys( FormatJson::decode( $row->mr_blob, true ) ) !== $module->getMessages() ) {
332 $retval[$row->mr_resource] = self::updateModule( $row->mr_resource, $lang );
333 } else {
334 $retval[$row->mr_resource] = $row->mr_blob;
335 }
336 }
337
338 return $retval;
339 }
340
341 /**
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
346 */
347 private static function generateMessageBlob( $module, $lang ) {
348 $mod = ResourceLoader::getModule( $module );
349 $messages = array();
350
351 foreach ( $mod->getMessages() as $key ) {
352 $messages[$key] = wfMsgExt( $key, array( 'language' => $lang ) );
353 }
354
355 return FormatJson::encode( $messages );
356 }
357 }