Merge "Turn HTMLBlockedUsersItemSelect into HTMLSelectLimitField"
[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->setBatchSize( 200 );
31 }
32
33 function execute() {
34 $lastName = $this->getOption( 'start', '' );
35
36 $repo = RepoGroup::singleton()->getLocalRepo();
37 $dbr = $repo->getSlaveDB();
38 $be = $repo->getBackend();
39
40 do {
41 $res = $dbr->select( 'image',
42 'img_name',
43 array( "img_name >= " . $dbr->addQuotes( $lastName ) ),
44 __METHOD__,
45 array( 'ORDER BY' => 'img_name', 'LIMIT' => $this->mBatchSize )
46 );
47
48 // Check if any of these files are missing...
49 $pathsByName = array();
50 foreach ( $res as $row ) {
51 $file = $repo->newFile( $row->img_name );
52 $pathsByName[$row->img_name] = $file->getPath();
53 $lastName = $row->img_name;
54 }
55 $be->preloadFileStat( array( 'srcs' => $pathsByName ) );
56 foreach ( $pathsByName as $path ) {
57 if ( $be->fileExists( array( 'src' => $path ) ) === false ) {
58 $this->output( "$path\n" );
59 }
60 }
61
62 // Find all missing old versions of any of the files in this batch...
63 if ( count( $pathsByName ) ) {
64 $ores = $dbr->select( 'oldimage',
65 array( 'oi_name', 'oi_archive_name' ),
66 array( 'oi_name' => array_keys( $pathsByName ) ),
67 __METHOD__
68 );
69 foreach ( $ores as $row ) {
70 if ( !strlen( $row->oi_archive_name ) ) {
71 continue; // broken row
72 }
73 $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
74 $path = $file->getPath();
75 if ( $be->fileExists( array( 'src' => $path ) ) === false ) {
76 $this->output( "$path\n" );
77 }
78 }
79 }
80 } while ( $res->numRows() >= $this->mBatchSize );
81 }
82 }
83
84 $maintClass = 'FindMissingFiles';
85 require_once RUN_MAINTENANCE_IF_MAIN;