Merge "Fix styling of deletion page when $wgUseMediaWikiUIEverywhere enabled"
[lhc/web/wiklou.git] / maintenance / findMissingFiles.php
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 * @author Aaron Schulz
20 */
21
22 require_once __DIR__ . '/Maintenance.php';
23
24 class FindMissingFiles extends Maintenance {
25 function __construct() {
26 parent::__construct();
27
28 $this->mDescription = 'Find registered files with no corresponding file.';
29 $this->addOption( 'start', 'Starting 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 );
33 }
34
35 function execute() {
36 $lastName = $this->getOption( 'start', '' );
37
38 $repo = RepoGroup::singleton()->getLocalRepo();
39 $dbr = $repo->getSlaveDB();
40 $be = $repo->getBackend();
41
42 $mtime1 = $dbr->timestampOrNull( $this->getOption( 'mtimeafter', null ) );
43 $mtime2 = $dbr->timestampOrNull( $this->getOption( 'mtimebefore', null ) );
44
45 $joinTables = array( 'image' );
46 $joinConds = array( 'image' => array( 'INNER JOIN', 'img_name = page_title' ) );
47 if ( $mtime1 || $mtime2 ) {
48 $joinTables[] = 'logging';
49 $on = array( 'log_page = page_id', 'log_type' => array( 'upload', 'move', 'delete' ) );
50 if ( $mtime1 ) {
51 $on[] = "log_timestamp > {$dbr->addQuotes($mtime1)}";
52 }
53 if ( $mtime2 ) {
54 $on[] = "log_timestamp < {$dbr->addQuotes($mtime2)}";
55 }
56 $joinConds['logging'] = array( 'INNER JOIN', $on );
57 }
58
59 do {
60 $res = $dbr->select(
61 array_merge( array( 'page' ), $joinTables ),
62 array( 'name' => 'img_name' ),
63 array( 'page_namespace' => NS_FILE,
64 "page_title >= " . $dbr->addQuotes( $lastName ) ),
65 __METHOD__,
66 // DISTINCT causes a pointless filesort
67 array( 'ORDER BY' => 'name', 'GROUP BY' => 'name',
68 'LIMIT' => $this->mBatchSize ),
69 $joinConds
70 );
71
72 // Check if any of these files are missing...
73 $pathsByName = array();
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( array( 'srcs' => $pathsByName ) );
80 foreach ( $pathsByName as $path ) {
81 if ( $be->fileExists( array( '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->select( 'oldimage',
89 array( 'oi_name', 'oi_archive_name' ),
90 array( 'oi_name' => array_keys( $pathsByName ) ),
91 __METHOD__
92 );
93
94 $checkPaths = array();
95 foreach ( $ores as $row ) {
96 if ( !strlen( $row->oi_archive_name ) ) {
97 continue; // broken row
98 }
99 $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
100 $checkPaths[] = $file->getPath();
101 }
102
103 foreach ( array_chunk( $checkPaths, $this->mBatchSize ) as $paths ) {
104 $be->preloadFileStat( array( 'srcs' => $paths ) );
105 foreach ( $paths as $path ) {
106 if ( $be->fileExists( array( 'src' => $path ) ) === false ) {
107 $this->output( "$path\n" );
108 }
109 }
110 }
111 }
112 } while ( $res->numRows() >= $this->mBatchSize );
113 }
114 }
115
116 $maintClass = 'FindMissingFiles';
117 require_once RUN_MAINTENANCE_IF_MAIN;