summaryrefslogtreecommitdiff
path: root/www/wiki/maintenance/hhvm/makeRepo.php
blob: cef0dadc04618fb937d4ff04ba543b852f237fd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php

require __DIR__ . '/../Maintenance.php';

class HHVMMakeRepo extends Maintenance {
	function __construct() {
		parent::__construct();
		$this->addDescription( 'Compile PHP sources for this MediaWiki instance, ' .
			'and generate an HHVM bytecode file to be used with HHVM\'s ' .
			'RepoAuthoritative mode. The MediaWiki core installation path and ' .
			'all registered extensions are automatically searched for the file ' .
			'extensions *.php, *.inc, *.php5 and *.phtml.' );
		$this->addOption( 'output', 'Output filename', true, true, 'o' );
		$this->addOption( 'input-dir', 'Add an input directory. ' .
			'This can be specified multiple times.', false, true, 'd', true );
		$this->addOption( 'exclude-dir', 'Directory to exclude. ' .
			'This can be specified multiple times.', false, true, false, true );
		$this->addOption( 'extension', 'Extra file extension', false, true, false, true );
		$this->addOption( 'hhvm', 'Location of HHVM binary', false, true );
		$this->addOption( 'base-dir', 'The root of all source files. ' .
			'This must match hhvm.server.source_root in the server\'s configuration file. ' .
			'By default, the MW core install path will be used.',
			false, true );
		$this->addOption( 'verbose', 'Log level 0-3', false, true, 'v' );
	}

	private static function startsWith( $subject, $search ) {
		return substr( $subject, 0, strlen( $search ) === $search );
	}

	function execute() {
		global $wgExtensionCredits, $IP;

		$dirs = [ $IP ];

		foreach ( $wgExtensionCredits as $type => $extensions ) {
			foreach ( $extensions as $extension ) {
				if ( isset( $extension['path'] )
					&& !self::startsWith( $extension['path'], $IP )
				) {
					$dirs[] = dirname( $extension['path'] );
				}
			}
		}

		$dirs = array_merge( $dirs, $this->getOption( 'input-dir', [] ) );
		$fileExts =
			[
				'php' => true,
				'inc' => true,
				'php5' => true,
				'phtml' => true
			] +
			array_flip( $this->getOption( 'extension', [] ) );

		$dirs = array_unique( $dirs );

		$baseDir = $this->getOption( 'base-dir', $IP );
		$excludeDirs = array_map( 'realpath', $this->getOption( 'exclude-dir', [] ) );

		if ( $baseDir !== '' && substr( $baseDir, -1 ) !== '/' ) {
			$baseDir .= '/';
		}

		$unfilteredFiles = [ "$IP/LocalSettings.php" ];
		foreach ( $dirs as $dir ) {
			$this->appendDir( $unfilteredFiles, $dir );
		}

		$files = [];
		foreach ( $unfilteredFiles as $file ) {
			$dotPos = strrpos( $file, '.' );
			$slashPos = strrpos( $file, '/' );
			if ( $dotPos === false || $slashPos === false || $dotPos < $slashPos ) {
				continue;
			}
			$extension = substr( $file, $dotPos + 1 );
			if ( !isset( $fileExts[$extension] ) ) {
				continue;
			}
			$canonical = realpath( $file );
			foreach ( $excludeDirs as $excluded ) {
				if ( self::startsWith( $canonical, $excluded ) ) {
					continue 2;
				}
			}
			if ( self::startsWith( $file, $baseDir ) ) {
				$file = substr( $file, strlen( $baseDir ) );
			}
			$files[] = $file;
		}

		$files = array_unique( $files );

		print "Found " . count( $files ) . " files in " .
			count( $dirs ) . " directories\n";

		$tmpDir = wfTempDir() . '/mw-make-repo' . mt_rand( 0, 1 << 31 );
		if ( !mkdir( $tmpDir ) ) {
			$this->fatalError( 'Unable to create temporary directory' );
		}
		file_put_contents( "$tmpDir/file-list", implode( "\n", $files ) );

		$hhvm = $this->getOption( 'hhvm', 'hhvm' );
		$verbose = $this->getOption( 'verbose', 3 );
		$cmd = wfEscapeShellArg(
			$hhvm,
			'--hphp',
			'--target', 'hhbc',
			'--format', 'binary',
			'--force', '1',
			'--keep-tempdir', '1',
			'--log', $verbose,
			'-v', 'AllVolatile=true',
			'--input-dir', $baseDir,
			'--input-list', "$tmpDir/file-list",
			'--output-dir', $tmpDir );
		print "$cmd\n";
		passthru( $cmd, $ret );
		if ( $ret ) {
			$this->cleanupTemp( $tmpDir );
			$this->fatalError( "Error: HHVM returned error code $ret" );
		}
		if ( !rename( "$tmpDir/hhvm.hhbc", $this->getOption( 'output' ) ) ) {
			$this->cleanupTemp( $tmpDir );
			$this->fatalError( "Error: unable to rename output file" );
		}
		$this->cleanupTemp( $tmpDir );
		return 0;
	}

	private function cleanupTemp( $tmpDir ) {
		if ( file_exists( "$tmpDir/hhvm.hhbc" ) ) {
			unlink( "$tmpDir/hhvm.hhbc" );
		}
		if ( file_exists( "$tmpDir/Stats.js" ) ) {
			unlink( "$tmpDir/Stats.js" );
		}

		unlink( "$tmpDir/file-list" );
		rmdir( $tmpDir );
	}

	private function appendDir( &$files, $dir ) {
		$iter = new RecursiveIteratorIterator(
			new RecursiveDirectoryIterator(
				$dir,
				FilesystemIterator::UNIX_PATHS
			),
			RecursiveIteratorIterator::LEAVES_ONLY
		);
		foreach ( $iter as $file => $fileInfo ) {
			if ( $fileInfo->isFile() ) {
				$files[] = $file;
			}
		}
	}
}

$maintClass = HHVMMakeRepo::class;
require RUN_MAINTENANCE_IF_MAIN;