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 require_once __DIR__
. '/Maintenance.php';
27 * Maintenance script to delete archived (non-current) files from storage.
29 * @todo Maybe add some simple logging
31 * @ingroup Maintenance
34 class EraseArchivedFile
extends Maintenance
{
35 public function __construct() {
36 parent
::__construct();
37 $this->addDescription( 'Erases traces of deleted files.' );
38 $this->addOption( 'delete', 'Perform the deletion' );
39 $this->addOption( 'filename', 'File name', false, true );
40 $this->addOption( 'filekey', 'File storage key (with extension) or "*"', true, true );
43 public function execute() {
44 if ( !$this->hasOption( 'delete' ) ) {
45 $this->output( "Use --delete to actually confirm this script\n" );
48 $filekey = $this->getOption( 'filekey' );
49 $filename = $this->getOption( 'filename' );
51 if ( $filekey === '*' ) { // all versions by name
52 if ( !strlen( $filename ) ) {
53 $this->error( "Missing --filename parameter.", 1 );
56 } else { // specified version
57 $dbw = $this->getDB( DB_MASTER
);
58 $row = $dbw->selectRow( 'filearchive', '*',
59 [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $filekey ],
62 $this->error( "No deleted file exists with key '$filekey'.", 1 );
64 $filename = $row->fa_name
;
65 $afile = ArchivedFile
::newFromRow( $row );
68 $file = wfLocalFile( $filename );
69 if ( $file->exists() ) {
70 $this->error( "File '$filename' is still a public file, use the delete form.\n", 1 );
73 $this->output( "Purging all thumbnails for file '$filename'..." );
75 $this->output( "done.\n" );
77 if ( $afile instanceof ArchivedFile
) {
78 $this->scrubVersion( $afile );
80 $this->output( "Finding deleted versions of file '$filename'...\n" );
81 $this->scrubAllVersions( $filename );
82 $this->output( "Done\n" );
86 protected function scrubAllVersions( $name ) {
87 $dbw = $this->getDB( DB_MASTER
);
88 $res = $dbw->select( 'filearchive', '*',
89 [ 'fa_name' => $name, 'fa_storage_group' => 'deleted' ],
91 foreach ( $res as $row ) {
92 $this->scrubVersion( ArchivedFile
::newFromRow( $row ) );
96 protected function scrubVersion( ArchivedFile
$archivedFile ) {
97 $key = $archivedFile->getStorageKey();
98 $name = $archivedFile->getName();
99 $ts = $archivedFile->getTimestamp();
100 $repo = RepoGroup
::singleton()->getLocalRepo();
101 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
102 if ( $this->hasOption( 'delete' ) ) {
103 $status = $repo->getBackend()->delete( [ 'src' => $path ] );
104 if ( $status->isOK() ) {
105 $this->output( "Deleted version '$key' ($ts) of file '$name'\n" );
107 $this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" );
108 $this->output( print_r( $status->getErrorsArray(), true ) );
111 $this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" );
116 $maintClass = "EraseArchivedFile";
117 require_once RUN_MAINTENANCE_IF_MAIN
;