Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 79
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
FindOrphanedFiles
0.00% covered (danger)
0.00%
0 / 76
0.00% covered (danger)
0.00%
0 / 3
506
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 / 22
0.00% covered (danger)
0.00%
0 / 1
72
 checkFiles
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 1
182
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\Title\Title;
22use Wikimedia\Rdbms\OrExpressionGroup;
23
24require_once __DIR__ . '/Maintenance.php';
25
26class FindOrphanedFiles extends Maintenance {
27    public function __construct() {
28        parent::__construct();
29
30        $this->addDescription( "Find unregistered files in the 'public' repo zone." );
31        $this->addOption( 'subdir',
32            'Only scan files in this subdirectory (e.g. "a/a0")', false, true );
33        $this->addOption( 'verbose', "Mention file paths checked" );
34        $this->setBatchSize( 500 );
35    }
36
37    public function execute() {
38        $subdir = $this->getOption( 'subdir', '' );
39        $verbose = $this->hasOption( 'verbose' );
40
41        $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
42        if ( $repo->hasSha1Storage() ) {
43            $this->fatalError( "Local repo uses SHA-1 file storage names; aborting." );
44        }
45
46        $directory = $repo->getZonePath( 'public' );
47        if ( $subdir != '' ) {
48            $directory .= "/$subdir/";
49        }
50
51        if ( $verbose ) {
52            $this->output( "Scanning files under $directory:\n" );
53        }
54
55        $list = $repo->getBackend()->getFileList( [ 'dir' => $directory ] );
56        if ( $list === null ) {
57            $this->fatalError( "Could not get file listing." );
58        }
59
60        $pathBatch = [];
61        foreach ( $list as $path ) {
62            if ( preg_match( '#^(thumb|deleted)/#', $path ) ) {
63                continue; // handle ugly nested containers on stock installs
64            }
65
66            $pathBatch[] = $path;
67            if ( count( $pathBatch ) >= $this->getBatchSize() ) {
68                $this->checkFiles( $repo, $pathBatch, $verbose );
69                $pathBatch = [];
70            }
71        }
72        $this->checkFiles( $repo, $pathBatch, $verbose );
73    }
74
75    protected function checkFiles( LocalRepo $repo, array $paths, $verbose ) {
76        if ( !count( $paths ) ) {
77            return;
78        }
79
80        $dbr = $repo->getReplicaDB();
81
82        $curNames = [];
83        $oldNames = [];
84        $imgIN = [];
85        $oiWheres = [];
86        foreach ( $paths as $path ) {
87            $name = basename( $path );
88            if ( preg_match( '#^archive/#', $path ) ) {
89                if ( $verbose ) {
90                    $this->output( "Checking old file $name\n" );
91                }
92
93                $oldNames[] = $name;
94                [ , $base ] = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
95                $oiWheres[]  = $dbr->expr( 'oi_name', '=', $base )->and( 'oi_archive_name', '=', $name );
96            } else {
97                if ( $verbose ) {
98                    $this->output( "Checking current file $name\n" );
99                }
100
101                $curNames[] = $name;
102                $imgIN[] = $name;
103            }
104        }
105        $uqb = $dbr->newUnionQueryBuilder();
106        $uqb->add(
107            $dbr->newSelectQueryBuilder()
108                ->select( [ 'name' => 'img_name', 'old' => '0' ] )
109                ->from( 'image' )
110                ->where( $imgIN ? [ 'img_name' => $imgIN ] : '1=0' )
111        );
112        $uqb->add(
113            $dbr->newSelectQueryBuilder()
114                ->select( [ 'name' => 'oi_archive_name', 'old' => '1' ] )
115                ->from( 'oldimage' )
116                ->where( $oiWheres ? new OrExpressionGroup( ...$oiWheres ) : '1=0' )
117        );
118
119        $res = $uqb->all()->caller( __METHOD__ )->fetchResultSet();
120
121        $curNamesFound = [];
122        $oldNamesFound = [];
123        foreach ( $res as $row ) {
124            if ( $row->old ) {
125                $oldNamesFound[] = $row->name;
126            } else {
127                $curNamesFound[] = $row->name;
128            }
129        }
130
131        foreach ( array_diff( $curNames, $curNamesFound ) as $name ) {
132            $file = $repo->newFile( $name );
133            // Print name and public URL to ease recovery
134            if ( $file ) {
135                $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
136            } else {
137                $this->error( "Cannot get URL for bad file title '$name'" );
138            }
139        }
140
141        foreach ( array_diff( $oldNames, $oldNamesFound ) as $name ) {
142            [ , $base ] = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
143            $file = $repo->newFromArchiveName( Title::makeTitle( NS_FILE, $base ), $name );
144            // Print name and public URL to ease recovery
145            $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
146        }
147    }
148}
149
150$maintClass = FindOrphanedFiles::class;
151require_once RUN_MAINTENANCE_IF_MAIN;