3 * Minify a file or set of files
6 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
8 class MinifyScript extends Maintenance {
11 public function __construct() {
12 parent::__construct();
13 $this->addOption( 'outfile',
14 'File for output. Only a single file may be specified for input.',
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.",
20 $this->addOption( 'js-statements-on-own-line',
21 "Boolean value for putting statements on their own line when minifying JavaScript.",
23 $this->addOption( 'js-max-line-length',
24 "Maximum line length for JavaScript minification.",
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.";
32 public function execute() {
33 if ( !count( $this->mArgs ) ) {
34 $this->error( "minify.php: At least one input file must be specified." );
38 if ( $this->hasOption( 'outfile' ) ) {
39 if ( count( $this->mArgs ) > 1 ) {
40 $this->error( '--outfile may only be used with a single input file.' );
45 $this->minify( $this->getArg( 0 ), $this->getOption( 'outfile' ) );
49 $outDir = $this->getOption( 'outdir', false );
51 foreach ( $this->mArgs as $arg ) {
52 $inPath = realpath( $arg );
53 $inName = basename( $inPath );
54 $inDir = dirname( $inPath );
56 if ( strpos( $inName, '.min.' ) !== false ) {
57 $this->error( "Skipping $inName\n" );
61 if ( !file_exists( $inPath ) ) {
62 $this->error( "File does not exist: $arg", true );
65 $extension = $this->getExtension( $inName );
66 $outName = substr( $inName, 0, -strlen( $extension ) ) . 'min.' . $extension;
67 if ( $outDir === false ) {
68 $outPath = $inDir . '/' . $outName;
70 $outPath = $outDir . '/' . $outName;
73 $this->minify( $inPath, $outPath );
77 public function getExtension( $fileName ) {
78 $dotPos = strrpos( $fileName, '.' );
79 if ( $dotPos === false ) {
80 $this->error( "No file extension, cannot determine type: $fileName" );
83 return substr( $fileName, $dotPos + 1 );
86 public function minify( $inPath, $outPath ) {
87 global $wgResourceLoaderMinifierStatementsOnOwnLine, $wgResourceLoaderMinifierMaxLineLength;
89 $extension = $this->getExtension( $inPath );
90 $this->output( basename( $inPath ) . ' -> ' . basename( $outPath ) . '...' );
92 $inText = file_get_contents( $inPath );
93 if ( $inText === false ) {
94 $this->error( "Unable to open file $inPath for reading." );
97 $outFile = fopen( $outPath, 'w' );
99 $this->error( "Unable to open file $outPath for writing." );
103 switch ( $extension ) {
105 $outText = JavaScriptMinifier::minify( $inText,
106 $this->getOption( 'js-statements-on-own-line', $wgResourceLoaderMinifierStatementsOnOwnLine ),
107 $this->getOption( 'js-max-line-length', $wgResourceLoaderMinifierMaxLineLength )
111 $outText = CSSMin::minify( $inText );
114 $this->error( "No minifier defined for extension \"$extension\"" );
117 fwrite( $outFile, $outText );
119 $this->output( " ok\n" );
123 $maintClass = 'MinifyScript';
124 require_once( RUN_MAINTENANCE_IF_MAIN );