3 * Minify a file or set of files
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 require_once( dirname( __FILE__
) . '/Maintenance.php' );
26 class MinifyScript
extends Maintenance
{
29 public function __construct() {
30 parent
::__construct();
31 $this->addOption( 'outfile',
32 'File for output. Only a single file may be specified for input.',
34 $this->addOption( 'outdir',
35 "Directory for output. If this is not specified, and neither is --outfile, then the\n" .
36 "output files will be sent to the same directories as the input files.",
38 $this->addOption( 'minify-vertical-space',
39 "Boolean value for minifying the vertical space for javascript.",
41 $this->mDescription
= "Minify a file or set of files.\n\n" .
42 "If --outfile is not specified, then the output file names will have a .min extension\n" .
43 "added, e.g. jquery.js -> jquery.min.js.";
47 public function execute() {
48 if ( !count( $this->mArgs
) ) {
49 $this->error( "minify.php: At least one input file must be specified." );
53 if ( $this->hasOption( 'outfile' ) ) {
54 if ( count( $this->mArgs
) > 1 ) {
55 $this->error( '--outfile may only be used with a single input file.' );
60 $this->minify( $this->getArg( 0 ), $this->getOption( 'outfile' ) );
64 $outDir = $this->getOption( 'outdir', false );
66 foreach ( $this->mArgs
as $arg ) {
67 $inPath = realpath( $arg );
68 $inName = basename( $inPath );
69 $inDir = dirname( $inPath );
71 if ( strpos( $inName, '.min.' ) !== false ) {
72 $this->error( "Skipping $inName\n" );
76 if ( !file_exists( $inPath ) ) {
77 $this->error( "File does not exist: $arg", true );
80 $extension = $this->getExtension( $inName );
81 $outName = substr( $inName, 0, -strlen( $extension ) ) . 'min.' . $extension;
82 if ( $outDir === false ) {
83 $outPath = $inDir . '/' . $outName;
85 $outPath = $outDir . '/' . $outName;
88 $this->minify( $inPath, $outPath );
92 public function getExtension( $fileName ) {
93 $dotPos = strrpos( $fileName, '.' );
94 if ( $dotPos === false ) {
95 $this->error( "No file extension, cannot determine type: $arg" );
98 return substr( $fileName, $dotPos +
1 );
101 public function minify( $inPath, $outPath ) {
102 global $wgResourceLoaderMinifyJSVerticalSpace;
104 $extension = $this->getExtension( $inPath );
105 $this->output( basename( $inPath ) . ' -> ' . basename( $outPath ) . '...' );
107 $inText = file_get_contents( $inPath );
108 if ( $inText === false ) {
109 $this->error( "Unable to open file $inPath for reading." );
112 $outFile = fopen( $outPath, 'w' );
114 $this->error( "Unable to open file $outPath for writing." );
118 switch ( $extension ) {
120 $outText = JavaScriptDistiller
::stripWhiteSpace( $inText, $this->getOption( 'minify-vertical-space', $wgResourceLoaderMinifyJSVerticalSpace ) );
123 $outText = CSSMin
::minify( $inText );
126 $this->error( "No minifier defined for extension \"$extension\"" );
129 fwrite( $outFile, $outText );
131 $this->output( " ok\n" );
135 $maintClass = 'MinifyScript';
136 require_once( RUN_MAINTENANCE_IF_MAIN
);