MediaWiki master
findMissingFiles.php
Go to the documentation of this file.
1<?php
21// @codeCoverageIgnoreStart
22require_once __DIR__ . '/Maintenance.php';
23// @codeCoverageIgnoreEnd
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 = $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 ->where( $dbr->expr( 'img_name', '>', $lastName ) )
51 ->groupBy( 'name' )
52 ->orderBy( 'name' )
53 ->limit( $batchSize );
54
55 if ( $mtime1 || $mtime2 ) {
56 $queryBuilder->join( 'page', null, 'page_title = img_name' );
57 $queryBuilder->andWhere( [ 'page_namespace' => NS_FILE ] );
58
59 $queryBuilder->join( 'logging', null, 'log_page = page_id' );
60 $queryBuilder->andWhere( [ 'log_type' => [ 'upload', 'move', 'delete' ] ] );
61 if ( $mtime1 ) {
62 $queryBuilder->andWhere( $dbr->expr( 'log_timestamp', '>', $mtime1 ) );
63 }
64 if ( $mtime2 ) {
65 $queryBuilder->andWhere( $dbr->expr( 'log_timestamp', '<', $mtime2 ) );
66 }
67 }
68
69 do {
70 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
71
72 // Check if any of these files are missing...
73 $pathsByName = [];
74 foreach ( $res as $row ) {
75 $file = $repo->newFile( $row->name );
76 $pathsByName[$row->name] = $file->getPath();
77 $lastName = $row->name;
78 }
79 $be->preloadFileStat( [ 'srcs' => $pathsByName ] );
80 foreach ( $pathsByName as $path ) {
81 if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
82 $this->output( "$path\n" );
83 }
84 }
85
86 // Find all missing old versions of any of the files in this batch...
87 if ( count( $pathsByName ) ) {
88 $ores = $dbr->newSelectQueryBuilder()
89 ->select( [ 'oi_name', 'oi_archive_name' ] )
90 ->from( 'oldimage' )
91 ->where( [ 'oi_name' => array_map( 'strval', array_keys( $pathsByName ) ) ] )
92 ->caller( __METHOD__ )->fetchResultSet();
93
94 $checkPaths = [];
95 foreach ( $ores as $row ) {
96 if ( !strlen( $row->oi_archive_name ) ) {
97 // broken row
98 continue;
99 }
100 $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
101 $checkPaths[] = $file->getPath();
102 }
103
104 foreach ( array_chunk( $checkPaths, $batchSize ) 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() >= $batchSize );
114 }
115}
116
117// @codeCoverageIgnoreStart
118$maintClass = FindMissingFiles::class;
119require_once RUN_MAINTENANCE_IF_MAIN;
120// @codeCoverageIgnoreEnd
const NS_FILE
Definition Defines.php:71
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)