MediaWiki  1.29.1
findMissingFiles.php
Go to the documentation of this file.
1 <?php
22 require_once __DIR__ . '/Maintenance.php';
23 
25  function __construct() {
26  parent::__construct();
27 
28  $this->addDescription( 'Find registered files with no corresponding file.' );
29  $this->addOption( 'start', 'Start after this file name', false, true );
30  $this->addOption( 'mtimeafter', 'Only include files changed since this time', false, true );
31  $this->addOption( 'mtimebefore', 'Only includes files changed before this time', false, true );
32  $this->setBatchSize( 300 );
33  }
34 
35  function execute() {
36  $lastName = $this->getOption( 'start', '' );
37 
38  $repo = RepoGroup::singleton()->getLocalRepo();
39  $dbr = $repo->getReplicaDB();
40  $be = $repo->getBackend();
41 
42  $mtime1 = $dbr->timestampOrNull( $this->getOption( 'mtimeafter', null ) );
43  $mtime2 = $dbr->timestampOrNull( $this->getOption( 'mtimebefore', null ) );
44 
45  $joinTables = [];
46  $joinConds = [];
47  if ( $mtime1 || $mtime2 ) {
48  $joinTables[] = 'page';
49  $joinConds['page'] = [ 'INNER JOIN',
50  [ 'page_title = img_name', 'page_namespace' => NS_FILE ] ];
51  $joinTables[] = 'logging';
52  $on = [ 'log_page = page_id', 'log_type' => [ 'upload', 'move', 'delete' ] ];
53  if ( $mtime1 ) {
54  $on[] = "log_timestamp > {$dbr->addQuotes($mtime1)}";
55  }
56  if ( $mtime2 ) {
57  $on[] = "log_timestamp < {$dbr->addQuotes($mtime2)}";
58  }
59  $joinConds['logging'] = [ 'INNER JOIN', $on ];
60  }
61 
62  do {
63  $res = $dbr->select(
64  array_merge( [ 'image' ], $joinTables ),
65  [ 'name' => 'img_name' ],
66  [ "img_name > " . $dbr->addQuotes( $lastName ) ],
67  __METHOD__,
68  // DISTINCT causes a pointless filesort
69  [ 'ORDER BY' => 'name', 'GROUP BY' => 'name',
70  'LIMIT' => $this->mBatchSize ],
71  $joinConds
72  );
73 
74  // Check if any of these files are missing...
75  $pathsByName = [];
76  foreach ( $res as $row ) {
77  $file = $repo->newFile( $row->name );
78  $pathsByName[$row->name] = $file->getPath();
79  $lastName = $row->name;
80  }
81  $be->preloadFileStat( [ 'srcs' => $pathsByName ] );
82  foreach ( $pathsByName as $path ) {
83  if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
84  $this->output( "$path\n" );
85  }
86  }
87 
88  // Find all missing old versions of any of the files in this batch...
89  if ( count( $pathsByName ) ) {
90  $ores = $dbr->select( 'oldimage',
91  [ 'oi_name', 'oi_archive_name' ],
92  [ 'oi_name' => array_keys( $pathsByName ) ],
93  __METHOD__
94  );
95 
96  $checkPaths = [];
97  foreach ( $ores as $row ) {
98  if ( !strlen( $row->oi_archive_name ) ) {
99  continue; // broken row
100  }
101  $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
102  $checkPaths[] = $file->getPath();
103  }
104 
105  foreach ( array_chunk( $checkPaths, $this->mBatchSize ) as $paths ) {
106  $be->preloadFileStat( [ 'srcs' => $paths ] );
107  foreach ( $paths as $path ) {
108  if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
109  $this->output( "$path\n" );
110  }
111  }
112  }
113  }
114  } while ( $res->numRows() >= $this->mBatchSize );
115  }
116 }
117 
118 $maintClass = 'FindMissingFiles';
119 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\$mBatchSize
int $mBatchSize
Batch size.
Definition: Maintenance.php:103
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:59
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
captcha-old.count
count
Definition: captcha-old.py:225
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
$maintClass
$maintClass
Definition: findMissingFiles.php:118
NS_FILE
const NS_FILE
Definition: Defines.php:68
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$res
$res
Definition: database.txt:21
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
FindMissingFiles\execute
execute()
Do the actual work.
Definition: findMissingFiles.php:35
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
FindMissingFiles\__construct
__construct()
Default constructor.
Definition: findMissingFiles.php:25
FindMissingFiles
Definition: findMissingFiles.php:24
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
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\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:314