Merge "Reset Title cache when importing titles."
[mediawiki.git] / maintenance / findMissingFiles.php
blob5818ee25c4e7d8a8c755ff423994d740bd09680b
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
19 * @author Aaron Schulz
22 require_once __DIR__ . '/Maintenance.php';
24 class FindMissingFiles extends Maintenance {
25 function __construct() {
26 parent::__construct();
28 $this->mDescription = 'Find registered files with no corresponding file.';
29 $this->addOption( 'start', 'Start after this file name', false, true );
30 $this->addOption( 'mtimeafter', 'Only include files changed since this time', false, true );
31 $this->addOption( 'mtimebefore', 'Only includes files changed before this time', false, true );
32 $this->setBatchSize( 300 );
35 function execute() {
36 $lastName = $this->getOption( 'start', '' );
38 $repo = RepoGroup::singleton()->getLocalRepo();
39 $dbr = $repo->getSlaveDB();
40 $be = $repo->getBackend();
42 $mtime1 = $dbr->timestampOrNull( $this->getOption( 'mtimeafter', null ) );
43 $mtime2 = $dbr->timestampOrNull( $this->getOption( 'mtimebefore', null ) );
45 $joinTables = array();
46 $joinConds = array();
47 if ( $mtime1 || $mtime2 ) {
48 $joinTables[] = 'page';
49 $joinConds['page'] = array( 'INNER JOIN',
50 array( 'page_title = img_name', 'page_namespace' => NS_FILE ) );
51 $joinTables[] = 'logging';
52 $on = array( 'log_page = page_id', 'log_type' => array( 'upload', 'move', 'delete' ) );
53 if ( $mtime1 ) {
54 $on[] = "log_timestamp > {$dbr->addQuotes($mtime1)}";
56 if ( $mtime2 ) {
57 $on[] = "log_timestamp < {$dbr->addQuotes($mtime2)}";
59 $joinConds['logging'] = array( 'INNER JOIN', $on );
62 do {
63 $res = $dbr->select(
64 array_merge( array( 'image' ), $joinTables ),
65 array( 'name' => 'img_name' ),
66 array( "img_name > " . $dbr->addQuotes( $lastName ) ),
67 __METHOD__,
68 // DISTINCT causes a pointless filesort
69 array( 'ORDER BY' => 'name', 'GROUP BY' => 'name',
70 'LIMIT' => $this->mBatchSize ),
71 $joinConds
74 // Check if any of these files are missing...
75 $pathsByName = array();
76 foreach ( $res as $row ) {
77 $file = $repo->newFile( $row->name );
78 $pathsByName[$row->name] = $file->getPath();
79 $lastName = $row->name;
81 $be->preloadFileStat( array( 'srcs' => $pathsByName ) );
82 foreach ( $pathsByName as $path ) {
83 if ( $be->fileExists( array( 'src' => $path ) ) === false ) {
84 $this->output( "$path\n" );
88 // Find all missing old versions of any of the files in this batch...
89 if ( count( $pathsByName ) ) {
90 $ores = $dbr->select( 'oldimage',
91 array( 'oi_name', 'oi_archive_name' ),
92 array( 'oi_name' => array_keys( $pathsByName ) ),
93 __METHOD__
96 $checkPaths = array();
97 foreach ( $ores as $row ) {
98 if ( !strlen( $row->oi_archive_name ) ) {
99 continue; // broken row
101 $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
102 $checkPaths[] = $file->getPath();
105 foreach ( array_chunk( $checkPaths, $this->mBatchSize ) as $paths ) {
106 $be->preloadFileStat( array( 'srcs' => $paths ) );
107 foreach ( $paths as $path ) {
108 if ( $be->fileExists( array( 'src' => $path ) ) === false ) {
109 $this->output( "$path\n" );
114 } while ( $res->numRows() >= $this->mBatchSize );
118 $maintClass = 'FindMissingFiles';
119 require_once RUN_MAINTENANCE_IF_MAIN;