MediaWiki REL1_28
findOrphanedFiles.php
Go to the documentation of this file.
1<?php
22require_once __DIR__ . '/Maintenance.php';
23
25 function __construct() {
26 parent::__construct();
27
28 $this->addDescription( "Find unregistered files in the 'public' repo zone." );
29 $this->addOption( 'subdir',
30 'Only scan files in this subdirectory (e.g. "a/a0")', false, true );
31 $this->addOption( 'verbose', "Mention file paths checked" );
32 $this->setBatchSize( 500 );
33 }
34
35 function execute() {
36 $subdir = $this->getOption( 'subdir', '' );
37 $verbose = $this->hasOption( 'verbose' );
38
39 $repo = RepoGroup::singleton()->getLocalRepo();
40 if ( $repo->hasSha1Storage() ) {
41 $this->error( "Local repo uses SHA-1 file storage names; aborting.", 1 );
42 }
43
44 $directory = $repo->getZonePath( 'public' );
45 if ( $subdir != '' ) {
46 $directory .= "/$subdir/";
47 }
48
49 if ( $verbose ) {
50 $this->output( "Scanning files under $directory:\n" );
51 }
52
53 $list = $repo->getBackend()->getFileList( [ 'dir' => $directory ] );
54 if ( $list === null ) {
55 $this->error( "Could not get file listing.", 1 );
56 }
57
58 $pathBatch = [];
59 foreach ( $list as $path ) {
60 if ( preg_match( '#^(thumb|deleted)/#', $path ) ) {
61 continue; // handle ugly nested containers on stock installs
62 }
63
64 $pathBatch[] = $path;
65 if ( count( $pathBatch ) >= $this->mBatchSize ) {
66 $this->checkFiles( $repo, $pathBatch, $verbose );
67 $pathBatch = [];
68 }
69 }
70 $this->checkFiles( $repo, $pathBatch, $verbose );
71 }
72
73 protected function checkFiles( LocalRepo $repo, array $paths, $verbose ) {
74 if ( !count( $paths ) ) {
75 return;
76 }
77
78 $dbr = $repo->getSlaveDB();
79
80 $curNames = [];
81 $oldNames = [];
82 $imgIN = [];
83 $oiWheres = [];
84 foreach ( $paths as $path ) {
85 $name = basename( $path );
86 if ( preg_match( '#^archive/#', $path ) ) {
87 if ( $verbose ) {
88 $this->output( "Checking old file $name\n" );
89 }
90
91 $oldNames[] = $name;
92 list( , $base ) = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
93 $oiWheres[] = $dbr->makeList(
94 [ 'oi_name' => $base, 'oi_archive_name' => $name ],
96 );
97 } else {
98 if ( $verbose ) {
99 $this->output( "Checking current file $name\n" );
100 }
101
102 $curNames[] = $name;
103 $imgIN[] = $name;
104 }
105 }
106
107 $res = $dbr->query(
108 $dbr->unionQueries(
109 [
110 $dbr->selectSQLText(
111 'image',
112 [ 'name' => 'img_name', 'old' => 0 ],
113 $imgIN ? [ 'img_name' => $imgIN ] : '1=0'
114 ),
115 $dbr->selectSQLText(
116 'oldimage',
117 [ 'name' => 'oi_archive_name', 'old' => 1 ],
118 $oiWheres ? $dbr->makeList( $oiWheres, LIST_OR ) : '1=0'
119 )
120 ],
121 true // UNION ALL (performance)
122 ),
123 __METHOD__
124 );
125
126 $curNamesFound = [];
127 $oldNamesFound = [];
128 foreach ( $res as $row ) {
129 if ( $row->old ) {
130 $oldNamesFound[] = $row->name;
131 } else {
132 $curNamesFound[] = $row->name;
133 }
134 }
135
136 foreach ( array_diff( $curNames, $curNamesFound ) as $name ) {
137 $file = $repo->newFile( $name );
138 // Print name and public URL to ease recovery
139 if ( $file ) {
140 $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
141 } else {
142 $this->error( "Cannot get URL for bad file title '$name'" );
143 }
144 }
145
146 foreach ( array_diff( $oldNames, $oldNamesFound ) as $name ) {
147 list( , $base ) = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
148 $file = $repo->newFromArchiveName( Title::makeTitle( NS_FILE, $base ), $name );
149 // Print name and public URL to ease recovery
150 $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
151 }
152 }
153}
154
155$maintClass = 'FindOrphanedFiles';
156require_once RUN_MAINTENANCE_IF_MAIN;
newFile( $title, $time=false)
Create a new File object from the local repository.
Definition FileRepo.php:387
execute()
Do the actual work.
__construct()
Default constructor.
checkFiles(LocalRepo $repo, array $paths, $verbose)
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition LocalRepo.php:31
getSlaveDB()
Get a connection to the replica DB.
newFromArchiveName( $title, $archiveName)
Definition LocalRepo.php:80
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
hasOption( $name)
Checks to see if a particular param exists.
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)
Set the batch size.
static singleton()
Get a RepoGroup instance.
Definition RepoGroup.php:59
$res
Definition database.txt:21
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
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
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
const NS_FILE
Definition Defines.php:62
const LIST_OR
Definition Defines.php:38
const LIST_AND
Definition Defines.php:35
the array() calling protocol came about after MediaWiki 1.4rc1.
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:304
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:37
require_once RUN_MAINTENANCE_IF_MAIN