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