Fix a bug in the resource loader causing it to return no Content-Type header for...
[lhc/web/wiklou.git] / includes / MessageBlobStore.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @author Roan Kattouw
19 * @author Trevor Parscal
20 */
21
22 /**
23 * This class provides access to the resource message blobs storage used by
24 * the ResourceLoader.
25 *
26 * A message blob is a JSON object containing the interface messages for a
27 * certain resource in a certain language. These message blobs are cached
28 * in the msg_resource table and automatically invalidated when one of their
29 * consistuent messages or the resource itself is changed.
30 */
31 class MessageBlobStore {
32 /**
33 * Get the message blobs for a set of modules
34 * @param $modules array Array of module names
35 * @param $lang string Language code
36 * @return array An array mapping module names to message blobs
37 */
38 public static function get( $modules, $lang ) {
39 // TODO: Invalidate blob when module touched
40 if ( !count( $modules ) ) {
41 return array();
42 }
43 // Try getting from the DB first
44 $blobs = self::getFromDB( $modules, $lang );
45
46 // Generate blobs for any missing modules and store them in the DB
47 $missing = array_diff( $modules, array_keys( $blobs ) );
48 foreach ( $missing as $module ) {
49 $blob = self::insertMessageBlob( $module, $lang );
50 if ( $blob ) {
51 $blobs[$module] = $blob;
52 }
53 }
54 return $blobs;
55 }
56
57 /**
58 * Generate and insert a new message blob. If the blob was already
59 * present, it is not regenerated; instead, the preexisting blob
60 * is fetched and returned.
61 * @param $module string Module name
62 * @param $lang string Language code
63 * @return mixed Message blob or false if the module has no messages
64 */
65 public static function insertMessageBlob( $module, $lang ) {
66 $blob = self::generateMessageBlob( $module, $lang );
67 if ( !$blob ) {
68 return false;
69 }
70
71 $dbw = wfGetDb( DB_MASTER );
72 $success = $dbw->insert( 'msg_resource', array(
73 'mr_lang' => $lang,
74 'mr_resource' => $module,
75 'mr_blob' => $blob,
76 'mr_timestamp' => $dbw->timestamp()
77 ),
78 __METHOD__,
79 array( 'IGNORE' )
80 );
81 if ( $success ) {
82 if ( $dbw->affectedRows() == 0 ) {
83 // Blob was already present, fetch it
84 $blob = $dbr->selectField( 'msg_resource', 'mr_blob', array(
85 'mr_resource' => $module,
86 'mr_lang' => $lang,
87 ),
88 __METHOD__
89 );
90 } else {
91 // Update msg_resource_links
92 $rows = array();
93 $mod = ResourceLoader::getModule( $module );
94 foreach ( $mod->getMessages() as $key ) {
95 $rows[] = array(
96 'mrl_resource' => $module,
97 'mrl_message' => $key
98 );
99 }
100 $dbw->insert( 'msg_resource_links', $rows,
101 __METHOD__, array( 'IGNORE' )
102 );
103 }
104 }
105 return $blob;
106 }
107
108 /**
109 * Update all message blobs for a given module.
110 * @param $module string Module name
111 * @param $lang string Language code (optional)
112 * @return mixed If $lang is set, the new message blob for that language is returned if present. Otherwise, null is returned.
113 */
114 public static function updateModule( $module, $lang = null ) {
115 $retval = null;
116
117 // Find all existing blobs for this module
118 $dbw = wfGetDb( DB_MASTER );
119 $res = $dbw->select( 'msg_resource', array( 'mr_lang', 'mr_blob' ),
120 array( 'mr_resource' => $module ),
121 __METHOD__
122 );
123
124 // Build the new msg_resource rows
125 $newRows = array();
126 $now = $dbw->timestamp();
127 // Save the last-processed old and new blobs for later
128 $oldBlob = $newBlob = null;
129 foreach ( $res as $row ) {
130 $oldBlob = $row->mr_blob;
131 $newBlob = self::generateMessageBlob( $module, $row->mr_lang );
132 if ( $row->mr_lang === $lang ) {
133 $retval = $newBlob;
134 }
135 $newRows[] = array(
136 'mr_resource' => $module,
137 'mr_lang' => $row->mr_lang,
138 'mr_blob' => $newBlob,
139 'mr_timestamp' => $now
140 );
141 }
142
143 $dbw->replace( 'msg_resource',
144 array( array( 'mr_resource', 'mr_lang' ) ),
145 $newRows, __METHOD__
146 );
147
148 // Figure out which messages were added and removed
149 $oldMessages = array_keys( FormatJson::decode( $oldBlob, true ) );
150 $newMessages = array_keys( FormatJson::decode( $newBlob, true ) );
151 $added = array_diff( $newMessages, $oldMessages );
152 $removed = array_diff( $oldMessages, $newMessages );
153
154 // Delete removed messages, insert added ones
155 if ( $removed ) {
156 $dbw->delete( 'msg_resource_links', array(
157 'mrl_resource' => $module,
158 'mrl_message' => $removed
159 ), __METHOD__
160 );
161 }
162 $newLinksRows = array();
163 foreach ( $added as $message ) {
164 $newLinksRows[] = array(
165 'mrl_resource' => $module,
166 'mrl_message' => $message
167 );
168 }
169 if ( $newLinksRows ) {
170 $dbw->insert( 'msg_resource_links', $newLinksRows, __METHOD__,
171 array( 'IGNORE' ) // just in case
172 );
173 }
174
175 return $retval;
176 }
177
178 /**
179 * Update a single message in all message blobs it occurs in.
180 * @param $key string Message key
181 */
182 public static function updateMessage( $key ) {
183 $dbw = wfGetDb( DB_MASTER );
184
185 // Keep running until the updates queue is empty.
186 // Due to update conflicts, the queue might not be emptied
187 // in one iteration.
188 $updates = null;
189 do {
190 $updates = self::getUpdatesForMessage( $key, $updates );
191 foreach ( $updates as $key => $update ) {
192 // Update the row on the condition that it
193 // didn't change since we fetched it by putting
194 // the timestamp in the WHERE clause.
195 $success = $dbw->update( 'msg_resource',
196 array( 'mr_blob' => $update['newBlob'],
197 'mr_timestamp' => $dbw->timestamp() ),
198 array( 'mr_resource' => $update['resource'],
199 'mr_lang' => $update['lang'],
200 'mr_timestamp' => $update['timestamp'] ),
201 __METHOD__
202 );
203
204 // Only requeue conflicted updates.
205 // If update() returned false, don't retry, for
206 // fear of getting into an infinite loop
207 if ( !( $success && $dbw->affectedRows() == 0 ) ) {
208 // Not conflicted
209 unset( $updates[$key] );
210 }
211 }
212 } while ( count( $updates ) );
213
214 // No need to update msg_resource_links because we didn't add
215 // or remove any messages, we just changed their contents.
216 }
217
218 public static function clear() {
219 // TODO: Give this some more thought
220 // TODO: Is TRUNCATE better?
221 $dbw = wfGetDb( DB_MASTER );
222 $dbw->delete( 'msg_resource', '*', __METHOD__ );
223 $dbw->delete( 'msg_resource_links', '*', __METHOD__ );
224 }
225
226 /**
227 * Create an update queue for updateMessage()
228 * @param $key string Message key
229 * @param $prevUpdates array Updates queue to refresh or null to build a fresh update queue
230 * @return array Updates queue
231 */
232 private static function getUpdatesForMessage( $key, $prevUpdates = null ) {
233 $dbw = wfGetDb( DB_MASTER );
234 if ( is_null( $prevUpdates ) ) {
235 // Fetch all blobs referencing $key
236 $res = $dbw->select(
237 array( 'msg_resource', 'msg_resource_links' ),
238 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
239 array( 'mrl_message' => $key, 'mr_resource=mrl_resource' ),
240 __METHOD__
241 );
242 } else {
243 // Refetch the blobs referenced by $prevUpdates
244
245 // Reorganize the (resource, lang) pairs in the format
246 // expected by makeWhereFrom2d()
247 $twoD = array();
248 foreach ( $prevUpdates as $update ) {
249 $twoD[$update['resource']][$update['lang']] = true;
250 }
251
252 $res = $dbw->select( 'msg_resource',
253 array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
254 $dbw->makeWhereFrom2d( $twoD, 'mr_resource', 'mr_lang' ),
255 __METHOD__
256 );
257 }
258
259 // Build the new updates queue
260 $updates = array();
261 foreach ( $res as $row ) {
262 $updates[] = array(
263 'resource' => $row->mr_resource,
264 'lang' => $row->mr_lang,
265 'timestamp' => $row->mr_timestamp,
266 'newBlob' => self::reencodeBlob( $row->mr_blob,
267 $key, $row->mr_lang )
268 );
269 }
270 return $updates;
271 }
272
273 /**
274 * Reencode a message blob with the updated value for a message
275 * @param $blob string Message blob (JSON object)
276 * @param $key string Message key
277 * @param $lang string Language code
278 * @return Message blob with $key replaced with its new value
279 */
280 private static function reencodeBlob( $blob, $key, $lang ) {
281 $decoded = FormatJson::decode( $blob, true );
282 $decoded[$key] = wfMsgExt( $key, array( 'language' => $lang ) );
283 return FormatJson::encode( $decoded );
284 }
285
286 /**
287 * Get the message blobs for a set of modules from the database.
288 * Modules whose blobs are not in the database are silently dropped.
289 * @param $modules array Array of module names
290 * @param $lang string Language code
291 * @return array Array mapping module names to blobs
292 */
293 private static function getFromDB( $modules, $lang ) {
294 $retval = array();
295 $dbr = wfGetDB( DB_SLAVE );
296 $res = $dbr->select( 'msg_resource', array( 'mr_blob', 'mr_resource', 'mr_timestamp' ),
297 array( 'mr_resource' => $modules, 'mr_lang' => $lang ),
298 __METHOD__
299 );
300 foreach ( $res as $row ) {
301 $module = ResourceLoader::getModule( $row->mr_resource );
302 if ( !$module ) {
303 // This shouldn't be possible
304 throw new MWException( __METHOD__ . ' passed an invalid module name' );
305 }
306 if ( array_keys( FormatJson::decode( $row->mr_blob, true ) ) !== $module->getMessages() ) {
307 $retval[$row->mr_resource] = self::updateModule( $row->mr_resource, $lang );
308 } else {
309 $retval[$row->mr_resource] = $row->mr_blob;
310 }
311 }
312 return $retval;
313 }
314
315 /**
316 * Generate the message blob for a given module in a given language.
317 * @param $module string Module name
318 * @param $lang string Language code
319 * @return string JSON object
320 */
321 private static function generateMessageBlob( $module, $lang ) {
322 $mod = ResourceLoader::getModule( $module );
323 $messages = array();
324 foreach ( $mod->getMessages() as $key ) {
325 $messages[$key] = wfMsgExt( $key, array( 'language' => $lang ) );
326 }
327 return FormatJson::encode( $messages );
328 }
329 }