X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=maintenance%2FfindOrphanedFiles.php;h=abd170b30446b14bb1a2a87a58b895c9013160c8;hb=1c80fe572eec7610f35c87e0a43e49d886193d58;hp=2ee406fe5871db69efe32eef3c5443efef5d33a7;hpb=0f5ddf4d1e217519e236b1df44e8bccb62858edc;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/findOrphanedFiles.php b/maintenance/findOrphanedFiles.php index 2ee406fe58..abd170b304 100644 --- a/maintenance/findOrphanedFiles.php +++ b/maintenance/findOrphanedFiles.php @@ -25,7 +25,7 @@ class FindOrphanedFiles extends Maintenance { function __construct() { parent::__construct(); - $this->mDescription = "Find unregistered files in the 'public' repo zone."; + $this->addDescription( "Find unregistered files in the 'public' repo zone." ); $this->addOption( 'subdir', 'Only scan files in this subdirectory (e.g. "a/a0")', false, true ); $this->addOption( 'verbose', "Mention file paths checked" ); @@ -50,44 +50,48 @@ class FindOrphanedFiles extends Maintenance { $this->output( "Scanning files under $directory:\n" ); } - $list = $repo->getBackend()->getFileList( array( 'dir' => $directory ) ); + $list = $repo->getBackend()->getFileList( [ 'dir' => $directory ] ); if ( $list === null ) { $this->error( "Could not get file listing.", 1 ); } - $nameBatch = array(); + $pathBatch = []; foreach ( $list as $path ) { if ( preg_match( '#^(thumb|deleted)/#', $path ) ) { continue; // handle ugly nested containers on stock installs } - $nameBatch[] = basename( $path ); - if ( count( $nameBatch ) >= $this->mBatchSize ) { - $this->checkFiles( $repo, $nameBatch, $verbose ); - $nameBatch = array(); + $pathBatch[] = $path; + if ( count( $pathBatch ) >= $this->mBatchSize ) { + $this->checkFiles( $repo, $pathBatch, $verbose ); + $pathBatch = []; } } - $this->checkFiles( $repo, $nameBatch, $verbose ); + $this->checkFiles( $repo, $pathBatch, $verbose ); } - protected function checkFiles( LocalRepo $repo, array $names, $verbose ) { - if ( !count( $names ) ) { + protected function checkFiles( LocalRepo $repo, array $paths, $verbose ) { + if ( !count( $paths ) ) { return; } $dbr = $repo->getSlaveDB(); - $imgIN = array(); - $oiWheres = array(); - foreach ( $names as $name ) { - if ( strpos( $name, '!' ) !== false ) { + $curNames = []; + $oldNames = []; + $imgIN = []; + $oiWheres = []; + foreach ( $paths as $path ) { + $name = basename( $path ); + if ( preg_match( '#^archive/#', $path ) ) { if ( $verbose ) { $this->output( "Checking old file $name\n" ); } - list( , $base ) = explode( '!', $name ); // ! + $oldNames[] = $name; + list( , $base ) = explode( '!', $name, 2 ); // ! $oiWheres[] = $dbr->makeList( - array( 'oi_name' => $base, 'oi_archive_name' => $name ), + [ 'oi_name' => $base, 'oi_archive_name' => $name ], LIST_AND ); } else { @@ -95,44 +99,55 @@ class FindOrphanedFiles extends Maintenance { $this->output( "Checking current file $name\n" ); } + $curNames[] = $name; $imgIN[] = $name; } } $res = $dbr->query( $dbr->unionQueries( - array( + [ $dbr->selectSQLText( 'image', - array( 'name' => 'img_name' ), - array( 'img_name' => $imgIN ) + [ 'name' => 'img_name', 'old' => 0 ], + $imgIN ? [ 'img_name' => $imgIN ] : '1=0' ), $dbr->selectSQLText( 'oldimage', - array( 'name' => 'oi_archive_name' ), - $dbr->makeList( $oiWheres, LIST_OR ) + [ 'name' => 'oi_archive_name', 'old' => 1 ], + $oiWheres ? $dbr->makeList( $oiWheres, LIST_OR ) : '1=0' ) - ), + ], true // UNION ALL (performance) ), __METHOD__ ); - $namesFound = array(); + $curNamesFound = []; + $oldNamesFound = []; foreach ( $res as $row ) { - $namesFound[] = $row->name; + if ( $row->old ) { + $oldNamesFound[] = $row->name; + } else { + $curNamesFound[] = $row->name; + } } - $namesOrphans = array_diff( $names, $namesFound ); - foreach ( $namesOrphans as $name ) { + foreach ( array_diff( $curNames, $curNamesFound ) as $name ) { + $file = $repo->newFile( $name ); // Print name and public URL to ease recovery - if ( strpos( $name, '!' ) !== false ) { - list( , $base ) = explode( '!', $name ); // ! - $file = $repo->newFromArchiveName( Title::makeTitle( NS_FILE, $base ), $name ); + if ( $file ) { + $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" ); } else { - $file = $repo->newFile( $name ); + $this->error( "Cannot get URL for bad file title '$name'" ); } - $this->output( $name . "\n" . $file->getUrl() . "\n\n" ); + } + + foreach ( array_diff( $oldNames, $oldNamesFound ) as $name ) { + list( , $base ) = explode( '!', $name, 2 ); // ! + $file = $repo->newFromArchiveName( Title::makeTitle( NS_FILE, $base ), $name ); + // Print name and public URL to ease recovery + $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" ); } } }