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