Installer: Re-use ResourceLoader methods instead of duplicating
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 4 Jun 2014 19:30:14 +0000 (21:30 +0200)
committerTimo Tijhof <krinklemail@gmail.com>
Sat, 7 Jun 2014 08:02:55 +0000 (10:02 +0200)
Also:
* Fix incorrect documentation comment of ResourceLoaderModule::getStyles(),
  it doesn't return a string, it never has.
* Remove spurious space in count() call.
* Fix spelling of in "proper".

Change-Id: I000393636f7b9015ad1bacfbb3eb8a6ad8695d72

includes/installer/WebInstallerOutput.php
includes/resourceloader/ResourceLoader.php
includes/resourceloader/ResourceLoaderFileModule.php

index fd6ed00..d81111b 100644 (file)
@@ -119,87 +119,41 @@ class WebInstallerOutput {
        }
 
        /**
-        * Get the raw vector CSS, flipping if needed
-        *
-        * @todo Possibly get rid of this function and use ResourceLoader in the manner it was
-        *   designed to be used in, rather than just grabbing a list of filenames from it,
-        *   and not properly handling such details as media types in module definitions.
+        * Get the stylesheet of the MediaWiki skin.
         *
         * @return string
         */
        public function getCSS() {
-               // All CSS files these modules reference will be concatenated in sequence
-               // and loaded as one file.
                $moduleNames = array(
+                       // See SkinTemplate::setupSkinUserCss
                        'mediawiki.legacy.shared',
+                       // See Vector::setupSkinUserCss
                        'mediawiki.skinning.interface',
                        'skins.vector.styles',
+
                        'mediawiki.legacy.config',
                );
 
-               $prepend = '';
                $css = '';
 
                $resourceLoader = new ResourceLoader();
+               $rlContext = new ResourceLoaderContext( $resourceLoader, new FauxRequest( array(
+                               'debug' => 'true',
+                               'lang' => $this->getLanguageCode(),
+                               'only' => 'styles',
+                               'skin' => 'vector',
+               ) ) );
                foreach ( $moduleNames as $moduleName ) {
                        /** @var ResourceLoaderFileModule $module */
                        $module = $resourceLoader->getModule( $moduleName );
-                       $cssFileNames = $module->getAllStyleFiles();
-
-                       wfSuppressWarnings();
-                       foreach ( $cssFileNames as $cssFileName ) {
-                               if ( !file_exists( $cssFileName ) ) {
-                                       $prepend .= ResourceLoader::makeComment( "Unable to find $cssFileName." );
-                                       continue;
-                               }
-
-                               if ( !is_readable( $cssFileName ) ) {
-                                       $prepend .= ResourceLoader::makeComment( "Unable to read $cssFileName. " .
-                                               "Please check file permissions." );
-                                       continue;
-                               }
-
-                               try {
-
-                                       if ( preg_match( '/\.less$/', $cssFileName ) ) {
-                                               // Run the LESS compiler for *.less files (bug 55589)
-                                               $compiler = ResourceLoader::getLessCompiler();
-                                               $cssFileContents = $compiler->compileFile( $cssFileName );
-                                       } else {
-                                               // Regular CSS file
-                                               $cssFileContents = file_get_contents( $cssFileName );
-                                       }
-
-                                       if ( $cssFileContents ) {
-                                               // Rewrite URLs, though don't bother embedding images. While static image
-                                               // files may be cached, CSS returned by this function is definitely not.
-                                               $cssDirName = dirname( $cssFileName );
-                                               $css .= CSSMin::remap(
-                                                       /* source */ $cssFileContents,
-                                                       /* local */ $cssDirName,
-                                                       /* remote */ '..' . str_replace(
-                                                               array( $GLOBALS['IP'], DIRECTORY_SEPARATOR ),
-                                                               array( '', '/' ),
-                                                               $cssDirName
-                                                       ),
-                                                       /* embedData */ false
-                                               );
-                                       } else {
-                                               $prepend .= ResourceLoader::makeComment( "Unable to read $cssFileName." );
-                                       }
-                               } catch ( Exception $e ) {
-                                       $prepend .= ResourceLoader::formatException( $e );
-                               }
-
-                               $css .= "\n";
-                       }
-                       wfRestoreWarnings();
-               }
 
-               $css = $prepend . $css;
+                       // Based on: ResourceLoaderFileModule::getStyles (without the DB query)
+                       $styles = ResourceLoader::makeCombinedStyles( $module->readStyleFiles(
+                               $module->getStyleFiles( $rlContext ),
+                               $module->getFlip( $rlContext )
+                       ) );
 
-               if ( $this->getDir() == 'rtl' ) {
-                       $css = CSSJanus::transform( $css, true );
+                       $css .= implode( "\n", $styles );
                }
 
                return $css;
index 680bd99..7765714 100644 (file)
@@ -814,7 +814,7 @@ class ResourceLoader {
                                        // Don't create empty stylesheets like array( '' => '' ) for modules
                                        // that don't *have* any stylesheets (bug 38024).
                                        $stylePairs = $module->getStyles( $context );
-                                       if ( count ( $stylePairs ) ) {
+                                       if ( count( $stylePairs ) ) {
                                                // If we are in debug mode without &only= set, we'll want to return an array of URLs
                                                // See comment near shouldIncludeScripts() for more details
                                                if ( $context->getDebug() && !$context->getOnly() && $module->supportsURLLoading() ) {
@@ -988,7 +988,7 @@ class ResourceLoader {
         * @param array $stylePairs Array keyed by media type containing (arrays of) CSS strings
         * @return array
         */
-       private static function makeCombinedStyles( array $stylePairs ) {
+       public static function makeCombinedStyles( array $stylePairs ) {
                $out = array();
                foreach ( $stylePairs as $media => $styles ) {
                        // ResourceLoaderFileModule::getStyle can return the styles
index 382bdd9..f9ff029 100644 (file)
@@ -312,22 +312,22 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        }
 
        /**
-        * Gets loader script.
+        * Get loader script.
         *
-        * @return string JavaScript code to be added to startup module
+        * @return string|false JavaScript code to be added to startup module
         */
        public function getLoaderScript() {
-               if ( count( $this->loaderScripts ) == 0 ) {
+               if ( count( $this->loaderScripts ) === 0 ) {
                        return false;
                }
                return $this->readScriptFiles( $this->loaderScripts );
        }
 
        /**
-        * Gets all styles for a given context concatenated together.
+        * Get all styles for a given context.
         *
-        * @param ResourceLoaderContext $context Context in which to generate styles
-        * @return string CSS code for $context
+        * @param ResourceLoaderContext $context
+        * @return array CSS code for $context as an associative array mapping media type to CSS text.
         */
        public function getStyles( ResourceLoaderContext $context ) {
                $styles = $this->readStyleFiles(
@@ -581,13 +581,13 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        }
 
        /**
-        * Gets a list of element that match a key, optionally using a fallback key.
+        * Get a list of element that match a key, optionally using a fallback key.
         *
         * @param array $list List of lists to select from
         * @param string $key Key to look for in $map
         * @param string $fallback Key to look for in $list if $key doesn't exist
         * @return array List of elements from $map which matched $key or $fallback,
-        *     or an empty list in case of no match
+        *  or an empty list in case of no match
         */
        protected static function tryForKey( array $list, $key, $fallback = null ) {
                if ( isset( $list[$key] ) && is_array( $list[$key] ) ) {
@@ -602,7 +602,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        }
 
        /**
-        * Gets a list of file paths for all scripts in this module, in order of propper execution.
+        * Get a list of file paths for all scripts in this module, in order of proper execution.
         *
         * @param ResourceLoaderContext $context
         * @return array List of file paths
@@ -621,12 +621,12 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        }
 
        /**
-        * Gets a list of file paths for all styles in this module, in order of propper inclusion.
+        * Get a list of file paths for all styles in this module, in order of proper inclusion.
         *
         * @param ResourceLoaderContext $context
         * @return array List of file paths
         */
-       protected function getStyleFiles( ResourceLoaderContext $context ) {
+       public function getStyleFiles( ResourceLoaderContext $context ) {
                return array_merge_recursive(
                        self::collateFilePathListByOption( $this->styles, 'media', 'all' ),
                        self::collateFilePathListByOption(
@@ -696,7 +696,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         * @return array List of concatenated and remapped CSS data from $styles,
         *     keyed by media type
         */
-       protected function readStyleFiles( array $styles, $flip ) {
+       public function readStyleFiles( array $styles, $flip ) {
                if ( empty( $styles ) ) {
                        return array();
                }