MediaWiki REL1_41
findMissingFiles.php
Go to the documentation of this file.
1<?php
21require_once __DIR__ . '/Maintenance.php';
22
24 public function __construct() {
25 parent::__construct();
26
27 $this->addDescription( 'Find registered files with no corresponding file.' );
28 $this->addOption( 'start', 'Start after this file name', false, true );
29 $this->addOption( 'mtimeafter', 'Only include files changed since this time', false, true );
30 $this->addOption( 'mtimebefore', 'Only includes files changed before this time', false, true );
31 $this->setBatchSize( 300 );
32 }
33
34 public function execute() {
35 $lastName = $this->getOption( 'start', '' );
36
37 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
38 $dbr = $repo->getReplicaDB();
39 $be = $repo->getBackend();
40 $batchSize = $this->getBatchSize();
41
42 $mtime1 = $dbr->timestampOrNull( $this->getOption( 'mtimeafter', null ) );
43 $mtime2 = $dbr->timestampOrNull( $this->getOption( 'mtimebefore', null ) );
44
45 $queryBuilder = $dbr->newSelectQueryBuilder()
46 ->select( [ 'name' => 'img_name' ] )
47 ->from( 'image' )
48 ->where( [ "img_name > " . $dbr->addQuotes( $lastName ) ] )
49 ->groupBy( 'name' )
50 ->orderBy( 'name' )
51 ->limit( $batchSize );
52
53 if ( $mtime1 || $mtime2 ) {
54 $queryBuilder->join( 'page', null, 'page_title = img_name' );
55 $queryBuilder->andWhere( [ 'page_namespace' => NS_FILE ] );
56
57 $queryBuilder->join( 'logging', null, 'log_page = page_id' );
58 $queryBuilder->andWhere( [ 'log_type' => [ 'upload', 'move', 'delete' ] ] );
59 if ( $mtime1 ) {
60 $queryBuilder->andWhere( "log_timestamp > {$dbr->addQuotes($mtime1)}" );
61 }
62 if ( $mtime2 ) {
63 $queryBuilder->andWhere( "log_timestamp < {$dbr->addQuotes($mtime2)}" );
64 }
65 }
66
67 do {
68 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
69
70 // Check if any of these files are missing...
71 $pathsByName = [];
72 foreach ( $res as $row ) {
73 $file = $repo->newFile( $row->name );
74 $pathsByName[$row->name] = $file->getPath();
75 $lastName = $row->name;
76 }
77 $be->preloadFileStat( [ 'srcs' => $pathsByName ] );
78 foreach ( $pathsByName as $path ) {
79 if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
80 $this->output( "$path\n" );
81 }
82 }
83
84 // Find all missing old versions of any of the files in this batch...
85 if ( count( $pathsByName ) ) {
86 $ores = $dbr->newSelectQueryBuilder()
87 ->select( [ 'oi_name', 'oi_archive_name' ] )
88 ->from( 'oldimage' )
89 ->where( [ 'oi_name' => array_map( 'strval', array_keys( $pathsByName ) ) ] )
90 ->caller( __METHOD__ )->fetchResultSet();
91
92 $checkPaths = [];
93 foreach ( $ores as $row ) {
94 if ( !strlen( $row->oi_archive_name ) ) {
95 // broken row
96 continue;
97 }
98 $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
99 $checkPaths[] = $file->getPath();
100 }
101
102 foreach ( array_chunk( $checkPaths, $batchSize ) as $paths ) {
103 $be->preloadFileStat( [ 'srcs' => $paths ] );
104 foreach ( $paths as $path ) {
105 if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
106 $this->output( "$path\n" );
107 }
108 }
109 }
110 }
111 } while ( $res->numRows() >= $batchSize );
112 }
113}
114
115$maintClass = FindMissingFiles::class;
116require_once RUN_MAINTENANCE_IF_MAIN;
const NS_FILE
Definition Defines.php:70
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42