| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
require_once __DIR__ . '/Maintenance.php'; |
| 23 |
|
| 24 |
|
| 25 |
class FindMissingFiles extends Maintenance { |
| 26 |
public function __construct() { |
| 27 |
parent::__construct(); |
| 28 |
|
| 29 |
$this->addDescription( 'Find registered files with no corresponding file.' ); |
| 30 |
$this->addOption( 'start', 'Start after this file name', false, true ); |
| 31 |
$this->addOption( 'mtimeafter', 'Only include files changed since this time', false, true ); |
| 32 |
$this->addOption( 'mtimebefore', 'Only includes files changed before this time', false, true ); |
| 33 |
$this->setBatchSize( 300 ); |
| 34 |
} |
| 35 |
|
| 36 |
public function execute() { |
| 37 |
$lastName = $this->getOption( 'start', '' ); |
| 38 |
|
| 39 |
$repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo(); |
| 40 |
$dbr = $repo->getReplicaDB(); |
| 41 |
$be = $repo->getBackend(); |
| 42 |
$batchSize = $this->getBatchSize(); |
| 43 |
|
| 44 |
$mtime1 = $dbr->timestampOrNull( $this->getOption( 'mtimeafter', null ) ); |
| 45 |
$mtime2 = $dbr->timestampOrNull( $this->getOption( 'mtimebefore', null ) ); |
| 46 |
|
| 47 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 48 |
->select( [ 'name' => 'img_name' ] ) |
| 49 |
->from( 'image' ) |
| 50 |
->groupBy( 'name' ) |
| 51 |
->orderBy( 'name' ) |
| 52 |
->limit( $batchSize ); |
| 53 |
|
| 54 |
if ( $mtime1 || $mtime2 ) { |
| 55 |
$queryBuilder->join( 'page', null, 'page_title = img_name' ); |
| 56 |
$queryBuilder->andWhere( [ 'page_namespace' => NS_FILE ] ); |
| 57 |
|
| 58 |
$queryBuilder->join( 'logging', null, 'log_page = page_id' ); |
| 59 |
$queryBuilder->andWhere( [ 'log_type' => [ 'upload', 'move', 'delete' ] ] ); |
| 60 |
if ( $mtime1 ) { |
| 61 |
$queryBuilder->andWhere( $dbr->expr( 'log_timestamp', '>', $mtime1 ) ); |
| 62 |
} |
| 63 |
if ( $mtime2 ) { |
| 64 |
$queryBuilder->andWhere( $dbr->expr( 'log_timestamp', '<', $mtime2 ) ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
do { |
| 69 |
$res = ( clone $queryBuilder ) |
| 70 |
->where( $dbr->expr( 'img_name', '>', $lastName ) ) |
| 71 |
->caller( __METHOD__ )->fetchResultSet(); |
| 72 |
|
| 73 |
|
| 74 |
$pathsByName = []; |
| 75 |
foreach ( $res as $row ) { |
| 76 |
$file = $repo->newFile( $row->name ); |
| 77 |
$pathsByName[$row->name] = $file->getPath(); |
| 78 |
$lastName = $row->name; |
| 79 |
} |
| 80 |
$be->preloadFileStat( [ 'srcs' => $pathsByName ] ); |
| 81 |
foreach ( $pathsByName as $path ) { |
| 82 |
if ( $be->fileExists( [ 'src' => $path ] ) === false ) { |
| 83 |
$this->output( "$path\n" ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
if ( count( $pathsByName ) ) { |
| 89 |
$ores = $dbr->newSelectQueryBuilder() |
| 90 |
->select( [ 'oi_name', 'oi_archive_name' ] ) |
| 91 |
->from( 'oldimage' ) |
| 92 |
->where( [ 'oi_name' => array_map( 'strval', array_keys( $pathsByName ) ) ] ) |
| 93 |
->caller( __METHOD__ )->fetchResultSet(); |
| 94 |
|
| 95 |
$checkPaths = []; |
| 96 |
foreach ( $ores as $row ) { |
| 97 |
if ( !strlen( $row->oi_archive_name ) ) { |
| 98 |
|
| 99 |
continue; |
| 100 |
} |
| 101 |
$file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name ); |
| 102 |
$checkPaths[] = $file->getPath(); |
| 103 |
} |
| 104 |
|
| 105 |
foreach ( array_chunk( $checkPaths, $batchSize ) as $paths ) { |
| 106 |
$be->preloadFileStat( [ 'srcs' => $paths ] ); |
| 107 |
foreach ( $paths as $path ) { |
| 108 |
if ( $be->fileExists( [ 'src' => $path ] ) === false ) { |
| 109 |
$this->output( "$path\n" ); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} while ( $res->numRows() >= $batchSize ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
$maintClass = FindMissingFiles::class; |
| 120 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 121 |
|