]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/minify.php
MediaWiki 1.16.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->mDescription = "Minify a file or set of files.\n\n" .
21                         "If --outfile is not specified, then the output file names will have a .min extension\n" . 
22                         "added, e.g. jquery.js -> jquery.min.js.";
23
24         }
25
26         public function execute() {
27                 if ( !count( $this->mArgs ) ) {
28                         $this->error( "minify.php: At least one input file must be specified." );
29                         exit( 1 );
30                 }
31
32                 if ( $this->hasOption( 'outfile' ) ) {
33                         if ( count( $this->mArgs ) > 1 ) {
34                                 $this->error( '--outfile may only be used with a single input file.' );
35                                 exit( 1 );
36                         }
37
38                         // Minify one file
39                         $this->minify( $this->getArg( 0 ), $this->getOption( 'outfile' ) );
40                         return;
41                 }
42
43                 $outDir = $this->getOption( 'outdir', false );
44
45                 foreach ( $this->mArgs as $arg ) {
46                         $inPath = realpath( $arg );
47                         $inName = basename( $inPath );
48                         $inDir = dirname( $inPath );
49
50                         if ( strpos( $inName, '.min.' ) !== false ) {
51                                 echo "Skipping $inName\n";
52                                 continue;
53                         }
54
55                         if ( !file_exists( $inPath ) ) {
56                                 $this->error( "File does not exist: $arg" );
57                                 exit( 1 );
58                         }
59
60                         $extension = $this->getExtension( $inName );
61                         $outName = substr( $inName, 0, -strlen( $extension ) ) . 'min.' . $extension;
62                         if ( $outDir === false ) {
63                                 $outPath = $inDir . '/' . $outName;
64                         } else {
65                                 $outPath = $outDir . '/' . $outName;
66                         }
67
68                         $this->minify( $inPath, $outPath );
69                 }
70         }
71
72         public function getExtension( $fileName ) {
73                 $dotPos = strrpos( $fileName, '.' );
74                 if ( $dotPos === false ) {
75                         $this->error( "No file extension, cannot determine type: $arg" );
76                         exit( 1 );
77                 }
78                 return substr( $fileName, $dotPos + 1 );
79         }
80
81         public function minify( $inPath, $outPath ) {
82                 $extension = $this->getExtension( $inPath );
83                 echo basename( $inPath ) . ' -> ' . basename( $outPath ) . '...';
84
85                 $inText = file_get_contents( $inPath );
86                 if ( $inText === false ) {
87                         $this->error( "Unable to open file $inPath for reading." );
88                         exit( 1 );
89                 }
90                 $outFile = fopen( $outPath, 'w' );
91                 if ( !$outFile ) {
92                         $this->error( "Unable to open file $outPath for writing." );
93                         exit( 1 );
94                 }
95
96                 switch ( $extension ) {
97                         case 'js':
98                                 $outText = JSMin::minify( $inText );
99                                 break;
100                         default:
101                                 $this->error( "No minifier defined for extension \"$extension\"" );
102                 }
103
104                 fwrite( $outFile, $outText );
105                 fclose( $outFile );
106                 echo " ok\n";
107         }
108 }
109
110 $maintClass = 'MinifyScript';
111 require_once( DO_MAINTENANCE );