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