MediaWiki  master
findOrphanedFiles.php
Go to the documentation of this file.
1 <?php
22 
23 require_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 ],
96  LIST_AND
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;
159 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
FindOrphanedFiles
Definition: findOrphanedFiles.php:25
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:487
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
LocalRepo\getReplicaDB
getReplicaDB()
Get a connection to the replica DB.
Definition: LocalRepo.php:469
NS_FILE
const NS_FILE
Definition: Defines.php:75
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
FindOrphanedFiles\execute
execute()
Do the actual work.
Definition: findOrphanedFiles.php:36
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
$res
$res
Definition: testCompression.php:57
$base
$base
Definition: generateLocalAutoload.php:11
LIST_AND
const LIST_AND
Definition: Defines.php:48
$dbr
$dbr
Definition: testCompression.php:54
FindOrphanedFiles\checkFiles
checkFiles(LocalRepo $repo, array $paths, $verbose)
Definition: findOrphanedFiles.php:74
LIST_OR
const LIST_OR
Definition: Defines.php:51
FileRepo\newFile
newFile( $title, $time=false)
Create a new File object from the local repository.
Definition: FileRepo.php:416
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:592
LocalRepo\newFromArchiveName
newFromArchiveName( $title, $archiveName)
Definition: LocalRepo.php:86
$maintClass
$maintClass
Definition: findOrphanedFiles.php:158
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
$path
$path
Definition: NoLocalSettings.php:25
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:366
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:463
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266
LocalRepo
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition: LocalRepo.php:37
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:374
FindOrphanedFiles\__construct
__construct()
Default constructor.
Definition: findOrphanedFiles.php:26