Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 60
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FindMissingFiles
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 2
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 1
210
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 */
20
21require_once __DIR__ . '/Maintenance.php';
22
23class FindMissingFiles extends Maintenance {
24    public 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    public function execute() {
35        $lastName = $this->getOption( 'start', '' );
36
37        $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
38        $dbr = $repo->getReplicaDB();
39        $be = $repo->getBackend();
40        $batchSize = $this->getBatchSize();
41
42        $mtime1 = $dbr->timestampOrNull( $this->getOption( 'mtimeafter', null ) );
43        $mtime2 = $dbr->timestampOrNull( $this->getOption( 'mtimebefore', null ) );
44
45        $queryBuilder = $dbr->newSelectQueryBuilder()
46            ->select( [ 'name' => 'img_name' ] )
47            ->from( 'image' )
48            ->where( $dbr->expr( 'img_name', '>', $lastName ) )
49            ->groupBy( 'name' )
50            ->orderBy( 'name' )
51            ->limit( $batchSize );
52
53        if ( $mtime1 || $mtime2 ) {
54            $queryBuilder->join( 'page', null, 'page_title = img_name' );
55            $queryBuilder->andWhere( [ 'page_namespace' => NS_FILE ] );
56
57            $queryBuilder->join( 'logging', null, 'log_page = page_id' );
58            $queryBuilder->andWhere( [ 'log_type' => [ 'upload', 'move', 'delete' ] ] );
59            if ( $mtime1 ) {
60                $queryBuilder->andWhere( $dbr->expr( 'log_timestamp', '>', $mtime1 ) );
61            }
62            if ( $mtime2 ) {
63                $queryBuilder->andWhere( $dbr->expr( 'log_timestamp', '<', $mtime2 ) );
64            }
65        }
66
67        do {
68            $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
69
70            // Check if any of these files are missing...
71            $pathsByName = [];
72            foreach ( $res as $row ) {
73                $file = $repo->newFile( $row->name );
74                $pathsByName[$row->name] = $file->getPath();
75                $lastName = $row->name;
76            }
77            $be->preloadFileStat( [ 'srcs' => $pathsByName ] );
78            foreach ( $pathsByName as $path ) {
79                if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
80                    $this->output( "$path\n" );
81                }
82            }
83
84            // Find all missing old versions of any of the files in this batch...
85            if ( count( $pathsByName ) ) {
86                $ores = $dbr->newSelectQueryBuilder()
87                    ->select( [ 'oi_name', 'oi_archive_name' ] )
88                    ->from( 'oldimage' )
89                    ->where( [ 'oi_name' => array_map( 'strval', array_keys( $pathsByName ) ) ] )
90                    ->caller( __METHOD__ )->fetchResultSet();
91
92                $checkPaths = [];
93                foreach ( $ores as $row ) {
94                    if ( !strlen( $row->oi_archive_name ) ) {
95                        // broken row
96                        continue;
97                    }
98                    $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
99                    $checkPaths[] = $file->getPath();
100                }
101
102                foreach ( array_chunk( $checkPaths, $batchSize ) as $paths ) {
103                    $be->preloadFileStat( [ 'srcs' => $paths ] );
104                    foreach ( $paths as $path ) {
105                        if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
106                            $this->output( "$path\n" );
107                        }
108                    }
109                }
110            }
111        } while ( $res->numRows() >= $batchSize );
112    }
113}
114
115$maintClass = FindMissingFiles::class;
116require_once RUN_MAINTENANCE_IF_MAIN;