MediaWiki  master
findOrphanedFiles.php
Go to the documentation of this file.
1 <?php
23 
24 require_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 = MediaWikiServices::getInstance()->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->makeList(
96  [ 'oi_name' => $base, 'oi_archive_name' => $name ],
97  LIST_AND
98  );
99  } else {
100  if ( $verbose ) {
101  $this->output( "Checking current file $name\n" );
102  }
103 
104  $curNames[] = $name;
105  $imgIN[] = $name;
106  }
107  }
108 
109  $res = $dbr->query(
110  $dbr->unionQueries(
111  [
112  $dbr->selectSQLText(
113  'image',
114  [ 'name' => 'img_name', 'old' => 0 ],
115  $imgIN ? [ 'img_name' => $imgIN ] : '1=0',
116  __METHOD__
117  ),
118  $dbr->selectSQLText(
119  'oldimage',
120  [ 'name' => 'oi_archive_name', 'old' => 1 ],
121  $oiWheres ? $dbr->makeList( $oiWheres, LIST_OR ) : '1=0',
122  __METHOD__
123  )
124  ],
125  $dbr::UNION_ALL
126  ),
127  __METHOD__
128  );
129 
130  $curNamesFound = [];
131  $oldNamesFound = [];
132  foreach ( $res as $row ) {
133  if ( $row->old ) {
134  $oldNamesFound[] = $row->name;
135  } else {
136  $curNamesFound[] = $row->name;
137  }
138  }
139 
140  foreach ( array_diff( $curNames, $curNamesFound ) as $name ) {
141  $file = $repo->newFile( $name );
142  // Print name and public URL to ease recovery
143  if ( $file ) {
144  $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
145  } else {
146  $this->error( "Cannot get URL for bad file title '$name'" );
147  }
148  }
149 
150  foreach ( array_diff( $oldNames, $oldNamesFound ) as $name ) {
151  [ , $base ] = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
152  $file = $repo->newFromArchiveName( Title::makeTitle( NS_FILE, $base ), $name );
153  // Print name and public URL to ease recovery
154  $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
155  }
156  }
157 }
158 
159 $maintClass = FindOrphanedFiles::class;
160 require_once RUN_MAINTENANCE_IF_MAIN;
const NS_FILE
Definition: Defines.php:70
const LIST_OR
Definition: Defines.php:46
const LIST_AND
Definition: Defines.php:43
newFile( $title, $time=false)
Create a new File object from the local repository.
Definition: FileRepo.php:420
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:41
getReplicaDB()
Get a connection to the replica DB.
Definition: LocalRepo.php:516
newFromArchiveName( $title, $archiveName)
Definition: LocalRepo.php:129
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
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)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition: Title.php:82
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42