Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 57 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
FindMissingFiles | |
0.00% |
0 / 57 |
|
0.00% |
0 / 2 |
240 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 51 |
|
0.00% |
0 / 1 |
210 |
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 | // @codeCoverageIgnoreStart |
22 | require_once __DIR__ . '/Maintenance.php'; |
23 | // @codeCoverageIgnoreEnd |
24 | |
25 | class FindMissingFiles extends Maintenance { |
26 | public function __construct() { |
27 | parent::__construct(); |
28 | |
29 | $this->addDescription( 'Find registered files with no corresponding file.' ); |
30 | $this->addOption( 'start', 'Start after this file name', false, true ); |
31 | $this->addOption( 'mtimeafter', 'Only include files changed since this time', false, true ); |
32 | $this->addOption( 'mtimebefore', 'Only includes files changed before this time', false, true ); |
33 | $this->setBatchSize( 300 ); |
34 | } |
35 | |
36 | public function execute() { |
37 | $lastName = $this->getOption( 'start', '' ); |
38 | |
39 | $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo(); |
40 | $dbr = $repo->getReplicaDB(); |
41 | $be = $repo->getBackend(); |
42 | $batchSize = $this->getBatchSize(); |
43 | |
44 | $mtime1 = $dbr->timestampOrNull( $this->getOption( 'mtimeafter', null ) ); |
45 | $mtime2 = $dbr->timestampOrNull( $this->getOption( 'mtimebefore', null ) ); |
46 | |
47 | $queryBuilder = $dbr->newSelectQueryBuilder() |
48 | ->select( [ 'name' => 'img_name' ] ) |
49 | ->from( 'image' ) |
50 | ->where( $dbr->expr( 'img_name', '>', $lastName ) ) |
51 | ->groupBy( 'name' ) |
52 | ->orderBy( 'name' ) |
53 | ->limit( $batchSize ); |
54 | |
55 | if ( $mtime1 || $mtime2 ) { |
56 | $queryBuilder->join( 'page', null, 'page_title = img_name' ); |
57 | $queryBuilder->andWhere( [ 'page_namespace' => NS_FILE ] ); |
58 | |
59 | $queryBuilder->join( 'logging', null, 'log_page = page_id' ); |
60 | $queryBuilder->andWhere( [ 'log_type' => [ 'upload', 'move', 'delete' ] ] ); |
61 | if ( $mtime1 ) { |
62 | $queryBuilder->andWhere( $dbr->expr( 'log_timestamp', '>', $mtime1 ) ); |
63 | } |
64 | if ( $mtime2 ) { |
65 | $queryBuilder->andWhere( $dbr->expr( 'log_timestamp', '<', $mtime2 ) ); |
66 | } |
67 | } |
68 | |
69 | do { |
70 | $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
71 | |
72 | // Check if any of these files are missing... |
73 | $pathsByName = []; |
74 | foreach ( $res as $row ) { |
75 | $file = $repo->newFile( $row->name ); |
76 | $pathsByName[$row->name] = $file->getPath(); |
77 | $lastName = $row->name; |
78 | } |
79 | $be->preloadFileStat( [ 'srcs' => $pathsByName ] ); |
80 | foreach ( $pathsByName as $path ) { |
81 | if ( $be->fileExists( [ 'src' => $path ] ) === false ) { |
82 | $this->output( "$path\n" ); |
83 | } |
84 | } |
85 | |
86 | // Find all missing old versions of any of the files in this batch... |
87 | if ( count( $pathsByName ) ) { |
88 | $ores = $dbr->newSelectQueryBuilder() |
89 | ->select( [ 'oi_name', 'oi_archive_name' ] ) |
90 | ->from( 'oldimage' ) |
91 | ->where( [ 'oi_name' => array_map( 'strval', array_keys( $pathsByName ) ) ] ) |
92 | ->caller( __METHOD__ )->fetchResultSet(); |
93 | |
94 | $checkPaths = []; |
95 | foreach ( $ores as $row ) { |
96 | if ( !strlen( $row->oi_archive_name ) ) { |
97 | // broken row |
98 | continue; |
99 | } |
100 | $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name ); |
101 | $checkPaths[] = $file->getPath(); |
102 | } |
103 | |
104 | foreach ( array_chunk( $checkPaths, $batchSize ) as $paths ) { |
105 | $be->preloadFileStat( [ 'srcs' => $paths ] ); |
106 | foreach ( $paths as $path ) { |
107 | if ( $be->fileExists( [ 'src' => $path ] ) === false ) { |
108 | $this->output( "$path\n" ); |
109 | } |
110 | } |
111 | } |
112 | } |
113 | } while ( $res->numRows() >= $batchSize ); |
114 | } |
115 | } |
116 | |
117 | // @codeCoverageIgnoreStart |
118 | $maintClass = FindMissingFiles::class; |
119 | require_once RUN_MAINTENANCE_IF_MAIN; |
120 | // @codeCoverageIgnoreEnd |