Merge "mediawiki.jqueryMsg: Don't duplicate link contents if parse() is called multip...
[lhc/web/wiklou.git] / maintenance / findOrphanedFiles.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 FindOrphanedFiles extends Maintenance {
25 function __construct() {
26 parent::__construct();
27
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 );
33 }
34
35 function execute() {
36 $subdir = $this->getOption( 'subdir', '' );
37 $verbose = $this->hasOption( 'verbose' );
38
39 $repo = RepoGroup::singleton()->getLocalRepo();
40 if ( $repo->hasSha1Storage() ) {
41 $this->error( "Local repo uses SHA-1 file storage names; aborting.", 1 );
42 }
43
44 $directory = $repo->getZonePath( 'public' );
45 if ( $subdir != '' ) {
46 $directory .= "/$subdir/";
47 }
48
49 if ( $verbose ) {
50 $this->output( "Scanning files under $directory:\n" );
51 }
52
53 $list = $repo->getBackend()->getFileList( array( 'dir' => $directory ) );
54 if ( $list === null ) {
55 $this->error( "Could not get file listing.", 1 );
56 }
57
58 $nameBatch = array();
59 foreach ( $list as $path ) {
60 if ( preg_match( '#^(thumb|deleted)/#', $path ) ) {
61 continue; // handle ugly nested containers on stock installs
62 }
63
64 $nameBatch[] = basename( $path );
65 if ( count( $nameBatch ) >= $this->mBatchSize ) {
66 $this->checkFiles( $repo, $nameBatch, $verbose );
67 $nameBatch = array();
68 }
69 }
70 $this->checkFiles( $repo, $nameBatch, $verbose );
71 }
72
73 protected function checkFiles( LocalRepo $repo, array $names, $verbose ) {
74 if ( !count( $names ) ) {
75 return;
76 }
77
78 $dbr = $repo->getSlaveDB();
79
80 $imgIN = array();
81 $oiWheres = array();
82 foreach ( $names as $name ) {
83 if ( strpos( $name, '!' ) !== false ) {
84 if ( $verbose ) {
85 $this->output( "Checking old file $name\n" );
86 }
87
88 list( , $base ) = explode( '!', $name ); // <TS_MW>!<img_name>
89 $oiWheres[] = $dbr->makeList(
90 array( 'oi_name' => $base, 'oi_archive_name' => $name ),
91 LIST_AND
92 );
93 } else {
94 if ( $verbose ) {
95 $this->output( "Checking current file $name\n" );
96 }
97
98 $imgIN[] = $name;
99 }
100 }
101
102 $res = $dbr->query(
103 $dbr->unionQueries(
104 array(
105 $dbr->selectSQLText(
106 'image',
107 array( 'name' => 'img_name' ),
108 $imgIN ? array( 'img_name' => $imgIN ) : '1=0'
109 ),
110 $dbr->selectSQLText(
111 'oldimage',
112 array( 'name' => 'oi_archive_name' ),
113 $oiWheres ? $dbr->makeList( $oiWheres, LIST_OR ) : '1=0'
114 )
115 ),
116 true // UNION ALL (performance)
117 ),
118 __METHOD__
119 );
120
121 $namesFound = array();
122 foreach ( $res as $row ) {
123 $namesFound[] = $row->name;
124 }
125
126 $namesOrphans = array_diff( $names, $namesFound );
127 foreach ( $namesOrphans as $name ) {
128 // Print name and public URL to ease recovery
129 if ( strpos( $name, '!' ) !== false ) {
130 list( , $base ) = explode( '!', $name ); // <TS_MW>!<img_name>
131 $file = $repo->newFromArchiveName( Title::makeTitle( NS_FILE, $base ), $name );
132 } else {
133 $file = $repo->newFile( $name );
134 }
135 $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
136 }
137 }
138 }
139
140 $maintClass = 'FindOrphanedFiles';
141 require_once RUN_MAINTENANCE_IF_MAIN;