3 use MediaWiki\Shell\Shell
;
5 require __DIR__
. '/../Maintenance.php';
7 class HHVMMakeRepo
extends Maintenance
{
8 function __construct() {
10 $this->addDescription( 'Compile PHP sources for this MediaWiki instance, ' .
11 'and generate an HHVM bytecode file to be used with HHVM\'s ' .
12 'RepoAuthoritative mode. The MediaWiki core installation path and ' .
13 'all registered extensions are automatically searched for the file ' .
14 'extensions *.php, *.inc, *.php5 and *.phtml.' );
15 $this->addOption( 'output', 'Output filename', true, true, 'o' );
16 $this->addOption( 'input-dir', 'Add an input directory. ' .
17 'This can be specified multiple times.', false, true, 'd', true );
18 $this->addOption( 'exclude-dir', 'Directory to exclude. ' .
19 'This can be specified multiple times.', false, true, false, true );
20 $this->addOption( 'extension', 'Extra file extension', false, true, false, true );
21 $this->addOption( 'hhvm', 'Location of HHVM binary', false, true );
22 $this->addOption( 'base-dir', 'The root of all source files. ' .
23 'This must match hhvm.server.source_root in the server\'s configuration file. ' .
24 'By default, the MW core install path will be used.',
26 $this->addOption( 'verbose', 'Log level 0-3', false, true, 'v' );
29 private static function startsWith( $subject, $search ) {
30 return substr( $subject, 0, strlen( $search ) === $search );
34 global $wgExtensionCredits, $IP;
38 foreach ( $wgExtensionCredits as $type => $extensions ) {
39 foreach ( $extensions as $extension ) {
40 if ( isset( $extension['path'] )
41 && !self
::startsWith( $extension['path'], $IP )
43 $dirs[] = dirname( $extension['path'] );
48 $dirs = array_merge( $dirs, $this->getOption( 'input-dir', [] ) );
56 array_flip( $this->getOption( 'extension', [] ) );
58 $dirs = array_unique( $dirs );
60 $baseDir = $this->getOption( 'base-dir', $IP );
61 $excludeDirs = array_map( 'realpath', $this->getOption( 'exclude-dir', [] ) );
63 if ( $baseDir !== '' && substr( $baseDir, -1 ) !== '/' ) {
67 $unfilteredFiles = [ "$IP/LocalSettings.php" ];
68 foreach ( $dirs as $dir ) {
69 $this->appendDir( $unfilteredFiles, $dir );
73 foreach ( $unfilteredFiles as $file ) {
74 $dotPos = strrpos( $file, '.' );
75 $slashPos = strrpos( $file, '/' );
76 if ( $dotPos === false ||
$slashPos === false ||
$dotPos < $slashPos ) {
79 $extension = substr( $file, $dotPos +
1 );
80 if ( !isset( $fileExts[$extension] ) ) {
83 $canonical = realpath( $file );
84 foreach ( $excludeDirs as $excluded ) {
85 if ( self
::startsWith( $canonical, $excluded ) ) {
89 if ( self
::startsWith( $file, $baseDir ) ) {
90 $file = substr( $file, strlen( $baseDir ) );
95 $files = array_unique( $files );
97 print "Found " . count( $files ) . " files in " .
98 count( $dirs ) . " directories\n";
100 $tmpDir = wfTempDir() . '/mw-make-repo' . mt_rand( 0, 1 << 31 );
101 if ( !mkdir( $tmpDir ) ) {
102 $this->fatalError( 'Unable to create temporary directory' );
104 file_put_contents( "$tmpDir/file-list", implode( "\n", $files ) );
106 $hhvm = $this->getOption( 'hhvm', 'hhvm' );
107 $verbose = $this->getOption( 'verbose', 3 );
108 $cmd = Shell
::escape(
112 '--format', 'binary',
114 '--keep-tempdir', '1',
116 '-v', 'AllVolatile=true',
117 '--input-dir', $baseDir,
118 '--input-list', "$tmpDir/file-list",
119 '--output-dir', $tmpDir );
121 passthru( $cmd, $ret );
123 $this->cleanupTemp( $tmpDir );
124 $this->fatalError( "Error: HHVM returned error code $ret" );
126 if ( !rename( "$tmpDir/hhvm.hhbc", $this->getOption( 'output' ) ) ) {
127 $this->cleanupTemp( $tmpDir );
128 $this->fatalError( "Error: unable to rename output file" );
130 $this->cleanupTemp( $tmpDir );
134 private function cleanupTemp( $tmpDir ) {
135 if ( file_exists( "$tmpDir/hhvm.hhbc" ) ) {
136 unlink( "$tmpDir/hhvm.hhbc" );
138 if ( file_exists( "$tmpDir/Stats.js" ) ) {
139 unlink( "$tmpDir/Stats.js" );
142 unlink( "$tmpDir/file-list" );
146 private function appendDir( &$files, $dir ) {
147 $iter = new RecursiveIteratorIterator(
148 new RecursiveDirectoryIterator(
150 FilesystemIterator
::UNIX_PATHS
152 RecursiveIteratorIterator
::LEAVES_ONLY
154 /** @var SplFileInfo $fileInfo */
155 foreach ( $iter as $file => $fileInfo ) {
156 if ( $fileInfo->isFile() ) {
163 $maintClass = HHVMMakeRepo
::class;
164 require RUN_MAINTENANCE_IF_MAIN
;