Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / maintenance / eraseArchivedFile.php
blob1ddc0f509776f39187e963163e1ac7e5bcce233a
1 <?php
2 /**
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
20 * @file
21 * @ingroup Maintenance
22 * @author Aaron Schulz
25 require_once __DIR__ . '/Maintenance.php';
27 /**
28 * Maintenance script to delete archived (non-current) files from storage.
30 * @todo Maybe add some simple logging
32 * @ingroup Maintenance
33 * @since 1.22
35 class EraseArchivedFile extends Maintenance {
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Erases traces of deleted files.' );
39 $this->addOption( 'delete', 'Perform the deletion' );
40 $this->addOption( 'filename', 'File name', false, true );
41 $this->addOption( 'filekey', 'File storage key (with extension) or "*"', true, true );
44 public function execute() {
45 if ( !$this->hasOption( 'delete' ) ) {
46 $this->output( "Use --delete to actually confirm this script\n" );
49 $filekey = $this->getOption( 'filekey' );
50 $filename = $this->getOption( 'filename' );
52 if ( $filekey === '*' ) { // all versions by name
53 if ( !strlen( $filename ) ) {
54 $this->error( "Missing --filename parameter.", 1 );
56 $afile = false;
57 } else { // specified version
58 $dbw = $this->getDB( DB_MASTER );
59 $row = $dbw->selectRow( 'filearchive', '*',
60 [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $filekey ],
61 __METHOD__ );
62 if ( !$row ) {
63 $this->error( "No deleted file exists with key '$filekey'.", 1 );
65 $filename = $row->fa_name;
66 $afile = ArchivedFile::newFromRow( $row );
69 $file = wfLocalFile( $filename );
70 if ( $file->exists() ) {
71 $this->error( "File '$filename' is still a public file, use the delete form.\n", 1 );
74 $this->output( "Purging all thumbnails for file '$filename'..." );
75 $file->purgeCache();
76 $this->output( "done.\n" );
78 if ( $afile instanceof ArchivedFile ) {
79 $this->scrubVersion( $afile );
80 } else {
81 $this->output( "Finding deleted versions of file '$filename'...\n" );
82 $this->scrubAllVersions( $filename );
83 $this->output( "Done\n" );
87 protected function scrubAllVersions( $name ) {
88 $dbw = $this->getDB( DB_MASTER );
89 $res = $dbw->select( 'filearchive', '*',
90 [ 'fa_name' => $name, 'fa_storage_group' => 'deleted' ],
91 __METHOD__ );
92 foreach ( $res as $row ) {
93 $this->scrubVersion( ArchivedFile::newFromRow( $row ) );
97 protected function scrubVersion( ArchivedFile $archivedFile ) {
98 $key = $archivedFile->getStorageKey();
99 $name = $archivedFile->getName();
100 $ts = $archivedFile->getTimestamp();
101 $repo = RepoGroup::singleton()->getLocalRepo();
102 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
103 if ( $this->hasOption( 'delete' ) ) {
104 $status = $repo->getBackend()->delete( [ 'src' => $path ] );
105 if ( $status->isOK() ) {
106 $this->output( "Deleted version '$key' ($ts) of file '$name'\n" );
107 } else {
108 $this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" );
109 $this->output( print_r( $status->getErrorsArray(), true ) );
111 } else {
112 $this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" );
117 $maintClass = "EraseArchivedFile";
118 require_once RUN_MAINTENANCE_IF_MAIN;