ResourceLoaderFileModule: Support fallback in 'languageScripts'
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderFileModule.php
index 6128f19..7bbc9bb 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,16 @@ 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 ( $localBasePath === null ) {
+                       $localBasePath = $IP;
+               }
                if ( $remoteBasePath === null ) {
-                       $remoteBasePath = $wgResourceBasePath === null ? $wgScriptPath : $wgResourceBasePath;
+                       $remoteBasePath = $wgResourceBasePath;
                }
 
                if ( isset( $options['remoteExtPath'] ) ) {
@@ -385,7 +387,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 );
@@ -533,7 +536,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        $files,
                        $this->scripts,
                        $context->getDebug() ? $this->debugScripts : array(),
-                       self::tryForKey( $this->languageScripts, $context->getLanguage() ),
+                       $this->getLanguageScripts( $context->getLanguage() ),
                        self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' ),
                        $this->loaderScripts
                );
@@ -697,7 +700,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        protected function getScriptFiles( ResourceLoaderContext $context ) {
                $files = array_merge(
                        $this->scripts,
-                       self::tryForKey( $this->languageScripts, $context->getLanguage() ),
+                       $this->getLanguageScripts( $context->getLanguage() ),
                        self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' )
                );
                if ( $context->getDebug() ) {
@@ -707,6 +710,29 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                return array_unique( $files, SORT_REGULAR );
        }
 
+       /**
+        * Get the set of language scripts for the given language,
+        * possibly using a fallback language.
+        *
+        * @param string $lang
+        * @return array
+        */
+       private function getLanguageScripts( $lang ) {
+               $scripts = self::tryForKey( $this->languageScripts, $lang );
+               if ( $scripts ) {
+                       return $scripts;
+               }
+               $fallbacks = Language::getFallbacksFor( $lang );
+               foreach ( $fallbacks as $lang ) {
+                       $scripts = self::tryForKey( $this->languageScripts, $lang );
+                       if ( $scripts ) {
+                               return $scripts;
+                       }
+               }
+
+               return array();
+       }
+
        /**
         * Get a list of file paths for all styles in this module, in order of proper inclusion.
         *
@@ -816,14 +842,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 +857,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 +871,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 +886,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 );
@@ -867,8 +895,6 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
 
                if ( $flip ) {
                        $style = CSSJanus::transform( $style, true, false );
-               } else {
-                       $style = CSSJanus::nullTransform( $style );
                }
                $localDir = dirname( $localPath );
                $remoteDir = dirname( $remotePath );
@@ -908,12 +934,29 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         * @since 1.22
         * @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 compileLessFile( $fileName ) {
-               $compiler = ResourceLoader::getLessCompiler( $this->getConfig() );
+       protected function compileLessFile( $fileName, $compiler = null ) {
+               if ( !$compiler ) {
+                       $compiler = $this->getLessCompiler();
+               }
                $result = $compiler->compileFile( $fileName );
                $this->localFileRefs += array_keys( $compiler->allParsedFiles() );
                return $result;
        }
+
+       /**
+        * Get a LESS compiler instance for this module in given context.
+        *
+        * Just calls ResourceLoader::getLessCompiler() by default to get a global compiler.
+        *
+        * @param ResourceLoaderContext $context
+        * @throws MWException
+        * @since 1.24
+        * @return lessc
+        */
+       protected function getLessCompiler( ResourceLoaderContext $context = null ) {
+               return ResourceLoader::getLessCompiler( $this->getConfig() );
+       }
 }