3 * Resource message blobs storage used by ResourceLoader.
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.
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.
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
21 * @author Roan Kattouw
22 * @author Trevor Parscal
26 * This class provides access to the message blobs used by ResourceLoader modules.
28 * A message blob is a JSON object containing the interface messages for a
29 * certain module in a certain language. These message blobs are cached
30 * in the automatically invalidated when one of their constituent messages,
31 * or the module definition, is changed.
33 class MessageBlobStore
{
35 * In-process cache for message blobs.
37 * Keyed by language code, then module name.
41 protected $blobCache = array();
43 /* @var ResourceLoader */
44 protected $resourceloader;
47 * @param ResourceLoader $resourceloader
49 public function __construct( ResourceLoader
$resourceloader = null ) {
50 $this->resourceloader
= $resourceloader;
54 * Get the message blobs for a set of modules
56 * @param ResourceLoader $resourceLoader
57 * @param array $modules Array of module objects keyed by module name
58 * @param string $lang Language code
59 * @return array An array mapping module names to message blobs
61 public function get( ResourceLoader
$resourceLoader, $modules, $lang ) {
62 if ( !count( $modules ) ) {
68 // Try in-process cache
69 $missingFromCache = array();
70 foreach ( $modules as $name => $module ) {
71 if ( isset( $this->blobCache
[$lang][$name] ) ) {
72 $blobs[$name] = $this->blobCache
[$lang][$name];
74 $missingFromCache[$name] = $module;
79 if ( $missingFromCache ) {
80 $blobs +
= $this->getFromDB( $missingFromCache, $lang );
83 // Generate new blobs for any remaining modules and store in DB
84 $missingFromDb = array_diff( array_keys( $modules ), array_keys( $blobs ) );
85 foreach ( $missingFromDb as $name ) {
86 $blob = $this->insertMessageBlob( $name, $modules[$name], $lang );
88 $blobs[$name] = $blob;
92 // Update in-process cache
93 if ( isset( $this->blobCache
[$lang] ) ) {
94 $this->blobCache
[$lang] +
= $blobs;
96 $this->blobCache
[$lang] = $blobs;
103 * Generate and insert a new message blob. If the blob was already
104 * present, it is not regenerated; instead, the preexisting blob
105 * is fetched and returned.
107 * @param string $name Module name
108 * @param ResourceLoaderModule $module
109 * @param string $lang Language code
110 * @return string JSON blob
112 public function insertMessageBlob( $name, ResourceLoaderModule
$module, $lang ) {
113 $blob = $this->generateMessageBlob( $module, $lang );
120 $dbw = wfGetDB( DB_MASTER
);
121 $success = $dbw->insert( 'msg_resource', array(
123 'mr_resource' => $name,
125 'mr_timestamp' => $dbw->timestamp()
131 if ( $success && $dbw->affectedRows() == 0 ) {
132 // Blob was already present, fetch it
133 $blob = $dbw->selectField( 'msg_resource', 'mr_blob', array(
134 'mr_resource' => $name,
140 } catch ( DBError
$e ) {
141 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
147 * Update the message blob for a given module in a given language
149 * @param string $name Module name
150 * @param ResourceLoaderModule $module
151 * @param string $lang Language code
152 * @return string|null Regenerated message blob, or null if there was no blob for
153 * the given module/language pair.
155 public function updateModule( $name, ResourceLoaderModule
$module, $lang ) {
156 $dbw = wfGetDB( DB_MASTER
);
157 $row = $dbw->selectRow( 'msg_resource', 'mr_blob',
158 array( 'mr_resource' => $name, 'mr_lang' => $lang ),
165 $newBlob = $this->generateMessageBlob( $module, $lang );
169 'mr_resource' => $name,
171 'mr_blob' => $newBlob,
172 'mr_timestamp' => $dbw->timestamp()
175 $dbw->replace( 'msg_resource',
176 array( array( 'mr_resource', 'mr_lang' ) ),
179 } catch ( Exception
$e ) {
180 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
186 * Update a single message in all message blobs it occurs in.
188 * @param string $key Message key
190 public function updateMessage( $key ) {
192 $dbw = wfGetDB( DB_MASTER
);
194 // Keep running until the updates queue is empty.
195 // Due to update conflicts, the queue might not be emptied
199 $updates = $this->getUpdatesForMessage( $key, $updates );
201 foreach ( $updates as $k => $update ) {
202 // Update the row on the condition that it
203 // didn't change since we fetched it by putting
204 // the timestamp in the WHERE clause.
205 $success = $dbw->update( 'msg_resource',
207 'mr_blob' => $update['newBlob'],
208 'mr_timestamp' => $dbw->timestamp() ),
210 'mr_resource' => $update['resource'],
211 'mr_lang' => $update['lang'],
212 'mr_timestamp' => $update['timestamp'] ),
216 // Only requeue conflicted updates.
217 // If update() returned false, don't retry, for
218 // fear of getting into an infinite loop
219 if ( !( $success && $dbw->affectedRows() == 0 ) ) {
221 unset( $updates[$k] );
224 } while ( count( $updates ) );
226 } catch ( Exception
$e ) {
227 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
231 public function clear() {
233 // Not using TRUNCATE, because that needs extra permissions,
234 // which maybe not granted to the database user.
235 $dbw = wfGetDB( DB_MASTER
);
236 $dbw->delete( 'msg_resource', '*', __METHOD__
);
237 } catch ( Exception
$e ) {
238 wfDebug( __METHOD__
. " failed to update DB: $e\n" );
243 * @return ResourceLoader
245 protected function getResourceLoader() {
246 // For back-compat this class supports instantiation without passing ResourceLoader
247 // Lazy-initialise this property because most callers don't need it.
248 if ( $this->resourceloader
=== null ) {
249 wfDebug( __CLASS__
. ' created without a ResourceLoader instance' );
250 $this->resourceloader
= new ResourceLoader();
253 return $this->resourceloader
;
257 * Create an update queue for updateMessage()
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
263 private function getUpdatesForMessage( $key, $prevUpdates = null ) {
264 $dbw = wfGetDB( DB_MASTER
);
266 if ( is_null( $prevUpdates ) ) {
267 $rl = $this->getResourceLoader();
268 $moduleNames = $rl->getModulesByMessage( $key );
269 // Fetch all blobs referencing $key
271 array( 'msg_resource' ),
272 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
274 'mr_resource' => $moduleNames,
279 // Refetch the blobs referenced by $prevUpdates
281 // Reorganize the (resource, lang) pairs in the format
282 // expected by makeWhereFrom2d()
285 foreach ( $prevUpdates as $update ) {
286 $twoD[$update['resource']][$update['lang']] = true;
289 $res = $dbw->select( 'msg_resource',
290 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
291 $dbw->makeWhereFrom2d( $twoD, 'mr_resource', 'mr_lang' ),
296 // Build the new updates queue
299 foreach ( $res as $row ) {
301 'resource' => $row->mr_resource
,
302 'lang' => $row->mr_lang
,
303 'timestamp' => $row->mr_timestamp
,
304 'newBlob' => $this->reencodeBlob( $row->mr_blob
, $key, $row->mr_lang
)
312 * @param string $key Message key
313 * @param string $lang Language code
316 private function fetchMessage( $key, $lang ) {
317 $message = wfMessage( $key )->inLanguage( $lang );
318 if ( !$message->exists() ) {
319 wfDebugLog( 'resourceloader', __METHOD__
. " failed to find: '$key' ($lang)" );
321 return $message->plain();
325 * Reencode a message blob with the updated value for a message
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
332 private function reencodeBlob( $blob, $key, $lang ) {
333 $decoded = FormatJson
::decode( $blob, true );
334 $decoded[$key] = $this->fetchMessage( $key, $lang );
335 return FormatJson
::encode( (object)$decoded );
339 * Get the message blobs for a set of modules from the database.
340 * Modules whose blobs are not in the database are silently dropped.
342 * @param array $modules Array of module objects by name
343 * @param string $lang Language code
344 * @throws MWException
345 * @return array Array mapping module names to blobs
347 private function getFromDB( $modules, $lang ) {
348 if ( !count( $modules ) ) {
353 $dbr = wfGetDB( DB_SLAVE
);
354 $res = $dbr->select( 'msg_resource',
355 array( 'mr_blob', 'mr_resource', 'mr_timestamp' ),
356 array( 'mr_resource' => array_keys( $modules ), 'mr_lang' => $lang ),
360 foreach ( $res as $row ) {
361 if ( !isset( $modules[ $row->mr_resource
] ) ) {
362 // This shouldn't be possible
363 throw new MWException( __METHOD__
. ' passed an invalid module name' );
365 $module = $modules[ $row->mr_resource
];
367 // Update the module's blob if the list of messages changed
368 $blobKeys = array_keys( FormatJson
::decode( $row->mr_blob
, true ) );
369 $moduleMsgs = array_values( array_unique( $module->getMessages() ) );
370 if ( $blobKeys !== $moduleMsgs ) {
371 $retval[$row->mr_resource
] = $this->updateModule( $row->mr_resource
, $module, $lang );
373 $retval[$row->mr_resource
] = $row->mr_blob
;
381 * Generate the message blob for a given module in a given language.
383 * @param ResourceLoaderModule $module
384 * @param string $lang Language code
385 * @return string JSON blob
387 private function generateMessageBlob( ResourceLoaderModule
$module, $lang ) {
390 foreach ( $module->getMessages() as $key ) {
391 $messages[$key] = $this->fetchMessage( $key, $lang );
394 return FormatJson
::encode( (object)$messages );