3 * Abstraction for ResourceLoader modules.
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 Trevor Parscal
22 * @author Roan Kattouw
25 use MediaWiki\MediaWikiServices
;
26 use Psr\Log\LoggerAwareInterface
;
27 use Psr\Log\LoggerInterface
;
28 use Psr\Log\NullLogger
;
29 use Wikimedia\RelPath
;
30 use Wikimedia\ScopedCallback
;
33 * Abstraction for ResourceLoader modules, with name registration and maxage functionality.
35 abstract class ResourceLoaderModule
implements LoggerAwareInterface
{
37 const TYPE_SCRIPTS
= 'scripts';
38 const TYPE_STYLES
= 'styles';
39 const TYPE_COMBINED
= 'combined';
42 // Module only has styles (loaded via <style> or <link rel=stylesheet>)
43 const LOAD_STYLES
= 'styles';
44 // Module may have other resources (loaded via mw.loader from a script)
45 const LOAD_GENERAL
= 'general';
47 # sitewide core module like a skin file or jQuery component
48 const ORIGIN_CORE_SITEWIDE
= 1;
50 # per-user module generated by the software
51 const ORIGIN_CORE_INDIVIDUAL
= 2;
53 # sitewide module generated from user-editable files, like MediaWiki:Common.js, or
54 # modules accessible to multiple users, such as those generated by the Gadgets extension.
55 const ORIGIN_USER_SITEWIDE
= 3;
57 # per-user module generated from user-editable files, like User:Me/vector.js
58 const ORIGIN_USER_INDIVIDUAL
= 4;
60 # an access constant; make sure this is kept as the largest number in this group
61 const ORIGIN_ALL
= 10;
63 # script and style modules form a hierarchy of trustworthiness, with core modules like
64 # skins and jQuery as most trustworthy, and user scripts as least trustworthy. We can
65 # limit the types of scripts and styles we allow to load on, say, sensitive special
66 # pages like Special:UserLogin and Special:Preferences
67 protected $origin = self
::ORIGIN_CORE_SITEWIDE
;
69 protected $name = null;
70 protected $targets = [ 'desktop' ];
72 // In-object cache for file dependencies
73 protected $fileDeps = [];
74 // In-object cache for message blob (keyed by language)
75 protected $msgBlobs = [];
76 // In-object cache for version hash
77 protected $versionHash = [];
78 // In-object cache for module content
79 protected $contents = [];
89 protected $deprecated = false;
92 * @var LoggerInterface
97 * Get this module's name. This is set when the module is registered
98 * with ResourceLoader::register()
100 * @return string|null Name (string) or null if no name was set
102 public function getName() {
107 * Set this module's name. This is called by ResourceLoader::register()
108 * when registering the module. Other code should not call this.
110 * @param string $name
112 public function setName( $name ) {
117 * Get this module's origin. This is set when the module is registered
118 * with ResourceLoader::register()
120 * @return int ResourceLoaderModule class constant, the subclass default
121 * if not set manually
123 public function getOrigin() {
124 return $this->origin
;
128 * @param ResourceLoaderContext $context
131 public function getFlip( $context ) {
134 return $wgContLang->getDir() !== $context->getDirection();
138 * Get JS representing deprecation information for the current module if available
140 * @return string JavaScript code
142 protected function getDeprecationInformation() {
143 $deprecationInfo = $this->deprecated
;
144 if ( $deprecationInfo ) {
145 $name = $this->getName();
146 $warning = 'This page is using the deprecated ResourceLoader module "' . $name . '".';
147 if ( is_string( $deprecationInfo ) ) {
148 $warning .= "\n" . $deprecationInfo;
150 return Xml
::encodeJsCall(
160 * Get all JS for this module for a given language and skin.
161 * Includes all relevant JS except loader scripts.
163 * @param ResourceLoaderContext $context
164 * @return string JavaScript code
166 public function getScript( ResourceLoaderContext
$context ) {
167 // Stub, override expected
172 * Takes named templates by the module and returns an array mapping.
174 * @return array of templates mapping template alias to content
176 public function getTemplates() {
177 // Stub, override expected.
185 public function getConfig() {
186 if ( $this->config
=== null ) {
187 // Ugh, fall back to default
188 $this->config
= MediaWikiServices
::getInstance()->getMainConfig();
191 return $this->config
;
195 * @param Config $config
198 public function setConfig( Config
$config ) {
199 $this->config
= $config;
204 * @param LoggerInterface $logger
207 public function setLogger( LoggerInterface
$logger ) {
208 $this->logger
= $logger;
213 * @return LoggerInterface
215 protected function getLogger() {
216 if ( !$this->logger
) {
217 $this->logger
= new NullLogger();
219 return $this->logger
;
223 * Get the URL or URLs to load for this module's JS in debug mode.
224 * The default behavior is to return a load.php?only=scripts URL for
225 * the module, but file-based modules will want to override this to
226 * load the files directly.
228 * This function is called only when 1) we're in debug mode, 2) there
229 * is no only= parameter and 3) supportsURLLoading() returns true.
230 * #2 is important to prevent an infinite loop, therefore this function
231 * MUST return either an only= URL or a non-load.php URL.
233 * @param ResourceLoaderContext $context
234 * @return array Array of URLs
236 public function getScriptURLsForDebug( ResourceLoaderContext
$context ) {
237 $resourceLoader = $context->getResourceLoader();
238 $derivative = new DerivativeResourceLoaderContext( $context );
239 $derivative->setModules( [ $this->getName() ] );
240 $derivative->setOnly( 'scripts' );
241 $derivative->setDebug( true );
243 $url = $resourceLoader->createLoaderURL(
252 * Whether this module supports URL loading. If this function returns false,
253 * getScript() will be used even in cases (debug mode, no only param) where
254 * getScriptURLsForDebug() would normally be used instead.
257 public function supportsURLLoading() {
262 * Get all CSS for this module for a given skin.
264 * @param ResourceLoaderContext $context
265 * @return array List of CSS strings or array of CSS strings keyed by media type.
266 * like [ 'screen' => '.foo { width: 0 }' ];
267 * or [ 'screen' => [ '.foo { width: 0 }' ] ];
269 public function getStyles( ResourceLoaderContext
$context ) {
270 // Stub, override expected
275 * Get the URL or URLs to load for this module's CSS in debug mode.
276 * The default behavior is to return a load.php?only=styles URL for
277 * the module, but file-based modules will want to override this to
278 * load the files directly. See also getScriptURLsForDebug()
280 * @param ResourceLoaderContext $context
281 * @return array [ mediaType => [ URL1, URL2, ... ], ... ]
283 public function getStyleURLsForDebug( ResourceLoaderContext
$context ) {
284 $resourceLoader = $context->getResourceLoader();
285 $derivative = new DerivativeResourceLoaderContext( $context );
286 $derivative->setModules( [ $this->getName() ] );
287 $derivative->setOnly( 'styles' );
288 $derivative->setDebug( true );
290 $url = $resourceLoader->createLoaderURL(
295 return [ 'all' => [ $url ] ];
299 * Get the messages needed for this module.
301 * To get a JSON blob with messages, use MessageBlobStore::get()
303 * @return array List of message keys. Keys may occur more than once
305 public function getMessages() {
306 // Stub, override expected
311 * Get the group this module is in.
313 * @return string Group name
315 public function getGroup() {
316 // Stub, override expected
321 * Get the origin of this module. Should only be overridden for foreign modules.
323 * @return string Origin name, 'local' for local modules
325 public function getSource() {
326 // Stub, override expected
331 * Whether this module's JS expects to work without the client-side ResourceLoader module.
332 * Returning true from this function will prevent mw.loader.state() call from being
333 * appended to the bottom of the script.
337 public function isRaw() {
342 * Get a list of modules this module depends on.
344 * Dependency information is taken into account when loading a module
345 * on the client side.
347 * Note: It is expected that $context will be made non-optional in the near
350 * @param ResourceLoaderContext $context
351 * @return array List of module names as strings
353 public function getDependencies( ResourceLoaderContext
$context = null ) {
354 // Stub, override expected
359 * Get target(s) for the module, eg ['desktop'] or ['desktop', 'mobile']
361 * @return array Array of strings
363 public function getTargets() {
364 return $this->targets
;
368 * Get the module's load type.
371 * @return string ResourceLoaderModule LOAD_* constant
373 public function getType() {
374 return self
::LOAD_GENERAL
;
378 * Get the skip function.
380 * Modules that provide fallback functionality can provide a "skip function". This
381 * function, if provided, will be passed along to the module registry on the client.
382 * When this module is loaded (either directly or as a dependency of another module),
383 * then this function is executed first. If the function returns true, the module will
384 * instantly be considered "ready" without requesting the associated module resources.
386 * The value returned here must be valid javascript for execution in a private function.
387 * It must not contain the "function () {" and "}" wrapper though.
389 * @return string|null A JavaScript function body returning a boolean value, or null
391 public function getSkipFunction() {
396 * Get the files this module depends on indirectly for a given skin.
398 * These are only image files referenced by the module's stylesheet.
400 * @param ResourceLoaderContext $context
401 * @return array List of files
403 protected function getFileDependencies( ResourceLoaderContext
$context ) {
404 $vary = $context->getSkin() . '|' . $context->getLanguage();
406 // Try in-object cache first
407 if ( !isset( $this->fileDeps
[$vary] ) ) {
408 $dbr = wfGetDB( DB_REPLICA
);
409 $deps = $dbr->selectField( 'module_deps',
412 'md_module' => $this->getName(),
418 if ( !is_null( $deps ) ) {
419 $this->fileDeps
[$vary] = self
::expandRelativePaths(
420 (array)FormatJson
::decode( $deps, true )
423 $this->fileDeps
[$vary] = [];
426 return $this->fileDeps
[$vary];
430 * Set in-object cache for file dependencies.
432 * This is used to retrieve data in batches. See ResourceLoader::preloadModuleInfo().
433 * To save the data, use saveFileDependencies().
435 * @param ResourceLoaderContext $context
436 * @param string[] $files Array of file names
438 public function setFileDependencies( ResourceLoaderContext
$context, $files ) {
439 $vary = $context->getSkin() . '|' . $context->getLanguage();
440 $this->fileDeps
[$vary] = $files;
444 * Set the files this module depends on indirectly for a given skin.
447 * @param ResourceLoaderContext $context
448 * @param array $localFileRefs List of files
450 protected function saveFileDependencies( ResourceLoaderContext
$context, $localFileRefs ) {
452 // Related bugs and performance considerations:
453 // 1. Don't needlessly change the database value with the same list in a
454 // different order or with duplicates.
455 // 2. Use relative paths to avoid ghost entries when $IP changes. (T111481)
456 // 3. Don't needlessly replace the database with the same value
457 // just because $IP changed (e.g. when upgrading a wiki).
458 // 4. Don't create an endless replace loop on every request for this
459 // module when '../' is used anywhere. Even though both are expanded
460 // (one expanded by getFileDependencies from the DB, the other is
461 // still raw as originally read by RL), the latter has not
462 // been normalized yet.
465 $localFileRefs = array_values( array_unique( $localFileRefs ) );
466 sort( $localFileRefs );
467 $localPaths = self
::getRelativePaths( $localFileRefs );
469 $storedPaths = self
::getRelativePaths( $this->getFileDependencies( $context ) );
470 // If the list has been modified since last time we cached it, update the cache
471 if ( $localPaths !== $storedPaths ) {
472 $vary = $context->getSkin() . '|' . $context->getLanguage();
473 $cache = ObjectCache
::getLocalClusterInstance();
474 $key = $cache->makeKey( __METHOD__
, $this->getName(), $vary );
475 $scopeLock = $cache->getScopedLock( $key, 0 );
477 return; // T124649; avoid write slams
480 $deps = FormatJson
::encode( $localPaths );
481 $dbw = wfGetDB( DB_MASTER
);
482 $dbw->upsert( 'module_deps',
484 'md_module' => $this->getName(),
488 [ 'md_module', 'md_skin' ],
494 if ( $dbw->trxLevel() ) {
495 $dbw->onTransactionResolution(
496 function () use ( &$scopeLock ) {
497 ScopedCallback
::consume( $scopeLock ); // release after commit
503 } catch ( Exception
$e ) {
504 wfDebugLog( 'resourceloader', __METHOD__
. ": failed to update DB: $e" );
509 * Make file paths relative to MediaWiki directory.
511 * This is used to make file paths safe for storing in a database without the paths
512 * becoming stale or incorrect when MediaWiki is moved or upgraded (T111481).
515 * @param array $filePaths
518 public static function getRelativePaths( array $filePaths ) {
520 return array_map( function ( $path ) use ( $IP ) {
521 return RelPath
::getRelativePath( $path, $IP );
526 * Expand directories relative to $IP.
529 * @param array $filePaths
532 public static function expandRelativePaths( array $filePaths ) {
534 return array_map( function ( $path ) use ( $IP ) {
535 return RelPath
::joinPath( $IP, $path );
540 * Get the hash of the message blob.
543 * @param ResourceLoaderContext $context
544 * @return string|null JSON blob or null if module has no messages
546 protected function getMessageBlob( ResourceLoaderContext
$context ) {
547 if ( !$this->getMessages() ) {
548 // Don't bother consulting MessageBlobStore
551 // Message blobs may only vary language, not by context keys
552 $lang = $context->getLanguage();
553 if ( !isset( $this->msgBlobs
[$lang] ) ) {
554 $this->getLogger()->warning( 'Message blob for {module} should have been preloaded', [
555 'module' => $this->getName(),
557 $store = $context->getResourceLoader()->getMessageBlobStore();
558 $this->msgBlobs
[$lang] = $store->getBlob( $this, $lang );
560 return $this->msgBlobs
[$lang];
564 * Set in-object cache for message blobs.
566 * Used to allow fetching of message blobs in batches. See ResourceLoader::preloadModuleInfo().
569 * @param string|null $blob JSON blob or null
570 * @param string $lang Language code
572 public function setMessageBlob( $blob, $lang ) {
573 $this->msgBlobs
[$lang] = $blob;
577 * Get headers to send as part of a module web response.
579 * It is not supported to send headers through this method that are
580 * required to be unique or otherwise sent once in an HTTP response
581 * because clients may make batch requests for multiple modules (as
582 * is the default behaviour for ResourceLoader clients).
584 * For exclusive or aggregated headers, see ResourceLoader::sendResponseHeaders().
587 * @param ResourceLoaderContext $context
588 * @return string[] Array of HTTP response headers
590 final public function getHeaders( ResourceLoaderContext
$context ) {
593 $formattedLinks = [];
594 foreach ( $this->getPreloadLinks( $context ) as $url => $attribs ) {
595 $link = "<{$url}>;rel=preload";
596 foreach ( $attribs as $key => $val ) {
597 $link .= ";{$key}={$val}";
599 $formattedLinks[] = $link;
601 if ( $formattedLinks ) {
602 $headers[] = 'Link: ' . implode( ',', $formattedLinks );
609 * Get a list of resources that web browsers may preload.
611 * Behaviour of rel=preload link is specified at <https://www.w3.org/TR/preload/>.
613 * Use case for ResourceLoader originally part of T164299.
617 * protected function getPreloadLinks() {
619 * 'https://example.org/script.js' => [ 'as' => 'script' ],
620 * 'https://example.org/image.png' => [ 'as' => 'image' ],
625 * @par Example using HiDPI image variants
627 * protected function getPreloadLinks() {
629 * 'https://example.org/logo.png' => [
631 * 'media' => 'not all and (min-resolution: 2dppx)',
633 * 'https://example.org/logo@2x.png' => [
635 * 'media' => '(min-resolution: 2dppx)',
641 * @see ResourceLoaderModule::getHeaders
643 * @param ResourceLoaderContext $context
644 * @return array Keyed by url, values must be an array containing
645 * at least an 'as' key. Optionally a 'media' key as well.
647 protected function getPreloadLinks( ResourceLoaderContext
$context ) {
652 * Get module-specific LESS variables, if any.
655 * @param ResourceLoaderContext $context
656 * @return array Module-specific LESS variables.
658 protected function getLessVars( ResourceLoaderContext
$context ) {
663 * Get an array of this module's resources. Ready for serving to the web.
666 * @param ResourceLoaderContext $context
669 public function getModuleContent( ResourceLoaderContext
$context ) {
670 $contextHash = $context->getHash();
671 // Cache this expensive operation. This calls builds the scripts, styles, and messages
672 // content which typically involves filesystem and/or database access.
673 if ( !array_key_exists( $contextHash, $this->contents
) ) {
674 $this->contents
[$contextHash] = $this->buildContent( $context );
676 return $this->contents
[$contextHash];
680 * Bundle all resources attached to this module into an array.
683 * @param ResourceLoaderContext $context
686 final protected function buildContent( ResourceLoaderContext
$context ) {
687 $rl = $context->getResourceLoader();
688 $stats = MediaWikiServices
::getInstance()->getStatsdDataFactory();
689 $statStart = microtime( true );
691 // Only include properties that are relevant to this context (e.g. only=scripts)
692 // and that are non-empty (e.g. don't include "templates" for modules without
693 // templates). This helps prevent invalidating cache for all modules when new
694 // optional properties are introduced.
698 if ( $context->shouldIncludeScripts() ) {
699 // If we are in debug mode, we'll want to return an array of URLs if possible
700 // However, we can't do this if the module doesn't support it
701 // We also can't do this if there is an only= parameter, because we have to give
702 // the module a way to return a load.php URL without causing an infinite loop
703 if ( $context->getDebug() && !$context->getOnly() && $this->supportsURLLoading() ) {
704 $scripts = $this->getScriptURLsForDebug( $context );
706 $scripts = $this->getScript( $context );
707 // Make the script safe to concatenate by making sure there is at least one
708 // trailing new line at the end of the content. Previously, this looked for
709 // a semi-colon instead, but that breaks concatenation if the semicolon
710 // is inside a comment like "// foo();". Instead, simply use a
711 // line break as separator which matches JavaScript native logic for implicitly
712 // ending statements even if a semi-colon is missing.
713 // Bugs: T29054, T162719.
714 if ( is_string( $scripts )
715 && strlen( $scripts )
716 && substr( $scripts, -1 ) !== "\n"
721 $content['scripts'] = $scripts;
725 if ( $context->shouldIncludeStyles() ) {
727 // Don't create empty stylesheets like [ '' => '' ] for modules
728 // that don't *have* any stylesheets (T40024).
729 $stylePairs = $this->getStyles( $context );
730 if ( count( $stylePairs ) ) {
731 // If we are in debug mode without &only= set, we'll want to return an array of URLs
732 // See comment near shouldIncludeScripts() for more details
733 if ( $context->getDebug() && !$context->getOnly() && $this->supportsURLLoading() ) {
735 'url' => $this->getStyleURLsForDebug( $context )
738 // Minify CSS before embedding in mw.loader.implement call
739 // (unless in debug mode)
740 if ( !$context->getDebug() ) {
741 foreach ( $stylePairs as $media => $style ) {
742 // Can be either a string or an array of strings.
743 if ( is_array( $style ) ) {
744 $stylePairs[$media] = [];
745 foreach ( $style as $cssText ) {
746 if ( is_string( $cssText ) ) {
747 $stylePairs[$media][] =
748 ResourceLoader
::filter( 'minify-css', $cssText );
751 } elseif ( is_string( $style ) ) {
752 $stylePairs[$media] = ResourceLoader
::filter( 'minify-css', $style );
756 // Wrap styles into @media groups as needed and flatten into a numerical array
758 'css' => $rl->makeCombinedStyles( $stylePairs )
762 $content['styles'] = $styles;
766 $blob = $this->getMessageBlob( $context );
768 $content['messagesBlob'] = $blob;
771 $templates = $this->getTemplates();
773 $content['templates'] = $templates;
776 $headers = $this->getHeaders( $context );
778 $content['headers'] = $headers;
781 $statTiming = microtime( true ) - $statStart;
782 $statName = strtr( $this->getName(), '.', '_' );
783 $stats->timing( "resourceloader_build.all", 1000 * $statTiming );
784 $stats->timing( "resourceloader_build.$statName", 1000 * $statTiming );
790 * Get a string identifying the current version of this module in a given context.
792 * Whenever anything happens that changes the module's response (e.g. scripts, styles, and
793 * messages) this value must change. This value is used to store module responses in cache.
794 * (Both client-side and server-side.)
796 * It is not recommended to override this directly. Use getDefinitionSummary() instead.
797 * If overridden, one must call the parent getVersionHash(), append data and re-hash.
799 * This method should be quick because it is frequently run by ResourceLoaderStartUpModule to
800 * propagate changes to the client and effectively invalidate cache.
802 * For backward-compatibility, the following optional data providers are automatically included:
804 * - getModifiedTime()
805 * - getModifiedHash()
808 * @param ResourceLoaderContext $context
809 * @return string Hash (should use ResourceLoader::makeHash)
811 public function getVersionHash( ResourceLoaderContext
$context ) {
812 // The startup module produces a manifest with versions representing the entire module.
813 // Typically, the request for the startup module itself has only=scripts. That must apply
814 // only to the startup module content, and not to the module version computed here.
815 $context = new DerivativeResourceLoaderContext( $context );
816 $context->setModules( [] );
817 // Version hash must cover all resources, regardless of startup request itself.
818 $context->setOnly( null );
819 // Compute version hash based on content, not debug urls.
820 $context->setDebug( false );
822 // Cache this somewhat expensive operation. Especially because some classes
823 // (e.g. startup module) iterate more than once over all modules to get versions.
824 $contextHash = $context->getHash();
825 if ( !array_key_exists( $contextHash, $this->versionHash
) ) {
826 if ( $this->enableModuleContentVersion() ) {
827 // Detect changes directly
828 $str = json_encode( $this->getModuleContent( $context ) );
830 // Infer changes based on definition and other metrics
831 $summary = $this->getDefinitionSummary( $context );
832 if ( !isset( $summary['_cacheEpoch'] ) ) {
833 throw new LogicException( 'getDefinitionSummary must call parent method' );
835 $str = json_encode( $summary );
837 $mtime = $this->getModifiedTime( $context );
838 if ( $mtime !== null ) {
839 // Support: MediaWiki 1.25 and earlier
840 $str .= strval( $mtime );
843 $mhash = $this->getModifiedHash( $context );
844 if ( $mhash !== null ) {
845 // Support: MediaWiki 1.25 and earlier
846 $str .= strval( $mhash );
850 $this->versionHash
[$contextHash] = ResourceLoader
::makeHash( $str );
852 return $this->versionHash
[$contextHash];
856 * Whether to generate version hash based on module content.
858 * If a module requires database or file system access to build the module
859 * content, consider disabling this in favour of manually tracking relevant
860 * aspects in getDefinitionSummary(). See getVersionHash() for how this is used.
864 public function enableModuleContentVersion() {
869 * Get the definition summary for this module.
871 * This is the method subclasses are recommended to use to track values in their
872 * version hash. Call this in getVersionHash() and pass it to e.g. json_encode.
874 * Subclasses must call the parent getDefinitionSummary() and build on that.
875 * It is recommended that each subclass appends its own new array. This prevents
876 * clashes or accidental overwrites of existing keys and gives each subclass
877 * its own scope for simple array keys.
880 * $summary = parent::getDefinitionSummary( $context );
888 * Return an array containing values from all significant properties of this
889 * module's definition.
891 * Be careful not to normalise too much. Especially preserve the order of things
892 * that carry significance in getScript and getStyles (T39812).
894 * Avoid including things that are insiginificant (e.g. order of message keys is
895 * insignificant and should be sorted to avoid unnecessary cache invalidation).
897 * This data structure must exclusively contain arrays and scalars as values (avoid
898 * object instances) to allow simple serialisation using json_encode.
900 * If modules have a hash or timestamp from another source, that may be incuded as-is.
902 * A number of utility methods are available to help you gather data. These are not
903 * called by default and must be included by the subclass' getDefinitionSummary().
908 * @param ResourceLoaderContext $context
911 public function getDefinitionSummary( ResourceLoaderContext
$context ) {
913 '_class' => static::class,
914 '_cacheEpoch' => $this->getConfig()->get( 'CacheEpoch' ),
919 * Get this module's last modification timestamp for a given context.
921 * @deprecated since 1.26 Use getDefinitionSummary() instead
922 * @param ResourceLoaderContext $context
923 * @return int|null UNIX timestamp
925 public function getModifiedTime( ResourceLoaderContext
$context ) {
930 * Helper method for providing a version hash to getVersionHash().
932 * @deprecated since 1.26 Use getDefinitionSummary() instead
933 * @param ResourceLoaderContext $context
934 * @return string|null Hash
936 public function getModifiedHash( ResourceLoaderContext
$context ) {
941 * Back-compat dummy for old subclass implementations of getModifiedTime().
943 * This method used to use ObjectCache to track when a hash was first seen. That principle
944 * stems from a time that ResourceLoader could only identify module versions by timestamp.
945 * That is no longer the case. Use getDefinitionSummary() directly.
947 * @deprecated since 1.26 Superseded by getVersionHash()
948 * @param ResourceLoaderContext $context
949 * @return int UNIX timestamp
951 public function getHashMtime( ResourceLoaderContext
$context ) {
952 if ( !is_string( $this->getModifiedHash( $context ) ) ) {
960 * Back-compat dummy for old subclass implementations of getModifiedTime().
963 * @deprecated since 1.26 Superseded by getVersionHash()
964 * @param ResourceLoaderContext $context
965 * @return int UNIX timestamp
967 public function getDefinitionMtime( ResourceLoaderContext
$context ) {
968 if ( $this->getDefinitionSummary( $context ) === null ) {
976 * Check whether this module is known to be empty. If a child class
977 * has an easy and cheap way to determine that this module is
978 * definitely going to be empty, it should override this method to
979 * return true in that case. Callers may optimize the request for this
980 * module away if this function returns true.
981 * @param ResourceLoaderContext $context
984 public function isKnownEmpty( ResourceLoaderContext
$context ) {
989 * Check whether this module should be embeded rather than linked
991 * Modules returning true here will be embedded rather than loaded by
992 * ResourceLoaderClientHtml.
995 * @param ResourceLoaderContext $context
998 public function shouldEmbedModule( ResourceLoaderContext
$context ) {
999 return $this->getGroup() === 'private';
1002 /** @var JSParser Lazy-initialized; use self::javaScriptParser() */
1003 private static $jsParser;
1004 private static $parseCacheVersion = 1;
1007 * Validate a given script file; if valid returns the original source.
1008 * If invalid, returns replacement JS source that throws an exception.
1010 * @param string $fileName
1011 * @param string $contents
1012 * @return string JS with the original, or a replacement error
1014 protected function validateScriptFile( $fileName, $contents ) {
1015 if ( !$this->getConfig()->get( 'ResourceLoaderValidateJS' ) ) {
1018 $cache = ObjectCache
::getMainWANInstance();
1019 return $cache->getWithSetCallback(
1020 $cache->makeGlobalKey(
1023 self
::$parseCacheVersion,
1028 function () use ( $contents, $fileName ) {
1029 $parser = self
::javaScriptParser();
1031 $parser->parse( $contents, $fileName, 1 );
1032 $result = $contents;
1033 } catch ( Exception
$e ) {
1034 // We'll save this to cache to avoid having to re-validate broken JS
1035 $err = $e->getMessage();
1036 $result = "mw.log.error(" .
1037 Xml
::encodeJsVar( "JavaScript parse error: $err" ) . ");";
1047 protected static function javaScriptParser() {
1048 if ( !self
::$jsParser ) {
1049 self
::$jsParser = new JSParser();
1051 return self
::$jsParser;
1055 * Safe version of filemtime(), which doesn't throw a PHP warning if the file doesn't exist.
1058 * @param string $filePath File path
1059 * @return int UNIX timestamp
1061 protected static function safeFilemtime( $filePath ) {
1062 Wikimedia\
suppressWarnings();
1063 $mtime = filemtime( $filePath ) ?
: 1;
1064 Wikimedia\restoreWarnings
();
1069 * Compute a non-cryptographic string hash of a file's contents.
1070 * If the file does not exist or cannot be read, returns an empty string.
1072 * @since 1.26 Uses MD4 instead of SHA1.
1073 * @param string $filePath File path
1074 * @return string Hash
1076 protected static function safeFileHash( $filePath ) {
1077 return FileContentsHasher
::getFileContentsHash( $filePath );