Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 76 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
FindOrphanedFiles | |
0.00% |
0 / 76 |
|
0.00% |
0 / 3 |
506 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
72 | |||
checkFiles | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
182 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | use MediaWiki\Maintenance\Maintenance; |
22 | use MediaWiki\Title\Title; |
23 | |
24 | // @codeCoverageIgnoreStart |
25 | require_once __DIR__ . '/Maintenance.php'; |
26 | // @codeCoverageIgnoreEnd |
27 | |
28 | class FindOrphanedFiles extends Maintenance { |
29 | public function __construct() { |
30 | parent::__construct(); |
31 | |
32 | $this->addDescription( "Find unregistered files in the 'public' repo zone." ); |
33 | $this->addOption( 'subdir', |
34 | 'Only scan files in this subdirectory (e.g. "a/a0")', false, true ); |
35 | $this->addOption( 'verbose', "Mention file paths checked" ); |
36 | $this->setBatchSize( 500 ); |
37 | } |
38 | |
39 | public function execute() { |
40 | $subdir = $this->getOption( 'subdir', '' ); |
41 | $verbose = $this->hasOption( 'verbose' ); |
42 | |
43 | $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo(); |
44 | if ( $repo->hasSha1Storage() ) { |
45 | $this->fatalError( "Local repo uses SHA-1 file storage names; aborting." ); |
46 | } |
47 | |
48 | $directory = $repo->getZonePath( 'public' ); |
49 | if ( $subdir != '' ) { |
50 | $directory .= "/$subdir/"; |
51 | } |
52 | |
53 | if ( $verbose ) { |
54 | $this->output( "Scanning files under $directory:\n" ); |
55 | } |
56 | |
57 | $list = $repo->getBackend()->getFileList( [ 'dir' => $directory ] ); |
58 | if ( $list === null ) { |
59 | $this->fatalError( "Could not get file listing." ); |
60 | } |
61 | |
62 | $pathBatch = []; |
63 | foreach ( $list as $path ) { |
64 | if ( preg_match( '#^(thumb|deleted)/#', $path ) ) { |
65 | continue; // handle ugly nested containers on stock installs |
66 | } |
67 | |
68 | $pathBatch[] = $path; |
69 | if ( count( $pathBatch ) >= $this->getBatchSize() ) { |
70 | $this->checkFiles( $repo, $pathBatch, $verbose ); |
71 | $pathBatch = []; |
72 | } |
73 | } |
74 | $this->checkFiles( $repo, $pathBatch, $verbose ); |
75 | } |
76 | |
77 | protected function checkFiles( LocalRepo $repo, array $paths, $verbose ) { |
78 | if ( !count( $paths ) ) { |
79 | return; |
80 | } |
81 | |
82 | $dbr = $repo->getReplicaDB(); |
83 | |
84 | $curNames = []; |
85 | $oldNames = []; |
86 | $imgIN = []; |
87 | $oiWheres = []; |
88 | foreach ( $paths as $path ) { |
89 | $name = basename( $path ); |
90 | if ( preg_match( '#^archive/#', $path ) ) { |
91 | if ( $verbose ) { |
92 | $this->output( "Checking old file $name\n" ); |
93 | } |
94 | |
95 | $oldNames[] = $name; |
96 | [ , $base ] = explode( '!', $name, 2 ); // <TS_MW>!<img_name> |
97 | $oiWheres[] = $dbr->expr( 'oi_name', '=', $base )->and( 'oi_archive_name', '=', $name ); |
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->orExpr( $oiWheres ) : '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 | // @codeCoverageIgnoreStart |
153 | $maintClass = FindOrphanedFiles::class; |
154 | require_once RUN_MAINTENANCE_IF_MAIN; |
155 | // @codeCoverageIgnoreEnd |