Merge "[JobQueue] Added support for approximate FIFO job queues."
[lhc/web/wiklou.git] / includes / MessageBlobStore.php
1 <?php
2 /**
3 * Resource message blobs storage used by the resource loader.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Roan Kattouw
22 * @author Trevor Parscal
23 */
24
25 /**
26 * This class provides access to the resource message blobs storage used by
27 * the ResourceLoader.
28 *
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.
33 */
34 class MessageBlobStore {
35
36 /**
37 * Get the message blobs for a set of modules
38 *
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
43 */
44 public static function get( ResourceLoader $resourceLoader, $modules, $lang ) {
45 wfProfileIn( __METHOD__ );
46 if ( !count( $modules ) ) {
47 wfProfileOut( __METHOD__ );
48 return array();
49 }
50 // Try getting from the DB first
51 $blobs = self::getFromDB( $resourceLoader, array_keys( $modules ), $lang );
52
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 );
57 if ( $blob ) {
58 $blobs[$name] = $blob;
59 }
60 }
61
62 wfProfileOut( __METHOD__ );
63 return $blobs;
64 }
65
66 /**
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.
70 *
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
75 */
76 public static function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
77 $blob = self::generateMessageBlob( $module, $lang );
78
79 if ( !$blob ) {
80 return false;
81 }
82
83 $dbw = wfGetDB( DB_MASTER );
84 $success = $dbw->insert( 'msg_resource', array(
85 'mr_lang' => $lang,
86 'mr_resource' => $name,
87 'mr_blob' => $blob,
88 'mr_timestamp' => $dbw->timestamp()
89 ),
90 __METHOD__,
91 array( 'IGNORE' )
92 );
93
94 if ( $success ) {
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,
99 'mr_lang' => $lang,
100 ),
101 __METHOD__
102 );
103 } else {
104 // Update msg_resource_links
105 $rows = array();
106
107 foreach ( $module->getMessages() as $key ) {
108 $rows[] = array(
109 'mrl_resource' => $name,
110 'mrl_message' => $key
111 );
112 }
113 $dbw->insert( 'msg_resource_links', $rows,
114 __METHOD__, array( 'IGNORE' )
115 );
116 }
117 }
118
119 return $blob;
120 }
121
122 /**
123 * Update the message blob for a given module in a given language
124 *
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
129 */
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 ),
134 __METHOD__
135 );
136 if ( !$row ) {
137 return null;
138 }
139
140 // Save the old and new blobs for later
141 $oldBlob = $row->mr_blob;
142 $newBlob = self::generateMessageBlob( $module, $lang );
143
144 $newRow = array(
145 'mr_resource' => $name,
146 'mr_lang' => $lang,
147 'mr_blob' => $newBlob,
148 'mr_timestamp' => $dbw->timestamp()
149 );
150
151 $dbw->replace( 'msg_resource',
152 array( array( 'mr_resource', 'mr_lang' ) ),
153 $newRow, __METHOD__
154 );
155
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 );
161
162 // Delete removed messages, insert added ones
163 if ( $removed ) {
164 $dbw->delete( 'msg_resource_links', array(
165 'mrl_resource' => $name,
166 'mrl_message' => $removed
167 ), __METHOD__
168 );
169 }
170
171 $newLinksRows = array();
172
173 foreach ( $added as $message ) {
174 $newLinksRows[] = array(
175 'mrl_resource' => $name,
176 'mrl_message' => $message
177 );
178 }
179
180 if ( $newLinksRows ) {
181 $dbw->insert( 'msg_resource_links', $newLinksRows, __METHOD__,
182 array( 'IGNORE' ) // just in case
183 );
184 }
185
186 return $newBlob;
187 }
188
189 /**
190 * Update a single message in all message blobs it occurs in.
191 *
192 * @param $key String: message key
193 */
194 public static function updateMessage( $key ) {
195 $dbw = wfGetDB( DB_MASTER );
196
197 // Keep running until the updates queue is empty.
198 // Due to update conflicts, the queue might not be emptied
199 // in one iteration.
200 $updates = null;
201 do {
202 $updates = self::getUpdatesForMessage( $key, $updates );
203
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',
209 array(
210 'mr_blob' => $update['newBlob'],
211 'mr_timestamp' => $dbw->timestamp() ),
212 array(
213 'mr_resource' => $update['resource'],
214 'mr_lang' => $update['lang'],
215 'mr_timestamp' => $update['timestamp'] ),
216 __METHOD__
217 );
218
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 ) ) {
223 // Not conflicted
224 unset( $updates[$k] );
225 }
226 }
227 } while ( count( $updates ) );
228
229 // No need to update msg_resource_links because we didn't add
230 // or remove any messages, we just changed their contents.
231 }
232
233 public static function clear() {
234 // TODO: Give this some more thought
235 // TODO: Is TRUNCATE better?
236 $dbw = wfGetDB( DB_MASTER );
237 $dbw->delete( 'msg_resource', '*', __METHOD__ );
238 $dbw->delete( 'msg_resource_links', '*', __METHOD__ );
239 }
240
241 /**
242 * Create an update queue for updateMessage()
243 *
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 *
295 * @param $blob String: message blob (JSON object)
296 * @param $key String: message key
297 * @param $lang String: language code
298 * @return Message blob with $key replaced with its new value
299 */
300 private static function reencodeBlob( $blob, $key, $lang ) {
301 $decoded = FormatJson::decode( $blob, true );
302 $decoded[$key] = wfMessage( $key )->inLanguage( $lang )->plain();
303
304 return FormatJson::encode( (object)$decoded );
305 }
306
307 /**
308 * Get the message blobs for a set of modules from the database.
309 * Modules whose blobs are not in the database are silently dropped.
310 *
311 * @param $resourceLoader ResourceLoader object
312 * @param $modules Array of module names
313 * @param $lang String: language code
314 * @throws MWException
315 * @return array Array mapping module names to blobs
316 */
317 private static function getFromDB( ResourceLoader $resourceLoader, $modules, $lang ) {
318 global $wgCacheEpoch;
319 $retval = array();
320 $dbr = wfGetDB( DB_SLAVE );
321 $res = $dbr->select( 'msg_resource',
322 array( 'mr_blob', 'mr_resource', 'mr_timestamp' ),
323 array( 'mr_resource' => $modules, 'mr_lang' => $lang ),
324 __METHOD__
325 );
326
327 foreach ( $res as $row ) {
328 $module = $resourceLoader->getModule( $row->mr_resource );
329 if ( !$module ) {
330 // This shouldn't be possible
331 throw new MWException( __METHOD__ . ' passed an invalid module name' );
332 }
333 // Update the module's blobs if the set of messages changed or if the blob is
334 // older than $wgCacheEpoch
335 if ( array_keys( FormatJson::decode( $row->mr_blob, true ) ) !== array_values( array_unique( $module->getMessages() ) ) ||
336 wfTimestamp( TS_MW, $row->mr_timestamp ) <= $wgCacheEpoch ) {
337 $retval[$row->mr_resource] = self::updateModule( $row->mr_resource, $module, $lang );
338 } else {
339 $retval[$row->mr_resource] = $row->mr_blob;
340 }
341 }
342
343 return $retval;
344 }
345
346 /**
347 * Generate the message blob for a given module in a given language.
348 *
349 * @param $module ResourceLoaderModule object
350 * @param $lang String: language code
351 * @return String: JSON object
352 */
353 private static function generateMessageBlob( ResourceLoaderModule $module, $lang ) {
354 $messages = array();
355
356 foreach ( $module->getMessages() as $key ) {
357 $messages[$key] = wfMessage( $key )->inLanguage( $lang )->plain();
358 }
359
360 return FormatJson::encode( (object)$messages );
361 }
362 }