Follow-up r81619: Add new message key to maintenance file
[lhc/web/wiklou.git] / maintenance / minify.php
1 <?php
2 /**
3 * Minify a file or set of files
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
25
26 class MinifyScript extends Maintenance {
27 var $outDir;
28
29 public function __construct() {
30 parent::__construct();
31 $this->addOption( 'outfile',
32 'File for output. Only a single file may be specified for input.',
33 false, true );
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.",
37 false, true );
38 $this->addOption( 'minify-vertical-space',
39 "Boolean value for minifying the vertical space for javascript.",
40 false, true );
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.";
44
45 }
46
47 public function execute() {
48 if ( !count( $this->mArgs ) ) {
49 $this->error( "minify.php: At least one input file must be specified." );
50 exit( 1 );
51 }
52
53 if ( $this->hasOption( 'outfile' ) ) {
54 if ( count( $this->mArgs ) > 1 ) {
55 $this->error( '--outfile may only be used with a single input file.' );
56 exit( 1 );
57 }
58
59 // Minify one file
60 $this->minify( $this->getArg( 0 ), $this->getOption( 'outfile' ) );
61 return;
62 }
63
64 $outDir = $this->getOption( 'outdir', false );
65
66 foreach ( $this->mArgs as $arg ) {
67 $inPath = realpath( $arg );
68 $inName = basename( $inPath );
69 $inDir = dirname( $inPath );
70
71 if ( strpos( $inName, '.min.' ) !== false ) {
72 $this->error( "Skipping $inName\n" );
73 continue;
74 }
75
76 if ( !file_exists( $inPath ) ) {
77 $this->error( "File does not exist: $arg", true );
78 }
79
80 $extension = $this->getExtension( $inName );
81 $outName = substr( $inName, 0, -strlen( $extension ) ) . 'min.' . $extension;
82 if ( $outDir === false ) {
83 $outPath = $inDir . '/' . $outName;
84 } else {
85 $outPath = $outDir . '/' . $outName;
86 }
87
88 $this->minify( $inPath, $outPath );
89 }
90 }
91
92 public function getExtension( $fileName ) {
93 $dotPos = strrpos( $fileName, '.' );
94 if ( $dotPos === false ) {
95 $this->error( "No file extension, cannot determine type: $arg" );
96 exit( 1 );
97 }
98 return substr( $fileName, $dotPos + 1 );
99 }
100
101 public function minify( $inPath, $outPath ) {
102 global $wgResourceLoaderMinifyJSVerticalSpace;
103
104 $extension = $this->getExtension( $inPath );
105 $this->output( basename( $inPath ) . ' -> ' . basename( $outPath ) . '...' );
106
107 $inText = file_get_contents( $inPath );
108 if ( $inText === false ) {
109 $this->error( "Unable to open file $inPath for reading." );
110 exit( 1 );
111 }
112 $outFile = fopen( $outPath, 'w' );
113 if ( !$outFile ) {
114 $this->error( "Unable to open file $outPath for writing." );
115 exit( 1 );
116 }
117
118 switch ( $extension ) {
119 case 'js':
120 $outText = JavaScriptDistiller::stripWhiteSpace( $inText, $this->getOption( 'minify-vertical-space', $wgResourceLoaderMinifyJSVerticalSpace ) );
121 break;
122 case 'css':
123 $outText = CSSMin::minify( $inText );
124 break;
125 default:
126 $this->error( "No minifier defined for extension \"$extension\"" );
127 }
128
129 fwrite( $outFile, $outText );
130 fclose( $outFile );
131 $this->output( " ok\n" );
132 }
133 }
134
135 $maintClass = 'MinifyScript';
136 require_once( RUN_MAINTENANCE_IF_MAIN );