Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 50 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
EraseArchivedFile | |
0.00% |
0 / 50 |
|
0.00% |
0 / 4 |
210 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
72 | |||
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\ArchivedFile; |
25 | use MediaWiki\FileRepo\File\FileSelectQueryBuilder; |
26 | use MediaWiki\Maintenance\Maintenance; |
27 | |
28 | // @codeCoverageIgnoreStart |
29 | require_once __DIR__ . '/Maintenance.php'; |
30 | // @codeCoverageIgnoreEnd |
31 | |
32 | /** |
33 | * Maintenance script to delete archived (non-current) files from storage. |
34 | * |
35 | * @todo Maybe add some simple logging |
36 | * |
37 | * @ingroup Maintenance |
38 | * @since 1.22 |
39 | */ |
40 | class EraseArchivedFile extends Maintenance { |
41 | public function __construct() { |
42 | parent::__construct(); |
43 | $this->addDescription( 'Erases traces of deleted files.' ); |
44 | $this->addOption( 'delete', 'Perform the deletion' ); |
45 | $this->addOption( 'filename', 'File name', false, true ); |
46 | $this->addOption( 'filekey', 'File storage key (with extension) or "*"', true, true ); |
47 | } |
48 | |
49 | public function execute() { |
50 | if ( !$this->hasOption( 'delete' ) ) { |
51 | $this->output( "Use --delete to actually confirm this script\n" ); |
52 | } |
53 | |
54 | $filekey = $this->getOption( 'filekey' ); |
55 | $filename = $this->getOption( 'filename' ); |
56 | |
57 | if ( $filekey === '*' ) { |
58 | // all versions by name |
59 | if ( $filename === null || $filename === '' ) { |
60 | $this->fatalError( "Missing --filename parameter." ); |
61 | } |
62 | $afile = false; |
63 | } else { |
64 | // specified version |
65 | $dbw = $this->getPrimaryDB(); |
66 | $queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbw ); |
67 | $queryBuilder->where( [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $filekey ] ); |
68 | $row = $queryBuilder->caller( __METHOD__ )->fetchRow(); |
69 | |
70 | if ( !$row ) { |
71 | $this->fatalError( "No deleted file exists with key '$filekey'." ); |
72 | } |
73 | $filename = $row->fa_name; |
74 | $afile = ArchivedFile::newFromRow( $row ); |
75 | } |
76 | |
77 | $file = $this->getServiceContainer()->getRepoGroup()->getLocalRepo()->newFile( $filename ); |
78 | if ( $file->exists() ) { |
79 | $this->fatalError( "File '$filename' is still a public file, use the delete form.\n" ); |
80 | } |
81 | |
82 | $this->output( "Purging all thumbnails for file '$filename'..." ); |
83 | $file->purgeCache(); |
84 | $this->output( "done.\n" ); |
85 | |
86 | if ( $afile instanceof ArchivedFile ) { |
87 | $this->scrubVersion( $afile ); |
88 | } else { |
89 | $this->output( "Finding deleted versions of file '$filename'...\n" ); |
90 | $this->scrubAllVersions( $filename ); |
91 | $this->output( "Done\n" ); |
92 | } |
93 | } |
94 | |
95 | protected function scrubAllVersions( string $name ) { |
96 | $dbw = $this->getPrimaryDB(); |
97 | $queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbw ); |
98 | $queryBuilder->where( [ 'fa_name' => $name, 'fa_storage_group' => 'deleted' ] ); |
99 | $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
100 | foreach ( $res as $row ) { |
101 | $this->scrubVersion( ArchivedFile::newFromRow( $row ) ); |
102 | } |
103 | } |
104 | |
105 | protected function scrubVersion( ArchivedFile $archivedFile ) { |
106 | $key = $archivedFile->getStorageKey(); |
107 | $name = $archivedFile->getName(); |
108 | $ts = $archivedFile->getTimestamp(); |
109 | $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo(); |
110 | $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key; |
111 | if ( $this->hasOption( 'delete' ) ) { |
112 | $status = $repo->getBackend()->delete( [ 'src' => $path ] ); |
113 | if ( $status->isOK() ) { |
114 | $this->output( "Deleted version '$key' ($ts) of file '$name'\n" ); |
115 | } else { |
116 | $this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" ); |
117 | $this->error( $status ); |
118 | } |
119 | } else { |
120 | $this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" ); |
121 | } |
122 | } |
123 | } |
124 | |
125 | // @codeCoverageIgnoreStart |
126 | $maintClass = EraseArchivedFile::class; |
127 | require_once RUN_MAINTENANCE_IF_MAIN; |
128 | // @codeCoverageIgnoreEnd |