Debian Codegraph @

maintenance/findMissingFiles.php

raw symbols SCIP
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 ->groupBy( 'name' )
51 ->orderBy( 'name' )
52 ->limit( $batchSize );
53
54 if ( $mtime1 || $mtime2 ) {
55 $queryBuilder->join( 'page', null, 'page_title = img_name' );
56 $queryBuilder->andWhere( [ 'page_namespace' => NS_FILE ] );
57
58 $queryBuilder->join( 'logging', null, 'log_page = page_id' );
59 $queryBuilder->andWhere( [ 'log_type' => [ 'upload', 'move', 'delete' ] ] );
60 if ( $mtime1 ) {
61 $queryBuilder->andWhere( $dbr->expr( 'log_timestamp', '>', $mtime1 ) );
62 }
63 if ( $mtime2 ) {
64 $queryBuilder->andWhere( $dbr->expr( 'log_timestamp', '<', $mtime2 ) );
65 }
66 }
67
68 do {
69 $res = ( clone $queryBuilder )
70 ->where( $dbr->expr( 'img_name', '>', $lastName ) )
71 ->caller( __METHOD__ )->fetchResultSet();
72
73 // Check if any of these files are missing...
74 $pathsByName = [];
75 foreach ( $res as $row ) {
76 $file = $repo->newFile( $row->name );
77 $pathsByName[$row->name] = $file->getPath();
78 $lastName = $row->name;
79 }
80 $be->preloadFileStat( [ 'srcs' => $pathsByName ] );
81 foreach ( $pathsByName as $path ) {
82 if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
83 $this->output( "$path\n" );
84 }
85 }
86
87 // Find all missing old versions of any of the files in this batch...
88 if ( count( $pathsByName ) ) {
89 $ores = $dbr->newSelectQueryBuilder()
90 ->select( [ 'oi_name', 'oi_archive_name' ] )
91 ->from( 'oldimage' )
92 ->where( [ 'oi_name' => array_map( 'strval', array_keys( $pathsByName ) ) ] )
93 ->caller( __METHOD__ )->fetchResultSet();
94
95 $checkPaths = [];
96 foreach ( $ores as $row ) {
97 if ( !strlen( $row->oi_archive_name ) ) {
98 // broken row
99 continue;
100 }
101 $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
102 $checkPaths[] = $file->getPath();
103 }
104
105 foreach ( array_chunk( $checkPaths, $batchSize ) 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() >= $batchSize );
115 }
116 }
117
118 // @codeCoverageIgnoreStart
119 $maintClass = FindMissingFiles::class;
120 require_once RUN_MAINTENANCE_IF_MAIN;
121 // @codeCoverageIgnoreEnd