MediaWiki  1.23.8
minify.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
31 class MinifyScript extends Maintenance {
32  public $outDir;
33 
34  public function __construct() {
35  parent::__construct();
36  $this->addOption( 'outfile',
37  'File for output. Only a single file may be specified for input.',
38  false, true );
39  $this->addOption( 'outdir',
40  "Directory for output. If this is not specified, and neither is --outfile, then the\n" .
41  "output files will be sent to the same directories as the input files.",
42  false, true );
43  $this->addOption( 'js-statements-on-own-line',
44  "Boolean value for putting statements on their own line when minifying JavaScript.",
45  false, true );
46  $this->addOption( 'js-max-line-length',
47  "Maximum line length for JavaScript minification.",
48  false, true );
49  $this->mDescription = "Minify a file or set of files.\n\n" .
50  "If --outfile is not specified, then the output file names will have a .min extension\n" .
51  "added, e.g. jquery.js -> jquery.min.js.";
52 
53  }
54 
55  public function execute() {
56  if ( !count( $this->mArgs ) ) {
57  $this->error( "minify.php: At least one input file must be specified." );
58  exit( 1 );
59  }
60 
61  if ( $this->hasOption( 'outfile' ) ) {
62  if ( count( $this->mArgs ) > 1 ) {
63  $this->error( '--outfile may only be used with a single input file.' );
64  exit( 1 );
65  }
66 
67  // Minify one file
68  $this->minify( $this->getArg( 0 ), $this->getOption( 'outfile' ) );
69  return;
70  }
71 
72  $outDir = $this->getOption( 'outdir', false );
73 
74  foreach ( $this->mArgs as $arg ) {
75  $inPath = realpath( $arg );
76  $inName = basename( $inPath );
77  $inDir = dirname( $inPath );
78 
79  if ( strpos( $inName, '.min.' ) !== false ) {
80  $this->error( "Skipping $inName\n" );
81  continue;
82  }
83 
84  if ( !file_exists( $inPath ) ) {
85  $this->error( "File does not exist: $arg", true );
86  }
87 
88  $extension = $this->getExtension( $inName );
89  $outName = substr( $inName, 0, -strlen( $extension ) ) . 'min.' . $extension;
90  if ( $outDir === false ) {
91  $outPath = $inDir . '/' . $outName;
92  } else {
93  $outPath = $outDir . '/' . $outName;
94  }
95 
96  $this->minify( $inPath, $outPath );
97  }
98  }
99 
100  public function getExtension( $fileName ) {
101  $dotPos = strrpos( $fileName, '.' );
102  if ( $dotPos === false ) {
103  $this->error( "No file extension, cannot determine type: $fileName" );
104  exit( 1 );
105  }
106  return substr( $fileName, $dotPos + 1 );
107  }
108 
109  public function minify( $inPath, $outPath ) {
110  global $wgResourceLoaderMinifierStatementsOnOwnLine, $wgResourceLoaderMinifierMaxLineLength;
111 
112  $extension = $this->getExtension( $inPath );
113  $this->output( basename( $inPath ) . ' -> ' . basename( $outPath ) . '...' );
114 
115  $inText = file_get_contents( $inPath );
116  if ( $inText === false ) {
117  $this->error( "Unable to open file $inPath for reading." );
118  exit( 1 );
119  }
120  $outFile = fopen( $outPath, 'w' );
121  if ( !$outFile ) {
122  $this->error( "Unable to open file $outPath for writing." );
123  exit( 1 );
124  }
125 
126  switch ( $extension ) {
127  case 'js':
128  $outText = JavaScriptMinifier::minify( $inText,
129  $this->getOption( 'js-statements-on-own-line', $wgResourceLoaderMinifierStatementsOnOwnLine ),
130  $this->getOption( 'js-max-line-length', $wgResourceLoaderMinifierMaxLineLength )
131  );
132  break;
133  case 'css':
134  $outText = CSSMin::minify( $inText );
135  break;
136  default:
137  $this->error( "No minifier defined for extension \"$extension\"" );
138  }
139 
140  fwrite( $outFile, $outText );
141  fclose( $outFile );
142  $this->output( " ok\n" );
143  }
144 }
145 
146 $maintClass = 'MinifyScript';
147 require_once RUN_MAINTENANCE_IF_MAIN;
CSSMin\minify
static minify( $css)
Removes whitespace from CSS data.
Definition: CSSMin.php:306
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
JavaScriptMinifier\minify
static minify( $s, $statementsOnOwnLine=false, $maxLineLength=1000)
Returns minified JavaScript code.
Definition: JavaScriptMinifier.php:80
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
MinifyScript\minify
minify( $inPath, $outPath)
Definition: minify.php:109
$maintClass
$maintClass
Definition: minify.php:146
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
MinifyScript\getExtension
getExtension( $fileName)
Definition: minify.php:100
MinifyScript
Maintenance script that minifies a file or set of files.
Definition: minify.php:31
MinifyScript\$outDir
$outDir
Definition: minify.php:32
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
MinifyScript\__construct
__construct()
Default constructor.
Definition: minify.php:34
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
MinifyScript\execute
execute()
Do the actual work.
Definition: minify.php:55
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:181
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:246