MediaWiki  1.30.0
findOrphanedFiles.php
Go to the documentation of this file.
1 <?php
21 require_once __DIR__ . '/Maintenance.php';
22 
24  function __construct() {
25  parent::__construct();
26 
27  $this->addDescription( "Find unregistered files in the 'public' repo zone." );
28  $this->addOption( 'subdir',
29  'Only scan files in this subdirectory (e.g. "a/a0")', false, true );
30  $this->addOption( 'verbose', "Mention file paths checked" );
31  $this->setBatchSize( 500 );
32  }
33 
34  function execute() {
35  $subdir = $this->getOption( 'subdir', '' );
36  $verbose = $this->hasOption( 'verbose' );
37 
38  $repo = RepoGroup::singleton()->getLocalRepo();
39  if ( $repo->hasSha1Storage() ) {
40  $this->error( "Local repo uses SHA-1 file storage names; aborting.", 1 );
41  }
42 
43  $directory = $repo->getZonePath( 'public' );
44  if ( $subdir != '' ) {
45  $directory .= "/$subdir/";
46  }
47 
48  if ( $verbose ) {
49  $this->output( "Scanning files under $directory:\n" );
50  }
51 
52  $list = $repo->getBackend()->getFileList( [ 'dir' => $directory ] );
53  if ( $list === null ) {
54  $this->error( "Could not get file listing.", 1 );
55  }
56 
57  $pathBatch = [];
58  foreach ( $list as $path ) {
59  if ( preg_match( '#^(thumb|deleted)/#', $path ) ) {
60  continue; // handle ugly nested containers on stock installs
61  }
62 
63  $pathBatch[] = $path;
64  if ( count( $pathBatch ) >= $this->mBatchSize ) {
65  $this->checkFiles( $repo, $pathBatch, $verbose );
66  $pathBatch = [];
67  }
68  }
69  $this->checkFiles( $repo, $pathBatch, $verbose );
70  }
71 
72  protected function checkFiles( LocalRepo $repo, array $paths, $verbose ) {
73  if ( !count( $paths ) ) {
74  return;
75  }
76 
77  $dbr = $repo->getReplicaDB();
78 
79  $curNames = [];
80  $oldNames = [];
81  $imgIN = [];
82  $oiWheres = [];
83  foreach ( $paths as $path ) {
84  $name = basename( $path );
85  if ( preg_match( '#^archive/#', $path ) ) {
86  if ( $verbose ) {
87  $this->output( "Checking old file $name\n" );
88  }
89 
90  $oldNames[] = $name;
91  list( , $base ) = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
92  $oiWheres[] = $dbr->makeList(
93  [ 'oi_name' => $base, 'oi_archive_name' => $name ],
94  LIST_AND
95  );
96  } else {
97  if ( $verbose ) {
98  $this->output( "Checking current file $name\n" );
99  }
100 
101  $curNames[] = $name;
102  $imgIN[] = $name;
103  }
104  }
105 
106  $res = $dbr->query(
107  $dbr->unionQueries(
108  [
109  $dbr->selectSQLText(
110  'image',
111  [ 'name' => 'img_name', 'old' => 0 ],
112  $imgIN ? [ 'img_name' => $imgIN ] : '1=0'
113  ),
114  $dbr->selectSQLText(
115  'oldimage',
116  [ 'name' => 'oi_archive_name', 'old' => 1 ],
117  $oiWheres ? $dbr->makeList( $oiWheres, LIST_OR ) : '1=0'
118  )
119  ],
120  true // UNION ALL (performance)
121  ),
122  __METHOD__
123  );
124 
125  $curNamesFound = [];
126  $oldNamesFound = [];
127  foreach ( $res as $row ) {
128  if ( $row->old ) {
129  $oldNamesFound[] = $row->name;
130  } else {
131  $curNamesFound[] = $row->name;
132  }
133  }
134 
135  foreach ( array_diff( $curNames, $curNamesFound ) as $name ) {
136  $file = $repo->newFile( $name );
137  // Print name and public URL to ease recovery
138  if ( $file ) {
139  $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
140  } else {
141  $this->error( "Cannot get URL for bad file title '$name'" );
142  }
143  }
144 
145  foreach ( array_diff( $oldNames, $oldNamesFound ) as $name ) {
146  list( , $base ) = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
147  $file = $repo->newFromArchiveName( Title::makeTitle( NS_FILE, $base ), $name );
148  // Print name and public URL to ease recovery
149  $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
150  }
151  }
152 }
153 
154 $maintClass = 'FindOrphanedFiles';
155 require_once RUN_MAINTENANCE_IF_MAIN;
FindOrphanedFiles
Definition: findOrphanedFiles.php:23
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:59
captcha-old.count
count
Definition: captcha-old.py:249
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
LocalRepo\getReplicaDB
getReplicaDB()
Get a connection to the replica DB.
Definition: LocalRepo.php:458
NS_FILE
const NS_FILE
Definition: Defines.php:71
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
FindOrphanedFiles\execute
execute()
Do the actual work.
Definition: findOrphanedFiles.php:34
$res
$res
Definition: database.txt:21
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
$base
$base
Definition: generateLocalAutoload.php:10
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
LIST_AND
const LIST_AND
Definition: Defines.php:44
FindOrphanedFiles\checkFiles
checkFiles(LocalRepo $repo, array $paths, $verbose)
Definition: findOrphanedFiles.php:72
LIST_OR
const LIST_OR
Definition: Defines.php:47
FileRepo\newFile
newFile( $title, $time=false)
Create a new File object from the local repository.
Definition: FileRepo.php:387
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:529
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
LocalRepo\newFromArchiveName
newFromArchiveName( $title, $archiveName)
Definition: LocalRepo.php:84
$maintClass
$maintClass
Definition: findOrphanedFiles.php:154
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:250
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
$path
$path
Definition: NoLocalSettings.php:26
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:392
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:236
LocalRepo
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition: LocalRepo.php:35
array
the array() calling protocol came about after MediaWiki 1.4rc1.
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:314
FindOrphanedFiles\__construct
__construct()
Default constructor.
Definition: findOrphanedFiles.php:24