minify.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. require_once __DIR__ . '/Maintenance.php';
  24. /**
  25. * Maintenance script that minifies a file or set of files.
  26. *
  27. * @ingroup Maintenance
  28. */
  29. class MinifyScript extends Maintenance {
  30. public $outDir;
  31. public function __construct() {
  32. parent::__construct();
  33. $this->addOption( 'outfile',
  34. 'File for output. Only a single file may be specified for input.',
  35. false, true );
  36. $this->addOption( 'outdir',
  37. "Directory for output. If this is not specified, and neither is --outfile, then the\n" .
  38. "output files will be sent to the same directories as the input files.",
  39. false, true );
  40. $this->addDescription( "Minify a file or set of files.\n\n" .
  41. "If --outfile is not specified, then the output file names will have a .min extension\n" .
  42. "added, e.g. jquery.js -> jquery.min.js."
  43. );
  44. }
  45. public function execute() {
  46. if ( !count( $this->mArgs ) ) {
  47. $this->error( "minify.php: At least one input file must be specified." );
  48. exit( 1 );
  49. }
  50. if ( $this->hasOption( 'outfile' ) ) {
  51. if ( count( $this->mArgs ) > 1 ) {
  52. $this->error( '--outfile may only be used with a single input file.' );
  53. exit( 1 );
  54. }
  55. // Minify one file
  56. $this->minify( $this->getArg( 0 ), $this->getOption( 'outfile' ) );
  57. return;
  58. }
  59. $outDir = $this->getOption( 'outdir', false );
  60. foreach ( $this->mArgs as $arg ) {
  61. $inPath = realpath( $arg );
  62. $inName = basename( $inPath );
  63. $inDir = dirname( $inPath );
  64. if ( strpos( $inName, '.min.' ) !== false ) {
  65. $this->error( "Skipping $inName\n" );
  66. continue;
  67. }
  68. if ( !file_exists( $inPath ) ) {
  69. $this->error( "File does not exist: $arg", true );
  70. }
  71. $extension = $this->getExtension( $inName );
  72. $outName = substr( $inName, 0, -strlen( $extension ) ) . 'min.' . $extension;
  73. if ( $outDir === false ) {
  74. $outPath = $inDir . '/' . $outName;
  75. } else {
  76. $outPath = $outDir . '/' . $outName;
  77. }
  78. $this->minify( $inPath, $outPath );
  79. }
  80. }
  81. public function getExtension( $fileName ) {
  82. $dotPos = strrpos( $fileName, '.' );
  83. if ( $dotPos === false ) {
  84. $this->error( "No file extension, cannot determine type: $fileName" );
  85. exit( 1 );
  86. }
  87. return substr( $fileName, $dotPos + 1 );
  88. }
  89. public function minify( $inPath, $outPath ) {
  90. $extension = $this->getExtension( $inPath );
  91. $this->output( basename( $inPath ) . ' -> ' . basename( $outPath ) . '...' );
  92. $inText = file_get_contents( $inPath );
  93. if ( $inText === false ) {
  94. $this->error( "Unable to open file $inPath for reading." );
  95. exit( 1 );
  96. }
  97. $outFile = fopen( $outPath, 'w' );
  98. if ( !$outFile ) {
  99. $this->error( "Unable to open file $outPath for writing." );
  100. exit( 1 );
  101. }
  102. switch ( $extension ) {
  103. case 'js':
  104. $outText = JavaScriptMinifier::minify( $inText );
  105. break;
  106. case 'css':
  107. $outText = CSSMin::minify( $inText );
  108. break;
  109. default:
  110. $this->error( "No minifier defined for extension \"$extension\"" );
  111. }
  112. fwrite( $outFile, $outText );
  113. fclose( $outFile );
  114. $this->output( " ok\n" );
  115. }
  116. }
  117. $maintClass = 'MinifyScript';
  118. require_once RUN_MAINTENANCE_IF_MAIN;