Merge "CologneBlue rewrite: remove hard dependency on SkinLegacy, kill Quickbar nonsense"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderFileModule.php
index e1dfc49..d0c56ae 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 /**
+ * Resource loader module based on local JavaScript/CSS files.
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -33,47 +35,74 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        protected $remoteBasePath = '';
        /**
         * Array: List of paths to JavaScript files to always include
-        * @example array( [file-path], [file-path], ... )
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $scripts = array();
        /**
         * Array: List of JavaScript files to include when using a specific language
-        * @example array( [language-code] => array( [file-path], [file-path], ... ), ... )
+        * @par Usage:
+        * @code
+        * array( [language-code] => array( [file-path], [file-path], ... ), ... )
+        * @endcode
         */
        protected $languageScripts = array();
        /**
         * Array: List of JavaScript files to include when using a specific skin
-        * @example array( [skin-name] => array( [file-path], [file-path], ... ), ... )
+        * @par Usage:
+        * @code
+        * array( [skin-name] => array( [file-path], [file-path], ... ), ... )
+        * @endcode
         */
        protected $skinScripts = array();
        /**
         * Array: List of paths to JavaScript files to include in debug mode
-        * @example array( [skin-name] => array( [file-path], [file-path], ... ), ... )
+        * @par Usage:
+        * @code
+        * array( [skin-name] => array( [file-path], [file-path], ... ), ... )
+        * @endcode
         */
        protected $debugScripts = array();
        /**
         * Array: List of paths to JavaScript files to include in the startup module
-        * @example array( [file-path], [file-path], ... )
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $loaderScripts = array();
        /**
         * Array: List of paths to CSS files to always include
-        * @example array( [file-path], [file-path], ... )
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $styles = array();
        /**
         * Array: List of paths to CSS files to include when using specific skins
-        * @example array( [file-path], [file-path], ... )
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $skinStyles = array();
        /**
         * Array: List of modules this module depends on
-        * @example array( [file-path], [file-path], ... )
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $dependencies = array();
        /**
         * Array: List of message keys used by this module
-        * @example array( [message-key], [message-key], ... )
+        * @par Usage:
+        * @code
+        * array( [message-key], [message-key], ... )
+        * @endcode
         */
        protected $messages = array();
        /** String: Name of group to load this module in */
@@ -82,14 +111,22 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        protected $position = 'bottom';
        /** Boolean: Link to raw files in debug mode */
        protected $debugRaw = true;
+       /** Boolean: Whether mw.loader.state() call should be omitted */
+       protected $raw = false;
        /**
         * Array: Cache for mtime
-        * @example array( [hash] => [mtime], [hash] => [mtime], ... )
+        * @par Usage:
+        * @code
+        * array( [hash] => [mtime], [hash] => [mtime], ... )
+        * @endcode
         */
        protected $modifiedTime = array();
        /**
         * Array: Place where readStyleFile() tracks file dependencies
-        * @example array( [file-path], [file-path], ... )
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $localFileRefs = array();
 
@@ -106,6 +143,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         *     to $wgScriptPath
         *
         * Below is a description for the $options array:
+        * @par Construction options:
         * @code
         *      array(
         *              // Base path to prepend to all local paths in $options. Defaults to $IP
@@ -148,9 +186,13 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        public function __construct( $options = array(), $localBasePath = null,
                $remoteBasePath = null )
        {
-               global $IP, $wgScriptPath;
+               global $IP, $wgScriptPath, $wgResourceBasePath;
                $this->localBasePath = $localBasePath === null ? $IP : $localBasePath;
-               $this->remoteBasePath = $remoteBasePath === null ? $wgScriptPath : $remoteBasePath;
+               if ( $remoteBasePath !== null ) {
+                       $this->remoteBasePath = $remoteBasePath;
+               } else {
+                       $this->remoteBasePath = $wgResourceBasePath === null ? $wgScriptPath : $wgResourceBasePath;
+               }
 
                if ( isset( $options['remoteExtPath'] ) ) {
                        global $wgExtensionAssetsPath;
@@ -200,12 +242,14 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                                        break;
                                // Single booleans
                                case 'debugRaw':
+                               case 'raw':
                                        $this->{$member} = (bool) $option;
                                        break;
                        }
                }
-               // Make sure the remote base path is a complete valid url
-               $this->remoteBasePath = wfExpandUrl( $this->remoteBasePath );
+               // Make sure the remote base path is a complete valid URL,
+               // but possibly protocol-relative to avoid cache pollution
+               $this->remoteBasePath = wfExpandUrl( $this->remoteBasePath, PROTO_RELATIVE );
        }
 
        /**
@@ -216,16 +260,28 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         */
        public function getScript( ResourceLoaderContext $context ) {
                $files = $this->getScriptFiles( $context );
-               if ( $context->getDebug() && $this->debugRaw ) {
-                       $urls = array();
-                       foreach ( $this->getScriptFiles( $context ) as $file ) {
-                               $urls[] = $this->getRemotePath( $file );
-                       }
-                       return $urls;
-               }
                return $this->readScriptFiles( $files );
        }
 
+       /**
+        * @param $context ResourceLoaderContext
+        * @return array
+        */
+       public function getScriptURLsForDebug( ResourceLoaderContext $context ) {
+               $urls = array();
+               foreach ( $this->getScriptFiles( $context ) as $file ) {
+                       $urls[] = $this->getRemotePath( $file );
+               }
+               return $urls;
+       }
+
+       /**
+        * @return bool
+        */
+       public function supportsURLLoading() {
+               return $this->debugRaw;
+       }
+
        /**
         * Gets loader script.
         *
@@ -249,20 +305,10 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        $this->getStyleFiles( $context ),
                        $this->getFlip( $context )
                );
-               if ( !$context->getOnly() && $context->getDebug() && $this->debugRaw ) {
-                       $urls = array();
-                       foreach ( $this->getStyleFiles( $context ) as $mediaType => $list ) {
-                               $urls[$mediaType] = array();
-                               foreach ( $list as $file ) {
-                                       $urls[$mediaType][] = $this->getRemotePath( $file );
-                               }
-                       }
-                       return $urls;
-               }
                // Collect referenced files
                $this->localFileRefs = array_unique( $this->localFileRefs );
                // If the list has been modified since last time we cached it, update the cache
-               if ( $this->localFileRefs !== $this->getFileDependencies( $context->getSkin() ) ) {
+               if ( $this->localFileRefs !== $this->getFileDependencies( $context->getSkin() ) && !wfReadOnly() ) {
                        $dbw = wfGetDB( DB_MASTER );
                        $dbw->replace( 'module_deps',
                                array( array( 'md_module', 'md_skin' ) ), array(
@@ -275,6 +321,21 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                return $styles;
        }
 
+       /**
+        * @param $context ResourceLoaderContext
+        * @return array
+        */
+       public function getStyleURLsForDebug( ResourceLoaderContext $context ) {
+               $urls = array();
+               foreach ( $this->getStyleFiles( $context ) as $mediaType => $list ) {
+                       $urls[$mediaType] = array();
+                       foreach ( $list as $file ) {
+                               $urls[$mediaType][] = $this->getRemotePath( $file );
+                       }
+               }
+               return $urls;
+       }
+
        /**
         * Gets list of message keys used by this module.
         *
@@ -309,6 +370,13 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                return $this->dependencies;
        }
 
+       /**
+        * @return bool
+        */
+       public function isRaw() {
+               return $this->raw;
+       }
+
        /**
         * Get the last modified timestamp of this module.
         *
@@ -366,7 +434,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                }
 
                wfProfileIn( __METHOD__.'-filemtime' );
-               $filesMtime = max( array_map( 'filemtime', $files ) );
+               $filesMtime = max( array_map( array( __CLASS__, 'safeFilemtime' ), $files ) );
                wfProfileOut( __METHOD__.'-filemtime' );
                $this->modifiedTime[$context->getHash()] = max(
                        $filesMtime,
@@ -492,10 +560,10 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                $js = '';
                foreach ( array_unique( $scripts ) as $fileName ) {
                        $localPath = $this->getLocalPath( $fileName );
-                       $contents = file_get_contents( $localPath );
-                       if ( $contents === false ) {
+                       if ( !file_exists( $localPath ) ) {
                                throw new MWException( __METHOD__.": script file not found: \"$localPath\"" );
                        }
+                       $contents = file_get_contents( $localPath );
                        if ( $wgResourceLoaderValidateStaticJS ) {
                                // Static files don't really need to be checked as often; unlike
                                // on-wiki module they shouldn't change unexpectedly without
@@ -541,17 +609,18 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         *
         * This method can be used as a callback for array_map()
         *
-        * @param $path String: File path of script file to read
+        * @param $path String: File path of style file to read
         * @param $flip bool
         *
         * @return String: CSS data in script file
+        * @throws MWException if the file doesn't exist
         */
        protected function readStyleFile( $path, $flip ) {
                $localPath = $this->getLocalPath( $path );
-               $style = file_get_contents( $localPath );
-               if ( $style === false ) {
+               if ( !file_exists( $localPath ) ) {
                        throw new MWException( __METHOD__.": style file not found: \"$localPath\"" );
                }
+               $style = file_get_contents( $localPath );
                if ( $flip ) {
                        $style = CSSJanus::transform( $style, true, false );
                }
@@ -565,12 +634,30 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                // Get and register local file references
                $this->localFileRefs = array_merge(
                        $this->localFileRefs,
-                       CSSMin::getLocalFileReferences( $style, $dir ) );
+                       CSSMin::getLocalFileReferences( $style, $dir )
+               );
                return CSSMin::remap(
                        $style, $dir, $remoteDir, true
                );
        }
 
+       /**
+        * Safe version of filemtime(), which doesn't throw a PHP warning if the file doesn't exist
+        * but returns 1 instead.
+        * @param $filename string File name
+        * @return int UNIX timestamp, or 1 if the file doesn't exist
+        */
+       protected static function safeFilemtime( $filename ) {
+               if ( file_exists( $filename ) ) {
+                       return filemtime( $filename );
+               } else {
+                       // We only ever map this function on an array if we're gonna call max() after,
+                       // so return our standard minimum timestamps here. This is 1, not 0, because
+                       // wfTimestamp(0) == NOW
+                       return 1;
+               }
+       }
+
        /**
         * Get whether CSS for this module should be flipped
         * @param $context ResourceLoaderContext