23 use Composer\Spdx\SpdxLicenses;
28 use RecursiveDirectoryIterator;
29 use RecursiveIteratorIterator;
31 use Symfony\Component\Yaml\Yaml;
42 private $defaultAlgo =
'sha384';
45 private $hasErrors =
false;
48 private $registryFile;
54 private $tmpParentDir;
69 private $errorPrinter;
74 private $verbosePrinter;
93 callable $infoPrinter =
null,
94 callable $errorPrinter =
null,
95 callable $verbosePrinter =
null
97 $this->registryFile = $registryFile;
98 $this->libDir = $libDir;
99 $this->infoPrinter = $infoPrinter ??
static function ( $_ ) {
101 $this->errorPrinter = $errorPrinter ?? $this->infoPrinter;
102 $this->verbosePrinter = $verbosePrinter ??
static function ( $_ ) {
107 if ( ( $cacheHome = getenv(
'XDG_CACHE_HOME' ) ) !==
false ) {
108 $this->cacheDir = realpath( $cacheHome ) .
'/mw-foreign';
110 $this->cacheDir =
"$cacheConf/ForeignResourceManager";
112 $this->cacheDir =
"{$this->libDir}/.foreign/cache";
122 public function run( $action, $module ) {
123 $actions = [
'update',
'verify',
'make-sri' ];
124 if ( !in_array( $action, $actions ) ) {
125 $this->error(
"Invalid action.\n\nMust be one of " . implode(
', ', $actions ) .
'.' );
128 $this->action = $action;
129 $this->setupTempDir( $action );
131 $this->registry = Yaml::parseFile( $this->registryFile );
132 if ( $module ===
'all' ) {
134 } elseif ( isset( $this->registry[$module] ) ) {
135 $modules = [ $module => $this->registry[$module] ];
137 $this->error(
"Unknown module name.\n\nMust be one of:\n" .
138 wordwrap( implode(
', ', array_keys( $this->registry ) ), 80 ) .
144 foreach (
$modules as $moduleName => $info ) {
145 $this->verbose(
"\n### {$moduleName}\n\n" );
147 if ( $this->action ===
'update' ) {
148 $this->output(
"... updating '{$moduleName}'\n" );
149 } elseif ( $this->action ===
'verify' ) {
150 $this->output(
"... verifying '{$moduleName}'\n" );
152 $this->output(
"... checking '{$moduleName}'\n" );
159 if ( !isset( $info[
'type'] ) ) {
160 throw new Exception(
"Module '$moduleName' must have a 'type' key." );
163 $this->validateLicense( $moduleName, $info );
165 if ( $info[
'type'] ===
'doc-only' ) {
166 $this->output(
"... {$moduleName} is documentation-only, skipping\n" );
170 $destDir =
"{$this->libDir}/$moduleName";
172 if ( $this->action ===
'update' ) {
173 $this->verbose(
"... emptying directory for $moduleName\n" );
177 $this->verbose(
"... preparing {$this->tmpParentDir}\n" );
180 throw new Exception(
"Unable to create {$this->tmpParentDir}" );
183 switch ( $info[
'type'] ) {
186 $this->handleTypeTar( $moduleName, $destDir, $info, $info[
'type'] );
189 $this->handleTypeFile( $moduleName, $destDir, $info );
192 $this->handleTypeMultiFile( $moduleName, $destDir, $info );
195 throw new Exception(
"Unknown type '{$info['type']}' for '$moduleName'" );
198 if ( $this->action ===
'update' ) {
199 foreach ( $info[
'transforms'] ?? [] as
$file => $transforms ) {
200 $fullFilePath =
"$destDir/$file";
201 if ( !file_exists( $fullFilePath ) ) {
202 throw new Exception(
"$moduleName: invalid transform target $file" );
204 if ( !is_array( $transforms ) || !array_is_list( $transforms ) ) {
205 $transforms = [ $transforms ];
207 foreach ( $transforms as $transform ) {
208 if ( $transform ===
'nomin' ) {
210 file_put_contents( $fullFilePath,
"/*@nomin*/\n" . file_get_contents( $fullFilePath ) );
212 throw new Exception(
"$moduleName: invalid transform $transform" );
220 if ( $this->hasErrors ) {
236 private function setupTempDir( $action ) {
237 if ( $action ===
'verify' ) {
238 $this->tmpParentDir =
wfTempDir() .
'/ForeignResourceManager';
243 $this->tmpParentDir =
"{$this->libDir}/.foreign/tmp";
253 private function cacheKey( $src, $integrity, $moduleName ) {
255 .
'_' . hash(
'fnv132', $integrity )
256 .
'_' . hash(
'fnv132', $src )
258 .
'_' . basename( $src );
259 $key = preg_replace(
'/[.\/+?=_-]+/',
'_', $key );
260 return rtrim( $key,
'_' );
267 private function cacheGet( $key ) {
269 return @file_get_contents(
"{$this->cacheDir}/$key.data" );
276 private function cacheSet( $key, $data ) {
278 @mkdir( $this->cacheDir, 0777,
true );
279 file_put_contents(
"{$this->cacheDir}/$key.data", $data, LOCK_EX );
288 private function fetch(
string $src, $integrity,
string $moduleName ) {
289 if ( $integrity !==
null ) {
290 $key = $this->cacheKey( $src, $integrity, $moduleName );
291 $data = $this->cacheGet( $key );
298 ->create( $src, [
'method' =>
'GET',
'followRedirects' =>
false ], __METHOD__ );
299 if ( !$req->execute()->isOK() ) {
300 throw new Exception(
"Failed to download resource at {$src}" );
302 if ( $req->getStatus() !== 200 ) {
303 throw new Exception(
"Unexpected HTTP {$req->getStatus()} response from {$src}" );
305 $data = $req->getContent();
306 $algo = $integrity ===
null ? $this->defaultAlgo : explode(
'-', $integrity )[0];
307 $actualIntegrity = $algo .
'-' . base64_encode( hash( $algo, $data,
true ) );
308 if ( $integrity === $actualIntegrity ) {
309 $this->verbose(
"... passed integrity check for {$src}\n" );
310 $key = $this->cacheKey( $src, $actualIntegrity, $moduleName );
311 $this->cacheSet( $key, $data );
312 } elseif ( $this->action ===
'make-sri' ) {
313 $this->output(
"Integrity for {$src}\n\tintegrity: {$actualIntegrity}\n" );
315 $expectedIntegrity = $integrity ??
'null';
316 throw new Exception(
"Integrity check failed for {$src}\n" .
317 "\tExpected: {$expectedIntegrity}\n" .
318 "\tActual: {$actualIntegrity}"
329 private function handleTypeFile( $moduleName, $destDir, array $info ) {
330 if ( !isset( $info[
'src'] ) ) {
331 throw new Exception(
"Module '$moduleName' must have a 'src' key." );
333 $data = $this->fetch( $info[
'src'], $info[
'integrity'] ??
null, $moduleName );
334 $dest = $info[
'dest'] ?? basename( $info[
'src'] );
335 $path =
"$destDir/$dest";
336 if ( $this->action ===
'verify' && sha1_file(
$path ) !== sha1( $data ) ) {
337 $this->error(
"File for '$moduleName' is different.\n" );
339 if ( $this->action ===
'update' ) {
341 file_put_contents(
"$destDir/$dest", $data );
350 private function handleTypeMultiFile( $moduleName, $destDir, array $info ) {
351 if ( !isset( $info[
'files'] ) ) {
352 throw new Exception(
"Module '$moduleName' must have a 'files' key." );
354 foreach ( $info[
'files'] as $dest =>
$file ) {
355 if ( !isset(
$file[
'src'] ) ) {
356 throw new Exception(
"Module '$moduleName' file '$dest' must have a 'src' key." );
358 $data = $this->fetch(
$file[
'src'],
$file[
'integrity'] ??
null, $moduleName );
359 $path =
"$destDir/$dest";
360 if ( $this->action ===
'verify' && sha1_file(
$path ) !== sha1( $data ) ) {
361 $this->error(
"File '$dest' for '$moduleName' is different.\n" );
362 } elseif ( $this->action ===
'update' ) {
364 file_put_contents(
"$destDir/$dest", $data );
375 private function handleTypeTar( $moduleName, $destDir, array $info,
string $fileType ) {
376 $info += [
'src' =>
null,
'integrity' =>
null,
'dest' => null ];
377 if ( $info[
'src'] ===
null ) {
378 throw new Exception(
"Module '$moduleName' must have a 'src' key." );
381 $data = $this->fetch( $info[
'src'], $info[
'integrity'], $moduleName );
382 $tmpFile =
"{$this->tmpParentDir}/$moduleName." . $fileType;
383 $this->verbose(
"... writing '$moduleName' src to $tmpFile\n" );
384 file_put_contents( $tmpFile, $data );
385 $p =
new PharData( $tmpFile );
386 $tmpDir =
"{$this->tmpParentDir}/$moduleName";
387 $p->extractTo( $tmpDir );
390 if ( $info[
'dest'] ===
null ) {
392 $toCopy = [ $tmpDir => $destDir ];
396 foreach ( $info[
'dest'] as $fromSubPath => $toSubPath ) {
398 $fromPaths = glob(
"{$tmpDir}/{$fromSubPath}", GLOB_BRACE );
400 throw new Exception(
"Path '$fromSubPath' of '$moduleName' not found." );
402 foreach ( $fromPaths as $fromPath ) {
403 $toCopy[$fromPath] = $toSubPath ===
null
404 ?
"$destDir/" . basename( $fromPath )
405 :
"$destDir/$toSubPath/" . basename( $fromPath );
409 foreach ( $toCopy as $from => $to ) {
410 if ( $this->action ===
'verify' ) {
411 $this->verbose(
"... verifying $to\n" );
412 if ( is_dir( $from ) ) {
413 $rii =
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
415 RecursiveDirectoryIterator::SKIP_DOTS
418 foreach ( $rii as
$file ) {
419 $remote =
$file->getPathname();
420 $local = strtr( $remote, [ $from => $to ] );
421 if ( sha1_file( $remote ) !== sha1_file( $local ) ) {
422 $this->error(
"File '$local' is different.\n" );
425 } elseif ( sha1_file( $from ) !== sha1_file( $to ) ) {
426 $this->error(
"File '$to' is different.\n" );
428 } elseif ( $this->action ===
'update' ) {
429 $this->verbose(
"... moving $from to $to\n" );
431 if ( !rename( $from, $to ) ) {
432 throw new Exception(
"Could not move $from to $to." );
441 private function verbose( $text ) {
442 ( $this->verbosePrinter )( $text );
448 private function output( $text ) {
449 ( $this->infoPrinter )( $text );
455 private function error( $text ) {
456 $this->hasErrors =
true;
457 ( $this->errorPrinter )( $text );
460 private function cleanUp() {
465 foreach ( $this->registry as $module => $info ) {
466 if ( $info[
'type'] ===
'file' || $info[
'type'] ===
'tar' ) {
467 $knownKeys[] = $this->cacheKey( $info[
'src'], $info[
'integrity'], $module );
468 } elseif ( $info[
'type'] ===
'multi-file' ) {
469 foreach ( $info[
'files'] as
$file ) {
470 $knownKeys[] = $this->cacheKey(
$file[
'src'],
$file[
'integrity'], $module );
474 foreach ( glob(
"{$this->cacheDir}/*" ) as $cacheFile ) {
475 if ( !in_array( basename( $cacheFile,
'.data' ), $knownKeys ) ) {
476 unlink( $cacheFile );
485 private function validateLicense( $moduleName, $info ) {
486 if ( !isset( $info[
'license'] ) || !is_string( $info[
'license'] ) ) {
487 throw new Exception(
"Module '$moduleName' needs a valid SPDX license; no license is currently present" );
489 $licenses =
new SpdxLicenses();
490 if ( !$licenses->validate( $info[
'license'] ) ) {
492 "Module '$moduleName' has an invalid SPDX license identifier '{$info['license']}', "
493 .
'see <https://spdx.org/licenses/>'
500 class_alias( ForeignResourceManager::class,
'ForeignResourceManager' );
wfTempDir()
Tries to get the system directory for temporary files.
wfRecursiveRemoveDir( $dir)
Remove a directory and all its content.
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
A class containing constants representing the names of configuration variables.
const CacheDirectory
Name constant for the CacheDirectory setting, for use with Config::get()
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.