- $this->action = $this->getArg( 0 );
- if ( !in_array( $this->action, [ 'update', 'verify', 'make-sri' ] ) ) {
- $this->fatalError( "Invalid action argument." );
- }
-
- $registry = $this->parseBasicYaml(
- file_get_contents( __DIR__ . '/foreign-resources.yaml' )
- );
- $module = $this->getArg( 1, 'all' );
- foreach ( $registry as $moduleName => $info ) {
- if ( $module !== 'all' && $moduleName !== $module ) {
- continue;
- }
- $this->verbose( "\n### {$moduleName}\n\n" );
- $destDir = "{$IP}/resources/lib/$moduleName";
-
- if ( $this->action === 'update' ) {
- $this->output( "... updating '{$moduleName}'\n" );
- $this->verbose( "... emptying /resources/lib/$moduleName\n" );
- wfRecursiveRemoveDir( $destDir );
- } elseif ( $this->action === 'verify' ) {
- $this->output( "... verifying '{$moduleName}'\n" );
- } else {
- $this->output( "... checking '{$moduleName}'\n" );
- }
-
- $this->verbose( "... preparing {$this->tmpParentDir}\n" );
- wfRecursiveRemoveDir( $this->tmpParentDir );
- if ( !wfMkdirParents( $this->tmpParentDir ) ) {
- $this->fatalError( "Unable to create {$this->tmpParentDir}" );
- }
-
- if ( !isset( $info['type'] ) ) {
- $this->fatalError( "Module '$moduleName' must have a 'type' key." );
- }
- switch ( $info['type'] ) {
- case 'tar':
- $this->handleTypeTar( $moduleName, $destDir, $info );
- break;
- case 'file':
- $this->handleTypeFile( $moduleName, $destDir, $info );
- break;
- case 'multi-file':
- $this->handleTypeMultiFile( $moduleName, $destDir, $info );
- break;
- default:
- $this->fatalError( "Unknown type '{$info['type']}' for '$moduleName'" );
- }
- }
-
- $this->cleanUp();
- $this->output( "\nDone!\n" );
- if ( $this->failAfterOutput ) {
- // The verify mode should check all modules/files and fail after, not during.
- return false;
- }
- }
-
- private function fetch( $src, $integrity ) {
- $data = Http::get( $src, [ 'followRedirects' => false ] );
- if ( $data === false ) {
- $this->fatalError( "Failed to download resource at {$src}" );
- }
- $algo = $integrity === null ? $this->defaultAlgo : explode( '-', $integrity )[0];
- $actualIntegrity = $algo . '-' . base64_encode( hash( $algo, $data, true ) );
- if ( $integrity === $actualIntegrity ) {
- $this->verbose( "... passed integrity check for {$src}\n" );
- } else {
- if ( $this->action === 'make-sri' ) {
- $this->output( "Integrity for {$src}\n\tintegrity: ${actualIntegrity}\n" );
- } else {
- $this->fatalError( "Integrity check failed for {$src}\n" .
- "\tExpected: {$integrity}\n" .
- "\tActual: {$actualIntegrity}"
- );
- }
- }
- return $data;
- }
-
- private function handleTypeFile( $moduleName, $destDir, array $info ) {
- if ( !isset( $info['src'] ) ) {
- $this->fatalError( "Module '$moduleName' must have a 'src' key." );
- }
- $data = $this->fetch( $info['src'], $info['integrity'] ?? null );
- $dest = $info['dest'] ?? basename( $info['src'] );
- $path = "$destDir/$dest";
- if ( $this->action === 'verify' && sha1_file( $path ) !== sha1( $data ) ) {
- $this->fatalError( "File for '$moduleName' is different." );
- } elseif ( $this->action === 'update' ) {
- wfMkdirParents( $destDir );
- file_put_contents( "$destDir/$dest", $data );
- }
- }
-
- private function handleTypeMultiFile( $moduleName, $destDir, array $info ) {
- if ( !isset( $info['files'] ) ) {
- $this->fatalError( "Module '$moduleName' must have a 'files' key." );
- }
- foreach ( $info['files'] as $dest => $file ) {
- if ( !isset( $file['src'] ) ) {
- $this->fatalError( "Module '$moduleName' file '$dest' must have a 'src' key." );
- }
- $data = $this->fetch( $file['src'], $file['integrity'] ?? null );
- $path = "$destDir/$dest";
- if ( $this->action === 'verify' && sha1_file( $path ) !== sha1( $data ) ) {
- $this->fatalError( "File '$dest' for '$moduleName' is different." );
- } elseif ( $this->action === 'update' ) {
- wfMkdirParents( $destDir );
- file_put_contents( "$destDir/$dest", $data );
- }
- }
- }
-
- private function handleTypeTar( $moduleName, $destDir, array $info ) {
- $info += [ 'src' => null, 'integrity' => null, 'dest' => null ];
- if ( $info['src'] === null ) {
- $this->fatalError( "Module '$moduleName' must have a 'src' key." );
- }
- // Download the resource to a temporary file and open it
- $data = $this->fetch( $info['src'], $info['integrity' ] );
- $tmpFile = "{$this->tmpParentDir}/$moduleName.tar";
- $this->verbose( "... writing '$moduleName' src to $tmpFile\n" );
- file_put_contents( $tmpFile, $data );
- $p = new PharData( $tmpFile );
- $tmpDir = "{$this->tmpParentDir}/$moduleName";
- $p->extractTo( $tmpDir );
- unset( $data, $p );
-
- if ( $info['dest'] === null ) {
- // Default: Replace the entire directory
- $toCopy = [ $tmpDir => $destDir ];
- } else {
- // Expand and normalise the 'dest' entries
- $toCopy = [];
- foreach ( $info['dest'] as $fromSubPath => $toSubPath ) {
- // Use glob() to expand wildcards and check existence
- $fromPaths = glob( "{$tmpDir}/{$fromSubPath}", GLOB_BRACE );
- if ( !$fromPaths ) {
- $this->fatalError( "Path '$fromSubPath' of '$moduleName' not found." );
- }
- foreach ( $fromPaths as $fromPath ) {
- $toCopy[$fromPath] = $toSubPath === null
- ? "$destDir/" . basename( $fromPath )
- : "$destDir/$toSubPath/" . basename( $fromPath );
- }
- }
- }
- foreach ( $toCopy as $from => $to ) {
- if ( $this->action === 'verify' ) {
- $this->verbose( "... verifying $to\n" );
- if ( is_dir( $from ) ) {
- $rii = new RecursiveIteratorIterator( new RecursiveDirectoryIterator(
- $from,
- RecursiveDirectoryIterator::SKIP_DOTS
- ) );
- foreach ( $rii as $file ) {
- $remote = $file->getPathname();
- $local = strtr( $remote, [ $from => $to ] );
- if ( sha1_file( $remote ) !== sha1_file( $local ) ) {
- $this->error( "File '$local' is different." );
- $this->failAfterOutput = true;
- }
- }
- } elseif ( sha1_file( $from ) !== sha1_file( $to ) ) {
- $this->error( "File '$to' is different." );
- $this->failAfterOutput = true;
- }
- } elseif ( $this->action === 'update' ) {
- $this->verbose( "... moving $from to $to\n" );
- wfMkdirParents( dirname( $to ) );
- if ( !rename( $from, $to ) ) {
- $this->fatalError( "Could not move $from to $to." );