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 = $this->getServiceContainer()->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  [ , $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  $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->makeList( $oiWheres, LIST_OR ) : '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 $maintClass = FindOrphanedFiles::class;
153 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:422
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:45
getReplicaDB()
Get a connection to the replica DB.
Definition: LocalRepo.php:507
newFromArchiveName( $title, $archiveName)
Definition: LocalRepo.php:135
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.
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:76
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42