resources: Collapse all jQuery UI modules into one deprecated mega-module
[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 use Wikimedia\AtEase\AtEase;
24
25 require_once __DIR__ . '/Maintenance.php';
26
27 /**
28 * Maintenance script that minifies a file or set of files.
29 *
30 * @ingroup Maintenance
31 */
32 class MinifyScript extends Maintenance {
33 public $outDir;
34
35 public function __construct() {
36 parent::__construct();
37 $this->addOption( 'outfile',
38 'Write minified output to this file (instead of standard out).',
39 false, true, 'o'
40 );
41 $this->addOption( 'type',
42 'Override the input type (one of "js" or "css"). Defaults to file extension. ' .
43 'Required if reading from standard input.',
44 false, true, 'o'
45 );
46 $this->addArg( 'file', 'Input file. Use - to read from standard input.' );
47 $this->addDescription(
48 "Minify one or more JavaScript or CSS files.\n" .
49 "If multiple input files are given, they will be concatenated."
50 );
51 }
52
53 public function execute() {
54 $outputFile = $this->getOption( 'outfile', false );
55 if ( $outputFile === false ) {
56 // Only output the minified result (or errors)
57 // Avoid output() because this should not honour --quiet
58 foreach ( $this->mArgs as $arg ) {
59 print $this->minify( $arg ) . "\n";
60 }
61 } else {
62 $result = '';
63 foreach ( $this->mArgs as $arg ) {
64 $this->output( "Minifying {$arg} ...\n" );
65 $result .= $this->minify( $arg );
66 }
67 $this->output( "Writing to {$outputFile} ...\n" );
68 file_put_contents( $outputFile, $result );
69 $this->output( "Done!\n" );
70 }
71 }
72
73 public function getExtension( $fileName ) {
74 $dotPos = strrpos( $fileName, '.' );
75 if ( $dotPos === false ) {
76 $this->fatalError( "Unknown file type ($fileName). Use --type." );
77 }
78 return substr( $fileName, $dotPos + 1 );
79 }
80
81 private function readFile( $fileName ) {
82 if ( $fileName === '-' ) {
83 $inText = $this->getStdin( self::STDIN_ALL );
84 } else {
85 AtEase::suppressWarnings();
86 $inText = file_get_contents( $fileName );
87 AtEase::restoreWarnings();
88 if ( $inText === false ) {
89 $this->fatalError( "Unable to open file $fileName for reading." );
90 }
91 }
92 return $inText;
93 }
94
95 public function minify( $inPath ) {
96 $extension = $this->getOption( 'type', null ) ?? $this->getExtension( $inPath );
97 $inText = $this->readFile( $inPath );
98
99 switch ( $extension ) {
100 case 'js':
101 $outText = JavaScriptMinifier::minify( $inText );
102 break;
103 case 'css':
104 $outText = CSSMin::minify( $inText );
105 break;
106 default:
107 $this->fatalError( "Unsupported file type \"$extension\"." );
108 }
109
110 return $outText;
111 }
112 }
113
114 $maintClass = MinifyScript::class;
115 require_once RUN_MAINTENANCE_IF_MAIN;