[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Minify a file or set of files 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License along 16 * with this program; if not, write to the Free Software Foundation, Inc., 17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 * http://www.gnu.org/copyleft/gpl.html 19 * 20 * @file 21 * @ingroup Maintenance 22 */ 23 24 require_once __DIR__ . '/Maintenance.php'; 25 26 /** 27 * Maintenance script that minifies a file or set of files. 28 * 29 * @ingroup Maintenance 30 */ 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 public function execute() { 55 if ( !count( $this->mArgs ) ) { 56 $this->error( "minify.php: At least one input file must be specified." ); 57 exit( 1 ); 58 } 59 60 if ( $this->hasOption( 'outfile' ) ) { 61 if ( count( $this->mArgs ) > 1 ) { 62 $this->error( '--outfile may only be used with a single input file.' ); 63 exit( 1 ); 64 } 65 66 // Minify one file 67 $this->minify( $this->getArg( 0 ), $this->getOption( 'outfile' ) ); 68 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 107 return substr( $fileName, $dotPos + 1 ); 108 } 109 110 public function minify( $inPath, $outPath ) { 111 global $wgResourceLoaderMinifierStatementsOnOwnLine, $wgResourceLoaderMinifierMaxLineLength; 112 113 $extension = $this->getExtension( $inPath ); 114 $this->output( basename( $inPath ) . ' -> ' . basename( $outPath ) . '...' ); 115 116 $inText = file_get_contents( $inPath ); 117 if ( $inText === false ) { 118 $this->error( "Unable to open file $inPath for reading." ); 119 exit( 1 ); 120 } 121 $outFile = fopen( $outPath, 'w' ); 122 if ( !$outFile ) { 123 $this->error( "Unable to open file $outPath for writing." ); 124 exit( 1 ); 125 } 126 127 switch ( $extension ) { 128 case 'js': 129 $outText = JavaScriptMinifier::minify( $inText, 130 $this->getOption( 'js-statements-on-own-line', $wgResourceLoaderMinifierStatementsOnOwnLine ), 131 $this->getOption( 'js-max-line-length', $wgResourceLoaderMinifierMaxLineLength ) 132 ); 133 break; 134 case 'css': 135 $outText = CSSMin::minify( $inText ); 136 break; 137 default: 138 $this->error( "No minifier defined for extension \"$extension\"" ); 139 } 140 141 fwrite( $outFile, $outText ); 142 fclose( $outFile ); 143 $this->output( " ok\n" ); 144 } 145 } 146 147 $maintClass = 'MinifyScript'; 148 require_once RUN_MAINTENANCE_IF_MAIN;
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |