MediaWiki
master
Toggle main menu visibility
findOrphanedFiles.php
Go to the documentation of this file.
1
<?php
6
7
use
MediaWiki\FileRepo\File\FileSelectQueryBuilder
;
8
use
MediaWiki\FileRepo\LocalRepo
;
9
use
MediaWiki\Maintenance\Maintenance
;
10
use MediaWiki\Title\Title;
11
12
// @codeCoverageIgnoreStart
13
require_once __DIR__ .
'/Maintenance.php'
;
14
// @codeCoverageIgnoreEnd
15
16
class
FindOrphanedFiles
extends
Maintenance
{
17
18
public
function
__construct
() {
19
parent::__construct();
20
21
$this->addDescription(
"Find unregistered files in the 'public' repo zone."
);
22
$this->addOption(
'subdir'
,
23
'Only scan files in this subdirectory (e.g. "a/a0")'
,
false
,
true
);
24
$this->addOption(
'verbose'
,
"Mention file paths checked"
);
25
$this->setBatchSize( 500 );
26
}
27
28
public
function
execute
() {
29
$subdir = $this->getOption(
'subdir'
,
''
);
30
$verbose = $this->hasOption(
'verbose'
);
31
32
$repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
33
$directory = $repo->getZonePath(
'public'
);
34
if
( $subdir !=
''
) {
35
$directory .=
"/$subdir/"
;
36
}
37
38
if
( $verbose ) {
39
$this->output(
"Scanning files under $directory:\n"
);
40
}
41
42
$list = $repo->getBackend()->getFileList( [
'dir'
=> $directory ] );
43
if
( $list ===
null
) {
44
$this->fatalError(
"Could not get file listing."
);
45
}
46
47
$pathBatch = [];
48
foreach
( $list as
$path
) {
49
if
( preg_match(
'#^(thumb|deleted)/#'
,
$path
) ) {
50
continue
;
// handle ugly nested containers on stock installs
51
}
52
53
$pathBatch[] =
$path
;
54
if
( count( $pathBatch ) >= $this->getBatchSize() ) {
55
$this->
checkFiles
( $repo, $pathBatch, $verbose );
56
$pathBatch = [];
57
}
58
}
59
$this->
checkFiles
( $repo, $pathBatch, $verbose );
60
}
61
62
protected
function
checkFiles
(
LocalRepo
$repo, array $paths,
bool
$verbose ) {
63
if
( !count( $paths ) ) {
64
return
;
65
}
66
67
$dbr = $repo->
getReplicaDB
();
68
69
$curNames = [];
70
$oldNames = [];
71
$imgIN = [];
72
$oiWheres = [];
73
foreach
( $paths as
$path
) {
74
$name = basename(
$path
);
75
if
( preg_match(
'#^archive/#'
,
$path
) ) {
76
if
( $verbose ) {
77
$this->output(
"Checking old file $name\n"
);
78
}
79
80
$oldNames[] = $name;
81
[ , $base ] = explode(
'!'
, $name, 2 );
// <TS::MW>!<img_name>
82
$oiWheres[] = $dbr->expr(
'oi_name'
,
'='
, $base )->and(
'oi_archive_name'
,
'='
, $name );
83
}
else
{
84
if
( $verbose ) {
85
$this->output(
"Checking current file $name\n"
);
86
}
87
88
$curNames[] = $name;
89
$imgIN[] = $name;
90
}
91
}
92
93
$res1 =
FileSelectQueryBuilder::newForFile
( $dbr )
94
->where( $imgIN ? [
'img_name'
=> $imgIN ] :
'1=0'
)
95
->caller( __METHOD__ )
96
->fetchResultSet();
97
$res2 =
FileSelectQueryBuilder::newForOldFile
( $dbr )
98
->where( $oiWheres ? $dbr->orExpr( $oiWheres ) :
'1=0'
)
99
->caller( __METHOD__ )
100
->fetchResultSet();
101
102
$curNamesFound = [];
103
$oldNamesFound = [];
104
105
foreach
( $res1 as $row ) {
106
$curNamesFound[] = $row->img_name;
107
}
108
foreach
( $res2 as $row ) {
109
$oldNamesFound[] = $row->oi_name;
110
}
111
112
foreach
( array_diff( $curNames, $curNamesFound ) as $name ) {
113
$file = $repo->
newFile
( $name );
114
// Print name and public URL to ease recovery
115
if
( $file ) {
116
$this->output( $name .
"\n"
. $file->getCanonicalUrl() .
"\n\n"
);
117
}
else
{
118
$this->error(
"Cannot get URL for bad file title '$name'"
);
119
}
120
}
121
122
foreach
( array_diff( $oldNames, $oldNamesFound ) as $name ) {
123
[ , $base ] = explode(
'!'
, $name, 2 );
// <TS::MW>!<img_name>
124
$file = $repo->
newFromArchiveName
(
Title::makeTitle
(
NS_FILE
, $base ), $name );
125
// Print name and public URL to ease recovery
126
$this->output( $name .
"\n"
. $file->getCanonicalUrl() .
"\n\n"
);
127
}
128
}
129
}
130
131
// @codeCoverageIgnoreStart
132
$maintClass
= FindOrphanedFiles::class;
133
require_once RUN_MAINTENANCE_IF_MAIN;
134
// @codeCoverageIgnoreEnd
NS_FILE
const NS_FILE
Definition
Defines.php:57
$path
$path
Definition
NoLocalSettings.php:14
$maintClass
$maintClass
Definition
addChangeTag.php:56
FindOrphanedFiles
Definition
findOrphanedFiles.php:16
FindOrphanedFiles\execute
execute()
Definition
findOrphanedFiles.php:28
FindOrphanedFiles\__construct
__construct()
Definition
findOrphanedFiles.php:18
FindOrphanedFiles\checkFiles
checkFiles(LocalRepo $repo, array $paths, bool $verbose)
Definition
findOrphanedFiles.php:62
MediaWiki\FileRepo\FileRepo\newFile
newFile( $title, $time=false)
Create a new File object from the local repository.
Definition
FileRepo.php:422
MediaWiki\FileRepo\File\FileSelectQueryBuilder
Definition
FileSelectQueryBuilder.php:15
MediaWiki\FileRepo\File\FileSelectQueryBuilder\newForOldFile
static newForOldFile(IReadableDatabase $db, array $options=[])
Definition
FileSelectQueryBuilder.php:55
MediaWiki\FileRepo\File\FileSelectQueryBuilder\newForFile
static newForFile(IReadableDatabase $db, array $options=[])
Definition
FileSelectQueryBuilder.php:51
MediaWiki\FileRepo\LocalRepo
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition
LocalRepo.php:43
MediaWiki\FileRepo\LocalRepo\newFromArchiveName
newFromArchiveName( $title, $archiveName)
Definition
LocalRepo.php:122
MediaWiki\FileRepo\LocalRepo\getReplicaDB
getReplicaDB()
Get a connection to the replica DB.
Definition
LocalRepo.php:500
MediaWiki\Title\Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition
Title.php:609
MediaWiki\Maintenance
Definition
applyChangeTag.php:11
maintenance
findOrphanedFiles.php
Generated on
for MediaWiki by
1.18.0