resourceloader: Make sure hashmtime cache key is different by language
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderModule.php
1 <?php
2 /**
3 * Abstraction for resource loader modules.
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 Trevor Parscal
22 * @author Roan Kattouw
23 */
24
25 /**
26 * Abstraction for resource loader modules, with name registration and maxage functionality.
27 */
28 abstract class ResourceLoaderModule {
29
30 # Type of resource
31 const TYPE_SCRIPTS = 'scripts';
32 const TYPE_STYLES = 'styles';
33 const TYPE_MESSAGES = 'messages';
34 const TYPE_COMBINED = 'combined';
35
36 # sitewide core module like a skin file or jQuery component
37 const ORIGIN_CORE_SITEWIDE = 1;
38
39 # per-user module generated by the software
40 const ORIGIN_CORE_INDIVIDUAL = 2;
41
42 # sitewide module generated from user-editable files, like MediaWiki:Common.js, or
43 # modules accessible to multiple users, such as those generated by the Gadgets extension.
44 const ORIGIN_USER_SITEWIDE = 3;
45
46 # per-user module generated from user-editable files, like User:Me/vector.js
47 const ORIGIN_USER_INDIVIDUAL = 4;
48
49 # an access constant; make sure this is kept as the largest number in this group
50 const ORIGIN_ALL = 10;
51
52 # script and style modules form a hierarchy of trustworthiness, with core modules like
53 # skins and jQuery as most trustworthy, and user scripts as least trustworthy. We can
54 # limit the types of scripts and styles we allow to load on, say, sensitive special
55 # pages like Special:UserLogin and Special:Preferences
56 protected $origin = self::ORIGIN_CORE_SITEWIDE;
57
58 /* Protected Members */
59
60 protected $name = null;
61 protected $targets = array( 'desktop' );
62
63 // In-object cache for file dependencies
64 protected $fileDeps = array();
65 // In-object cache for message blob mtime
66 protected $msgBlobMtime = array();
67
68 /* Methods */
69
70 /**
71 * Get this module's name. This is set when the module is registered
72 * with ResourceLoader::register()
73 *
74 * @return mixed: Name (string) or null if no name was set
75 */
76 public function getName() {
77 return $this->name;
78 }
79
80 /**
81 * Set this module's name. This is called by ResourceLoader::register()
82 * when registering the module. Other code should not call this.
83 *
84 * @param string $name Name
85 */
86 public function setName( $name ) {
87 $this->name = $name;
88 }
89
90 /**
91 * Get this module's origin. This is set when the module is registered
92 * with ResourceLoader::register()
93 *
94 * @return int: ResourceLoaderModule class constant, the subclass default
95 * if not set manually
96 */
97 public function getOrigin() {
98 return $this->origin;
99 }
100
101 /**
102 * Set this module's origin. This is called by ResourceLoader::register()
103 * when registering the module. Other code should not call this.
104 *
105 * @param int $origin origin
106 */
107 public function setOrigin( $origin ) {
108 $this->origin = $origin;
109 }
110
111 /**
112 * @param ResourceLoaderContext $context
113 * @return bool
114 */
115 public function getFlip( $context ) {
116 global $wgContLang;
117
118 return $wgContLang->getDir() !== $context->getDirection();
119 }
120
121 /**
122 * Get all JS for this module for a given language and skin.
123 * Includes all relevant JS except loader scripts.
124 *
125 * @param ResourceLoaderContext $context
126 * @return string: JavaScript code
127 */
128 public function getScript( ResourceLoaderContext $context ) {
129 // Stub, override expected
130 return '';
131 }
132
133 /**
134 * Get the URL or URLs to load for this module's JS in debug mode.
135 * The default behavior is to return a load.php?only=scripts URL for
136 * the module, but file-based modules will want to override this to
137 * load the files directly.
138 *
139 * This function is called only when 1) we're in debug mode, 2) there
140 * is no only= parameter and 3) supportsURLLoading() returns true.
141 * #2 is important to prevent an infinite loop, therefore this function
142 * MUST return either an only= URL or a non-load.php URL.
143 *
144 * @param ResourceLoaderContext $context
145 * @return array: Array of URLs
146 */
147 public function getScriptURLsForDebug( ResourceLoaderContext $context ) {
148 $url = ResourceLoader::makeLoaderURL(
149 array( $this->getName() ),
150 $context->getLanguage(),
151 $context->getSkin(),
152 $context->getUser(),
153 $context->getVersion(),
154 true, // debug
155 'scripts', // only
156 $context->getRequest()->getBool( 'printable' ),
157 $context->getRequest()->getBool( 'handheld' )
158 );
159 return array( $url );
160 }
161
162 /**
163 * Whether this module supports URL loading. If this function returns false,
164 * getScript() will be used even in cases (debug mode, no only param) where
165 * getScriptURLsForDebug() would normally be used instead.
166 * @return bool
167 */
168 public function supportsURLLoading() {
169 return true;
170 }
171
172 /**
173 * Get all CSS for this module for a given skin.
174 *
175 * @param ResourceLoaderContext $context
176 * @return array: List of CSS strings or array of CSS strings keyed by media type.
177 * like array( 'screen' => '.foo { width: 0 }' );
178 * or array( 'screen' => array( '.foo { width: 0 }' ) );
179 */
180 public function getStyles( ResourceLoaderContext $context ) {
181 // Stub, override expected
182 return array();
183 }
184
185 /**
186 * Get the URL or URLs to load for this module's CSS in debug mode.
187 * The default behavior is to return a load.php?only=styles URL for
188 * the module, but file-based modules will want to override this to
189 * load the files directly. See also getScriptURLsForDebug()
190 *
191 * @param ResourceLoaderContext $context
192 * @return array: array( mediaType => array( URL1, URL2, ... ), ... )
193 */
194 public function getStyleURLsForDebug( ResourceLoaderContext $context ) {
195 $url = ResourceLoader::makeLoaderURL(
196 array( $this->getName() ),
197 $context->getLanguage(),
198 $context->getSkin(),
199 $context->getUser(),
200 $context->getVersion(),
201 true, // debug
202 'styles', // only
203 $context->getRequest()->getBool( 'printable' ),
204 $context->getRequest()->getBool( 'handheld' )
205 );
206 return array( 'all' => array( $url ) );
207 }
208
209 /**
210 * Get the messages needed for this module.
211 *
212 * To get a JSON blob with messages, use MessageBlobStore::get()
213 *
214 * @return array: List of message keys. Keys may occur more than once
215 */
216 public function getMessages() {
217 // Stub, override expected
218 return array();
219 }
220
221 /**
222 * Get the group this module is in.
223 *
224 * @return string: Group name
225 */
226 public function getGroup() {
227 // Stub, override expected
228 return null;
229 }
230
231 /**
232 * Get the origin of this module. Should only be overridden for foreign modules.
233 *
234 * @return string: Origin name, 'local' for local modules
235 */
236 public function getSource() {
237 // Stub, override expected
238 return 'local';
239 }
240
241 /**
242 * Where on the HTML page should this module's JS be loaded?
243 * - 'top': in the "<head>"
244 * - 'bottom': at the bottom of the "<body>"
245 *
246 * @return string
247 */
248 public function getPosition() {
249 return 'bottom';
250 }
251
252 /**
253 * Whether this module's JS expects to work without the client-side ResourceLoader module.
254 * Returning true from this function will prevent mw.loader.state() call from being
255 * appended to the bottom of the script.
256 *
257 * @return bool
258 */
259 public function isRaw() {
260 return false;
261 }
262
263 /**
264 * Get the loader JS for this module, if set.
265 *
266 * @return mixed: JavaScript loader code as a string or boolean false if no custom loader set
267 */
268 public function getLoaderScript() {
269 // Stub, override expected
270 return false;
271 }
272
273 /**
274 * Get a list of modules this module depends on.
275 *
276 * Dependency information is taken into account when loading a module
277 * on the client side.
278 *
279 * To add dependencies dynamically on the client side, use a custom
280 * loader script, see getLoaderScript()
281 * @return array: List of module names as strings
282 */
283 public function getDependencies() {
284 // Stub, override expected
285 return array();
286 }
287
288 /**
289 * Get target(s) for the module, eg ['desktop'] or ['desktop', 'mobile']
290 *
291 * @return array: Array of strings
292 */
293 public function getTargets() {
294 return $this->targets;
295 }
296
297 /**
298 * Get the files this module depends on indirectly for a given skin.
299 * Currently these are only image files referenced by the module's CSS.
300 *
301 * @param string $skin Skin name
302 * @return array: List of files
303 */
304 public function getFileDependencies( $skin ) {
305 // Try in-object cache first
306 if ( isset( $this->fileDeps[$skin] ) ) {
307 return $this->fileDeps[$skin];
308 }
309
310 $dbr = wfGetDB( DB_SLAVE );
311 $deps = $dbr->selectField( 'module_deps', 'md_deps', array(
312 'md_module' => $this->getName(),
313 'md_skin' => $skin,
314 ), __METHOD__
315 );
316 if ( !is_null( $deps ) ) {
317 $this->fileDeps[$skin] = (array)FormatJson::decode( $deps, true );
318 } else {
319 $this->fileDeps[$skin] = array();
320 }
321 return $this->fileDeps[$skin];
322 }
323
324 /**
325 * Set preloaded file dependency information. Used so we can load this
326 * information for all modules at once.
327 * @param string $skin Skin name
328 * @param array $deps Array of file names
329 */
330 public function setFileDependencies( $skin, $deps ) {
331 $this->fileDeps[$skin] = $deps;
332 }
333
334 /**
335 * Get the last modification timestamp of the message blob for this
336 * module in a given language.
337 * @param string $lang Language code
338 * @return int: UNIX timestamp, or 0 if the module doesn't have messages
339 */
340 public function getMsgBlobMtime( $lang ) {
341 if ( !isset( $this->msgBlobMtime[$lang] ) ) {
342 if ( !count( $this->getMessages() ) ) {
343 return 0;
344 }
345
346 $dbr = wfGetDB( DB_SLAVE );
347 $msgBlobMtime = $dbr->selectField( 'msg_resource', 'mr_timestamp', array(
348 'mr_resource' => $this->getName(),
349 'mr_lang' => $lang
350 ), __METHOD__
351 );
352 // If no blob was found, but the module does have messages, that means we need
353 // to regenerate it. Return NOW
354 if ( $msgBlobMtime === false ) {
355 $msgBlobMtime = wfTimestampNow();
356 }
357 $this->msgBlobMtime[$lang] = wfTimestamp( TS_UNIX, $msgBlobMtime );
358 }
359 return $this->msgBlobMtime[$lang];
360 }
361
362 /**
363 * Set a preloaded message blob last modification timestamp. Used so we
364 * can load this information for all modules at once.
365 * @param string $lang Language code
366 * @param $mtime Integer: UNIX timestamp or 0 if there is no such blob
367 */
368 public function setMsgBlobMtime( $lang, $mtime ) {
369 $this->msgBlobMtime[$lang] = $mtime;
370 }
371
372 /* Abstract Methods */
373
374 /**
375 * Get this module's last modification timestamp for a given
376 * combination of language, skin and debug mode flag. This is typically
377 * the highest of each of the relevant components' modification
378 * timestamps. Whenever anything happens that changes the module's
379 * contents for these parameters, the mtime should increase.
380 *
381 * NOTE: The mtime of the module's messages is NOT automatically included.
382 * If you want this to happen, you'll need to call getMsgBlobMtime()
383 * yourself and take its result into consideration.
384 *
385 * NOTE: The mtime of the module's hash is NOT automatically included.
386 * If your module provides a getModifiedHash() method, you'll need to call getHashMtime()
387 * yourself and take its result into consideration.
388 *
389 * @param ResourceLoaderContext $context Context object
390 * @return integer UNIX timestamp
391 */
392 public function getModifiedTime( ResourceLoaderContext $context ) {
393 // 0 would mean now
394 return 1;
395 }
396
397 /**
398 * Helper method for calculating when the module's hash (if it has one) changed.
399 *
400 * @param ResourceLoaderContext $context
401 * @return integer: UNIX timestamp or 0 if no hash was provided
402 * by getModifiedHash()
403 */
404 public function getHashMtime( ResourceLoaderContext $context ) {
405 $hash = $this->getModifiedHash( $context );
406 if ( !is_string( $hash ) ) {
407 return 0;
408 }
409
410 $cache = wfGetCache( CACHE_ANYTHING );
411 $key = wfMemcKey( 'resourceloader', 'modulemodifiedhash', $this->getName(), $hash );
412
413 $data = $cache->get( $key );
414 if ( is_array( $data ) && $data['hash'] === $hash ) {
415 // Hash is still the same, re-use the timestamp of when we first saw this hash.
416 return $data['timestamp'];
417 }
418
419 $timestamp = wfTimestamp();
420 $cache->set( $key, array(
421 'hash' => $hash,
422 'timestamp' => $timestamp,
423 ) );
424
425 return $timestamp;
426 }
427
428 /**
429 * Get the hash for whatever this module may contain.
430 *
431 * This is the method subclasses should implement if they want to make
432 * use of getHashMTime() inside getModifiedTime().
433 *
434 * @param ResourceLoaderContext $context
435 * @return string|null: Hash
436 */
437 public function getModifiedHash( ResourceLoaderContext $context ) {
438 return null;
439 }
440
441 /**
442 * Helper method for calculating when this module's definition summary was last changed.
443 *
444 * @return integer: UNIX timestamp or 0 if no definition summary was provided
445 * by getDefinitionSummary()
446 */
447 public function getDefinitionMtime( ResourceLoaderContext $context ) {
448 wfProfileIn( __METHOD__ );
449 $summary = $this->getDefinitionSummary( $context );
450 if ( $summary === null ) {
451 wfProfileOut( __METHOD__ );
452 return 0;
453 }
454
455 $hash = md5( json_encode( $summary ) );
456
457 $cache = wfGetCache( CACHE_ANYTHING );
458
459 // Embed the hash itself in the cache key. This allows for a few nifty things:
460 // - During deployment, servers with old and new versions of the code communicating
461 // with the same memcached will not override the same key repeatedly increasing
462 // the timestamp.
463 // - In case of the definition changing and then changing back in a short period of time
464 // (e.g. in case of a revert or a corrupt server) the old timestamp and client-side cache
465 // url will be re-used.
466 // - If different context-combinations (e.g. same skin, same language or some combination
467 // thereof) result in the same definition, they will use the same hash and timestamp.
468 $key = wfMemcKey( 'resourceloader', 'moduledefinition', $this->getName(), $hash );
469
470 $data = $cache->get( $key );
471 if ( is_int( $data ) && $data > 0 ) {
472 // We've seen this hash before, re-use the timestamp of when we first saw it.
473 wfProfileOut( __METHOD__ );
474 return $data;
475 }
476
477 wfDebugLog( 'resourceloader', __METHOD__ . ": New definition hash for module {$this->getName()} in context {$context->getHash()}: $hash." );
478
479 $timestamp = time();
480 $cache->set( $key, $timestamp );
481
482 wfProfileOut( __METHOD__ );
483 return $timestamp;
484 }
485
486 /**
487 * Get the definition summary for this module.
488 *
489 * This is the method subclasses should implement if they want to make
490 * use of getDefinitionMTime() inside getModifiedTime().
491 *
492 * Return an array containing values from all significant properties of this
493 * module's definition. Be sure to include things that are explicitly ordered,
494 * in their actaul order (bug 37812).
495 *
496 * Avoid including things that are insiginificant (e.g. order of message
497 * keys is insignificant and should be sorted to avoid unnecessary cache
498 * invalidation).
499 *
500 * Avoid including things already considered by other methods inside your
501 * getModifiedTime(), such as file mtime timestamps.
502 *
503 * Serialisation is done using json_encode, which means object state is not
504 * taken into account when building the hash. This data structure must only
505 * contain arrays and scalars as values (avoid object instances) which means
506 * it requires abstraction.
507 *
508 * @return Array|null
509 */
510 public function getDefinitionSummary( ResourceLoaderContext $context ) {
511 return array(
512 'class' => get_class( $this ),
513 );
514 }
515
516 /**
517 * Check whether this module is known to be empty. If a child class
518 * has an easy and cheap way to determine that this module is
519 * definitely going to be empty, it should override this method to
520 * return true in that case. Callers may optimize the request for this
521 * module away if this function returns true.
522 * @param ResourceLoaderContext $context
523 * @return bool
524 */
525 public function isKnownEmpty( ResourceLoaderContext $context ) {
526 return false;
527 }
528
529 /** @var JSParser lazy-initialized; use self::javaScriptParser() */
530 private static $jsParser;
531 private static $parseCacheVersion = 1;
532
533 /**
534 * Validate a given script file; if valid returns the original source.
535 * If invalid, returns replacement JS source that throws an exception.
536 *
537 * @param string $fileName
538 * @param string $contents
539 * @return string: JS with the original, or a replacement error
540 */
541 protected function validateScriptFile( $fileName, $contents ) {
542 global $wgResourceLoaderValidateJS;
543 if ( $wgResourceLoaderValidateJS ) {
544 // Try for cache hit
545 // Use CACHE_ANYTHING since filtering is very slow compared to DB queries
546 $key = wfMemcKey( 'resourceloader', 'jsparse', self::$parseCacheVersion, md5( $contents ) );
547 $cache = wfGetCache( CACHE_ANYTHING );
548 $cacheEntry = $cache->get( $key );
549 if ( is_string( $cacheEntry ) ) {
550 return $cacheEntry;
551 }
552
553 $parser = self::javaScriptParser();
554 try {
555 $parser->parse( $contents, $fileName, 1 );
556 $result = $contents;
557 } catch ( Exception $e ) {
558 // We'll save this to cache to avoid having to validate broken JS over and over...
559 $err = $e->getMessage();
560 $result = "throw new Error(" . Xml::encodeJsVar( "JavaScript parse error: $err" ) . ");";
561 }
562
563 $cache->set( $key, $result );
564 return $result;
565 } else {
566 return $contents;
567 }
568 }
569
570 /**
571 * @return JSParser
572 */
573 protected static function javaScriptParser() {
574 if ( !self::$jsParser ) {
575 self::$jsParser = new JSParser();
576 }
577 return self::$jsParser;
578 }
579
580 /**
581 * Safe version of filemtime(), which doesn't throw a PHP warning if the file doesn't exist
582 * but returns 1 instead.
583 * @param string $filename File name
584 * @return int UNIX timestamp, or 1 if the file doesn't exist
585 */
586 protected static function safeFilemtime( $filename ) {
587 if ( file_exists( $filename ) ) {
588 return filemtime( $filename );
589 } else {
590 // We only ever map this function on an array if we're gonna call max() after,
591 // so return our standard minimum timestamps here. This is 1, not 0, because
592 // wfTimestamp(0) == NOW
593 return 1;
594 }
595 }
596 }