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