3 * Delete archived (non-current) files from storage
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.
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.
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
21 * @ingroup Maintenance
24 use MediaWiki\MediaWikiServices
;
26 require_once __DIR__
. '/Maintenance.php';
29 * Maintenance script to delete archived (non-current) files from storage.
31 * @todo Maybe add some simple logging
33 * @ingroup Maintenance
36 class EraseArchivedFile
extends Maintenance
{
37 public function __construct() {
38 parent
::__construct();
39 $this->addDescription( 'Erases traces of deleted files.' );
40 $this->addOption( 'delete', 'Perform the deletion' );
41 $this->addOption( 'filename', 'File name', false, true );
42 $this->addOption( 'filekey', 'File storage key (with extension) or "*"', true, true );
45 public function execute() {
46 if ( !$this->hasOption( 'delete' ) ) {
47 $this->output( "Use --delete to actually confirm this script\n" );
50 $filekey = $this->getOption( 'filekey' );
51 $filename = $this->getOption( 'filename' );
53 if ( $filekey === '*' ) { // all versions by name
54 if ( !strlen( $filename ) ) {
55 $this->fatalError( "Missing --filename parameter." );
58 } else { // specified version
59 $dbw = $this->getDB( DB_MASTER
);
60 $fileQuery = ArchivedFile
::getQueryInfo();
61 $row = $dbw->selectRow( $fileQuery['tables'], $fileQuery['fields'],
62 [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $filekey ],
63 __METHOD__
, [], $fileQuery['joins'] );
65 $this->fatalError( "No deleted file exists with key '$filekey'." );
67 $filename = $row->fa_name
;
68 $afile = ArchivedFile
::newFromRow( $row );
71 $file = MediaWikiServices
::getInstance()->getRepoGroup()->getLocalRepo()->newFile( $filename );
72 if ( $file->exists() ) {
73 $this->fatalError( "File '$filename' is still a public file, use the delete form.\n" );
76 $this->output( "Purging all thumbnails for file '$filename'..." );
78 $this->output( "done.\n" );
80 if ( $afile instanceof ArchivedFile
) {
81 $this->scrubVersion( $afile );
83 $this->output( "Finding deleted versions of file '$filename'...\n" );
84 $this->scrubAllVersions( $filename );
85 $this->output( "Done\n" );
89 protected function scrubAllVersions( $name ) {
90 $dbw = $this->getDB( DB_MASTER
);
91 $fileQuery = ArchivedFile
::getQueryInfo();
92 $res = $dbw->select( $fileQuery['tables'], $fileQuery['fields'],
93 [ 'fa_name' => $name, 'fa_storage_group' => 'deleted' ],
94 __METHOD__
, [], $fileQuery['joins'] );
95 foreach ( $res as $row ) {
96 $this->scrubVersion( ArchivedFile
::newFromRow( $row ) );
100 protected function scrubVersion( ArchivedFile
$archivedFile ) {
101 $key = $archivedFile->getStorageKey();
102 $name = $archivedFile->getName();
103 $ts = $archivedFile->getTimestamp();
104 $repo = RepoGroup
::singleton()->getLocalRepo();
105 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
106 if ( $this->hasOption( 'delete' ) ) {
107 $status = $repo->getBackend()->delete( [ 'src' => $path ] );
108 if ( $status->isOK() ) {
109 $this->output( "Deleted version '$key' ($ts) of file '$name'\n" );
111 $this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" );
112 $this->output( print_r( $status->getErrorsArray(), true ) );
115 $this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" );
120 $maintClass = EraseArchivedFile
::class;
121 require_once RUN_MAINTENANCE_IF_MAIN
;