]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/minify.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / maintenance / minify.php
1 <?php
2 /**
3  * Minify a file or set of files
4  */
5
6 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
7
8 class MinifyScript extends Maintenance {
9         var $outDir;
10
11         public function __construct() {
12                 parent::__construct();
13                 $this->addOption( 'outfile',
14                         'File for output. Only a single file may be specified for input.',
15                         false, true );
16                 $this->addOption( 'outdir',
17                         "Directory for output. If this is not specified, and neither is --outfile, then the\n" .
18                         "output files will be sent to the same directories as the input files.",
19                         false, true );
20                 $this->addOption( 'js-statements-on-own-line',
21                         "Boolean value for putting statements on their own line when minifying JavaScript.",
22                         false, true );
23                 $this->addOption( 'js-max-line-length',
24                         "Maximum line length for JavaScript minification.",
25                         false, true );
26                 $this->mDescription = "Minify a file or set of files.\n\n" .
27                         "If --outfile is not specified, then the output file names will have a .min extension\n" .
28                         "added, e.g. jquery.js -> jquery.min.js.";
29
30         }
31
32         public function execute() {
33                 if ( !count( $this->mArgs ) ) {
34                         $this->error( "minify.php: At least one input file must be specified." );
35                         exit( 1 );
36                 }
37
38                 if ( $this->hasOption( 'outfile' ) ) {
39                         if ( count( $this->mArgs ) > 1 ) {
40                                 $this->error( '--outfile may only be used with a single input file.' );
41                                 exit( 1 );
42                         }
43
44                         // Minify one file
45                         $this->minify( $this->getArg( 0 ), $this->getOption( 'outfile' ) );
46                         return;
47                 }
48
49                 $outDir = $this->getOption( 'outdir', false );
50
51                 foreach ( $this->mArgs as $arg ) {
52                         $inPath = realpath( $arg );
53                         $inName = basename( $inPath );
54                         $inDir = dirname( $inPath );
55
56                         if ( strpos( $inName, '.min.' ) !== false ) {
57                                 $this->error( "Skipping $inName\n" );
58                                 continue;
59                         }
60
61                         if ( !file_exists( $inPath ) ) {
62                                 $this->error( "File does not exist: $arg", true );
63                         }
64
65                         $extension = $this->getExtension( $inName );
66                         $outName = substr( $inName, 0, -strlen( $extension ) ) . 'min.' . $extension;
67                         if ( $outDir === false ) {
68                                 $outPath = $inDir . '/' . $outName;
69                         } else {
70                                 $outPath = $outDir . '/' . $outName;
71                         }
72
73                         $this->minify( $inPath, $outPath );
74                 }
75         }
76
77         public function getExtension( $fileName ) {
78                 $dotPos = strrpos( $fileName, '.' );
79                 if ( $dotPos === false ) {
80                         $this->error( "No file extension, cannot determine type: $fileName" );
81                         exit( 1 );
82                 }
83                 return substr( $fileName, $dotPos + 1 );
84         }
85
86         public function minify( $inPath, $outPath ) {
87                 global $wgResourceLoaderMinifierStatementsOnOwnLine, $wgResourceLoaderMinifierMaxLineLength;
88
89                 $extension = $this->getExtension( $inPath );
90                 $this->output( basename( $inPath ) . ' -> ' . basename( $outPath ) . '...' );
91
92                 $inText = file_get_contents( $inPath );
93                 if ( $inText === false ) {
94                         $this->error( "Unable to open file $inPath for reading." );
95                         exit( 1 );
96                 }
97                 $outFile = fopen( $outPath, 'w' );
98                 if ( !$outFile ) {
99                         $this->error( "Unable to open file $outPath for writing." );
100                         exit( 1 );
101                 }
102
103                 switch ( $extension ) {
104                         case 'js':
105                                 $outText = JavaScriptMinifier::minify( $inText,
106                                         $this->getOption( 'js-statements-on-own-line', $wgResourceLoaderMinifierStatementsOnOwnLine ),
107                                         $this->getOption( 'js-max-line-length', $wgResourceLoaderMinifierMaxLineLength )
108                                 );
109                                 break;
110                         case 'css':
111                                 $outText = CSSMin::minify( $inText );
112                                 break;
113                         default:
114                                 $this->error( "No minifier defined for extension \"$extension\"" );
115                 }
116
117                 fwrite( $outFile, $outText );
118                 fclose( $outFile );
119                 $this->output( " ok\n" );
120         }
121 }
122
123 $maintClass = 'MinifyScript';
124 require_once( RUN_MAINTENANCE_IF_MAIN );