MediaWiki REL1_34
minify.php
Go to the documentation of this file.
1<?php
23use Wikimedia\AtEase\AtEase;
24
25require_once __DIR__ . '/Maintenance.php';
26
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;
115require_once RUN_MAINTENANCE_IF_MAIN;
const RUN_MAINTENANCE_IF_MAIN
static minify( $css)
Removes whitespace from CSS data.
Definition CSSMin.php:540
static minify( $s)
Returns minified JavaScript code.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
getStdin( $len=null)
Return input from stdin.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Maintenance script that minifies a file or set of files.
Definition minify.php:32
readFile( $fileName)
Definition minify.php:81
__construct()
Default constructor.
Definition minify.php:35
getExtension( $fileName)
Definition minify.php:73
minify( $inPath)
Definition minify.php:95
execute()
Do the actual work.
Definition minify.php:53
while(( $__line=Maintenance::readconsole()) !==false) print
Definition eval.php:64
$maintClass
Definition minify.php:114