Merge "Made findMissingFiles support scanning files changed in a time range"
[lhc/web/wiklou.git] / maintenance / findMissingFiles.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @author Aaron Schulz
20 */
21
22 require_once __DIR__ . '/Maintenance.php';
23
24 class FindMissingFiles extends Maintenance {
25 function __construct() {
26 parent::__construct();
27
28 $this->mDescription = 'Find registered files with no corresponding file.';
29 $this->addOption( 'start', 'Starting file name', false, true );
30 $this->addOption( 'mtimeafter', 'Only include files changed since this time', false, true );
31 $this->addOption( 'mtimebefore', 'Only includes files changed before this time', false, true );
32 $this->setBatchSize( 300 );
33 }
34
35 function execute() {
36 $lastName = $this->getOption( 'start', '' );
37
38 $repo = RepoGroup::singleton()->getLocalRepo();
39 $dbr = $repo->getSlaveDB();
40 $be = $repo->getBackend();
41
42 $mtime1 = $dbr->timestampOrNull( $this->getOption( 'mtimeafter', null ) );
43 $mtime2 = $dbr->timestampOrNull( $this->getOption( 'mtimebefore', null ) );
44
45 $tables = array( 'image' );
46 $logJoinOn = array( 'log_namespace' => NS_FILE, 'log_title = img_name' );
47 $logJoinOn['log_type'] = array( 'upload', 'move', 'delete' );
48 if ( $mtime1 ) {
49 $logJoinOn[] = "log_timestamp > {$dbr->addQuotes($mtime1)}";
50 }
51 if ( $mtime2 ) {
52 $logJoinOn[] = "log_timestamp < {$dbr->addQuotes($mtime2)}";
53 }
54 if ( $mtime1 || $mtime2 ) {
55 $tables[] = 'logging';
56 }
57
58 do {
59 $res = $dbr->select( $tables,
60 array( 'img_name' => 'DISTINCT(img_name)' ),
61 array( "img_name >= " . $dbr->addQuotes( $lastName ) ),
62 __METHOD__,
63 array( 'ORDER BY' => 'img_name', 'LIMIT' => $this->mBatchSize ),
64 array( 'logging' => array( 'INNER JOIN', $logJoinOn ) )
65 );
66
67 // Check if any of these files are missing...
68 $pathsByName = array();
69 foreach ( $res as $row ) {
70 $file = $repo->newFile( $row->img_name );
71 $pathsByName[$row->img_name] = $file->getPath();
72 $lastName = $row->img_name;
73 }
74 $be->preloadFileStat( array( 'srcs' => $pathsByName ) );
75 foreach ( $pathsByName as $path ) {
76 if ( $be->fileExists( array( 'src' => $path ) ) === false ) {
77 $this->output( "$path\n" );
78 }
79 }
80
81 // Find all missing old versions of any of the files in this batch...
82 if ( count( $pathsByName ) ) {
83 $ores = $dbr->select( 'oldimage',
84 array( 'oi_name', 'oi_archive_name' ),
85 array( 'oi_name' => array_keys( $pathsByName ) ),
86 __METHOD__
87 );
88 foreach ( $ores as $row ) {
89 if ( !strlen( $row->oi_archive_name ) ) {
90 continue; // broken row
91 }
92 $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
93 $path = $file->getPath();
94 if ( $be->fileExists( array( 'src' => $path ) ) === false ) {
95 $this->output( "$path\n" );
96 }
97 }
98 }
99 } while ( $res->numRows() >= $this->mBatchSize );
100 }
101 }
102
103 $maintClass = 'FindMissingFiles';
104 require_once RUN_MAINTENANCE_IF_MAIN;