Merge "Don't apply CSS columns if less than 3 results were found on AllPages & Prefix...
[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 singleton instance
56 *
57 * @since 1.24
58 * @deprecated since 1.25
59 * @return MessageBlobStore
60 */
61 public static function getInstance() {
62 wfDeprecated( __METHOD__, '1.25' );
63 return new self;
64 }
65
66 /**
67 * Get the message blobs for a set of modules
68 *
69 * @param ResourceLoader $resourceLoader
70 * @param array $modules Array of module objects keyed by module name
71 * @param string $lang Language code
72 * @return array An array mapping module names to message blobs
73 */
74 public function get( ResourceLoader $resourceLoader, $modules, $lang ) {
75 if ( !count( $modules ) ) {
76 return array();
77 }
78
79 $blobs = array();
80
81 // Try in-process cache
82 $missingFromCache = array();
83 foreach ( $modules as $name => $module ) {
84 if ( isset( $this->blobCache[$lang][$name] ) ) {
85 $blobs[$name] = $this->blobCache[$lang][$name];
86 } else {
87 $missingFromCache[] = $name;
88 }
89 }
90
91 // Try DB cache
92 if ( $missingFromCache ) {
93 $blobs += $this->getFromDB( $resourceLoader, $missingFromCache, $lang );
94 }
95
96 // Generate new blobs for any remaining modules and store in DB
97 $missingFromDb = array_diff( array_keys( $modules ), array_keys( $blobs ) );
98 foreach ( $missingFromDb as $name ) {
99 $blob = $this->insertMessageBlob( $name, $modules[$name], $lang );
100 if ( $blob ) {
101 $blobs[$name] = $blob;
102 }
103 }
104
105 // Update in-process cache
106 if ( isset( $this->blobCache[$lang] ) ) {
107 $this->blobCache[$lang] += $blobs;
108 } else {
109 $this->blobCache[$lang] = $blobs;
110 }
111
112 return $blobs;
113 }
114
115 /**
116 * Generate and insert a new message blob. If the blob was already
117 * present, it is not regenerated; instead, the preexisting blob
118 * is fetched and returned.
119 *
120 * @param string $name Module name
121 * @param ResourceLoaderModule $module
122 * @param string $lang Language code
123 * @return mixed Message blob or false if the module has no messages
124 */
125 public function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
126 $blob = $this->generateMessageBlob( $module, $lang );
127
128 if ( !$blob ) {
129 return false;
130 }
131
132 try {
133 $dbw = wfGetDB( DB_MASTER );
134 $success = $dbw->insert( 'msg_resource', array(
135 'mr_lang' => $lang,
136 'mr_resource' => $name,
137 'mr_blob' => $blob,
138 'mr_timestamp' => $dbw->timestamp()
139 ),
140 __METHOD__,
141 array( 'IGNORE' )
142 );
143
144 if ( $success && $dbw->affectedRows() == 0 ) {
145 // Blob was already present, fetch it
146 $blob = $dbw->selectField( 'msg_resource', 'mr_blob', array(
147 'mr_resource' => $name,
148 'mr_lang' => $lang,
149 ),
150 __METHOD__
151 );
152 }
153 } catch ( DBError $e ) {
154 wfDebug( __METHOD__ . " failed to update DB: $e\n" );
155 }
156 return $blob;
157 }
158
159 /**
160 * Update the message blob for a given module in a given language
161 *
162 * @param string $name Module name
163 * @param ResourceLoaderModule $module
164 * @param string $lang Language code
165 * @return string|null Regenerated message blob, or null if there was no blob for
166 * the given module/language pair.
167 */
168 public function updateModule( $name, ResourceLoaderModule $module, $lang ) {
169 $dbw = wfGetDB( DB_MASTER );
170 $row = $dbw->selectRow( 'msg_resource', 'mr_blob',
171 array( 'mr_resource' => $name, 'mr_lang' => $lang ),
172 __METHOD__
173 );
174 if ( !$row ) {
175 return null;
176 }
177
178 $newBlob = $this->generateMessageBlob( $module, $lang );
179
180 try {
181 $newRow = array(
182 'mr_resource' => $name,
183 'mr_lang' => $lang,
184 'mr_blob' => $newBlob,
185 'mr_timestamp' => $dbw->timestamp()
186 );
187
188 $dbw->replace( 'msg_resource',
189 array( array( 'mr_resource', 'mr_lang' ) ),
190 $newRow, __METHOD__
191 );
192 } catch ( Exception $e ) {
193 wfDebug( __METHOD__ . " failed to update DB: $e\n" );
194 }
195 return $newBlob;
196 }
197
198 /**
199 * Update a single message in all message blobs it occurs in.
200 *
201 * @param string $key Message key
202 */
203 public function updateMessage( $key ) {
204 try {
205 $dbw = wfGetDB( DB_MASTER );
206
207 // Keep running until the updates queue is empty.
208 // Due to update conflicts, the queue might not be emptied
209 // in one iteration.
210 $updates = null;
211 do {
212 $updates = $this->getUpdatesForMessage( $key, $updates );
213
214 foreach ( $updates as $k => $update ) {
215 // Update the row on the condition that it
216 // didn't change since we fetched it by putting
217 // the timestamp in the WHERE clause.
218 $success = $dbw->update( 'msg_resource',
219 array(
220 'mr_blob' => $update['newBlob'],
221 'mr_timestamp' => $dbw->timestamp() ),
222 array(
223 'mr_resource' => $update['resource'],
224 'mr_lang' => $update['lang'],
225 'mr_timestamp' => $update['timestamp'] ),
226 __METHOD__
227 );
228
229 // Only requeue conflicted updates.
230 // If update() returned false, don't retry, for
231 // fear of getting into an infinite loop
232 if ( !( $success && $dbw->affectedRows() == 0 ) ) {
233 // Not conflicted
234 unset( $updates[$k] );
235 }
236 }
237 } while ( count( $updates ) );
238
239 } catch ( Exception $e ) {
240 wfDebug( __METHOD__ . " failed to update DB: $e\n" );
241 }
242 }
243
244 public function clear() {
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 } catch ( Exception $e ) {
251 wfDebug( __METHOD__ . " failed to update DB: $e\n" );
252 }
253 }
254
255 /**
256 * @return ResourceLoader
257 */
258 protected function getResourceLoader() {
259 // For back-compat this class supports instantiation without passing ResourceLoader
260 // Lazy-initialise this property because most callers don't need it.
261 if ( $this->resourceloader === null ) {
262 wfDebug( __CLASS__ . ' created without a ResourceLoader instance' );
263 $this->resourceloader = new ResourceLoader();
264 }
265
266 return $this->resourceloader;
267 }
268
269 /**
270 * Create an update queue for updateMessage()
271 *
272 * @param string $key Message key
273 * @param array $prevUpdates Updates queue to refresh or null to build a fresh update queue
274 * @return array Updates queue
275 */
276 private function getUpdatesForMessage( $key, $prevUpdates = null ) {
277 $dbw = wfGetDB( DB_MASTER );
278
279 if ( is_null( $prevUpdates ) ) {
280 $rl = $this->getResourceLoader();
281 $moduleNames = $rl->getModulesByMessage( $key );
282 // Fetch all blobs referencing $key
283 $res = $dbw->select(
284 array( 'msg_resource' ),
285 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
286 array(
287 'mr_resource' => $moduleNames,
288 ),
289 __METHOD__
290 );
291 } else {
292 // Refetch the blobs referenced by $prevUpdates
293
294 // Reorganize the (resource, lang) pairs in the format
295 // expected by makeWhereFrom2d()
296 $twoD = array();
297
298 foreach ( $prevUpdates as $update ) {
299 $twoD[$update['resource']][$update['lang']] = true;
300 }
301
302 $res = $dbw->select( 'msg_resource',
303 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
304 $dbw->makeWhereFrom2d( $twoD, 'mr_resource', 'mr_lang' ),
305 __METHOD__
306 );
307 }
308
309 // Build the new updates queue
310 $updates = array();
311
312 foreach ( $res as $row ) {
313 $updates[] = array(
314 'resource' => $row->mr_resource,
315 'lang' => $row->mr_lang,
316 'timestamp' => $row->mr_timestamp,
317 'newBlob' => $this->reencodeBlob( $row->mr_blob, $key, $row->mr_lang )
318 );
319 }
320
321 return $updates;
322 }
323
324 /**
325 * Reencode a message blob with the updated value for a message
326 *
327 * @param string $blob Message blob (JSON object)
328 * @param string $key Message key
329 * @param string $lang Language code
330 * @return string Message blob with $key replaced with its new value
331 */
332 private function reencodeBlob( $blob, $key, $lang ) {
333 $decoded = FormatJson::decode( $blob, true );
334 $decoded[$key] = wfMessage( $key )->inLanguage( $lang )->plain();
335
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] = wfMessage( $key )->inLanguage( $lang )->plain();
394 }
395
396 return FormatJson::encode( (object)$messages );
397 }
398 }