Update RelPath calls
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderModule.php
index 1608901..a6ec72a 100644 (file)
@@ -26,6 +26,7 @@ use MediaWiki\MediaWikiServices;
 use Psr\Log\LoggerAwareInterface;
 use Psr\Log\LoggerInterface;
 use Psr\Log\NullLogger;
+use Wikimedia\RelPath;
 use Wikimedia\ScopedCallback;
 
 /**
@@ -65,8 +66,6 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
        # pages like Special:UserLogin and Special:Preferences
        protected $origin = self::ORIGIN_CORE_SITEWIDE;
 
-       /* Protected Members */
-
        protected $name = null;
        protected $targets = [ 'desktop' ];
 
@@ -94,8 +93,6 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
         */
        protected $logger;
 
-       /* Methods */
-
        /**
         * Get this module's name. This is set when the module is registered
         * with ResourceLoader::register()
@@ -110,7 +107,7 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
         * Set this module's name. This is called by ResourceLoader::register()
         * when registering the module. Other code should not call this.
         *
-        * @param string $name Name
+        * @param string $name
         */
        public function setName( $name ) {
                $this->name = $name;
@@ -531,7 +528,7 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
        public static function getRelativePaths( array $filePaths ) {
                global $IP;
                return array_map( function ( $path ) use ( $IP ) {
-                       return RelPath\getRelativePath( $path, $IP );
+                       return RelPath::getRelativePath( $path, $IP );
                }, $filePaths );
        }
 
@@ -545,7 +542,7 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
        public static function expandRelativePaths( array $filePaths ) {
                global $IP;
                return array_map( function ( $path ) use ( $IP ) {
-                       return RelPath\joinPath( $IP, $path );
+                       return RelPath::joinPath( $IP, $path );
                }, $filePaths );
        }
 
@@ -586,6 +583,81 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
                $this->msgBlobs[$lang] = $blob;
        }
 
+       /**
+        * Get headers to send as part of a module web response.
+        *
+        * It is not supported to send headers through this method that are
+        * required to be unique or otherwise sent once in an HTTP response
+        * because clients may make batch requests for multiple modules (as
+        * is the default behaviour for ResourceLoader clients).
+        *
+        * For exclusive or aggregated headers, see ResourceLoader::sendResponseHeaders().
+        *
+        * @since 1.30
+        * @param ResourceLoaderContext $context
+        * @return string[] Array of HTTP response headers
+        */
+       final public function getHeaders( ResourceLoaderContext $context ) {
+               $headers = [];
+
+               $formattedLinks = [];
+               foreach ( $this->getPreloadLinks( $context ) as $url => $attribs ) {
+                       $link = "<{$url}>;rel=preload";
+                       foreach ( $attribs as $key => $val ) {
+                               $link .= ";{$key}={$val}";
+                       }
+                       $formattedLinks[] = $link;
+               }
+               if ( $formattedLinks ) {
+                       $headers[] = 'Link: ' . implode( ',', $formattedLinks );
+               }
+
+               return $headers;
+       }
+
+       /**
+        * Get a list of resources that web browsers may preload.
+        *
+        * Behaviour of rel=preload link is specified at <https://www.w3.org/TR/preload/>.
+        *
+        * Use case for ResourceLoader originally part of T164299.
+        *
+        * @par Example
+        * @code
+        *     protected function getPreloadLinks() {
+        *         return [
+        *             'https://example.org/script.js' => [ 'as' => 'script' ],
+        *             'https://example.org/image.png' => [ 'as' => 'image' ],
+        *         ];
+        *     }
+        * @encode
+        *
+        * @par Example using HiDPI image variants
+        * @code
+        *     protected function getPreloadLinks() {
+        *         return [
+        *             'https://example.org/logo.png' => [
+        *                 'as' => 'image',
+        *                 'media' => 'not all and (min-resolution: 2dppx)',
+        *             ],
+        *             'https://example.org/logo@2x.png' => [
+        *                 'as' => 'image',
+        *                 'media' => '(min-resolution: 2dppx)',
+        *             ],
+        *         ];
+        *     }
+        * @encode
+        *
+        * @see ResourceLoaderModule::getHeaders
+        * @since 1.30
+        * @param ResourceLoaderContext $context
+        * @return array Keyed by url, values must be an array containing
+        *  at least an 'as' key. Optionally a 'media' key as well.
+        */
+       protected function getPreloadLinks( ResourceLoaderContext $context ) {
+               return [];
+       }
+
        /**
         * Get module-specific LESS variables, if any.
         *
@@ -711,6 +783,11 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
                        $content['templates'] = $templates;
                }
 
+               $headers = $this->getHeaders( $context );
+               if ( $headers ) {
+                       $content['headers'] = $headers;
+               }
+
                $statTiming = microtime( true ) - $statStart;
                $statName = strtr( $this->getName(), '.', '_' );
                $stats->timing( "resourceloader_build.all", 1000 * $statTiming );
@@ -852,7 +929,7 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
         * Get this module's last modification timestamp for a given context.
         *
         * @deprecated since 1.26 Use getDefinitionSummary() instead
-        * @param ResourceLoaderContext $context Context object
+        * @param ResourceLoaderContext $context
         * @return int|null UNIX timestamp
         */
        public function getModifiedTime( ResourceLoaderContext $context ) {