MediaWiki master
findOrphanedFiles.php
Go to the documentation of this file.
1<?php
6
10use MediaWiki\Title\Title;
11
12// @codeCoverageIgnoreStart
13require_once __DIR__ . '/Maintenance.php';
14// @codeCoverageIgnoreEnd
15
17
18 public function __construct() {
19 parent::__construct();
20
21 $this->addDescription( "Find unregistered files in the 'public' repo zone." );
22 $this->addOption( 'subdir',
23 'Only scan files in this subdirectory (e.g. "a/a0")', false, true );
24 $this->addOption( 'verbose', "Mention file paths checked" );
25 $this->setBatchSize( 500 );
26 }
27
28 public function execute() {
29 $subdir = $this->getOption( 'subdir', '' );
30 $verbose = $this->hasOption( 'verbose' );
31
32 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
33 $directory = $repo->getZonePath( 'public' );
34 if ( $subdir != '' ) {
35 $directory .= "/$subdir/";
36 }
37
38 if ( $verbose ) {
39 $this->output( "Scanning files under $directory:\n" );
40 }
41
42 $list = $repo->getBackend()->getFileList( [ 'dir' => $directory ] );
43 if ( $list === null ) {
44 $this->fatalError( "Could not get file listing." );
45 }
46
47 $pathBatch = [];
48 foreach ( $list as $path ) {
49 if ( preg_match( '#^(thumb|deleted)/#', $path ) ) {
50 continue; // handle ugly nested containers on stock installs
51 }
52
53 $pathBatch[] = $path;
54 if ( count( $pathBatch ) >= $this->getBatchSize() ) {
55 $this->checkFiles( $repo, $pathBatch, $verbose );
56 $pathBatch = [];
57 }
58 }
59 $this->checkFiles( $repo, $pathBatch, $verbose );
60 }
61
62 protected function checkFiles( LocalRepo $repo, array $paths, bool $verbose ) {
63 if ( !count( $paths ) ) {
64 return;
65 }
66
67 $dbr = $repo->getReplicaDB();
68
69 $curNames = [];
70 $oldNames = [];
71 $imgIN = [];
72 $oiWheres = [];
73 foreach ( $paths as $path ) {
74 $name = basename( $path );
75 if ( preg_match( '#^archive/#', $path ) ) {
76 if ( $verbose ) {
77 $this->output( "Checking old file $name\n" );
78 }
79
80 $oldNames[] = $name;
81 [ , $base ] = explode( '!', $name, 2 ); // <TS::MW>!<img_name>
82 $oiWheres[] = $dbr->expr( 'oi_name', '=', $base )->and( 'oi_archive_name', '=', $name );
83 } else {
84 if ( $verbose ) {
85 $this->output( "Checking current file $name\n" );
86 }
87
88 $curNames[] = $name;
89 $imgIN[] = $name;
90 }
91 }
92
94 ->where( $imgIN ? [ 'img_name' => $imgIN ] : '1=0' )
95 ->caller( __METHOD__ )
96 ->fetchResultSet();
98 ->where( $oiWheres ? $dbr->orExpr( $oiWheres ) : '1=0' )
99 ->caller( __METHOD__ )
100 ->fetchResultSet();
101
102 $curNamesFound = [];
103 $oldNamesFound = [];
104
105 foreach ( $res1 as $row ) {
106 $curNamesFound[] = $row->img_name;
107 }
108 foreach ( $res2 as $row ) {
109 $oldNamesFound[] = $row->oi_name;
110 }
111
112 foreach ( array_diff( $curNames, $curNamesFound ) as $name ) {
113 $file = $repo->newFile( $name );
114 // Print name and public URL to ease recovery
115 if ( $file ) {
116 $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
117 } else {
118 $this->error( "Cannot get URL for bad file title '$name'" );
119 }
120 }
121
122 foreach ( array_diff( $oldNames, $oldNamesFound ) as $name ) {
123 [ , $base ] = explode( '!', $name, 2 ); // <TS::MW>!<img_name>
124 $file = $repo->newFromArchiveName( Title::makeTitle( NS_FILE, $base ), $name );
125 // Print name and public URL to ease recovery
126 $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
127 }
128 }
129}
130
131// @codeCoverageIgnoreStart
132$maintClass = FindOrphanedFiles::class;
133require_once RUN_MAINTENANCE_IF_MAIN;
134// @codeCoverageIgnoreEnd
const NS_FILE
Definition Defines.php:57
$maintClass
checkFiles(LocalRepo $repo, array $paths, bool $verbose)
newFile( $title, $time=false)
Create a new File object from the local repository.
Definition FileRepo.php:422
static newForOldFile(IReadableDatabase $db, array $options=[])
static newForFile(IReadableDatabase $db, array $options=[])
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:43
newFromArchiveName( $title, $archiveName)
getReplicaDB()
Get a connection to the replica DB.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition Title.php:609