Merge "Add tests for API's assert={user|bot}"
[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 * constituent messages or the resource itself is changed.
33 */
34 class MessageBlobStore {
35 /**
36 * Get the message blobs for a set of modules
37 *
38 * @param ResourceLoader $resourceLoader
39 * @param array $modules Array of module objects keyed by module name
40 * @param string $lang Language code
41 * @return array An array mapping module names to message blobs
42 */
43 public static function get( ResourceLoader $resourceLoader, $modules, $lang ) {
44 wfProfileIn( __METHOD__ );
45 if ( !count( $modules ) ) {
46 wfProfileOut( __METHOD__ );
47 return array();
48 }
49 // Try getting from the DB first
50 $blobs = self::getFromDB( $resourceLoader, array_keys( $modules ), $lang );
51
52 // Generate blobs for any missing modules and store them in the DB
53 $missing = array_diff( array_keys( $modules ), array_keys( $blobs ) );
54 foreach ( $missing as $name ) {
55 $blob = self::insertMessageBlob( $name, $modules[$name], $lang );
56 if ( $blob ) {
57 $blobs[$name] = $blob;
58 }
59 }
60
61 wfProfileOut( __METHOD__ );
62 return $blobs;
63 }
64
65 /**
66 * Generate and insert a new message blob. If the blob was already
67 * present, it is not regenerated; instead, the preexisting blob
68 * is fetched and returned.
69 *
70 * @param string $name Module name
71 * @param ResourceLoaderModule $module
72 * @param string $lang Language code
73 * @return mixed Message blob or false if the module has no messages
74 */
75 public static function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
76 $blob = self::generateMessageBlob( $module, $lang );
77
78 if ( !$blob ) {
79 return false;
80 }
81
82 try {
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 } catch ( Exception $e ) {
119 wfDebug( __METHOD__ . " failed to update DB: $e\n" );
120 }
121 return $blob;
122 }
123
124 /**
125 * Update the message blob for a given module in a given language
126 *
127 * @param string $name Module name
128 * @param ResourceLoaderModule $module
129 * @param string $lang Language code
130 * @return string Regenerated message blob, or null if there was no blob for
131 * the given module/language pair.
132 */
133 public static function updateModule( $name, ResourceLoaderModule $module, $lang ) {
134 $dbw = wfGetDB( DB_MASTER );
135 $row = $dbw->selectRow( 'msg_resource', 'mr_blob',
136 array( 'mr_resource' => $name, 'mr_lang' => $lang ),
137 __METHOD__
138 );
139 if ( !$row ) {
140 return null;
141 }
142
143 // Save the old and new blobs for later
144 $oldBlob = $row->mr_blob;
145 $newBlob = self::generateMessageBlob( $module, $lang );
146
147 try {
148 $newRow = array(
149 'mr_resource' => $name,
150 'mr_lang' => $lang,
151 'mr_blob' => $newBlob,
152 'mr_timestamp' => $dbw->timestamp()
153 );
154
155 $dbw->replace( 'msg_resource',
156 array( array( 'mr_resource', 'mr_lang' ) ),
157 $newRow, __METHOD__
158 );
159
160 // Figure out which messages were added and removed
161 $oldMessages = array_keys( FormatJson::decode( $oldBlob, true ) );
162 $newMessages = array_keys( FormatJson::decode( $newBlob, true ) );
163 $added = array_diff( $newMessages, $oldMessages );
164 $removed = array_diff( $oldMessages, $newMessages );
165
166 // Delete removed messages, insert added ones
167 if ( $removed ) {
168 $dbw->delete( 'msg_resource_links', array(
169 'mrl_resource' => $name,
170 'mrl_message' => $removed
171 ), __METHOD__
172 );
173 }
174
175 $newLinksRows = array();
176
177 foreach ( $added as $message ) {
178 $newLinksRows[] = array(
179 'mrl_resource' => $name,
180 'mrl_message' => $message
181 );
182 }
183
184 if ( $newLinksRows ) {
185 $dbw->insert( 'msg_resource_links', $newLinksRows, __METHOD__,
186 array( 'IGNORE' ) // just in case
187 );
188 }
189 } catch ( Exception $e ) {
190 wfDebug( __METHOD__ . " failed to update DB: $e\n" );
191 }
192 return $newBlob;
193 }
194
195 /**
196 * Update a single message in all message blobs it occurs in.
197 *
198 * @param string $key Message key
199 */
200 public static function updateMessage( $key ) {
201 try {
202 $dbw = wfGetDB( DB_MASTER );
203
204 // Keep running until the updates queue is empty.
205 // Due to update conflicts, the queue might not be emptied
206 // in one iteration.
207 $updates = null;
208 do {
209 $updates = self::getUpdatesForMessage( $key, $updates );
210
211 foreach ( $updates as $k => $update ) {
212 // Update the row on the condition that it
213 // didn't change since we fetched it by putting
214 // the timestamp in the WHERE clause.
215 $success = $dbw->update( 'msg_resource',
216 array(
217 'mr_blob' => $update['newBlob'],
218 'mr_timestamp' => $dbw->timestamp() ),
219 array(
220 'mr_resource' => $update['resource'],
221 'mr_lang' => $update['lang'],
222 'mr_timestamp' => $update['timestamp'] ),
223 __METHOD__
224 );
225
226 // Only requeue conflicted updates.
227 // If update() returned false, don't retry, for
228 // fear of getting into an infinite loop
229 if ( !( $success && $dbw->affectedRows() == 0 ) ) {
230 // Not conflicted
231 unset( $updates[$k] );
232 }
233 }
234 } while ( count( $updates ) );
235
236 // No need to update msg_resource_links because we didn't add
237 // or remove any messages, we just changed their contents.
238 } catch ( Exception $e ) {
239 wfDebug( __METHOD__ . " failed to update DB: $e\n" );
240 }
241 }
242
243 public static function clear() {
244 // TODO: Give this some more thought
245 try {
246 // Not using TRUNCATE, because that needs extra permissions,
247 // which maybe not granted to the database user.
248 $dbw = wfGetDB( DB_MASTER );
249 $dbw->delete( 'msg_resource', '*', __METHOD__ );
250 $dbw->delete( 'msg_resource_links', '*', __METHOD__ );
251 } catch ( Exception $e ) {
252 wfDebug( __METHOD__ . " failed to update DB: $e\n" );
253 }
254 }
255
256 /**
257 * Create an update queue for updateMessage()
258 *
259 * @param string $key Message key
260 * @param array $prevUpdates Updates queue to refresh or null to build a fresh update queue
261 * @return array Updates queue
262 */
263 private static function getUpdatesForMessage( $key, $prevUpdates = null ) {
264 $dbw = wfGetDB( DB_MASTER );
265
266 if ( is_null( $prevUpdates ) ) {
267 // Fetch all blobs referencing $key
268 $res = $dbw->select(
269 array( 'msg_resource', 'msg_resource_links' ),
270 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
271 array( 'mrl_message' => $key, 'mr_resource=mrl_resource' ),
272 __METHOD__
273 );
274 } else {
275 // Refetch the blobs referenced by $prevUpdates
276
277 // Reorganize the (resource, lang) pairs in the format
278 // expected by makeWhereFrom2d()
279 $twoD = array();
280
281 foreach ( $prevUpdates as $update ) {
282 $twoD[$update['resource']][$update['lang']] = true;
283 }
284
285 $res = $dbw->select( 'msg_resource',
286 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
287 $dbw->makeWhereFrom2d( $twoD, 'mr_resource', 'mr_lang' ),
288 __METHOD__
289 );
290 }
291
292 // Build the new updates queue
293 $updates = array();
294
295 foreach ( $res as $row ) {
296 $updates[] = array(
297 'resource' => $row->mr_resource,
298 'lang' => $row->mr_lang,
299 'timestamp' => $row->mr_timestamp,
300 'newBlob' => self::reencodeBlob( $row->mr_blob, $key, $row->mr_lang )
301 );
302 }
303
304 return $updates;
305 }
306
307 /**
308 * Reencode a message blob with the updated value for a message
309 *
310 * @param string $blob Message blob (JSON object)
311 * @param string $key Message key
312 * @param string $lang Language code
313 * @return string Message blob with $key replaced with its new value
314 */
315 private static function reencodeBlob( $blob, $key, $lang ) {
316 $decoded = FormatJson::decode( $blob, true );
317 $decoded[$key] = wfMessage( $key )->inLanguage( $lang )->plain();
318
319 return FormatJson::encode( (object)$decoded );
320 }
321
322 /**
323 * Get the message blobs for a set of modules from the database.
324 * Modules whose blobs are not in the database are silently dropped.
325 *
326 * @param ResourceLoader $resourceLoader
327 * @param array $modules Array of module names
328 * @param string $lang Language code
329 * @throws MWException
330 * @return array Array mapping module names to blobs
331 */
332 private static function getFromDB( ResourceLoader $resourceLoader, $modules, $lang ) {
333 global $wgCacheEpoch;
334
335 $retval = array();
336 $dbr = wfGetDB( DB_SLAVE );
337 $res = $dbr->select( 'msg_resource',
338 array( 'mr_blob', 'mr_resource', 'mr_timestamp' ),
339 array( 'mr_resource' => $modules, 'mr_lang' => $lang ),
340 __METHOD__
341 );
342
343 foreach ( $res as $row ) {
344 $module = $resourceLoader->getModule( $row->mr_resource );
345 if ( !$module ) {
346 // This shouldn't be possible
347 throw new MWException( __METHOD__ . ' passed an invalid module name' );
348 }
349
350 // Update the module's blobs if the set of messages changed or if the blob is
351 // older than $wgCacheEpoch
352 $keys = array_keys( FormatJson::decode( $row->mr_blob, true ) );
353 $values = array_values( array_unique( $module->getMessages() ) );
354 if ( $keys !== $values
355 || wfTimestamp( TS_MW, $row->mr_timestamp ) <= $wgCacheEpoch
356 ) {
357 $retval[$row->mr_resource] = self::updateModule( $row->mr_resource, $module, $lang );
358 } else {
359 $retval[$row->mr_resource] = $row->mr_blob;
360 }
361 }
362
363 return $retval;
364 }
365
366 /**
367 * Generate the message blob for a given module in a given language.
368 *
369 * @param ResourceLoaderModule $module
370 * @param string $lang Language code
371 * @return string JSON object
372 */
373 private static function generateMessageBlob( ResourceLoaderModule $module, $lang ) {
374 $messages = array();
375
376 foreach ( $module->getMessages() as $key ) {
377 $messages[$key] = wfMessage( $key )->inLanguage( $lang )->plain();
378 }
379
380 return FormatJson::encode( (object)$messages );
381 }
382 }