Merge "MediaWikiTestCase: Centralise insertPage() logic from SearchEngineTest"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderFileModule.php
index 8a223b0..dc8b14a 100644 (file)
@@ -168,7 +168,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         * @param string $localBasePath Base path to prepend to all local paths in $options. Defaults
         *     to $IP
         * @param string $remoteBasePath Base path to prepend to all remote paths in $options. Defaults
-        *     to $wgScriptPath
+        *     to $wgResourceBasePath
         *
         * Below is a description for the $options array:
         * @throws MWException
@@ -177,7 +177,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         *     array(
         *         // Base path to prepend to all local paths in $options. Defaults to $IP
         *         'localBasePath' => [base path],
-        *         // Base path to prepend to all remote paths in $options. Defaults to $wgScriptPath
+        *         // Base path to prepend to all remote paths in $options. Defaults to $wgResourceBasePath
         *         'remoteBasePath' => [base path],
         *         // Equivalent of remoteBasePath, but relative to $wgExtensionAssetsPath
         *         'remoteExtPath' => [base path],
@@ -291,7 +291,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         * @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
+        *     to $wgResourceBasePath
         * @return array Array( localBasePath, remoteBasePath )
         */
        public static function extractBasePaths(
@@ -299,14 +299,14 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                $localBasePath = null,
                $remoteBasePath = null
        ) {
-               global $IP, $wgScriptPath, $wgResourceBasePath;
+               global $IP, $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;
+                       $remoteBasePath = $wgResourceBasePath;
                }
 
                if ( isset( $options['remoteExtPath'] ) ) {
@@ -385,7 +385,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        public function getStyles( ResourceLoaderContext $context ) {
                $styles = $this->readStyleFiles(
                        $this->getStyleFiles( $context ),
-                       $this->getFlip( $context )
+                       $this->getFlip( $context ),
+                       $context
                );
                // Collect referenced files
                $this->localFileRefs = array_unique( $this->localFileRefs );
@@ -816,14 +817,14 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         *
         * @param array $styles List of media type/list of file paths pairs, to read, remap and
         * concetenate
-        *
         * @param bool $flip
+        * @param ResourceLoaderContext $context (optional)
         *
         * @throws MWException
         * @return array List of concatenated and remapped CSS data from $styles,
         *     keyed by media type
         */
-       public function readStyleFiles( array $styles, $flip ) {
+       public function readStyleFiles( array $styles, $flip, $context = null ) {
                if ( empty( $styles ) ) {
                        return array();
                }
@@ -831,7 +832,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        $uniqueFiles = array_unique( $files, SORT_REGULAR );
                        $styleFiles = array();
                        foreach ( $uniqueFiles as $file ) {
-                               $styleFiles[] = $this->readStyleFile( $file, $flip );
+                               $styleFiles[] = $this->readStyleFile( $file, $flip, $context );
                        }
                        $styles[$media] = implode( "\n", $styleFiles );
                }
@@ -845,11 +846,12 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         *
         * @param string $path File path of style file to read
         * @param bool $flip
+        * @param ResourceLoaderContext $context (optional)
         *
         * @return string CSS data in script file
         * @throws MWException If the file doesn't exist
         */
-       protected function readStyleFile( $path, $flip ) {
+       protected function readStyleFile( $path, $flip, $context = null ) {
                $localPath = $this->getLocalPath( $path );
                $remotePath = $this->getRemotePath( $path );
                if ( !file_exists( $localPath ) ) {
@@ -859,7 +861,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                }
 
                if ( $this->getStyleSheetLang( $localPath ) === 'less' ) {
-                       $style = $this->compileLESSFile( $localPath );
+                       $compiler = $this->getLessCompiler( $context );
+                       $style = $this->compileLessFile( $localPath, $compiler );
                        $this->hasGeneratedStyles = true;
                } else {
                        $style = file_get_contents( $localPath );
@@ -899,60 +902,36 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        }
 
        /**
-        * Generate a cache key for a LESS file.
+        * Compile a LESS file into CSS.
         *
-        * The cache key varies on the file name and the names and values of global
-        * LESS variables.
+        * Keeps track of all used files and adds them to localFileRefs.
         *
         * @since 1.22
-        * @param string $fileName File name of root LESS file.
-        * @return string Cache key
+        * @throws Exception If lessc encounters a parse error
+        * @param string $fileName File path of LESS source
+        * @param lessc $compiler Compiler to use, if not default
+        * @return string CSS source
         */
-       protected function getLESSCacheKey( $fileName ) {
-               $vars = json_encode( ResourceLoader::getLESSVars( $this->getConfig() ) );
-               $hash = md5( $fileName . $vars );
-               return wfMemcKey( 'resourceloader', 'less', $hash );
+       protected function compileLessFile( $fileName, $compiler = null ) {
+               if ( !$compiler ) {
+                       $compiler = $this->getLessCompiler();
+               }
+               $result = $compiler->compileFile( $fileName );
+               $this->localFileRefs += array_keys( $compiler->allParsedFiles() );
+               return $result;
        }
 
        /**
-        * Compile a LESS file into CSS.
+        * Get a LESS compiler instance for this module in given context.
         *
-        * If invalid, returns replacement CSS source consisting of the compilation
-        * error message encoded as a comment. To save work, we cache a result object
-        * which comprises the compiled CSS and the names & mtimes of the files
-        * that were processed. lessphp compares the cached & current mtimes and
-        * recompiles as necessary.
+        * Just calls ResourceLoader::getLessCompiler() by default to get a global compiler.
         *
-        * @since 1.22
-        * @throws Exception If Less encounters a parse error
-        * @throws MWException If Less compilation returns unexpection result
-        * @param string $fileName File path of LESS source
-        * @return string CSS source
+        * @param ResourceLoaderContext $context
+        * @throws MWException
+        * @since 1.24
+        * @return lessc
         */
-       protected function compileLESSFile( $fileName ) {
-               $key = $this->getLESSCacheKey( $fileName );
-               $cache = wfGetCache( CACHE_ANYTHING );
-
-               // The input to lessc. Either an associative array representing the
-               // cached results of a previous compilation, or the string file name if
-               // no cache result exists.
-               $source = $cache->get( $key );
-               if ( !is_array( $source ) || !isset( $source['root'] ) ) {
-                       $source = $fileName;
-               }
-
-               $compiler = ResourceLoader::getLessCompiler( $this->getConfig() );
-               $result = null;
-
-               $result = $compiler->cachedCompile( $source );
-
-               if ( !is_array( $result ) ) {
-                       throw new MWException( 'LESS compiler result has type '
-                               . gettype( $result ) . '; array expected.' );
-               }
-
-               $this->localFileRefs += array_keys( $result['files'] );
-               $cache->set( $key, $result );
-               return $result['compiled'];
+       protected function getLessCompiler( ResourceLoaderContext $context = null ) {
+               return ResourceLoader::getLessCompiler( $this->getConfig() );
        }
 }