Merge "Use local context to get messages and don't use implicit Message object to...
[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( 'js-statements-on-own-line',
39 "Boolean value for putting statements on their own line when minifying JavaScript.",
40 false, true );
41 $this->addOption( 'js-max-line-length',
42 "Maximum line length for JavaScript minification.",
43 false, true );
44 $this->mDescription = "Minify a file or set of files.\n\n" .
45 "If --outfile is not specified, then the output file names will have a .min extension\n" .
46 "added, e.g. jquery.js -> jquery.min.js.";
47
48 }
49
50 public function execute() {
51 if ( !count( $this->mArgs ) ) {
52 $this->error( "minify.php: At least one input file must be specified." );
53 exit( 1 );
54 }
55
56 if ( $this->hasOption( 'outfile' ) ) {
57 if ( count( $this->mArgs ) > 1 ) {
58 $this->error( '--outfile may only be used with a single input file.' );
59 exit( 1 );
60 }
61
62 // Minify one file
63 $this->minify( $this->getArg( 0 ), $this->getOption( 'outfile' ) );
64 return;
65 }
66
67 $outDir = $this->getOption( 'outdir', false );
68
69 foreach ( $this->mArgs as $arg ) {
70 $inPath = realpath( $arg );
71 $inName = basename( $inPath );
72 $inDir = dirname( $inPath );
73
74 if ( strpos( $inName, '.min.' ) !== false ) {
75 $this->error( "Skipping $inName\n" );
76 continue;
77 }
78
79 if ( !file_exists( $inPath ) ) {
80 $this->error( "File does not exist: $arg", true );
81 }
82
83 $extension = $this->getExtension( $inName );
84 $outName = substr( $inName, 0, -strlen( $extension ) ) . 'min.' . $extension;
85 if ( $outDir === false ) {
86 $outPath = $inDir . '/' . $outName;
87 } else {
88 $outPath = $outDir . '/' . $outName;
89 }
90
91 $this->minify( $inPath, $outPath );
92 }
93 }
94
95 public function getExtension( $fileName ) {
96 $dotPos = strrpos( $fileName, '.' );
97 if ( $dotPos === false ) {
98 $this->error( "No file extension, cannot determine type: $fileName" );
99 exit( 1 );
100 }
101 return substr( $fileName, $dotPos + 1 );
102 }
103
104 public function minify( $inPath, $outPath ) {
105 global $wgResourceLoaderMinifierStatementsOnOwnLine, $wgResourceLoaderMinifierMaxLineLength;
106
107 $extension = $this->getExtension( $inPath );
108 $this->output( basename( $inPath ) . ' -> ' . basename( $outPath ) . '...' );
109
110 $inText = file_get_contents( $inPath );
111 if ( $inText === false ) {
112 $this->error( "Unable to open file $inPath for reading." );
113 exit( 1 );
114 }
115 $outFile = fopen( $outPath, 'w' );
116 if ( !$outFile ) {
117 $this->error( "Unable to open file $outPath for writing." );
118 exit( 1 );
119 }
120
121 switch ( $extension ) {
122 case 'js':
123 $outText = JavaScriptMinifier::minify( $inText,
124 $this->getOption( 'js-statements-on-own-line', $wgResourceLoaderMinifierStatementsOnOwnLine ),
125 $this->getOption( 'js-max-line-length', $wgResourceLoaderMinifierMaxLineLength )
126 );
127 break;
128 case 'css':
129 $outText = CSSMin::minify( $inText );
130 break;
131 default:
132 $this->error( "No minifier defined for extension \"$extension\"" );
133 }
134
135 fwrite( $outFile, $outText );
136 fclose( $outFile );
137 $this->output( " ok\n" );
138 }
139 }
140
141 $maintClass = 'MinifyScript';
142 require_once( RUN_MAINTENANCE_IF_MAIN );