resourceloader: Fix undefined variable in ResourceLoaderFileModule::getSkipFunction
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderFileModule.php
index 43bd562..06f7146 100644 (file)
@@ -218,27 +218,17 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         *     )
         * @endcode
         */
-       public function __construct( $options = array(), $localBasePath = null,
+       public function __construct(
+               $options = array(),
+               $localBasePath = null,
                $remoteBasePath = null
        ) {
-               global $IP, $wgScriptPath, $wgResourceBasePath;
-               $this->localBasePath = $localBasePath === null ? $IP : $localBasePath;
-               if ( $remoteBasePath !== null ) {
-                       $this->remoteBasePath = $remoteBasePath;
-               } else {
-                       $this->remoteBasePath = $wgResourceBasePath === null ? $wgScriptPath : $wgResourceBasePath;
-               }
-
-               if ( isset( $options['remoteExtPath'] ) ) {
-                       global $wgExtensionAssetsPath;
-                       $this->remoteBasePath = $wgExtensionAssetsPath . '/' . $options['remoteExtPath'];
-               }
-
-               if ( isset( $options['remoteSkinPath'] ) ) {
-                       global $wgStylePath;
-                       $this->remoteBasePath = $wgStylePath . '/' . $options['remoteSkinPath'];
-               }
+               // localBasePath and remoteBasePath both have unbelievably long fallback chains
+               // and need to be handled separately.
+               list( $this->localBasePath, $this->remoteBasePath ) =
+                       self::extractBasePaths( $options, $localBasePath, $remoteBasePath );
 
+               // Extract, validate and normalise remaining options
                foreach ( $options as $member => $option ) {
                        switch ( $member ) {
                                // Lists of file paths
@@ -281,8 +271,6 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                                // Single strings
                                case 'group':
                                case 'position':
-                               case 'localBasePath':
-                               case 'remoteBasePath':
                                case 'skipFunction':
                                        $this->{$member} = (string)$option;
                                        break;
@@ -293,9 +281,57 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                                        break;
                        }
                }
+       }
+
+       /**
+        * Extract a pair of local and remote base paths from module definition information.
+        * Implementation note: the amount of global state used in this function is staggering.
+        *
+        * @param array $options Module definition
+        * @param string $localBasePath Path to use if not provided in module definition. Defaults
+        *     to $IP
+        * @param string $remoteBasePath Path to use if not provided in module definition. Defaults
+        *     to $wgScriptPath
+        * @return array Array( localBasePath, remoteBasePath )
+        */
+       public static function extractBasePaths(
+               $options = array(),
+               $localBasePath = null,
+               $remoteBasePath = null
+       ) {
+               global $IP, $wgScriptPath, $wgResourceBasePath;
+
+               // The different ways these checks are done, and their ordering, look very silly,
+               // but were preserved for backwards-compatibility just in case. Tread lightly.
+
+               $localBasePath = $localBasePath === null ? $IP : $localBasePath;
+               if ( $remoteBasePath === null ) {
+                       $remoteBasePath = $wgResourceBasePath === null ? $wgScriptPath : $wgResourceBasePath;
+               }
+
+               if ( isset( $options['remoteExtPath'] ) ) {
+                       global $wgExtensionAssetsPath;
+                       $remoteBasePath = $wgExtensionAssetsPath . '/' . $options['remoteExtPath'];
+               }
+
+               if ( isset( $options['remoteSkinPath'] ) ) {
+                       global $wgStylePath;
+                       $remoteBasePath = $wgStylePath . '/' . $options['remoteSkinPath'];
+               }
+
+               if ( array_key_exists( 'localBasePath', $options ) ) {
+                       $localBasePath = (string)$options['localBasePath'];
+               }
+
+               if ( array_key_exists( 'remoteBasePath', $options ) ) {
+                       $remoteBasePath = (string)$options['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 );
+               $remoteBasePath = wfExpandUrl( $remoteBasePath, PROTO_RELATIVE );
+
+               return array( $localBasePath, $remoteBasePath );
        }
 
        /**
@@ -437,14 +473,13 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        return null;
                }
 
-               global $wgResourceLoaderValidateStaticJS;
                $localPath = $this->getLocalPath( $this->skipFunction );
                if ( !file_exists( $localPath ) ) {
                        throw new MWException( __METHOD__ . ": skip function file not found: \"$localPath\"" );
                }
                $contents = file_get_contents( $localPath );
-               if ( $wgResourceLoaderValidateStaticJS ) {
-                       $contents = $this->validateScriptFile( $fileName, $contents );
+               if ( $this->getConfig()->get( 'ResourceLoaderValidateStaticJS' ) ) {
+                       $contents = $this->validateScriptFile( $localPath, $contents );
                }
                return $contents;
        }
@@ -567,18 +602,26 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        /* Protected Methods */
 
        /**
-        * @param string $path
+        * @param string|ResourceLoaderFilePath $path
         * @return string
         */
        protected function getLocalPath( $path ) {
+               if ( $path instanceof ResourceLoaderFilePath ) {
+                       return $path->getLocalPath();
+               }
+
                return "{$this->localBasePath}/$path";
        }
 
        /**
-        * @param string $path
+        * @param string|ResourceLoaderFilePath $path
         * @return string
         */
        protected function getRemotePath( $path ) {
+               if ( $path instanceof ResourceLoaderFilePath ) {
+                       return $path->getRemotePath();
+               }
+
                return "{$this->remoteBasePath}/$path";
        }
 
@@ -660,7 +703,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        $files = array_merge( $files, $this->debugScripts );
                }
 
-               return array_unique( $files );
+               return array_unique( $files, SORT_REGULAR );
        }
 
        /**
@@ -746,18 +789,17 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         * @return string Concatenated and remapped JavaScript data from $scripts
         */
        protected function readScriptFiles( array $scripts ) {
-               global $wgResourceLoaderValidateStaticJS;
                if ( empty( $scripts ) ) {
                        return '';
                }
                $js = '';
-               foreach ( array_unique( $scripts ) as $fileName ) {
+               foreach ( array_unique( $scripts, SORT_REGULAR ) as $fileName ) {
                        $localPath = $this->getLocalPath( $fileName );
                        if ( !file_exists( $localPath ) ) {
                                throw new MWException( __METHOD__ . ": script file not found: \"$localPath\"" );
                        }
                        $contents = file_get_contents( $localPath );
-                       if ( $wgResourceLoaderValidateStaticJS ) {
+                       if ( $this->getConfig()->get( 'ResourceLoaderValidateStaticJS' ) ) {
                                // Static files don't really need to be checked as often; unlike
                                // on-wiki module they shouldn't change unexpectedly without
                                // admin interference.
@@ -785,7 +827,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        return array();
                }
                foreach ( $styles as $media => $files ) {
-                       $uniqueFiles = array_unique( $files );
+                       $uniqueFiles = array_unique( $files, SORT_REGULAR );
                        $styleFiles = array();
                        foreach ( $uniqueFiles as $file ) {
                                $styleFiles[] = $this->readStyleFile( $file, $flip );
@@ -808,13 +850,14 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         */
        protected function readStyleFile( $path, $flip ) {
                $localPath = $this->getLocalPath( $path );
+               $remotePath = $this->getRemotePath( $path );
                if ( !file_exists( $localPath ) ) {
                        $msg = __METHOD__ . ": style file not found: \"$localPath\"";
                        wfDebugLog( 'resourceloader', $msg );
                        throw new MWException( $msg );
                }
 
-               if ( $this->getStyleSheetLang( $path ) === 'less' ) {
+               if ( $this->getStyleSheetLang( $localPath ) === 'less' ) {
                        $style = $this->compileLESSFile( $localPath );
                        $this->hasGeneratedStyles = true;
                } else {
@@ -824,20 +867,15 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                if ( $flip ) {
                        $style = CSSJanus::transform( $style, true, false );
                }
-               $dirname = dirname( $path );
-               if ( $dirname == '.' ) {
-                       // If $path doesn't have a directory component, don't prepend a dot
-                       $dirname = '';
-               }
-               $dir = $this->getLocalPath( $dirname );
-               $remoteDir = $this->getRemotePath( $dirname );
+               $localDir = dirname( $localPath );
+               $remoteDir = dirname( $remotePath );
                // Get and register local file references
                $this->localFileRefs = array_merge(
                        $this->localFileRefs,
-                       CSSMin::getLocalFileReferences( $style, $dir )
+                       CSSMin::getLocalFileReferences( $style, $localDir )
                );
                return CSSMin::remap(
-                       $style, $dir, $remoteDir, true
+                       $style, $localDir, $remoteDir, true
                );
        }
 
@@ -869,8 +907,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         * @param string $fileName File name of root LESS file.
         * @return string Cache key
         */
-       protected static function getLESSCacheKey( $fileName ) {
-               $vars = json_encode( ResourceLoader::getLESSVars() );
+       protected function getLESSCacheKey( $fileName ) {
+               $vars = json_encode( ResourceLoader::getLESSVars( $this->getConfig() ) );
                $hash = md5( $fileName . $vars );
                return wfMemcKey( 'resourceloader', 'less', $hash );
        }
@@ -891,7 +929,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         * @return string CSS source
         */
        protected function compileLESSFile( $fileName ) {
-               $key = self::getLESSCacheKey( $fileName );
+               $key = $this->getLESSCacheKey( $fileName );
                $cache = wfGetCache( CACHE_ANYTHING );
 
                // The input to lessc. Either an associative array representing the
@@ -902,7 +940,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        $source = $fileName;
                }
 
-               $compiler = ResourceLoader::getLessCompiler();
+               $compiler = ResourceLoader::getLessCompiler( $this->getConfig() );
                $result = null;
 
                $result = $compiler->cachedCompile( $source );