Debian Codegraph @

maintenance/findOrphanedFiles.php

raw symbols SCIP
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 */
20
21 use MediaWiki\Title\Title;
22
23 // @codeCoverageIgnoreStart
24 require_once __DIR__ . '/Maintenance.php';
25 // @codeCoverageIgnoreEnd
26
27 class FindOrphanedFiles extends Maintenance {
28 public function __construct() {
29 parent::__construct();
30
31 $this->addDescription( "Find unregistered files in the 'public' repo zone." );
32 $this->addOption( 'subdir',
33 'Only scan files in this subdirectory (e.g. "a/a0")', false, true );
34 $this->addOption( 'verbose', "Mention file paths checked" );
35 $this->setBatchSize( 500 );
36 }
37
38 public function execute() {
39 $subdir = $this->getOption( 'subdir', '' );
40 $verbose = $this->hasOption( 'verbose' );
41
42 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
43 if ( $repo->hasSha1Storage() ) {
44 $this->fatalError( "Local repo uses SHA-1 file storage names; aborting." );
45 }
46
47 $directory = $repo->getZonePath( 'public' );
48 if ( $subdir != '' ) {
49 $directory .= "/$subdir/";
50 }
51
52 if ( $verbose ) {
53 $this->output( "Scanning files under $directory:\n" );
54 }
55
56 $list = $repo->getBackend()->getFileList( [ 'dir' => $directory ] );
57 if ( $list === null ) {
58 $this->fatalError( "Could not get file listing." );
59 }
60
61 $pathBatch = [];
62 foreach ( $list as $path ) {
63 if ( preg_match( '#^(thumb|deleted)/#', $path ) ) {
64 continue; // handle ugly nested containers on stock installs
65 }
66
67 $pathBatch[] = $path;
68 if ( count( $pathBatch ) >= $this->getBatchSize() ) {
69 $this->checkFiles( $repo, $pathBatch, $verbose );
70 $pathBatch = [];
71 }
72 }
73 $this->checkFiles( $repo, $pathBatch, $verbose );
74 }
75
76 protected function checkFiles( LocalRepo $repo, array $paths, $verbose ) {
77 if ( !count( $paths ) ) {
78 return;
79 }
80
81 $dbr = $repo->getReplicaDB();
82
83 $curNames = [];
84 $oldNames = [];
85 $imgIN = [];
86 $oiWheres = [];
87 foreach ( $paths as $path ) {
88 $name = basename( $path );
89 if ( preg_match( '#^archive/#', $path ) ) {
90 if ( $verbose ) {
91 $this->output( "Checking old file $name\n" );
92 }
93
94 $oldNames[] = $name;
95 [ , $base ] = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
96 $oiWheres[] = $dbr->expr( 'oi_name', '=', $base )->and( 'oi_archive_name', '=', $name );
97 } else {
98 if ( $verbose ) {
99 $this->output( "Checking current file $name\n" );
100 }
101
102 $curNames[] = $name;
103 $imgIN[] = $name;
104 }
105 }
106 $uqb = $dbr->newUnionQueryBuilder();
107 $uqb->add(
108 $dbr->newSelectQueryBuilder()
109 ->select( [ 'name' => 'img_name', 'old' => '0' ] )
110 ->from( 'image' )
111 ->where( $imgIN ? [ 'img_name' => $imgIN ] : '1=0' )
112 );
113 $uqb->add(
114 $dbr->newSelectQueryBuilder()
115 ->select( [ 'name' => 'oi_archive_name', 'old' => '1' ] )
116 ->from( 'oldimage' )
117 ->where( $oiWheres ? $dbr->orExpr( $oiWheres ) : '1=0' )
118 );
119
120 $res = $uqb->all()->caller( __METHOD__ )->fetchResultSet();
121
122 $curNamesFound = [];
123 $oldNamesFound = [];
124 foreach ( $res as $row ) {
125 if ( $row->old ) {
126 $oldNamesFound[] = $row->name;
127 } else {
128 $curNamesFound[] = $row->name;
129 }
130 }
131
132 foreach ( array_diff( $curNames, $curNamesFound ) as $name ) {
133 $file = $repo->newFile( $name );
134 // Print name and public URL to ease recovery
135 if ( $file ) {
136 $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
137 } else {
138 $this->error( "Cannot get URL for bad file title '$name'" );
139 }
140 }
141
142 foreach ( array_diff( $oldNames, $oldNamesFound ) as $name ) {
143 [ , $base ] = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
144 $file = $repo->newFromArchiveName( Title::makeTitle( NS_FILE, $base ), $name );
145 // Print name and public URL to ease recovery
146 $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
147 }
148 }
149 }
150
151 // @codeCoverageIgnoreStart
152 $maintClass = FindOrphanedFiles::class;
153 require_once RUN_MAINTENANCE_IF_MAIN;
154 // @codeCoverageIgnoreEnd