ILoadBalancer: Rename runMasterTransactionListenerCallbacks to runPrimaryTransactionL...
[mediawiki.git] / maintenance / findMissingFiles.php
blob0850e1e4cc49e7193eef18e2dd9d7f913e095ca7
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use MediaWiki\MediaWikiServices;
23 require_once __DIR__ . '/Maintenance.php';
25 class FindMissingFiles extends Maintenance {
26 public function __construct() {
27 parent::__construct();
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 );
36 public function execute() {
37 $lastName = $this->getOption( 'start', '' );
39 $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
40 $dbr = $repo->getReplicaDB();
41 $be = $repo->getBackend();
42 $batchSize = $this->getBatchSize();
44 $mtime1 = $dbr->timestampOrNull( $this->getOption( 'mtimeafter', null ) );
45 $mtime2 = $dbr->timestampOrNull( $this->getOption( 'mtimebefore', null ) );
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)}";
58 if ( $mtime2 ) {
59 $on[] = "log_timestamp < {$dbr->addQuotes($mtime2)}";
61 $joinConds['logging'] = [ 'JOIN', $on ];
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
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;
83 $be->preloadFileStat( [ 'srcs' => $pathsByName ] );
84 foreach ( $pathsByName as $path ) {
85 if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
86 $this->output( "$path\n" );
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__
98 $checkPaths = [];
99 foreach ( $ores as $row ) {
100 if ( !strlen( $row->oi_archive_name ) ) {
101 // broken row
102 continue;
104 $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
105 $checkPaths[] = $file->getPath();
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" );
117 } while ( $res->numRows() >= $batchSize );
121 $maintClass = FindMissingFiles::class;
122 require_once RUN_MAINTENANCE_IF_MAIN;