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