Merge "Add error checking for file argument"
[mediawiki.git] / maintenance / findOrphanedFiles.php
blob41650bd0e6774529aa67c945e9d1db06f757f5e3
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 FindOrphanedFiles extends Maintenance {
25 function __construct() {
26 parent::__construct();
28 $this->mDescription = "Find unregistered files in the 'public' repo zone.";
29 $this->addOption( 'subdir',
30 'Only scan files in this subdirectory (e.g. "a/a0")', false, true );
31 $this->addOption( 'verbose', "Mention file paths checked" );
32 $this->setBatchSize( 500 );
35 function execute() {
36 $subdir = $this->getOption( 'subdir', '' );
37 $verbose = $this->hasOption( 'verbose' );
39 $repo = RepoGroup::singleton()->getLocalRepo();
40 if ( $repo->hasSha1Storage() ) {
41 $this->error( "Local repo uses SHA-1 file storage names; aborting.", 1 );
44 $directory = $repo->getZonePath( 'public' );
45 if ( $subdir != '' ) {
46 $directory .= "/$subdir/";
49 if ( $verbose ) {
50 $this->output( "Scanning files under $directory:\n" );
53 $list = $repo->getBackend()->getFileList( array( 'dir' => $directory ) );
54 if ( $list === null ) {
55 $this->error( "Could not get file listing.", 1 );
58 $pathBatch = array();
59 foreach ( $list as $path ) {
60 if ( preg_match( '#^(thumb|deleted)/#', $path ) ) {
61 continue; // handle ugly nested containers on stock installs
64 $pathBatch[] = $path;
65 if ( count( $pathBatch ) >= $this->mBatchSize ) {
66 $this->checkFiles( $repo, $pathBatch, $verbose );
67 $pathBatch = array();
70 $this->checkFiles( $repo, $pathBatch, $verbose );
73 protected function checkFiles( LocalRepo $repo, array $paths, $verbose ) {
74 if ( !count( $paths ) ) {
75 return;
78 $dbr = $repo->getSlaveDB();
80 $curNames = array();
81 $oldNames = array();
82 $imgIN = array();
83 $oiWheres = array();
84 foreach ( $paths as $path ) {
85 $name = basename( $path );
86 if ( preg_match( '#^archive/#', $path ) ) {
87 if ( $verbose ) {
88 $this->output( "Checking old file $name\n" );
91 $oldNames[] = $name;
92 list( , $base ) = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
93 $oiWheres[] = $dbr->makeList(
94 array( 'oi_name' => $base, 'oi_archive_name' => $name ),
95 LIST_AND
97 } else {
98 if ( $verbose ) {
99 $this->output( "Checking current file $name\n" );
102 $curNames[] = $name;
103 $imgIN[] = $name;
107 $res = $dbr->query(
108 $dbr->unionQueries(
109 array(
110 $dbr->selectSQLText(
111 'image',
112 array( 'name' => 'img_name', 'old' => 0 ),
113 $imgIN ? array( 'img_name' => $imgIN ) : '1=0'
115 $dbr->selectSQLText(
116 'oldimage',
117 array( 'name' => 'oi_archive_name', 'old' => 1 ),
118 $oiWheres ? $dbr->makeList( $oiWheres, LIST_OR ) : '1=0'
121 true // UNION ALL (performance)
123 __METHOD__
126 $curNamesFound = array();
127 $oldNamesFound = array();
128 foreach ( $res as $row ) {
129 if ( $row->old ) {
130 $oldNamesFound[] = $row->name;
131 } else {
132 $curNamesFound[] = $row->name;
136 foreach ( array_diff( $curNames, $curNamesFound ) as $name ) {
137 $file = $repo->newFile( $name );
138 // Print name and public URL to ease recovery
139 if ( $file ) {
140 $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
141 } else {
142 $this->error( "Cannot get URL for bad file title '$name'" );
146 foreach ( array_diff( $oldNames, $oldNamesFound ) as $name ) {
147 list( , $base ) = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
148 $file = $repo->newFromArchiveName( Title::makeTitle( NS_FILE, $base ), $name );
149 // Print name and public URL to ease recovery
150 $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
155 $maintClass = 'FindOrphanedFiles';
156 require_once RUN_MAINTENANCE_IF_MAIN;