* Improved on r73971 by removing the $name parameter from MessageBlobStore::gerenateM...
[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 objects keyed by module name
35 * @param $lang string Language code
36 * @return array An array mapping module names to message blobs
37 */
38 public static function get( ResourceLoader $resourceLoader, $modules, $lang ) {
39 wfProfileIn( __METHOD__ );
40 if ( !count( $modules ) ) {
41 wfProfileOut( __METHOD__ );
42 return array();
43 }
44 // Try getting from the DB first
45 $blobs = self::getFromDB( $resourceLoader, array_keys( $modules ), $lang );
46
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 );
51 if ( $blob ) {
52 $blobs[$name] = $blob;
53 }
54 }
55
56 wfProfileOut( __METHOD__ );
57 return $blobs;
58 }
59
60 /**
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
67 */
68 public static function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
69 $blob = self::generateMessageBlob( $module, $lang );
70
71 if ( !$blob ) {
72 return false;
73 }
74
75 $dbw = wfGetDB( DB_MASTER );
76 $success = $dbw->insert( 'msg_resource', array(
77 'mr_lang' => $lang,
78 'mr_resource' => $name,
79 'mr_blob' => $blob,
80 'mr_timestamp' => $dbw->timestamp()
81 ),
82 __METHOD__,
83 array( 'IGNORE' )
84 );
85
86 if ( $success ) {
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,
91 'mr_lang' => $lang,
92 ),
93 __METHOD__
94 );
95 } else {
96 // Update msg_resource_links
97 $rows = array();
98
99 foreach ( $module->getMessages() as $key ) {
100 $rows[] = array(
101 'mrl_resource' => $name,
102 'mrl_message' => $key
103 );
104 }
105 $dbw->insert( 'msg_resource_links', $rows,
106 __METHOD__, array( 'IGNORE' )
107 );
108 }
109 }
110
111 return $blob;
112 }
113
114 /**
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.
120 */
121 public static function updateModule( $name, ResourceLoaderModule $module, $lang = null ) {
122 $retval = null;
123
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 ),
129 __METHOD__
130 );
131
132 // Build the new msg_resource rows
133 $newRows = array();
134 $now = $dbw->timestamp();
135 // Save the last-processed old and new blobs for later
136 $oldBlob = $newBlob = null;
137
138 foreach ( $res as $row ) {
139 $oldBlob = $row->mr_blob;
140 $newBlob = self::generateMessageBlob( $module, $row->mr_lang );
141
142 if ( $row->mr_lang === $lang ) {
143 $retval = $newBlob;
144 }
145 $newRows[] = array(
146 'mr_resource' => $name,
147 'mr_lang' => $row->mr_lang,
148 'mr_blob' => $newBlob,
149 'mr_timestamp' => $now
150 );
151 }
152
153 $dbw->replace( 'msg_resource',
154 array( array( 'mr_resource', 'mr_lang' ) ),
155 $newRows, __METHOD__
156 );
157
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 );
163
164 // Delete removed messages, insert added ones
165 if ( $removed ) {
166 $dbw->delete( 'msg_resource_links', array(
167 'mrl_resource' => $name,
168 'mrl_message' => $removed
169 ), __METHOD__
170 );
171 }
172
173 $newLinksRows = array();
174
175 foreach ( $added as $message ) {
176 $newLinksRows[] = array(
177 'mrl_resource' => $name,
178 'mrl_message' => $message
179 );
180 }
181
182 if ( $newLinksRows ) {
183 $dbw->insert( 'msg_resource_links', $newLinksRows, __METHOD__,
184 array( 'IGNORE' ) // just in case
185 );
186 }
187
188 return $retval;
189 }
190
191 /**
192 * Update a single message in all message blobs it occurs in.
193 * @param $key string Message key
194 */
195 public static function updateMessage( $key ) {
196 $dbw = wfGetDB( DB_MASTER );
197
198 // Keep running until the updates queue is empty.
199 // Due to update conflicts, the queue might not be emptied
200 // in one iteration.
201 $updates = null;
202 do {
203 $updates = self::getUpdatesForMessage( $key, $updates );
204
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',
210 array(
211 'mr_blob' => $update['newBlob'],
212 'mr_timestamp' => $dbw->timestamp() ),
213 array(
214 'mr_resource' => $update['resource'],
215 'mr_lang' => $update['lang'],
216 'mr_timestamp' => $update['timestamp'] ),
217 __METHOD__
218 );
219
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 ) ) {
224 // Not conflicted
225 unset( $updates[$key] );
226 }
227 }
228 } while ( count( $updates ) );
229
230 // No need to update msg_resource_links because we didn't add
231 // or remove any messages, we just changed their contents.
232 }
233
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__ );
240 }
241
242 /**
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
247 */
248 private static function getUpdatesForMessage( $key, $prevUpdates = null ) {
249 $dbw = wfGetDB( DB_MASTER );
250
251 if ( is_null( $prevUpdates ) ) {
252 // Fetch all blobs referencing $key
253 $res = $dbw->select(
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' ),
257 __METHOD__
258 );
259 } else {
260 // Refetch the blobs referenced by $prevUpdates
261
262 // Reorganize the (resource, lang) pairs in the format
263 // expected by makeWhereFrom2d()
264 $twoD = array();
265
266 foreach ( $prevUpdates as $update ) {
267 $twoD[$update['resource']][$update['lang']] = true;
268 }
269
270 $res = $dbw->select( 'msg_resource',
271 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
272 $dbw->makeWhereFrom2d( $twoD, 'mr_resource', 'mr_lang' ),
273 __METHOD__
274 );
275 }
276
277 // Build the new updates queue
278 $updates = array();
279
280 foreach ( $res as $row ) {
281 $updates[] = array(
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 )
286 );
287 }
288
289 return $updates;
290 }
291
292 /**
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
298 */
299 private static function reencodeBlob( $blob, $key, $lang ) {
300 $decoded = FormatJson::decode( $blob, true );
301 $decoded[$key] = wfMsgExt( $key, array( 'language' => $lang ) );
302
303 return FormatJson::encode( $decoded );
304 }
305
306 /**
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
312 */
313 private static function getFromDB( ResourceLoader $resourceLoader, $modules, $lang ) {
314 global $wgCacheEpoch;
315 $retval = array();
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 ),
320 __METHOD__
321 );
322
323 foreach ( $res as $row ) {
324 $module = $resourceLoader->getModule( $row->mr_resource );
325 if ( !$module ) {
326 // This shouldn't be possible
327 throw new MWException( __METHOD__ . ' passed an invalid module name' );
328 }
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 );
334 } else {
335 $retval[$row->mr_resource] = $row->mr_blob;
336 }
337 }
338
339 return $retval;
340 }
341
342 /**
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
347 */
348 private static function generateMessageBlob( ResourceLoaderModule $module, $lang ) {
349 $messages = array();
350
351 foreach ( $module->getMessages() as $key ) {
352 $messages[$key] = wfMsgExt( $key, array( 'language' => $lang ) );
353 }
354
355 return FormatJson::encode( $messages );
356 }
357 }