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.
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.
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
21 require_once __DIR__
. '/Maintenance.php';
23 class FindOrphanedFiles
extends Maintenance
{
24 function __construct() {
25 parent
::__construct();
27 $this->addDescription( "Find unregistered files in the 'public' repo zone." );
28 $this->addOption( 'subdir',
29 'Only scan files in this subdirectory (e.g. "a/a0")', false, true );
30 $this->addOption( 'verbose', "Mention file paths checked" );
31 $this->setBatchSize( 500 );
35 $subdir = $this->getOption( 'subdir', '' );
36 $verbose = $this->hasOption( 'verbose' );
38 $repo = RepoGroup
::singleton()->getLocalRepo();
39 if ( $repo->hasSha1Storage() ) {
40 $this->fatalError( "Local repo uses SHA-1 file storage names; aborting." );
43 $directory = $repo->getZonePath( 'public' );
44 if ( $subdir != '' ) {
45 $directory .= "/$subdir/";
49 $this->output( "Scanning files under $directory:\n" );
52 $list = $repo->getBackend()->getFileList( [ 'dir' => $directory ] );
53 if ( $list === null ) {
54 $this->fatalError( "Could not get file listing." );
58 foreach ( $list as $path ) {
59 if ( preg_match( '#^(thumb|deleted)/#', $path ) ) {
60 continue; // handle ugly nested containers on stock installs
64 if ( count( $pathBatch ) >= $this->getBatchSize() ) {
65 $this->checkFiles( $repo, $pathBatch, $verbose );
69 $this->checkFiles( $repo, $pathBatch, $verbose );
72 protected function checkFiles( LocalRepo
$repo, array $paths, $verbose ) {
73 if ( !count( $paths ) ) {
77 $dbr = $repo->getReplicaDB();
83 foreach ( $paths as $path ) {
84 $name = basename( $path );
85 if ( preg_match( '#^archive/#', $path ) ) {
87 $this->output( "Checking old file $name\n" );
91 list( , $base ) = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
92 $oiWheres[] = $dbr->makeList(
93 [ 'oi_name' => $base, 'oi_archive_name' => $name ],
98 $this->output( "Checking current file $name\n" );
111 [ 'name' => 'img_name', 'old' => 0 ],
112 $imgIN ?
[ 'img_name' => $imgIN ] : '1=0'
116 [ 'name' => 'oi_archive_name', 'old' => 1 ],
117 $oiWheres ?
$dbr->makeList( $oiWheres, LIST_OR
) : '1=0'
120 true // UNION ALL (performance)
127 foreach ( $res as $row ) {
129 $oldNamesFound[] = $row->name
;
131 $curNamesFound[] = $row->name
;
135 foreach ( array_diff( $curNames, $curNamesFound ) as $name ) {
136 $file = $repo->newFile( $name );
137 // Print name and public URL to ease recovery
139 $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
141 $this->error( "Cannot get URL for bad file title '$name'" );
145 foreach ( array_diff( $oldNames, $oldNamesFound ) as $name ) {
146 list( , $base ) = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
147 $file = $repo->newFromArchiveName( Title
::makeTitle( NS_FILE
, $base ), $name );
148 // Print name and public URL to ease recovery
149 $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
154 $maintClass = FindOrphanedFiles
::class;
155 require_once RUN_MAINTENANCE_IF_MAIN
;