MediaWiki REL1_30
findMissingFiles.php
Go to the documentation of this file.
1<?php
21require_once __DIR__ . '/Maintenance.php';
22
24 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 function execute() {
35 $lastName = $this->getOption( 'start', '' );
36
37 $repo = RepoGroup::singleton()->getLocalRepo();
38 $dbr = $repo->getReplicaDB();
39 $be = $repo->getBackend();
40
41 $mtime1 = $dbr->timestampOrNull( $this->getOption( 'mtimeafter', null ) );
42 $mtime2 = $dbr->timestampOrNull( $this->getOption( 'mtimebefore', null ) );
43
44 $joinTables = [];
45 $joinConds = [];
46 if ( $mtime1 || $mtime2 ) {
47 $joinTables[] = 'page';
48 $joinConds['page'] = [ 'INNER JOIN',
49 [ 'page_title = img_name', 'page_namespace' => NS_FILE ] ];
50 $joinTables[] = 'logging';
51 $on = [ 'log_page = page_id', 'log_type' => [ 'upload', 'move', 'delete' ] ];
52 if ( $mtime1 ) {
53 $on[] = "log_timestamp > {$dbr->addQuotes($mtime1)}";
54 }
55 if ( $mtime2 ) {
56 $on[] = "log_timestamp < {$dbr->addQuotes($mtime2)}";
57 }
58 $joinConds['logging'] = [ 'INNER JOIN', $on ];
59 }
60
61 do {
62 $res = $dbr->select(
63 array_merge( [ 'image' ], $joinTables ),
64 [ 'name' => 'img_name' ],
65 [ "img_name > " . $dbr->addQuotes( $lastName ) ],
66 __METHOD__,
67 // DISTINCT causes a pointless filesort
68 [ 'ORDER BY' => 'name', 'GROUP BY' => 'name',
69 'LIMIT' => $this->mBatchSize ],
70 $joinConds
71 );
72
73 // Check if any of these files are missing...
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 // Find all missing old versions of any of the files in this batch...
88 if ( count( $pathsByName ) ) {
89 $ores = $dbr->select( 'oldimage',
90 [ 'oi_name', 'oi_archive_name' ],
91 [ 'oi_name' => array_keys( $pathsByName ) ],
92 __METHOD__
93 );
94
95 $checkPaths = [];
96 foreach ( $ores as $row ) {
97 if ( !strlen( $row->oi_archive_name ) ) {
98 continue; // broken row
99 }
100 $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
101 $checkPaths[] = $file->getPath();
102 }
103
104 foreach ( array_chunk( $checkPaths, $this->mBatchSize ) as $paths ) {
105 $be->preloadFileStat( [ 'srcs' => $paths ] );
106 foreach ( $paths as $path ) {
107 if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
108 $this->output( "$path\n" );
109 }
110 }
111 }
112 }
113 } while ( $res->numRows() >= $this->mBatchSize );
114 }
115}
116
117$maintClass = 'FindMissingFiles';
118require_once RUN_MAINTENANCE_IF_MAIN;
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
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)
Set the batch size.
static singleton()
Get a RepoGroup instance.
Definition RepoGroup.php:59
if(! $regexes) $dbr
Definition cleanup.php:94
$res
Definition database.txt:21
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
const NS_FILE
Definition Defines.php:71
require_once RUN_MAINTENANCE_IF_MAIN