MediaWiki REL1_35
findOrphanedFiles.php
Go to the documentation of this file.
1<?php
22
23require_once __DIR__ . '/Maintenance.php';
24
26 public function __construct() {
27 parent::__construct();
28
29 $this->addDescription( "Find unregistered files in the 'public' repo zone." );
30 $this->addOption( 'subdir',
31 'Only scan files in this subdirectory (e.g. "a/a0")', false, true );
32 $this->addOption( 'verbose', "Mention file paths checked" );
33 $this->setBatchSize( 500 );
34 }
35
36 public function execute() {
37 $subdir = $this->getOption( 'subdir', '' );
38 $verbose = $this->hasOption( 'verbose' );
39
40 $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
41 if ( $repo->hasSha1Storage() ) {
42 $this->fatalError( "Local repo uses SHA-1 file storage names; aborting." );
43 }
44
45 $directory = $repo->getZonePath( 'public' );
46 if ( $subdir != '' ) {
47 $directory .= "/$subdir/";
48 }
49
50 if ( $verbose ) {
51 $this->output( "Scanning files under $directory:\n" );
52 }
53
54 $list = $repo->getBackend()->getFileList( [ 'dir' => $directory ] );
55 if ( $list === null ) {
56 $this->fatalError( "Could not get file listing." );
57 }
58
59 $pathBatch = [];
60 foreach ( $list as $path ) {
61 if ( preg_match( '#^(thumb|deleted)/#', $path ) ) {
62 continue; // handle ugly nested containers on stock installs
63 }
64
65 $pathBatch[] = $path;
66 if ( count( $pathBatch ) >= $this->getBatchSize() ) {
67 $this->checkFiles( $repo, $pathBatch, $verbose );
68 $pathBatch = [];
69 }
70 }
71 $this->checkFiles( $repo, $pathBatch, $verbose );
72 }
73
74 protected function checkFiles( LocalRepo $repo, array $paths, $verbose ) {
75 if ( !count( $paths ) ) {
76 return;
77 }
78
79 $dbr = $repo->getReplicaDB();
80
81 $curNames = [];
82 $oldNames = [];
83 $imgIN = [];
84 $oiWheres = [];
85 foreach ( $paths as $path ) {
86 $name = basename( $path );
87 if ( preg_match( '#^archive/#', $path ) ) {
88 if ( $verbose ) {
89 $this->output( "Checking old file $name\n" );
90 }
91
92 $oldNames[] = $name;
93 list( , $base ) = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
94 $oiWheres[] = $dbr->makeList(
95 [ 'oi_name' => $base, 'oi_archive_name' => $name ],
97 );
98 } else {
99 if ( $verbose ) {
100 $this->output( "Checking current file $name\n" );
101 }
102
103 $curNames[] = $name;
104 $imgIN[] = $name;
105 }
106 }
107
108 $res = $dbr->query(
109 $dbr->unionQueries(
110 [
111 $dbr->selectSQLText(
112 'image',
113 [ 'name' => 'img_name', 'old' => 0 ],
114 $imgIN ? [ 'img_name' => $imgIN ] : '1=0',
115 __METHOD__
116 ),
117 $dbr->selectSQLText(
118 'oldimage',
119 [ 'name' => 'oi_archive_name', 'old' => 1 ],
120 $oiWheres ? $dbr->makeList( $oiWheres, LIST_OR ) : '1=0',
121 __METHOD__
122 )
123 ],
124 $dbr::UNION_ALL
125 ),
126 __METHOD__
127 );
128
129 $curNamesFound = [];
130 $oldNamesFound = [];
131 foreach ( $res as $row ) {
132 if ( $row->old ) {
133 $oldNamesFound[] = $row->name;
134 } else {
135 $curNamesFound[] = $row->name;
136 }
137 }
138
139 foreach ( array_diff( $curNames, $curNamesFound ) as $name ) {
140 $file = $repo->newFile( $name );
141 // Print name and public URL to ease recovery
142 if ( $file ) {
143 $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
144 } else {
145 $this->error( "Cannot get URL for bad file title '$name'" );
146 }
147 }
148
149 foreach ( array_diff( $oldNames, $oldNamesFound ) as $name ) {
150 list( , $base ) = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
151 $file = $repo->newFromArchiveName( Title::makeTitle( NS_FILE, $base ), $name );
152 // Print name and public URL to ease recovery
153 $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
154 }
155 }
156}
157
158$maintClass = FindOrphanedFiles::class;
159require_once RUN_MAINTENANCE_IF_MAIN;
const RUN_MAINTENANCE_IF_MAIN
newFile( $title, $time=false)
Create a new File object from the local repository.
Definition FileRepo.php:416
execute()
Do the actual work.
__construct()
Default constructor.
checkFiles(LocalRepo $repo, array $paths, $verbose)
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition LocalRepo.php:37
getReplicaDB()
Get a connection to the replica DB.
newFromArchiveName( $title, $archiveName)
Definition LocalRepo.php:86
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.
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)
Set the batch size.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
MediaWikiServices is the service locator for the application scope of MediaWiki.
const NS_FILE
Definition Defines.php:76
const LIST_OR
Definition Defines.php:52
const LIST_AND
Definition Defines.php:49
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42