summaryrefslogtreecommitdiff
path: root/maintenance/findMissingFiles.php
blob: 5818ee25c4e7d8a8c755ff423994d740bd09680b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @author Aaron Schulz
 */

require_once __DIR__ . '/Maintenance.php';

class FindMissingFiles extends Maintenance {
	function __construct() {
		parent::__construct();

		$this->mDescription = 'Find registered files with no corresponding file.';
		$this->addOption( 'start', 'Start after this file name', false, true );
		$this->addOption( 'mtimeafter', 'Only include files changed since this time', false, true );
		$this->addOption( 'mtimebefore', 'Only includes files changed before this time', false, true );
		$this->setBatchSize( 300 );
	}

	function execute() {
		$lastName = $this->getOption( 'start', '' );

		$repo = RepoGroup::singleton()->getLocalRepo();
		$dbr = $repo->getSlaveDB();
		$be = $repo->getBackend();

		$mtime1 = $dbr->timestampOrNull( $this->getOption( 'mtimeafter', null ) );
		$mtime2 = $dbr->timestampOrNull( $this->getOption( 'mtimebefore', null ) );

		$joinTables = array();
		$joinConds = array();
		if ( $mtime1 || $mtime2 ) {
			$joinTables[] = 'page';
			$joinConds['page'] = array( 'INNER JOIN',
				array( 'page_title = img_name', 'page_namespace' => NS_FILE ) );
			$joinTables[] = 'logging';
			$on = array( 'log_page = page_id', 'log_type' => array( 'upload', 'move', 'delete' ) );
			if ( $mtime1 ) {
				$on[] = "log_timestamp > {$dbr->addQuotes($mtime1)}";
			}
			if ( $mtime2 ) {
				$on[] = "log_timestamp < {$dbr->addQuotes($mtime2)}";
			}
			$joinConds['logging'] = array( 'INNER JOIN', $on );
		}

		do {
			$res = $dbr->select(
				array_merge( array( 'image' ), $joinTables ),
				array( 'name' => 'img_name' ),
				array( "img_name > " . $dbr->addQuotes( $lastName ) ),
				__METHOD__,
				// DISTINCT causes a pointless filesort
				array( 'ORDER BY' => 'name', 'GROUP BY' => 'name',
					'LIMIT' => $this->mBatchSize ),
				$joinConds
			);

			// Check if any of these files are missing...
			$pathsByName = array();
			foreach ( $res as $row ) {
				$file = $repo->newFile( $row->name );
				$pathsByName[$row->name] = $file->getPath();
				$lastName = $row->name;
			}
			$be->preloadFileStat( array( 'srcs' => $pathsByName ) );
			foreach ( $pathsByName as $path ) {
				if ( $be->fileExists( array( 'src' => $path ) ) === false ) {
					$this->output( "$path\n" );
				}
			}

			// Find all missing old versions of any of the files in this batch...
			if ( count( $pathsByName ) ) {
				$ores = $dbr->select( 'oldimage',
					array( 'oi_name', 'oi_archive_name' ),
					array( 'oi_name' => array_keys( $pathsByName ) ),
					__METHOD__
				);

				$checkPaths = array();
				foreach ( $ores as $row ) {
					if ( !strlen( $row->oi_archive_name ) ) {
						continue; // broken row
					}
					$file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
					$checkPaths[] = $file->getPath();
				}

				foreach ( array_chunk( $checkPaths, $this->mBatchSize ) as $paths ) {
					$be->preloadFileStat( array( 'srcs' => $paths ) );
					foreach ( $paths as $path ) {
						if ( $be->fileExists( array( 'src' => $path ) ) === false ) {
							$this->output( "$path\n" );
						}
					}
				}
			}
		} while ( $res->numRows() >= $this->mBatchSize );
	}
}

$maintClass = 'FindMissingFiles';
require_once RUN_MAINTENANCE_IF_MAIN;