]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blobdiff - maintenance/minify.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / maintenance / minify.php
index 601a4d67ca403f80128ab16fa5139f2d0a1bb78e..2884fd7b2b7a3d44e6c32515c6b3380b9cb85500 100644 (file)
@@ -10,15 +10,21 @@ class MinifyScript extends Maintenance {
 
        public function __construct() {
                parent::__construct();
-               $this->addOption( 'outfile', 
-                       'File for output. Only a single file may be specified for input.', 
+               $this->addOption( 'outfile',
+                       'File for output. Only a single file may be specified for input.',
                        false, true );
                $this->addOption( 'outdir',
-                       "Directory for output. If this is not specified, and neither is --outfile, then the\n" . 
+                       "Directory for output. If this is not specified, and neither is --outfile, then the\n" .
                        "output files will be sent to the same directories as the input files.",
                        false, true );
+               $this->addOption( 'js-statements-on-own-line',
+                       "Boolean value for putting statements on their own line when minifying JavaScript.",
+                       false, true );
+               $this->addOption( 'js-max-line-length',
+                       "Maximum line length for JavaScript minification.",
+                       false, true );
                $this->mDescription = "Minify a file or set of files.\n\n" .
-                       "If --outfile is not specified, then the output file names will have a .min extension\n" . 
+                       "If --outfile is not specified, then the output file names will have a .min extension\n" .
                        "added, e.g. jquery.js -> jquery.min.js.";
 
        }
@@ -48,13 +54,12 @@ class MinifyScript extends Maintenance {
                        $inDir = dirname( $inPath );
 
                        if ( strpos( $inName, '.min.' ) !== false ) {
-                               echo "Skipping $inName\n";
+                               $this->error( "Skipping $inName\n" );
                                continue;
                        }
 
                        if ( !file_exists( $inPath ) ) {
-                               $this->error( "File does not exist: $arg" );
-                               exit( 1 );
+                               $this->error( "File does not exist: $arg", true );
                        }
 
                        $extension = $this->getExtension( $inName );
@@ -72,15 +77,17 @@ class MinifyScript extends Maintenance {
        public function getExtension( $fileName ) {
                $dotPos = strrpos( $fileName, '.' );
                if ( $dotPos === false ) {
-                       $this->error( "No file extension, cannot determine type: $arg" );
+                       $this->error( "No file extension, cannot determine type: $fileName" );
                        exit( 1 );
                }
                return substr( $fileName, $dotPos + 1 );
        }
 
        public function minify( $inPath, $outPath ) {
+               global $wgResourceLoaderMinifierStatementsOnOwnLine, $wgResourceLoaderMinifierMaxLineLength;
+
                $extension = $this->getExtension( $inPath );
-               echo basename( $inPath ) . ' -> ' . basename( $outPath ) . '...';
+               $this->output( basename( $inPath ) . ' -> ' . basename( $outPath ) . '...' );
 
                $inText = file_get_contents( $inPath );
                if ( $inText === false ) {
@@ -95,7 +102,13 @@ class MinifyScript extends Maintenance {
 
                switch ( $extension ) {
                        case 'js':
-                               $outText = JSMin::minify( $inText );
+                               $outText = JavaScriptMinifier::minify( $inText,
+                                       $this->getOption( 'js-statements-on-own-line', $wgResourceLoaderMinifierStatementsOnOwnLine ),
+                                       $this->getOption( 'js-max-line-length', $wgResourceLoaderMinifierMaxLineLength )
+                               );
+                               break;
+                       case 'css':
+                               $outText = CSSMin::minify( $inText );
                                break;
                        default:
                                $this->error( "No minifier defined for extension \"$extension\"" );
@@ -103,9 +116,9 @@ class MinifyScript extends Maintenance {
 
                fwrite( $outFile, $outText );
                fclose( $outFile );
-               echo " ok\n";
+               $this->output( " ok\n" );
        }
 }
 
 $maintClass = 'MinifyScript';
-require_once( DO_MAINTENANCE );
+require_once( RUN_MAINTENANCE_IF_MAIN );