resourceloader: Use WAN cache for validateScriptFile cache
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderModule.php
1 <?php
2 /**
3 * Abstraction for ResourceLoader 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 ResourceLoader modules, with name registration and maxage functionality.
27 */
28 abstract class ResourceLoaderModule {
29 # Type of resource
30 const TYPE_SCRIPTS = 'scripts';
31 const TYPE_STYLES = 'styles';
32 const TYPE_COMBINED = 'combined';
33
34 # sitewide core module like a skin file or jQuery component
35 const ORIGIN_CORE_SITEWIDE = 1;
36
37 # per-user module generated by the software
38 const ORIGIN_CORE_INDIVIDUAL = 2;
39
40 # sitewide module generated from user-editable files, like MediaWiki:Common.js, or
41 # modules accessible to multiple users, such as those generated by the Gadgets extension.
42 const ORIGIN_USER_SITEWIDE = 3;
43
44 # per-user module generated from user-editable files, like User:Me/vector.js
45 const ORIGIN_USER_INDIVIDUAL = 4;
46
47 # an access constant; make sure this is kept as the largest number in this group
48 const ORIGIN_ALL = 10;
49
50 # script and style modules form a hierarchy of trustworthiness, with core modules like
51 # skins and jQuery as most trustworthy, and user scripts as least trustworthy. We can
52 # limit the types of scripts and styles we allow to load on, say, sensitive special
53 # pages like Special:UserLogin and Special:Preferences
54 protected $origin = self::ORIGIN_CORE_SITEWIDE;
55
56 /* Protected Members */
57
58 protected $name = null;
59 protected $targets = array( 'desktop' );
60
61 // In-object cache for file dependencies
62 protected $fileDeps = array();
63 // In-object cache for message blob mtime
64 protected $msgBlobMtime = array();
65 // In-object cache for version hash
66 protected $versionHash = array();
67 // In-object cache for module content
68 protected $contents = array();
69
70 /**
71 * @var Config
72 */
73 protected $config;
74
75 /* Methods */
76
77 /**
78 * Get this module's name. This is set when the module is registered
79 * with ResourceLoader::register()
80 *
81 * @return string|null Name (string) or null if no name was set
82 */
83 public function getName() {
84 return $this->name;
85 }
86
87 /**
88 * Set this module's name. This is called by ResourceLoader::register()
89 * when registering the module. Other code should not call this.
90 *
91 * @param string $name Name
92 */
93 public function setName( $name ) {
94 $this->name = $name;
95 }
96
97 /**
98 * Get this module's origin. This is set when the module is registered
99 * with ResourceLoader::register()
100 *
101 * @return int ResourceLoaderModule class constant, the subclass default
102 * if not set manually
103 */
104 public function getOrigin() {
105 return $this->origin;
106 }
107
108 /**
109 * Set this module's origin. This is called by ResourceLoader::register()
110 * when registering the module. Other code should not call this.
111 *
112 * @param int $origin Origin
113 */
114 public function setOrigin( $origin ) {
115 $this->origin = $origin;
116 }
117
118 /**
119 * @param ResourceLoaderContext $context
120 * @return bool
121 */
122 public function getFlip( $context ) {
123 global $wgContLang;
124
125 return $wgContLang->getDir() !== $context->getDirection();
126 }
127
128 /**
129 * Get all JS for this module for a given language and skin.
130 * Includes all relevant JS except loader scripts.
131 *
132 * @param ResourceLoaderContext $context
133 * @return string JavaScript code
134 */
135 public function getScript( ResourceLoaderContext $context ) {
136 // Stub, override expected
137 return '';
138 }
139
140 /**
141 * Takes named templates by the module and returns an array mapping.
142 *
143 * @return array of templates mapping template alias to content
144 */
145 public function getTemplates() {
146 // Stub, override expected.
147 return array();
148 }
149
150 /**
151 * @return Config
152 * @since 1.24
153 */
154 public function getConfig() {
155 if ( $this->config === null ) {
156 // Ugh, fall back to default
157 $this->config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
158 }
159
160 return $this->config;
161 }
162
163 /**
164 * @param Config $config
165 * @since 1.24
166 */
167 public function setConfig( Config $config ) {
168 $this->config = $config;
169 }
170
171 /**
172 * Get the URL or URLs to load for this module's JS in debug mode.
173 * The default behavior is to return a load.php?only=scripts URL for
174 * the module, but file-based modules will want to override this to
175 * load the files directly.
176 *
177 * This function is called only when 1) we're in debug mode, 2) there
178 * is no only= parameter and 3) supportsURLLoading() returns true.
179 * #2 is important to prevent an infinite loop, therefore this function
180 * MUST return either an only= URL or a non-load.php URL.
181 *
182 * @param ResourceLoaderContext $context
183 * @return array Array of URLs
184 */
185 public function getScriptURLsForDebug( ResourceLoaderContext $context ) {
186 $resourceLoader = $context->getResourceLoader();
187 $derivative = new DerivativeResourceLoaderContext( $context );
188 $derivative->setModules( array( $this->getName() ) );
189 $derivative->setOnly( 'scripts' );
190 $derivative->setDebug( true );
191
192 $url = $resourceLoader->createLoaderURL(
193 $this->getSource(),
194 $derivative
195 );
196
197 return array( $url );
198 }
199
200 /**
201 * Whether this module supports URL loading. If this function returns false,
202 * getScript() will be used even in cases (debug mode, no only param) where
203 * getScriptURLsForDebug() would normally be used instead.
204 * @return bool
205 */
206 public function supportsURLLoading() {
207 return true;
208 }
209
210 /**
211 * Get all CSS for this module for a given skin.
212 *
213 * @param ResourceLoaderContext $context
214 * @return array List of CSS strings or array of CSS strings keyed by media type.
215 * like array( 'screen' => '.foo { width: 0 }' );
216 * or array( 'screen' => array( '.foo { width: 0 }' ) );
217 */
218 public function getStyles( ResourceLoaderContext $context ) {
219 // Stub, override expected
220 return array();
221 }
222
223 /**
224 * Get the URL or URLs to load for this module's CSS in debug mode.
225 * The default behavior is to return a load.php?only=styles URL for
226 * the module, but file-based modules will want to override this to
227 * load the files directly. See also getScriptURLsForDebug()
228 *
229 * @param ResourceLoaderContext $context
230 * @return array Array( mediaType => array( URL1, URL2, ... ), ... )
231 */
232 public function getStyleURLsForDebug( ResourceLoaderContext $context ) {
233 $resourceLoader = $context->getResourceLoader();
234 $derivative = new DerivativeResourceLoaderContext( $context );
235 $derivative->setModules( array( $this->getName() ) );
236 $derivative->setOnly( 'styles' );
237 $derivative->setDebug( true );
238
239 $url = $resourceLoader->createLoaderURL(
240 $this->getSource(),
241 $derivative
242 );
243
244 return array( 'all' => array( $url ) );
245 }
246
247 /**
248 * Get the messages needed for this module.
249 *
250 * To get a JSON blob with messages, use MessageBlobStore::get()
251 *
252 * @return array List of message keys. Keys may occur more than once
253 */
254 public function getMessages() {
255 // Stub, override expected
256 return array();
257 }
258
259 /**
260 * Get the group this module is in.
261 *
262 * @return string Group name
263 */
264 public function getGroup() {
265 // Stub, override expected
266 return null;
267 }
268
269 /**
270 * Get the origin of this module. Should only be overridden for foreign modules.
271 *
272 * @return string Origin name, 'local' for local modules
273 */
274 public function getSource() {
275 // Stub, override expected
276 return 'local';
277 }
278
279 /**
280 * Where on the HTML page should this module's JS be loaded?
281 * - 'top': in the "<head>"
282 * - 'bottom': at the bottom of the "<body>"
283 *
284 * @return string
285 */
286 public function getPosition() {
287 return 'bottom';
288 }
289
290 /**
291 * Whether this module's JS expects to work without the client-side ResourceLoader module.
292 * Returning true from this function will prevent mw.loader.state() call from being
293 * appended to the bottom of the script.
294 *
295 * @return bool
296 */
297 public function isRaw() {
298 return false;
299 }
300
301 /**
302 * Get a list of modules this module depends on.
303 *
304 * Dependency information is taken into account when loading a module
305 * on the client side.
306 *
307 * Note: It is expected that $context will be made non-optional in the near
308 * future.
309 *
310 * @param ResourceLoaderContext $context
311 * @return array List of module names as strings
312 */
313 public function getDependencies( ResourceLoaderContext $context = null ) {
314 // Stub, override expected
315 return array();
316 }
317
318 /**
319 * Get target(s) for the module, eg ['desktop'] or ['desktop', 'mobile']
320 *
321 * @return array Array of strings
322 */
323 public function getTargets() {
324 return $this->targets;
325 }
326
327 /**
328 * Get the skip function.
329 *
330 * Modules that provide fallback functionality can provide a "skip function". This
331 * function, if provided, will be passed along to the module registry on the client.
332 * When this module is loaded (either directly or as a dependency of another module),
333 * then this function is executed first. If the function returns true, the module will
334 * instantly be considered "ready" without requesting the associated module resources.
335 *
336 * The value returned here must be valid javascript for execution in a private function.
337 * It must not contain the "function () {" and "}" wrapper though.
338 *
339 * @return string|null A JavaScript function body returning a boolean value, or null
340 */
341 public function getSkipFunction() {
342 return null;
343 }
344
345 /**
346 * Get the files this module depends on indirectly for a given skin.
347 *
348 * These are only image files referenced by the module's stylesheet.
349 *
350 * @param ResourceLoaderContext $context
351 * @return array List of files
352 */
353 protected function getFileDependencies( ResourceLoaderContext $context ) {
354 $vary = $context->getSkin() . '|' . $context->getLanguage();
355
356 // Try in-object cache first
357 if ( !isset( $this->fileDeps[$vary] ) ) {
358 $dbr = wfGetDB( DB_SLAVE );
359 $deps = $dbr->selectField( 'module_deps',
360 'md_deps',
361 array(
362 'md_module' => $this->getName(),
363 'md_skin' => $vary,
364 ),
365 __METHOD__
366 );
367
368 if ( !is_null( $deps ) ) {
369 $this->fileDeps[$vary] = self::expandRelativePaths(
370 (array)FormatJson::decode( $deps, true )
371 );
372 } else {
373 $this->fileDeps[$vary] = array();
374 }
375 }
376 return $this->fileDeps[$vary];
377 }
378
379 /**
380 * Set in-object cache for file dependencies.
381 *
382 * This is used to retrieve data in batches. See ResourceLoader::preloadModuleInfo().
383 * To save the data, use saveFileDependencies().
384 *
385 * @param string $skin Skin name
386 * @param array $deps Array of file names
387 */
388 public function setFileDependencies( ResourceLoaderContext $context, $files ) {
389 $vary = $context->getSkin() . '|' . $context->getLanguage();
390 $this->fileDeps[$vary] = $files;
391 }
392
393 /**
394 * Set the files this module depends on indirectly for a given skin.
395 *
396 * @since 1.27
397 * @param ResourceLoaderContext $context
398 * @param array $localFileRefs List of files
399 */
400 protected function saveFileDependencies( ResourceLoaderContext $context, $localFileRefs ) {
401 // Normalise array
402 $localFileRefs = array_values( array_unique( $localFileRefs ) );
403 sort( $localFileRefs );
404
405 try {
406 // If the list has been modified since last time we cached it, update the cache
407 if ( $localFileRefs !== $this->getFileDependencies( $context ) ) {
408 $vary = $context->getSkin() . '|' . $context->getLanguage();
409 $dbw = wfGetDB( DB_MASTER );
410 $dbw->replace( 'module_deps',
411 array( array( 'md_module', 'md_skin' ) ), array(
412 'md_module' => $this->getName(),
413 'md_skin' => $vary,
414 // Use relative paths to avoid ghost entries when $IP changes (T111481)
415 'md_deps' => FormatJson::encode( self::getRelativePaths( $localFileRefs ) ),
416 )
417 );
418 }
419 } catch ( Exception $e ) {
420 wfDebugLog( 'resourceloader', __METHOD__ . ": failed to update DB: $e" );
421 }
422 }
423
424 /**
425 * Make file paths relative to MediaWiki directory.
426 *
427 * This is used to make file paths safe for storing in a database without the paths
428 * becoming stale or incorrect when MediaWiki is moved or upgraded (T111481).
429 *
430 * @since 1.27
431 * @param array $filePaths
432 * @return array
433 */
434 public static function getRelativePaths( Array $filePaths ) {
435 global $IP;
436 return array_map( function ( $path ) use ( $IP ) {
437 return RelPath\getRelativePath( $path, $IP );
438 }, $filePaths );
439 }
440
441 /**
442 * Expand directories relative to $IP.
443 *
444 * @since 1.27
445 * @param array $filePaths
446 * @return array
447 */
448 public static function expandRelativePaths( Array $filePaths ) {
449 global $IP;
450 return array_map( function ( $path ) use ( $IP ) {
451 return RelPath\joinPath( $IP, $path );
452 }, $filePaths );
453 }
454
455 /**
456 * Get the last modification timestamp of the messages in this module for a given language.
457 * @param string $lang Language code
458 * @return int UNIX timestamp
459 */
460 public function getMsgBlobMtime( $lang ) {
461 if ( !isset( $this->msgBlobMtime[$lang] ) ) {
462 if ( !count( $this->getMessages() ) ) {
463 return 1;
464 }
465
466 $dbr = wfGetDB( DB_SLAVE );
467 $msgBlobMtime = $dbr->selectField( 'msg_resource',
468 'mr_timestamp',
469 array(
470 'mr_resource' => $this->getName(),
471 'mr_lang' => $lang
472 ),
473 __METHOD__
474 );
475 // If no blob was found, but the module does have messages, that means we need
476 // to regenerate it. Return NOW
477 if ( $msgBlobMtime === false ) {
478 $msgBlobMtime = wfTimestampNow();
479 }
480 $this->msgBlobMtime[$lang] = wfTimestamp( TS_UNIX, $msgBlobMtime );
481 }
482 return $this->msgBlobMtime[$lang];
483 }
484
485 /**
486 * Set in-object cache for message blob time.
487 *
488 * This is used to retrieve data in batches. See ResourceLoader::preloadModuleInfo().
489 *
490 * @param string $lang Language code
491 * @param int $mtime UNIX timestamp
492 */
493 public function setMsgBlobMtime( $lang, $mtime ) {
494 $this->msgBlobMtime[$lang] = $mtime;
495 }
496
497 /**
498 * Get module-specific LESS variables, if any.
499 *
500 * @since 1.27
501 * @param ResourceLoaderContext $context
502 * @return array Module-specific LESS variables.
503 */
504 protected function getLessVars( ResourceLoaderContext $context ) {
505 return array();
506 }
507
508 /**
509 * Get an array of this module's resources. Ready for serving to the web.
510 *
511 * @since 1.26
512 * @param ResourceLoaderContext $context
513 * @return array
514 */
515 public function getModuleContent( ResourceLoaderContext $context ) {
516 $contextHash = $context->getHash();
517 // Cache this expensive operation. This calls builds the scripts, styles, and messages
518 // content which typically involves filesystem and/or database access.
519 if ( !array_key_exists( $contextHash, $this->contents ) ) {
520 $this->contents[$contextHash] = $this->buildContent( $context );
521 }
522 return $this->contents[$contextHash];
523 }
524
525 /**
526 * Bundle all resources attached to this module into an array.
527 *
528 * @since 1.26
529 * @param ResourceLoaderContext $context
530 * @return array
531 */
532 final protected function buildContent( ResourceLoaderContext $context ) {
533 $rl = $context->getResourceLoader();
534 $stats = RequestContext::getMain()->getStats();
535 $statStart = microtime( true );
536
537 // Only include properties that are relevant to this context (e.g. only=scripts)
538 // and that are non-empty (e.g. don't include "templates" for modules without
539 // templates). This helps prevent invalidating cache for all modules when new
540 // optional properties are introduced.
541 $content = array();
542
543 // Scripts
544 if ( $context->shouldIncludeScripts() ) {
545 // If we are in debug mode, we'll want to return an array of URLs if possible
546 // However, we can't do this if the module doesn't support it
547 // We also can't do this if there is an only= parameter, because we have to give
548 // the module a way to return a load.php URL without causing an infinite loop
549 if ( $context->getDebug() && !$context->getOnly() && $this->supportsURLLoading() ) {
550 $scripts = $this->getScriptURLsForDebug( $context );
551 } else {
552 $scripts = $this->getScript( $context );
553 // rtrim() because there are usually a few line breaks
554 // after the last ';'. A new line at EOF, a new line
555 // added by ResourceLoaderFileModule::readScriptFiles, etc.
556 if ( is_string( $scripts )
557 && strlen( $scripts )
558 && substr( rtrim( $scripts ), -1 ) !== ';'
559 ) {
560 // Append semicolon to prevent weird bugs caused by files not
561 // terminating their statements right (bug 27054)
562 $scripts .= ";\n";
563 }
564 }
565 $content['scripts'] = $scripts;
566 }
567
568 // Styles
569 if ( $context->shouldIncludeStyles() ) {
570 $styles = array();
571 // Don't create empty stylesheets like array( '' => '' ) for modules
572 // that don't *have* any stylesheets (bug 38024).
573 $stylePairs = $this->getStyles( $context );
574 if ( count( $stylePairs ) ) {
575 // If we are in debug mode without &only= set, we'll want to return an array of URLs
576 // See comment near shouldIncludeScripts() for more details
577 if ( $context->getDebug() && !$context->getOnly() && $this->supportsURLLoading() ) {
578 $styles = array(
579 'url' => $this->getStyleURLsForDebug( $context )
580 );
581 } else {
582 // Minify CSS before embedding in mw.loader.implement call
583 // (unless in debug mode)
584 if ( !$context->getDebug() ) {
585 foreach ( $stylePairs as $media => $style ) {
586 // Can be either a string or an array of strings.
587 if ( is_array( $style ) ) {
588 $stylePairs[$media] = array();
589 foreach ( $style as $cssText ) {
590 if ( is_string( $cssText ) ) {
591 $stylePairs[$media][] =
592 ResourceLoader::filter( 'minify-css', $cssText );
593 }
594 }
595 } elseif ( is_string( $style ) ) {
596 $stylePairs[$media] = ResourceLoader::filter( 'minify-css', $style );
597 }
598 }
599 }
600 // Wrap styles into @media groups as needed and flatten into a numerical array
601 $styles = array(
602 'css' => $rl->makeCombinedStyles( $stylePairs )
603 );
604 }
605 }
606 $content['styles'] = $styles;
607 }
608
609 // Messages
610 $blobs = $rl->getMessageBlobStore()->get(
611 $rl,
612 array( $this->getName() => $this ),
613 $context->getLanguage()
614 );
615 if ( isset( $blobs[$this->getName()] ) ) {
616 $content['messagesBlob'] = $blobs[$this->getName()];
617 }
618
619 $templates = $this->getTemplates();
620 if ( $templates ) {
621 $content['templates'] = $templates;
622 }
623
624 $statTiming = microtime( true ) - $statStart;
625 $statName = strtr( $this->getName(), '.', '_' );
626 $stats->timing( "resourceloader_build.all", 1000 * $statTiming );
627 $stats->timing( "resourceloader_build.$statName", 1000 * $statTiming );
628
629 return $content;
630 }
631
632 /**
633 * Get a string identifying the current version of this module in a given context.
634 *
635 * Whenever anything happens that changes the module's response (e.g. scripts, styles, and
636 * messages) this value must change. This value is used to store module responses in cache.
637 * (Both client-side and server-side.)
638 *
639 * It is not recommended to override this directly. Use getDefinitionSummary() instead.
640 * If overridden, one must call the parent getVersionHash(), append data and re-hash.
641 *
642 * This method should be quick because it is frequently run by ResourceLoaderStartUpModule to
643 * propagate changes to the client and effectively invalidate cache.
644 *
645 * For backward-compatibility, the following optional data providers are automatically included:
646 *
647 * - getModifiedTime()
648 * - getModifiedHash()
649 *
650 * @since 1.26
651 * @param ResourceLoaderContext $context
652 * @return string Hash (should use ResourceLoader::makeHash)
653 */
654 public function getVersionHash( ResourceLoaderContext $context ) {
655 // The startup module produces a manifest with versions representing the entire module.
656 // Typically, the request for the startup module itself has only=scripts. That must apply
657 // only to the startup module content, and not to the module version computed here.
658 $context = new DerivativeResourceLoaderContext( $context );
659 $context->setModules( array() );
660 // Version hash must cover all resources, regardless of startup request itself.
661 $context->setOnly( null );
662 // Compute version hash based on content, not debug urls.
663 $context->setDebug( false );
664
665 // Cache this somewhat expensive operation. Especially because some classes
666 // (e.g. startup module) iterate more than once over all modules to get versions.
667 $contextHash = $context->getHash();
668 if ( !array_key_exists( $contextHash, $this->versionHash ) ) {
669
670 if ( $this->enableModuleContentVersion() ) {
671 // Detect changes directly
672 $str = json_encode( $this->getModuleContent( $context ) );
673 } else {
674 // Infer changes based on definition and other metrics
675 $summary = $this->getDefinitionSummary( $context );
676 if ( !isset( $summary['_cacheEpoch'] ) ) {
677 throw new LogicException( 'getDefinitionSummary must call parent method' );
678 }
679 $str = json_encode( $summary );
680
681 $mtime = $this->getModifiedTime( $context );
682 if ( $mtime !== null ) {
683 // Support: MediaWiki 1.25 and earlier
684 $str .= strval( $mtime );
685 }
686
687 $mhash = $this->getModifiedHash( $context );
688 if ( $mhash !== null ) {
689 // Support: MediaWiki 1.25 and earlier
690 $str .= strval( $mhash );
691 }
692 }
693
694 $this->versionHash[$contextHash] = ResourceLoader::makeHash( $str );
695 }
696 return $this->versionHash[$contextHash];
697 }
698
699 /**
700 * Whether to generate version hash based on module content.
701 *
702 * If a module requires database or file system access to build the module
703 * content, consider disabling this in favour of manually tracking relevant
704 * aspects in getDefinitionSummary(). See getVersionHash() for how this is used.
705 *
706 * @return bool
707 */
708 public function enableModuleContentVersion() {
709 return false;
710 }
711
712 /**
713 * Get the definition summary for this module.
714 *
715 * This is the method subclasses are recommended to use to track values in their
716 * version hash. Call this in getVersionHash() and pass it to e.g. json_encode.
717 *
718 * Subclasses must call the parent getDefinitionSummary() and build on that.
719 * It is recommended that each subclass appends its own new array. This prevents
720 * clashes or accidental overwrites of existing keys and gives each subclass
721 * its own scope for simple array keys.
722 *
723 * @code
724 * $summary = parent::getDefinitionSummary( $context );
725 * $summary[] = array(
726 * 'foo' => 123,
727 * 'bar' => 'quux',
728 * );
729 * return $summary;
730 * @endcode
731 *
732 * Return an array containing values from all significant properties of this
733 * module's definition.
734 *
735 * Be careful not to normalise too much. Especially preserve the order of things
736 * that carry significance in getScript and getStyles (T39812).
737 *
738 * Avoid including things that are insiginificant (e.g. order of message keys is
739 * insignificant and should be sorted to avoid unnecessary cache invalidation).
740 *
741 * This data structure must exclusively contain arrays and scalars as values (avoid
742 * object instances) to allow simple serialisation using json_encode.
743 *
744 * If modules have a hash or timestamp from another source, that may be incuded as-is.
745 *
746 * A number of utility methods are available to help you gather data. These are not
747 * called by default and must be included by the subclass' getDefinitionSummary().
748 *
749 * - getMsgBlobMtime()
750 *
751 * @since 1.23
752 * @param ResourceLoaderContext $context
753 * @return array|null
754 */
755 public function getDefinitionSummary( ResourceLoaderContext $context ) {
756 return array(
757 '_class' => get_class( $this ),
758 '_cacheEpoch' => $this->getConfig()->get( 'CacheEpoch' ),
759 );
760 }
761
762 /**
763 * Get this module's last modification timestamp for a given context.
764 *
765 * @deprecated since 1.26 Use getDefinitionSummary() instead
766 * @param ResourceLoaderContext $context Context object
767 * @return int|null UNIX timestamp
768 */
769 public function getModifiedTime( ResourceLoaderContext $context ) {
770 return null;
771 }
772
773 /**
774 * Helper method for providing a version hash to getVersionHash().
775 *
776 * @deprecated since 1.26 Use getDefinitionSummary() instead
777 * @param ResourceLoaderContext $context
778 * @return string|null Hash
779 */
780 public function getModifiedHash( ResourceLoaderContext $context ) {
781 return null;
782 }
783
784 /**
785 * Back-compat dummy for old subclass implementations of getModifiedTime().
786 *
787 * This method used to use ObjectCache to track when a hash was first seen. That principle
788 * stems from a time that ResourceLoader could only identify module versions by timestamp.
789 * That is no longer the case. Use getDefinitionSummary() directly.
790 *
791 * @deprecated since 1.26 Superseded by getVersionHash()
792 * @param ResourceLoaderContext $context
793 * @return int UNIX timestamp
794 */
795 public function getHashMtime( ResourceLoaderContext $context ) {
796 if ( !is_string( $this->getModifiedHash( $context ) ) ) {
797 return 1;
798 }
799 // Dummy that is > 1
800 return 2;
801 }
802
803 /**
804 * Back-compat dummy for old subclass implementations of getModifiedTime().
805 *
806 * @since 1.23
807 * @deprecated since 1.26 Superseded by getVersionHash()
808 * @param ResourceLoaderContext $context
809 * @return int UNIX timestamp
810 */
811 public function getDefinitionMtime( ResourceLoaderContext $context ) {
812 if ( $this->getDefinitionSummary( $context ) === null ) {
813 return 1;
814 }
815 // Dummy that is > 1
816 return 2;
817 }
818
819 /**
820 * Check whether this module is known to be empty. If a child class
821 * has an easy and cheap way to determine that this module is
822 * definitely going to be empty, it should override this method to
823 * return true in that case. Callers may optimize the request for this
824 * module away if this function returns true.
825 * @param ResourceLoaderContext $context
826 * @return bool
827 */
828 public function isKnownEmpty( ResourceLoaderContext $context ) {
829 return false;
830 }
831
832 /** @var JSParser Lazy-initialized; use self::javaScriptParser() */
833 private static $jsParser;
834 private static $parseCacheVersion = 1;
835
836 /**
837 * Validate a given script file; if valid returns the original source.
838 * If invalid, returns replacement JS source that throws an exception.
839 *
840 * @param string $fileName
841 * @param string $contents
842 * @return string JS with the original, or a replacement error
843 */
844 protected function validateScriptFile( $fileName, $contents ) {
845 if ( $this->getConfig()->get( 'ResourceLoaderValidateJS' ) ) {
846 // Try for cache hit
847 $cache = ObjectCache::getMainWANInstance();
848 $key = $cache->makeKey(
849 'resourceloader',
850 'jsparse',
851 self::$parseCacheVersion,
852 md5( $contents )
853 );
854 $cacheEntry = $cache->get( $key );
855 if ( is_string( $cacheEntry ) ) {
856 return $cacheEntry;
857 }
858
859 $parser = self::javaScriptParser();
860 try {
861 $parser->parse( $contents, $fileName, 1 );
862 $result = $contents;
863 } catch ( Exception $e ) {
864 // We'll save this to cache to avoid having to validate broken JS over and over...
865 $err = $e->getMessage();
866 $result = "mw.log.error(" .
867 Xml::encodeJsVar( "JavaScript parse error: $err" ) . ");";
868 }
869
870 $cache->set( $key, $result );
871 return $result;
872 } else {
873 return $contents;
874 }
875 }
876
877 /**
878 * @return JSParser
879 */
880 protected static function javaScriptParser() {
881 if ( !self::$jsParser ) {
882 self::$jsParser = new JSParser();
883 }
884 return self::$jsParser;
885 }
886
887 /**
888 * Safe version of filemtime(), which doesn't throw a PHP warning if the file doesn't exist.
889 * Defaults to 1.
890 *
891 * @param string $filePath File path
892 * @return int UNIX timestamp
893 */
894 protected static function safeFilemtime( $filePath ) {
895 MediaWiki\suppressWarnings();
896 $mtime = filemtime( $filePath ) ?: 1;
897 MediaWiki\restoreWarnings();
898 return $mtime;
899 }
900
901 /**
902 * Compute a non-cryptographic string hash of a file's contents.
903 * If the file does not exist or cannot be read, returns an empty string.
904 *
905 * @since 1.26 Uses MD4 instead of SHA1.
906 * @param string $filePath File path
907 * @return string Hash
908 */
909 protected static function safeFileHash( $filePath ) {
910 return FileContentsHasher::getFileContentsHash( $filePath );
911 }
912 }