installer: Run the LESS compiler for *.less files
authorKevin Israel <pleasestand@live.com>
Mon, 14 Oct 2013 01:34:58 +0000 (21:34 -0400)
committerKevin Israel <pleasestand@live.com>
Thu, 24 Oct 2013 05:02:47 +0000 (01:02 -0400)
Includes the following improvements:

* Use the ResourceLoader module definitions (with a hardcoded list of
  module names) instead of a hardcoded list of filenames.
* Make the errors more discoverable like we do in load.php, prepending
  them instead of burying them in the middle somewhere.

Bug: 55589
Change-Id: Iab78a60209ab46a10d4c492c75527e717f36f803

includes/installer/Installer.php
includes/installer/WebInstallerOutput.php
includes/resourceloader/ResourceLoader.php
resources/Resources.php

index 5eaacf8..4f04c22 100644 (file)
@@ -1730,6 +1730,11 @@ abstract class Installer {
 
                // Some of the environment checks make shell requests, remove limits
                $GLOBALS['wgMaxShellMemory'] = 0;
+
+               // Don't bother embedding images into generated CSS, which is not cached
+               $GLOBALS['wgResourceLoaderLESSFunctions']['embeddable'] = function( $frame, $less ) {
+                       return $less->toBool( false );
+               };
        }
 
        /**
index 1df8f05..f2dc37f 100644 (file)
@@ -104,47 +104,83 @@ 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.
+        *
         * @param string $dir 'ltr' or 'rtl'
         * @return String
         */
        public function getCSS( $dir ) {
-               $skinDir = dirname( dirname( __DIR__ ) ) . '/skins';
-
-               // All these files will be concatenated in sequence and loaded
-               // as one file.
-               // The string 'images/' in the files' contents will be replaced
-               // by '../skins/$skinName/images/', where $skinName is what appears
-               // before the last '/' in each of the strings.
-               $cssFileNames = array(
-
-                       // Basically the "skins.vector" ResourceLoader module styles
-                       'common/shared.css',
-                       'common/commonElements.css',
-                       'common/commonContent.css',
-                       'common/commonInterface.css',
-                       'vector/screen.css',
-
-                       // mw-config specific
-                       'common/config.css',
+               // All CSS files these modules reference will be concatenated in sequence
+               // and loaded as one file.
+               $moduleNames = array(
+                       'mediawiki.legacy.shared',
+                       'skins.vector',
+                       'mediawiki.legacy.config',
                );
 
+               $prepend = '';
                $css = '';
 
-               wfSuppressWarnings();
-               foreach ( $cssFileNames as $cssFileName ) {
-                       $fullCssFileName = "$skinDir/$cssFileName";
-                       $cssFileContents = file_get_contents( $fullCssFileName );
-                       if ( $cssFileContents ) {
-                               preg_match( "/^(\w+)\//", $cssFileName, $match );
-                               $skinName = $match[1];
-                               $css .= str_replace( 'images/', "../skins/$skinName/images/", $cssFileContents );
-                       } else {
-                               $css .= "/** Your webserver cannot read $fullCssFileName. Please check file permissions. */";
+               $cssFileNames = array();
+               $resourceLoader = new ResourceLoader();
+               foreach ( $moduleNames as $moduleName ) {
+                       $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";
                        }
-
-                       $css .= "\n";
+                       wfRestoreWarnings();
                }
-               wfRestoreWarnings();
+
+               $css = $prepend . $css;
 
                if ( $dir == 'rtl' ) {
                        $css = CSSJanus::transform( $css, true );
index 19d019d..e7ea20c 100644 (file)
@@ -1223,6 +1223,13 @@ class ResourceLoader {
        public static function getLessCompiler() {
                global $wgResourceLoaderLESSFunctions, $wgResourceLoaderLESSImportPaths;
 
+               // When called from the installer, it is possible that a required PHP extension
+               // is missing (at least for now; see bug 47564). If this is the case, throw an
+               // exception (caught by the installer) to prevent a fatal error later on.
+               if ( !function_exists( 'ctype_digit' ) ) {
+                       throw new MWException( 'lessc requires the Ctype extension' );
+               }
+
                $less = new lessc();
                $less->setPreserveComments( true );
                $less->setVariables( self::getLESSVars() );
index c033647..bd6e675 100644 (file)
@@ -87,7 +87,7 @@ return array(
                'localBasePath' => $GLOBALS['wgStyleDirectory'],
        ),
        'skins.vector' => array(
-               // Keep in sync with WebInstallerOutput::getCSS()
+               // Used in the web installer. Test it after modifying this definition!
                'styles' => array(
                        'common/commonElements.css' => array( 'media' => 'screen' ),
                        'common/commonContent.css' => array( 'media' => 'screen' ),
@@ -1106,8 +1106,9 @@ return array(
                'localBasePath' => $GLOBALS['wgStyleDirectory'],
        ),
        'mediawiki.legacy.config' => array(
+               // Used in the web installer. Test it after modifying this definition!
                'scripts' => 'common/config.js',
-               'styles' => array( 'common/config.css', 'common/config-cc.css' ),
+               'styles' => array( 'common/config.css' ),
                'remoteBasePath' => $GLOBALS['wgStylePath'],
                'localBasePath' => $GLOBALS['wgStyleDirectory'],
                'dependencies' => 'mediawiki.legacy.wikibits',
@@ -1129,6 +1130,7 @@ return array(
                'position' => 'top',
        ),
        'mediawiki.legacy.shared' => array(
+               // Used in the web installer. Test it after modifying this definition!
                'styles' => array( 'common/shared.css' => array( 'media' => 'screen' ) ),
                'remoteBasePath' => $GLOBALS['wgStylePath'],
                'localBasePath' => $GLOBALS['wgStyleDirectory'],