33 private $defaultAlgo =
'sha384';
36 private $hasErrors =
false;
39 private $registryFile;
45 private $tmpParentDir;
60 private $errorPrinter;
65 private $verbosePrinter;
84 callable $infoPrinter =
null,
85 callable $errorPrinter =
null,
86 callable $verbosePrinter =
null
88 $this->registryFile = $registryFile;
89 $this->libDir = $libDir;
90 $this->infoPrinter = $infoPrinter ??
static function ( $_ ) {
92 $this->errorPrinter = $errorPrinter ?? $this->infoPrinter;
93 $this->verbosePrinter = $verbosePrinter ??
static function ( $_ ) {
99 $this->tmpParentDir =
"{$this->libDir}/.foreign/tmp";
101 $cacheHome = getenv(
'XDG_CACHE_HOME' ) ? realpath( getenv(
'XDG_CACHE_HOME' ) ) :
false;
102 $this->cacheDir = $cacheHome ?
"$cacheHome/mw-foreign" :
"{$this->libDir}/.foreign/cache";
111 public function run( $action, $module ) {
112 $actions = [
'update',
'verify',
'make-sri' ];
113 if ( !in_array( $action, $actions ) ) {
114 $this->error(
"Invalid action.\n\nMust be one of " . implode(
', ', $actions ) .
'.' );
117 $this->action = $action;
119 $this->registry = Yaml::parseFile( $this->registryFile );
120 if ( $module ===
'all' ) {
122 } elseif ( isset( $this->registry[ $module ] ) ) {
123 $modules = [ $module => $this->registry[ $module ] ];
125 $this->error(
"Unknown module name.\n\nMust be one of:\n" .
126 wordwrap( implode(
', ', array_keys( $this->registry ) ), 80 ) .
132 foreach (
$modules as $moduleName => $info ) {
133 $this->
verbose(
"\n### {$moduleName}\n\n" );
134 $destDir =
"{$this->libDir}/$moduleName";
136 if ( $this->action ===
'update' ) {
137 $this->output(
"... updating '{$moduleName}'\n" );
138 $this->
verbose(
"... emptying directory for $moduleName\n" );
140 } elseif ( $this->action ===
'verify' ) {
141 $this->output(
"... verifying '{$moduleName}'\n" );
143 $this->output(
"... checking '{$moduleName}'\n" );
146 $this->
verbose(
"... preparing {$this->tmpParentDir}\n" );
149 throw new Exception(
"Unable to create {$this->tmpParentDir}" );
152 if ( !isset( $info[
'type'] ) ) {
153 throw new Exception(
"Module '$moduleName' must have a 'type' key." );
155 switch ( $info[
'type'] ) {
157 $this->handleTypeTar( $moduleName, $destDir, $info );
160 $this->handleTypeFile( $moduleName, $destDir, $info );
163 $this->handleTypeMultiFile( $moduleName, $destDir, $info );
166 throw new Exception(
"Unknown type '{$info['type']}' for '$moduleName'" );
170 $this->output(
"\nDone!\n" );
172 if ( $this->hasErrors ) {
186 private function cacheKey( $src, $integrity ) {
187 $key = basename( $src ) .
'_' . substr( $integrity, -12 );
188 $key = preg_replace(
'/[.\/+?=_-]+/',
'_', $key );
189 return rtrim( $key,
'_' );
196 private function cacheGet( $key ) {
197 return AtEase::quietCall(
'file_get_contents',
"{$this->cacheDir}/$key.data" );
204 private function cacheSet( $key, $data ) {
206 file_put_contents(
"{$this->cacheDir}/$key.data", $data, LOCK_EX );
215 private function fetch( $src, $integrity ) {
216 $key = $this->cacheKey( $src, $integrity );
217 $data = $this->cacheGet( $key );
222 $req = MediaWikiServices::getInstance()->getHttpRequestFactory()
223 ->create( $src, [
'method' =>
'GET',
'followRedirects' =>
false ], __METHOD__ );
224 if ( !$req->execute()->isOK() ) {
225 throw new Exception(
"Failed to download resource at {$src}" );
227 if ( $req->getStatus() !== 200 ) {
228 throw new Exception(
"Unexpected HTTP {$req->getStatus()} response from {$src}" );
230 $data = $req->getContent();
231 $algo = $integrity ===
null ? $this->defaultAlgo : explode(
'-', $integrity )[0];
232 $actualIntegrity = $algo .
'-' . base64_encode( hash( $algo, $data,
true ) );
233 if ( $integrity === $actualIntegrity ) {
234 $this->
verbose(
"... passed integrity check for {$src}\n" );
235 $this->cacheSet( $key, $data );
236 } elseif ( $this->action ===
'make-sri' ) {
237 $this->output(
"Integrity for {$src}\n\tintegrity: {$actualIntegrity}\n" );
239 $expectedIntegrity = $integrity ??
'null';
240 throw new Exception(
"Integrity check failed for {$src}\n" .
241 "\tExpected: {$expectedIntegrity}\n" .
242 "\tActual: {$actualIntegrity}"
253 private function handleTypeFile( $moduleName, $destDir, array $info ) {
254 if ( !isset( $info[
'src'] ) ) {
255 throw new Exception(
"Module '$moduleName' must have a 'src' key." );
257 $data = $this->fetch( $info[
'src'], $info[
'integrity'] ??
null );
258 $dest = $info[
'dest'] ?? basename( $info[
'src'] );
259 $path =
"$destDir/$dest";
260 if ( $this->action ===
'verify' && sha1_file(
$path ) !== sha1( $data ) ) {
261 throw new Exception(
"File for '$moduleName' is different." );
263 if ( $this->action ===
'update' ) {
265 file_put_contents(
"$destDir/$dest", $data );
274 private function handleTypeMultiFile( $moduleName, $destDir, array $info ) {
275 if ( !isset( $info[
'files'] ) ) {
276 throw new Exception(
"Module '$moduleName' must have a 'files' key." );
278 foreach ( $info[
'files'] as $dest =>
$file ) {
279 if ( !isset(
$file[
'src'] ) ) {
280 throw new Exception(
"Module '$moduleName' file '$dest' must have a 'src' key." );
282 $data = $this->fetch(
$file[
'src'],
$file[
'integrity'] ??
null );
283 $path =
"$destDir/$dest";
284 if ( $this->action ===
'verify' && sha1_file(
$path ) !== sha1( $data ) ) {
285 throw new Exception(
"File '$dest' for '$moduleName' is different." );
286 } elseif ( $this->action ===
'update' ) {
288 file_put_contents(
"$destDir/$dest", $data );
298 private function handleTypeTar( $moduleName, $destDir, array $info ) {
299 $info += [
'src' =>
null,
'integrity' =>
null,
'dest' => null ];
300 if ( $info[
'src'] ===
null ) {
301 throw new Exception(
"Module '$moduleName' must have a 'src' key." );
304 $data = $this->fetch( $info[
'src'], $info[
'integrity' ] );
305 $tmpFile =
"{$this->tmpParentDir}/$moduleName.tar";
306 $this->
verbose(
"... writing '$moduleName' src to $tmpFile\n" );
307 file_put_contents( $tmpFile, $data );
308 $p =
new PharData( $tmpFile );
309 $tmpDir =
"{$this->tmpParentDir}/$moduleName";
310 $p->extractTo( $tmpDir );
313 if ( $info[
'dest'] ===
null ) {
315 $toCopy = [ $tmpDir => $destDir ];
319 foreach ( $info[
'dest'] as $fromSubPath => $toSubPath ) {
321 $fromPaths = glob(
"{$tmpDir}/{$fromSubPath}", GLOB_BRACE );
323 throw new Exception(
"Path '$fromSubPath' of '$moduleName' not found." );
325 foreach ( $fromPaths as $fromPath ) {
326 $toCopy[$fromPath] = $toSubPath ===
null
327 ?
"$destDir/" . basename( $fromPath )
328 :
"$destDir/$toSubPath/" . basename( $fromPath );
332 foreach ( $toCopy as $from => $to ) {
333 if ( $this->action ===
'verify' ) {
334 $this->
verbose(
"... verifying $to\n" );
335 if ( is_dir( $from ) ) {
336 $rii =
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
338 RecursiveDirectoryIterator::SKIP_DOTS
341 foreach ( $rii as
$file ) {
342 $remote =
$file->getPathname();
343 $local = strtr( $remote, [ $from => $to ] );
344 if ( sha1_file( $remote ) !== sha1_file( $local ) ) {
345 $this->error(
"File '$local' is different." );
346 $this->hasErrors =
true;
349 } elseif ( sha1_file( $from ) !== sha1_file( $to ) ) {
350 $this->error(
"File '$to' is different." );
351 $this->hasErrors =
true;
353 } elseif ( $this->action ===
'update' ) {
354 $this->
verbose(
"... moving $from to $to\n" );
356 if ( !rename( $from, $to ) ) {
357 throw new Exception(
"Could not move $from to $to." );
366 private function verbose( $text ) {
367 ( $this->verbosePrinter )( $text );
373 private function output( $text ) {
374 ( $this->infoPrinter )( $text );
380 private function error( $text ) {
381 ( $this->errorPrinter )( $text );
384 private function cleanUp() {
389 foreach ( $this->registry as $info ) {
390 if ( $info[
'type'] ===
'file' || $info[
'type'] ===
'tar' ) {
391 $knownKeys[] = $this->cacheKey( $info[
'src'], $info[
'integrity'] );
392 } elseif ( $info[
'type'] ===
'multi-file' ) {
393 foreach ( $info[
'files'] as
$file ) {
394 $knownKeys[] = $this->cacheKey(
$file[
'src'],
$file[
'integrity'] );
398 foreach ( glob(
"{$this->cacheDir}/*" ) as $cacheFile ) {
399 if ( !in_array( basename( $cacheFile,
'.data' ), $knownKeys ) ) {
400 unlink( $cacheFile );