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