2015-10-28 20:01:50 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2020-02-04 15:44:08 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2015-10-28 20:01:50 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-10-28 20:01:50 +00:00
|
|
|
|
|
|
|
|
class FindOrphanedFiles extends Maintenance {
|
2019-10-09 18:41:33 +00:00
|
|
|
public function __construct() {
|
2015-10-28 20:01:50 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( "Find unregistered files in the 'public' repo zone." );
|
2015-10-28 20:01:50 +00:00
|
|
|
$this->addOption( 'subdir',
|
|
|
|
|
'Only scan files in this subdirectory (e.g. "a/a0")', false, true );
|
|
|
|
|
$this->addOption( 'verbose', "Mention file paths checked" );
|
|
|
|
|
$this->setBatchSize( 500 );
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:41:33 +00:00
|
|
|
public function execute() {
|
2015-10-28 20:01:50 +00:00
|
|
|
$subdir = $this->getOption( 'subdir', '' );
|
|
|
|
|
$verbose = $this->hasOption( 'verbose' );
|
|
|
|
|
|
2023-08-31 09:21:12 +00:00
|
|
|
$repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
|
2015-10-28 20:01:50 +00:00
|
|
|
if ( $repo->hasSha1Storage() ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Local repo uses SHA-1 file storage names; aborting." );
|
2015-10-28 20:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$directory = $repo->getZonePath( 'public' );
|
|
|
|
|
if ( $subdir != '' ) {
|
|
|
|
|
$directory .= "/$subdir/";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $verbose ) {
|
|
|
|
|
$this->output( "Scanning files under $directory:\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$list = $repo->getBackend()->getFileList( [ 'dir' => $directory ] );
|
2015-10-28 20:01:50 +00:00
|
|
|
if ( $list === null ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Could not get file listing." );
|
2015-10-28 20:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$pathBatch = [];
|
2015-10-28 20:01:50 +00:00
|
|
|
foreach ( $list as $path ) {
|
|
|
|
|
if ( preg_match( '#^(thumb|deleted)/#', $path ) ) {
|
|
|
|
|
continue; // handle ugly nested containers on stock installs
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-04 22:51:38 +00:00
|
|
|
$pathBatch[] = $path;
|
2017-11-05 08:09:51 +00:00
|
|
|
if ( count( $pathBatch ) >= $this->getBatchSize() ) {
|
2015-11-04 22:51:38 +00:00
|
|
|
$this->checkFiles( $repo, $pathBatch, $verbose );
|
2016-02-17 09:09:32 +00:00
|
|
|
$pathBatch = [];
|
2015-10-28 20:01:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-11-04 22:51:38 +00:00
|
|
|
$this->checkFiles( $repo, $pathBatch, $verbose );
|
2015-10-28 20:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-04 22:51:38 +00:00
|
|
|
protected function checkFiles( LocalRepo $repo, array $paths, $verbose ) {
|
|
|
|
|
if ( !count( $paths ) ) {
|
2015-10-28 20:01:50 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-18 15:42:39 +00:00
|
|
|
$dbr = $repo->getReplicaDB();
|
2015-10-28 20:01:50 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$curNames = [];
|
|
|
|
|
$oldNames = [];
|
|
|
|
|
$imgIN = [];
|
|
|
|
|
$oiWheres = [];
|
2015-11-04 22:51:38 +00:00
|
|
|
foreach ( $paths as $path ) {
|
|
|
|
|
$name = basename( $path );
|
|
|
|
|
if ( preg_match( '#^archive/#', $path ) ) {
|
2015-10-28 20:01:50 +00:00
|
|
|
if ( $verbose ) {
|
|
|
|
|
$this->output( "Checking old file $name\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-04 22:51:38 +00:00
|
|
|
$oldNames[] = $name;
|
2022-10-21 04:32:38 +00:00
|
|
|
[ , $base ] = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
|
2023-11-01 14:14:51 +00:00
|
|
|
$oiWheres[] = $dbr->expr( 'oi_name', '=', $base )->and( 'oi_archive_name', '=', $name );
|
2015-10-28 20:01:50 +00:00
|
|
|
} else {
|
|
|
|
|
if ( $verbose ) {
|
|
|
|
|
$this->output( "Checking current file $name\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-04 22:51:38 +00:00
|
|
|
$curNames[] = $name;
|
2015-10-28 20:01:50 +00:00
|
|
|
$imgIN[] = $name;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-05 20:21:24 +00:00
|
|
|
$uqb = $dbr->newUnionQueryBuilder();
|
|
|
|
|
$uqb->add(
|
|
|
|
|
$dbr->newSelectQueryBuilder()
|
|
|
|
|
->select( [ 'name' => 'img_name', 'old' => '0' ] )
|
|
|
|
|
->from( 'image' )
|
|
|
|
|
->where( $imgIN ? [ 'img_name' => $imgIN ] : '1=0' )
|
|
|
|
|
);
|
|
|
|
|
$uqb->add(
|
|
|
|
|
$dbr->newSelectQueryBuilder()
|
|
|
|
|
->select( [ 'name' => 'oi_archive_name', 'old' => '1' ] )
|
|
|
|
|
->from( 'oldimage' )
|
2024-06-29 20:31:02 +00:00
|
|
|
->where( $oiWheres ? $dbr->orExpr( $oiWheres ) : '1=0' )
|
2015-10-28 20:01:50 +00:00
|
|
|
);
|
|
|
|
|
|
2023-04-05 20:21:24 +00:00
|
|
|
$res = $uqb->all()->caller( __METHOD__ )->fetchResultSet();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$curNamesFound = [];
|
|
|
|
|
$oldNamesFound = [];
|
2015-10-28 20:01:50 +00:00
|
|
|
foreach ( $res as $row ) {
|
2015-11-04 22:51:38 +00:00
|
|
|
if ( $row->old ) {
|
|
|
|
|
$oldNamesFound[] = $row->name;
|
|
|
|
|
} else {
|
|
|
|
|
$curNamesFound[] = $row->name;
|
|
|
|
|
}
|
2015-10-28 20:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-04 22:51:38 +00:00
|
|
|
foreach ( array_diff( $curNames, $curNamesFound ) as $name ) {
|
|
|
|
|
$file = $repo->newFile( $name );
|
|
|
|
|
// Print name and public URL to ease recovery
|
2015-11-05 04:19:17 +00:00
|
|
|
if ( $file ) {
|
|
|
|
|
$this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
|
|
|
|
|
} else {
|
|
|
|
|
$this->error( "Cannot get URL for bad file title '$name'" );
|
|
|
|
|
}
|
2015-11-04 22:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( array_diff( $oldNames, $oldNamesFound ) as $name ) {
|
2022-10-21 04:32:38 +00:00
|
|
|
[ , $base ] = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
|
2015-11-04 22:51:38 +00:00
|
|
|
$file = $repo->newFromArchiveName( Title::makeTitle( NS_FILE, $base ), $name );
|
2015-10-28 20:01:50 +00:00
|
|
|
// Print name and public URL to ease recovery
|
2015-11-04 22:04:56 +00:00
|
|
|
$this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
|
2015-10-28 20:01:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = FindOrphanedFiles::class;
|
2015-10-28 20:01:50 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|