MediaWiki  master
findMissingFiles.php
Go to the documentation of this file.
1 <?php
22 
23 require_once __DIR__ . '/Maintenance.php';
24 
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 = MediaWikiServices::getInstance()->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  $joinTables = [];
48  $joinConds = [];
49  if ( $mtime1 || $mtime2 ) {
50  $joinTables[] = 'page';
51  $joinConds['page'] = [ 'JOIN',
52  [ 'page_title = img_name', 'page_namespace' => NS_FILE ] ];
53  $joinTables[] = 'logging';
54  $on = [ 'log_page = page_id', 'log_type' => [ 'upload', 'move', 'delete' ] ];
55  if ( $mtime1 ) {
56  $on[] = "log_timestamp > {$dbr->addQuotes($mtime1)}";
57  }
58  if ( $mtime2 ) {
59  $on[] = "log_timestamp < {$dbr->addQuotes($mtime2)}";
60  }
61  $joinConds['logging'] = [ 'JOIN', $on ];
62  }
63 
64  do {
65  $res = $dbr->select(
66  array_merge( [ 'image' ], $joinTables ),
67  [ 'name' => 'img_name' ],
68  [ "img_name > " . $dbr->addQuotes( $lastName ) ],
69  __METHOD__,
70  // DISTINCT causes a pointless filesort
71  [ 'ORDER BY' => 'name', 'GROUP BY' => 'name',
72  'LIMIT' => $batchSize ],
73  $joinConds
74  );
75 
76  // Check if any of these files are missing...
77  $pathsByName = [];
78  foreach ( $res as $row ) {
79  $file = $repo->newFile( $row->name );
80  $pathsByName[$row->name] = $file->getPath();
81  $lastName = $row->name;
82  }
83  $be->preloadFileStat( [ 'srcs' => $pathsByName ] );
84  foreach ( $pathsByName as $path ) {
85  if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
86  $this->output( "$path\n" );
87  }
88  }
89 
90  // Find all missing old versions of any of the files in this batch...
91  if ( count( $pathsByName ) ) {
92  $ores = $dbr->select( 'oldimage',
93  [ 'oi_name', 'oi_archive_name' ],
94  [ 'oi_name' => array_map( 'strval', array_keys( $pathsByName ) ) ],
95  __METHOD__
96  );
97 
98  $checkPaths = [];
99  foreach ( $ores as $row ) {
100  if ( !strlen( $row->oi_archive_name ) ) {
101  // broken row
102  continue;
103  }
104  $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
105  $checkPaths[] = $file->getPath();
106  }
107 
108  foreach ( array_chunk( $checkPaths, $batchSize ) as $paths ) {
109  $be->preloadFileStat( [ 'srcs' => $paths ] );
110  foreach ( $paths as $path ) {
111  if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
112  $this->output( "$path\n" );
113  }
114  }
115  }
116  }
117  } while ( $res->numRows() >= $batchSize );
118  }
119 }
120 
121 $maintClass = FindMissingFiles::class;
122 require_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...
Definition: Maintenance.php:66
output( $out, $channel=null)
Throw some output to the user.
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)
Service locator for MediaWiki core services.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42