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