Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 53 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
EraseArchivedFile | |
0.00% |
0 / 50 |
|
0.00% |
0 / 4 |
182 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
56 | |||
scrubAllVersions | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
scrubVersion | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | /** |
3 | * Delete archived (non-current) files from storage |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | * @ingroup Maintenance |
22 | */ |
23 | |
24 | use MediaWiki\FileRepo\File\FileSelectQueryBuilder; |
25 | use MediaWiki\Status\Status; |
26 | |
27 | require_once __DIR__ . '/Maintenance.php'; |
28 | |
29 | /** |
30 | * Maintenance script to delete archived (non-current) files from storage. |
31 | * |
32 | * @todo Maybe add some simple logging |
33 | * |
34 | * @ingroup Maintenance |
35 | * @since 1.22 |
36 | */ |
37 | class EraseArchivedFile extends Maintenance { |
38 | public function __construct() { |
39 | parent::__construct(); |
40 | $this->addDescription( 'Erases traces of deleted files.' ); |
41 | $this->addOption( 'delete', 'Perform the deletion' ); |
42 | $this->addOption( 'filename', 'File name', false, true ); |
43 | $this->addOption( 'filekey', 'File storage key (with extension) or "*"', true, true ); |
44 | } |
45 | |
46 | public function execute() { |
47 | if ( !$this->hasOption( 'delete' ) ) { |
48 | $this->output( "Use --delete to actually confirm this script\n" ); |
49 | } |
50 | |
51 | $filekey = $this->getOption( 'filekey' ); |
52 | $filename = $this->getOption( 'filename' ); |
53 | |
54 | if ( $filekey === '*' ) { |
55 | // all versions by name |
56 | if ( !strlen( $filename ) ) { |
57 | $this->fatalError( "Missing --filename parameter." ); |
58 | } |
59 | $afile = false; |
60 | } else { |
61 | // specified version |
62 | $dbw = $this->getPrimaryDB(); |
63 | $queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbw ); |
64 | $queryBuilder->where( [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $filekey ] ); |
65 | $row = $queryBuilder->caller( __METHOD__ )->fetchRow(); |
66 | |
67 | if ( !$row ) { |
68 | $this->fatalError( "No deleted file exists with key '$filekey'." ); |
69 | } |
70 | $filename = $row->fa_name; |
71 | $afile = ArchivedFile::newFromRow( $row ); |
72 | } |
73 | |
74 | $file = $this->getServiceContainer()->getRepoGroup()->getLocalRepo()->newFile( $filename ); |
75 | if ( $file->exists() ) { |
76 | $this->fatalError( "File '$filename' is still a public file, use the delete form.\n" ); |
77 | } |
78 | |
79 | $this->output( "Purging all thumbnails for file '$filename'..." ); |
80 | $file->purgeCache(); |
81 | $this->output( "done.\n" ); |
82 | |
83 | if ( $afile instanceof ArchivedFile ) { |
84 | $this->scrubVersion( $afile ); |
85 | } else { |
86 | $this->output( "Finding deleted versions of file '$filename'...\n" ); |
87 | $this->scrubAllVersions( $filename ); |
88 | $this->output( "Done\n" ); |
89 | } |
90 | } |
91 | |
92 | protected function scrubAllVersions( $name ) { |
93 | $dbw = $this->getPrimaryDB(); |
94 | $queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbw ); |
95 | $queryBuilder->where( [ 'fa_name' => $name, 'fa_storage_group' => 'deleted' ] ); |
96 | $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
97 | foreach ( $res as $row ) { |
98 | $this->scrubVersion( ArchivedFile::newFromRow( $row ) ); |
99 | } |
100 | } |
101 | |
102 | protected function scrubVersion( ArchivedFile $archivedFile ) { |
103 | $key = $archivedFile->getStorageKey(); |
104 | $name = $archivedFile->getName(); |
105 | $ts = $archivedFile->getTimestamp(); |
106 | $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo(); |
107 | $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key; |
108 | if ( $this->hasOption( 'delete' ) ) { |
109 | $status = $repo->getBackend()->delete( [ 'src' => $path ] ); |
110 | if ( $status->isOK() ) { |
111 | $this->output( "Deleted version '$key' ($ts) of file '$name'\n" ); |
112 | } else { |
113 | $this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" ); |
114 | $this->output( print_r( Status::wrap( $status )->getErrorsArray(), true ) ); |
115 | } |
116 | } else { |
117 | $this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" ); |
118 | } |
119 | } |
120 | } |
121 | |
122 | $maintClass = EraseArchivedFile::class; |
123 | require_once RUN_MAINTENANCE_IF_MAIN; |