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