MediaWiki master
findOrphanedFiles.php
Go to the documentation of this file.
1<?php
22
23// @codeCoverageIgnoreStart
24require_once __DIR__ . '/Maintenance.php';
25// @codeCoverageIgnoreEnd
26
28 public function __construct() {
29 parent::__construct();
30
31 $this->addDescription( "Find unregistered files in the 'public' repo zone." );
32 $this->addOption( 'subdir',
33 'Only scan files in this subdirectory (e.g. "a/a0")', false, true );
34 $this->addOption( 'verbose', "Mention file paths checked" );
35 $this->setBatchSize( 500 );
36 }
37
38 public function execute() {
39 $subdir = $this->getOption( 'subdir', '' );
40 $verbose = $this->hasOption( 'verbose' );
41
42 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
43 if ( $repo->hasSha1Storage() ) {
44 $this->fatalError( "Local repo uses SHA-1 file storage names; aborting." );
45 }
46
47 $directory = $repo->getZonePath( 'public' );
48 if ( $subdir != '' ) {
49 $directory .= "/$subdir/";
50 }
51
52 if ( $verbose ) {
53 $this->output( "Scanning files under $directory:\n" );
54 }
55
56 $list = $repo->getBackend()->getFileList( [ 'dir' => $directory ] );
57 if ( $list === null ) {
58 $this->fatalError( "Could not get file listing." );
59 }
60
61 $pathBatch = [];
62 foreach ( $list as $path ) {
63 if ( preg_match( '#^(thumb|deleted)/#', $path ) ) {
64 continue; // handle ugly nested containers on stock installs
65 }
66
67 $pathBatch[] = $path;
68 if ( count( $pathBatch ) >= $this->getBatchSize() ) {
69 $this->checkFiles( $repo, $pathBatch, $verbose );
70 $pathBatch = [];
71 }
72 }
73 $this->checkFiles( $repo, $pathBatch, $verbose );
74 }
75
76 protected function checkFiles( LocalRepo $repo, array $paths, $verbose ) {
77 if ( !count( $paths ) ) {
78 return;
79 }
80
81 $dbr = $repo->getReplicaDB();
82
83 $curNames = [];
84 $oldNames = [];
85 $imgIN = [];
86 $oiWheres = [];
87 foreach ( $paths as $path ) {
88 $name = basename( $path );
89 if ( preg_match( '#^archive/#', $path ) ) {
90 if ( $verbose ) {
91 $this->output( "Checking old file $name\n" );
92 }
93
94 $oldNames[] = $name;
95 [ , $base ] = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
96 $oiWheres[] = $dbr->expr( 'oi_name', '=', $base )->and( 'oi_archive_name', '=', $name );
97 } else {
98 if ( $verbose ) {
99 $this->output( "Checking current file $name\n" );
100 }
101
102 $curNames[] = $name;
103 $imgIN[] = $name;
104 }
105 }
106 $uqb = $dbr->newUnionQueryBuilder();
107 $uqb->add(
108 $dbr->newSelectQueryBuilder()
109 ->select( [ 'name' => 'img_name', 'old' => '0' ] )
110 ->from( 'image' )
111 ->where( $imgIN ? [ 'img_name' => $imgIN ] : '1=0' )
112 );
113 $uqb->add(
114 $dbr->newSelectQueryBuilder()
115 ->select( [ 'name' => 'oi_archive_name', 'old' => '1' ] )
116 ->from( 'oldimage' )
117 ->where( $oiWheres ? $dbr->orExpr( $oiWheres ) : '1=0' )
118 );
119
120 $res = $uqb->all()->caller( __METHOD__ )->fetchResultSet();
121
122 $curNamesFound = [];
123 $oldNamesFound = [];
124 foreach ( $res as $row ) {
125 if ( $row->old ) {
126 $oldNamesFound[] = $row->name;
127 } else {
128 $curNamesFound[] = $row->name;
129 }
130 }
131
132 foreach ( array_diff( $curNames, $curNamesFound ) as $name ) {
133 $file = $repo->newFile( $name );
134 // Print name and public URL to ease recovery
135 if ( $file ) {
136 $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
137 } else {
138 $this->error( "Cannot get URL for bad file title '$name'" );
139 }
140 }
141
142 foreach ( array_diff( $oldNames, $oldNamesFound ) as $name ) {
143 [ , $base ] = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
144 $file = $repo->newFromArchiveName( Title::makeTitle( NS_FILE, $base ), $name );
145 // Print name and public URL to ease recovery
146 $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
147 }
148 }
149}
150
151// @codeCoverageIgnoreStart
152$maintClass = FindOrphanedFiles::class;
153require_once RUN_MAINTENANCE_IF_MAIN;
154// @codeCoverageIgnoreEnd
const NS_FILE
Definition Defines.php:71
newFile( $title, $time=false)
Create a new File object from the local repository.
Definition FileRepo.php:422
execute()
Do the actual work.
__construct()
Default constructor.
checkFiles(LocalRepo $repo, array $paths, $verbose)
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:48
getReplicaDB()
Get a connection to the replica DB.
newFromArchiveName( $title, $archiveName)
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Represents a title within MediaWiki.
Definition Title.php:78