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
26 * Abstraction for ResourceLoader modules, with name registration and maxage functionality.
28 abstract class ResourceLoaderModule
{
30 const TYPE_SCRIPTS
= 'scripts';
31 const TYPE_STYLES
= 'styles';
32 const TYPE_COMBINED
= 'combined';
34 # sitewide core module like a skin file or jQuery component
35 const ORIGIN_CORE_SITEWIDE
= 1;
37 # per-user module generated by the software
38 const ORIGIN_CORE_INDIVIDUAL
= 2;
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;
44 # per-user module generated from user-editable files, like User:Me/vector.js
45 const ORIGIN_USER_INDIVIDUAL
= 4;
47 # an access constant; make sure this is kept as the largest number in this group
48 const ORIGIN_ALL
= 10;
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
;
56 /* Protected Members */
58 protected $name = null;
59 protected $targets = array( 'desktop' );
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();
78 * Get this module's name. This is set when the module is registered
79 * with ResourceLoader::register()
81 * @return string|null Name (string) or null if no name was set
83 public function getName() {
88 * Set this module's name. This is called by ResourceLoader::register()
89 * when registering the module. Other code should not call this.
91 * @param string $name Name
93 public function setName( $name ) {
98 * Get this module's origin. This is set when the module is registered
99 * with ResourceLoader::register()
101 * @return int ResourceLoaderModule class constant, the subclass default
102 * if not set manually
104 public function getOrigin() {
105 return $this->origin
;
109 * Set this module's origin. This is called by ResourceLoader::register()
110 * when registering the module. Other code should not call this.
112 * @param int $origin Origin
114 public function setOrigin( $origin ) {
115 $this->origin
= $origin;
119 * @param ResourceLoaderContext $context
122 public function getFlip( $context ) {
125 return $wgContLang->getDir() !== $context->getDirection();
129 * Get all JS for this module for a given language and skin.
130 * Includes all relevant JS except loader scripts.
132 * @param ResourceLoaderContext $context
133 * @return string JavaScript code
135 public function getScript( ResourceLoaderContext
$context ) {
136 // Stub, override expected
141 * Takes named templates by the module and returns an array mapping.
143 * @return array of templates mapping template alias to content
145 public function getTemplates() {
146 // Stub, override expected.
154 public function getConfig() {
155 if ( $this->config
=== null ) {
156 // Ugh, fall back to default
157 $this->config
= ConfigFactory
::getDefaultInstance()->makeConfig( 'main' );
160 return $this->config
;
164 * @param Config $config
167 public function setConfig( Config
$config ) {
168 $this->config
= $config;
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.
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.
182 * @param ResourceLoaderContext $context
183 * @return array Array of URLs
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 );
192 $url = $resourceLoader->createLoaderURL(
197 return array( $url );
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.
206 public function supportsURLLoading() {
211 * Get all CSS for this module for a given skin.
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 }' ) );
218 public function getStyles( ResourceLoaderContext
$context ) {
219 // Stub, override expected
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()
229 * @param ResourceLoaderContext $context
230 * @return array Array( mediaType => array( URL1, URL2, ... ), ... )
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 );
239 $url = $resourceLoader->createLoaderURL(
244 return array( 'all' => array( $url ) );
248 * Get the messages needed for this module.
250 * To get a JSON blob with messages, use MessageBlobStore::get()
252 * @return array List of message keys. Keys may occur more than once
254 public function getMessages() {
255 // Stub, override expected
260 * Get the group this module is in.
262 * @return string Group name
264 public function getGroup() {
265 // Stub, override expected
270 * Get the origin of this module. Should only be overridden for foreign modules.
272 * @return string Origin name, 'local' for local modules
274 public function getSource() {
275 // Stub, override expected
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>"
286 public function getPosition() {
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.
297 public function isRaw() {
302 * Get a list of modules this module depends on.
304 * Dependency information is taken into account when loading a module
305 * on the client side.
307 * Note: It is expected that $context will be made non-optional in the near
310 * @param ResourceLoaderContext $context
311 * @return array List of module names as strings
313 public function getDependencies( ResourceLoaderContext
$context = null ) {
314 // Stub, override expected
319 * Get target(s) for the module, eg ['desktop'] or ['desktop', 'mobile']
321 * @return array Array of strings
323 public function getTargets() {
324 return $this->targets
;
328 * Get the skip function.
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.
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.
339 * @return string|null A JavaScript function body returning a boolean value, or null
341 public function getSkipFunction() {
346 * Get the files this module depends on indirectly for a given skin.
348 * These are only image files referenced by the module's stylesheet.
350 * @param ResourceLoaderContext $context
351 * @return array List of files
353 protected function getFileDependencies( ResourceLoaderContext
$context ) {
354 $vary = $context->getSkin() . '|' . $context->getLanguage();
356 // Try in-object cache first
357 if ( !isset( $this->fileDeps
[$vary] ) ) {
358 $dbr = wfGetDB( DB_SLAVE
);
359 $deps = $dbr->selectField( 'module_deps',
362 'md_module' => $this->getName(),
368 if ( !is_null( $deps ) ) {
369 $this->fileDeps
[$vary] = self
::expandRelativePaths(
370 (array)FormatJson
::decode( $deps, true )
373 $this->fileDeps
[$vary] = array();
376 return $this->fileDeps
[$vary];
380 * Set in-object cache for file dependencies.
382 * This is used to retrieve data in batches. See ResourceLoader::preloadModuleInfo().
383 * To save the data, use saveFileDependencies().
385 * @param string $skin Skin name
386 * @param array $deps Array of file names
388 public function setFileDependencies( ResourceLoaderContext
$context, $files ) {
389 $vary = $context->getSkin() . '|' . $context->getLanguage();
390 $this->fileDeps
[$vary] = $files;
394 * Set the files this module depends on indirectly for a given skin.
397 * @param ResourceLoaderContext $context
398 * @param array $localFileRefs List of files
400 protected function saveFileDependencies( ResourceLoaderContext
$context, $localFileRefs ) {
402 $localFileRefs = array_values( array_unique( $localFileRefs ) );
403 sort( $localFileRefs );
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(),
414 // Use relative paths to avoid ghost entries when $IP changes (T111481)
415 'md_deps' => FormatJson
::encode( self
::getRelativePaths( $localFileRefs ) ),
419 } catch ( Exception
$e ) {
420 wfDebugLog( 'resourceloader', __METHOD__
. ": failed to update DB: $e" );
425 * Make file paths relative to MediaWiki directory.
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).
431 * @param array $filePaths
434 public static function getRelativePaths( Array $filePaths ) {
436 return array_map( function ( $path ) use ( $IP ) {
437 return RelPath\
getRelativePath( $path, $IP );
442 * Expand directories relative to $IP.
445 * @param array $filePaths
448 public static function expandRelativePaths( Array $filePaths ) {
450 return array_map( function ( $path ) use ( $IP ) {
451 return RelPath\
joinPath( $IP, $path );
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
460 public function getMsgBlobMtime( $lang ) {
461 if ( !isset( $this->msgBlobMtime
[$lang] ) ) {
462 if ( !count( $this->getMessages() ) ) {
466 $dbr = wfGetDB( DB_SLAVE
);
467 $msgBlobMtime = $dbr->selectField( 'msg_resource',
470 'mr_resource' => $this->getName(),
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();
480 $this->msgBlobMtime
[$lang] = wfTimestamp( TS_UNIX
, $msgBlobMtime );
482 return $this->msgBlobMtime
[$lang];
486 * Set in-object cache for message blob time.
488 * This is used to retrieve data in batches. See ResourceLoader::preloadModuleInfo().
490 * @param string $lang Language code
491 * @param int $mtime UNIX timestamp
493 public function setMsgBlobMtime( $lang, $mtime ) {
494 $this->msgBlobMtime
[$lang] = $mtime;
498 * Get module-specific LESS variables, if any.
501 * @param ResourceLoaderContext $context
502 * @return array Module-specific LESS variables.
504 protected function getLessVars( ResourceLoaderContext
$context ) {
509 * Get an array of this module's resources. Ready for serving to the web.
512 * @param ResourceLoaderContext $context
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 );
522 return $this->contents
[$contextHash];
526 * Bundle all resources attached to this module into an array.
529 * @param ResourceLoaderContext $context
532 final protected function buildContent( ResourceLoaderContext
$context ) {
533 $rl = $context->getResourceLoader();
534 $stats = RequestContext
::getMain()->getStats();
535 $statStart = microtime( true );
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.
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 );
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 ) !== ';'
560 // Append semicolon to prevent weird bugs caused by files not
561 // terminating their statements right (bug 27054)
565 $content['scripts'] = $scripts;
569 if ( $context->shouldIncludeStyles() ) {
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() ) {
579 'url' => $this->getStyleURLsForDebug( $context )
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 );
595 } elseif ( is_string( $style ) ) {
596 $stylePairs[$media] = ResourceLoader
::filter( 'minify-css', $style );
600 // Wrap styles into @media groups as needed and flatten into a numerical array
602 'css' => $rl->makeCombinedStyles( $stylePairs )
606 $content['styles'] = $styles;
610 $blobs = $rl->getMessageBlobStore()->get(
612 array( $this->getName() => $this ),
613 $context->getLanguage()
615 if ( isset( $blobs[$this->getName()] ) ) {
616 $content['messagesBlob'] = $blobs[$this->getName()];
619 $templates = $this->getTemplates();
621 $content['templates'] = $templates;
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 );
633 * Get a string identifying the current version of this module in a given context.
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.)
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.
642 * This method should be quick because it is frequently run by ResourceLoaderStartUpModule to
643 * propagate changes to the client and effectively invalidate cache.
645 * For backward-compatibility, the following optional data providers are automatically included:
647 * - getModifiedTime()
648 * - getModifiedHash()
651 * @param ResourceLoaderContext $context
652 * @return string Hash (should use ResourceLoader::makeHash)
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 );
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
) ) {
670 if ( $this->enableModuleContentVersion() ) {
671 // Detect changes directly
672 $str = json_encode( $this->getModuleContent( $context ) );
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' );
679 $str = json_encode( $summary );
681 $mtime = $this->getModifiedTime( $context );
682 if ( $mtime !== null ) {
683 // Support: MediaWiki 1.25 and earlier
684 $str .= strval( $mtime );
687 $mhash = $this->getModifiedHash( $context );
688 if ( $mhash !== null ) {
689 // Support: MediaWiki 1.25 and earlier
690 $str .= strval( $mhash );
694 $this->versionHash
[$contextHash] = ResourceLoader
::makeHash( $str );
696 return $this->versionHash
[$contextHash];
700 * Whether to generate version hash based on module content.
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.
708 public function enableModuleContentVersion() {
713 * Get the definition summary for this module.
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.
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.
724 * $summary = parent::getDefinitionSummary( $context );
725 * $summary[] = array(
732 * Return an array containing values from all significant properties of this
733 * module's definition.
735 * Be careful not to normalise too much. Especially preserve the order of things
736 * that carry significance in getScript and getStyles (T39812).
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).
741 * This data structure must exclusively contain arrays and scalars as values (avoid
742 * object instances) to allow simple serialisation using json_encode.
744 * If modules have a hash or timestamp from another source, that may be incuded as-is.
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().
749 * - getMsgBlobMtime()
752 * @param ResourceLoaderContext $context
755 public function getDefinitionSummary( ResourceLoaderContext
$context ) {
757 '_class' => get_class( $this ),
758 '_cacheEpoch' => $this->getConfig()->get( 'CacheEpoch' ),
763 * Get this module's last modification timestamp for a given context.
765 * @deprecated since 1.26 Use getDefinitionSummary() instead
766 * @param ResourceLoaderContext $context Context object
767 * @return int|null UNIX timestamp
769 public function getModifiedTime( ResourceLoaderContext
$context ) {
774 * Helper method for providing a version hash to getVersionHash().
776 * @deprecated since 1.26 Use getDefinitionSummary() instead
777 * @param ResourceLoaderContext $context
778 * @return string|null Hash
780 public function getModifiedHash( ResourceLoaderContext
$context ) {
785 * Back-compat dummy for old subclass implementations of getModifiedTime().
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.
791 * @deprecated since 1.26 Superseded by getVersionHash()
792 * @param ResourceLoaderContext $context
793 * @return int UNIX timestamp
795 public function getHashMtime( ResourceLoaderContext
$context ) {
796 if ( !is_string( $this->getModifiedHash( $context ) ) ) {
804 * Back-compat dummy for old subclass implementations of getModifiedTime().
807 * @deprecated since 1.26 Superseded by getVersionHash()
808 * @param ResourceLoaderContext $context
809 * @return int UNIX timestamp
811 public function getDefinitionMtime( ResourceLoaderContext
$context ) {
812 if ( $this->getDefinitionSummary( $context ) === null ) {
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
828 public function isKnownEmpty( ResourceLoaderContext
$context ) {
832 /** @var JSParser Lazy-initialized; use self::javaScriptParser() */
833 private static $jsParser;
834 private static $parseCacheVersion = 1;
837 * Validate a given script file; if valid returns the original source.
838 * If invalid, returns replacement JS source that throws an exception.
840 * @param string $fileName
841 * @param string $contents
842 * @return string JS with the original, or a replacement error
844 protected function validateScriptFile( $fileName, $contents ) {
845 if ( $this->getConfig()->get( 'ResourceLoaderValidateJS' ) ) {
847 $cache = ObjectCache
::getMainWANInstance();
848 $key = $cache->makeKey(
851 self
::$parseCacheVersion,
854 $cacheEntry = $cache->get( $key );
855 if ( is_string( $cacheEntry ) ) {
859 $parser = self
::javaScriptParser();
861 $parser->parse( $contents, $fileName, 1 );
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" ) . ");";
870 $cache->set( $key, $result );
880 protected static function javaScriptParser() {
881 if ( !self
::$jsParser ) {
882 self
::$jsParser = new JSParser();
884 return self
::$jsParser;
888 * Safe version of filemtime(), which doesn't throw a PHP warning if the file doesn't exist.
891 * @param string $filePath File path
892 * @return int UNIX timestamp
894 protected static function safeFilemtime( $filePath ) {
895 MediaWiki\
suppressWarnings();
896 $mtime = filemtime( $filePath ) ?
: 1;
897 MediaWiki\restoreWarnings
();
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.
905 * @since 1.26 Uses MD4 instead of SHA1.
906 * @param string $filePath File path
907 * @return string Hash
909 protected static function safeFileHash( $filePath ) {
910 return FileContentsHasher
::getFileContentsHash( $filePath );