MediaWiki master
findMissingFiles.php
Go to the documentation of this file.
1<?php
10
11// @codeCoverageIgnoreStart
12require_once __DIR__ . '/Maintenance.php';
13// @codeCoverageIgnoreEnd
14
16 public function __construct() {
17 parent::__construct();
18
19 $this->addDescription( 'Find registered files with no corresponding file.' );
20 $this->addOption( 'start', 'Start after this file name', false, true );
21 $this->addOption( 'mtimeafter', 'Only include files changed since this time', false, true );
22 $this->addOption( 'mtimebefore', 'Only includes files changed before this time', false, true );
23 $this->setBatchSize( 300 );
24 }
25
26 public function execute() {
27 $lastName = $this->getOption( 'start', '' );
28
29 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
30 $dbr = $repo->getReplicaDB();
31 $be = $repo->getBackend();
32 $batchSize = $this->getBatchSize();
33
34 $mtime1 = $dbr->timestampOrNull( $this->getOption( 'mtimeafter', null ) );
35 $mtime2 = $dbr->timestampOrNull( $this->getOption( 'mtimebefore', null ) );
36
37 $migrationStage = $this->getServiceContainer()->getMainConfig()->get(
38 MainConfigNames::FileSchemaMigrationStage
39 );
40 $nameField = ( $migrationStage & SCHEMA_COMPAT_READ_NEW ) ? 'file_name' : 'img_name';
41
42 $queryBuilder = FileSelectQueryBuilder::newForFile( $dbr )
43 ->groupBy( $nameField )
44 ->orderBy( $nameField )
45 ->limit( $batchSize );
46
47 if ( $mtime1 || $mtime2 ) {
48 $queryBuilder->join( 'page', null, 'page_title = ' . $nameField );
49 $queryBuilder->andWhere( [ 'page_namespace' => NS_FILE ] );
50
51 $queryBuilder->join( 'logging', null, 'log_page = page_id' );
52 $queryBuilder->andWhere( [ 'log_type' => [ 'upload', 'move', 'delete' ] ] );
53 if ( $mtime1 ) {
54 $queryBuilder->andWhere( $dbr->expr( 'log_timestamp', '>', $mtime1 ) );
55 }
56 if ( $mtime2 ) {
57 $queryBuilder->andWhere( $dbr->expr( 'log_timestamp', '<', $mtime2 ) );
58 }
59 }
60
61 do {
62 $res = ( clone $queryBuilder )
63 ->where( $dbr->expr( $nameField, '>', $lastName ) )
64 ->caller( __METHOD__ )->fetchResultSet();
65
66 // Check if any of these files are missing...
67 $pathsByName = [];
68 foreach ( $res as $row ) {
69 $file = $repo->newFile( $row->img_name );
70 $pathsByName[$row->img_name] = $file->getPath();
71 $lastName = $row->img_name;
72 }
73 $be->preloadFileStat( [ 'srcs' => $pathsByName ] );
74 foreach ( $pathsByName as $path ) {
75 if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
76 $this->output( "$path\n" );
77 }
78 }
79
80 // Find all missing old versions of any of the files in this batch...
81 if ( count( $pathsByName ) ) {
82 $ores = FileSelectQueryBuilder::newForOldFile( $dbr )
83 ->where( [ 'oi_name' => array_map( 'strval', array_keys( $pathsByName ) ) ] )
84 ->caller( __METHOD__ )->fetchResultSet();
85
86 $checkPaths = [];
87 foreach ( $ores as $row ) {
88 if ( $row->oi_archive_name === '' ) {
89 // broken row
90 continue;
91 }
92 $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
93 $checkPaths[] = $file->getPath();
94 }
95
96 foreach ( array_chunk( $checkPaths, $batchSize ) as $paths ) {
97 $be->preloadFileStat( [ 'srcs' => $paths ] );
98 foreach ( $paths as $path ) {
99 if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
100 $this->output( "$path\n" );
101 }
102 }
103 }
104 }
105 } while ( $res->numRows() >= $batchSize );
106 }
107}
108
109// @codeCoverageIgnoreStart
110$maintClass = FindMissingFiles::class;
111require_once RUN_MAINTENANCE_IF_MAIN;
112// @codeCoverageIgnoreEnd
const SCHEMA_COMPAT_READ_NEW
Definition Defines.php:298
const NS_FILE
Definition Defines.php:57
execute()
Do the actual work.
__construct()
Default constructor.
A class containing constants representing the names of configuration variables.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
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.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.