MediaWiki  REL1_19
minify.php
Go to the documentation of this file.
00001 <?php
00024 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
00025 
00026 class MinifyScript extends Maintenance {
00027         var $outDir;
00028 
00029         public function __construct() {
00030                 parent::__construct();
00031                 $this->addOption( 'outfile',
00032                         'File for output. Only a single file may be specified for input.',
00033                         false, true );
00034                 $this->addOption( 'outdir',
00035                         "Directory for output. If this is not specified, and neither is --outfile, then the\n" .
00036                         "output files will be sent to the same directories as the input files.",
00037                         false, true );
00038                 $this->addOption( 'js-statements-on-own-line',
00039                         "Boolean value for putting statements on their own line when minifying JavaScript.",
00040                         false, true );
00041                 $this->addOption( 'js-max-line-length',
00042                         "Maximum line length for JavaScript minification.",
00043                         false, true );
00044                 $this->mDescription = "Minify a file or set of files.\n\n" .
00045                         "If --outfile is not specified, then the output file names will have a .min extension\n" .
00046                         "added, e.g. jquery.js -> jquery.min.js.";
00047 
00048         }
00049 
00050         public function execute() {
00051                 if ( !count( $this->mArgs ) ) {
00052                         $this->error( "minify.php: At least one input file must be specified." );
00053                         exit( 1 );
00054                 }
00055 
00056                 if ( $this->hasOption( 'outfile' ) ) {
00057                         if ( count( $this->mArgs ) > 1 ) {
00058                                 $this->error( '--outfile may only be used with a single input file.' );
00059                                 exit( 1 );
00060                         }
00061 
00062                         // Minify one file
00063                         $this->minify( $this->getArg( 0 ), $this->getOption( 'outfile' ) );
00064                         return;
00065                 }
00066 
00067                 $outDir = $this->getOption( 'outdir', false );
00068 
00069                 foreach ( $this->mArgs as $arg ) {
00070                         $inPath = realpath( $arg );
00071                         $inName = basename( $inPath );
00072                         $inDir = dirname( $inPath );
00073 
00074                         if ( strpos( $inName, '.min.' ) !== false ) {
00075                                 $this->error( "Skipping $inName\n" );
00076                                 continue;
00077                         }
00078 
00079                         if ( !file_exists( $inPath ) ) {
00080                                 $this->error( "File does not exist: $arg", true );
00081                         }
00082 
00083                         $extension = $this->getExtension( $inName );
00084                         $outName = substr( $inName, 0, -strlen( $extension ) ) . 'min.' . $extension;
00085                         if ( $outDir === false ) {
00086                                 $outPath = $inDir . '/' . $outName;
00087                         } else {
00088                                 $outPath = $outDir . '/' . $outName;
00089                         }
00090 
00091                         $this->minify( $inPath, $outPath );
00092                 }
00093         }
00094 
00095         public function getExtension( $fileName ) {
00096                 $dotPos = strrpos( $fileName, '.' );
00097                 if ( $dotPos === false ) {
00098                         $this->error( "No file extension, cannot determine type: $fileName" );
00099                         exit( 1 );
00100                 }
00101                 return substr( $fileName, $dotPos + 1 );
00102         }
00103 
00104         public function minify( $inPath, $outPath ) {
00105                 global $wgResourceLoaderMinifierStatementsOnOwnLine, $wgResourceLoaderMinifierMaxLineLength;
00106 
00107                 $extension = $this->getExtension( $inPath );
00108                 $this->output( basename( $inPath ) . ' -> ' . basename( $outPath ) . '...' );
00109 
00110                 $inText = file_get_contents( $inPath );
00111                 if ( $inText === false ) {
00112                         $this->error( "Unable to open file $inPath for reading." );
00113                         exit( 1 );
00114                 }
00115                 $outFile = fopen( $outPath, 'w' );
00116                 if ( !$outFile ) {
00117                         $this->error( "Unable to open file $outPath for writing." );
00118                         exit( 1 );
00119                 }
00120 
00121                 switch ( $extension ) {
00122                         case 'js':
00123                                 $outText = JavaScriptMinifier::minify( $inText,
00124                                         $this->getOption( 'js-statements-on-own-line', $wgResourceLoaderMinifierStatementsOnOwnLine ),
00125                                         $this->getOption( 'js-max-line-length', $wgResourceLoaderMinifierMaxLineLength )
00126                                 );
00127                                 break;
00128                         case 'css':
00129                                 $outText = CSSMin::minify( $inText );
00130                                 break;
00131                         default:
00132                                 $this->error( "No minifier defined for extension \"$extension\"" );
00133                 }
00134 
00135                 fwrite( $outFile, $outText );
00136                 fclose( $outFile );
00137                 $this->output( " ok\n" );
00138         }
00139 }
00140 
00141 $maintClass = 'MinifyScript';
00142 require_once( RUN_MAINTENANCE_IF_MAIN );