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