Update git submodules
[mediawiki.git] / maintenance / findMissingFiles.php
blob85fdf40a4fbc2c1b16b07594adedf97f16188b93
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.
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.
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
18 * @file
21 require_once __DIR__ . '/Maintenance.php';
23 class FindMissingFiles extends Maintenance {
24 public function __construct() {
25 parent::__construct();
27 $this->addDescription( 'Find registered files with no corresponding file.' );
28 $this->addOption( 'start', 'Start after this file name', false, true );
29 $this->addOption( 'mtimeafter', 'Only include files changed since this time', false, true );
30 $this->addOption( 'mtimebefore', 'Only includes files changed before this time', false, true );
31 $this->setBatchSize( 300 );
34 public function execute() {
35 $lastName = $this->getOption( 'start', '' );
37 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
38 $dbr = $repo->getReplicaDB();
39 $be = $repo->getBackend();
40 $batchSize = $this->getBatchSize();
42 $mtime1 = $dbr->timestampOrNull( $this->getOption( 'mtimeafter', null ) );
43 $mtime2 = $dbr->timestampOrNull( $this->getOption( 'mtimebefore', null ) );
45 $queryBuilder = $dbr->newSelectQueryBuilder()
46 ->select( [ 'name' => 'img_name' ] )
47 ->from( 'image' )
48 ->where( [ "img_name > " . $dbr->addQuotes( $lastName ) ] )
49 ->groupBy( 'name' )
50 ->orderBy( 'name' )
51 ->limit( $batchSize );
53 if ( $mtime1 || $mtime2 ) {
54 $queryBuilder->join( 'page', null, 'page_title = img_name' );
55 $queryBuilder->andWhere( [ 'page_namespace' => NS_FILE ] );
57 $queryBuilder->join( 'logging', null, 'log_page = page_id' );
58 $queryBuilder->andWhere( [ 'log_type' => [ 'upload', 'move', 'delete' ] ] );
59 if ( $mtime1 ) {
60 $queryBuilder->andWhere( "log_timestamp > {$dbr->addQuotes($mtime1)}" );
62 if ( $mtime2 ) {
63 $queryBuilder->andWhere( "log_timestamp < {$dbr->addQuotes($mtime2)}" );
67 do {
68 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
70 // Check if any of these files are missing...
71 $pathsByName = [];
72 foreach ( $res as $row ) {
73 $file = $repo->newFile( $row->name );
74 $pathsByName[$row->name] = $file->getPath();
75 $lastName = $row->name;
77 $be->preloadFileStat( [ 'srcs' => $pathsByName ] );
78 foreach ( $pathsByName as $path ) {
79 if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
80 $this->output( "$path\n" );
84 // Find all missing old versions of any of the files in this batch...
85 if ( count( $pathsByName ) ) {
86 $ores = $dbr->newSelectQueryBuilder()
87 ->select( [ 'oi_name', 'oi_archive_name' ] )
88 ->from( 'oldimage' )
89 ->where( [ 'oi_name' => array_map( 'strval', array_keys( $pathsByName ) ) ] )
90 ->caller( __METHOD__ )->fetchResultSet();
92 $checkPaths = [];
93 foreach ( $ores as $row ) {
94 if ( !strlen( $row->oi_archive_name ) ) {
95 // broken row
96 continue;
98 $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
99 $checkPaths[] = $file->getPath();
102 foreach ( array_chunk( $checkPaths, $batchSize ) as $paths ) {
103 $be->preloadFileStat( [ 'srcs' => $paths ] );
104 foreach ( $paths as $path ) {
105 if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
106 $this->output( "$path\n" );
111 } while ( $res->numRows() >= $batchSize );
115 $maintClass = FindMissingFiles::class;
116 require_once RUN_MAINTENANCE_IF_MAIN;