RCFilters: define consistent interface in ChangesListFilterGroup
[lhc/web/wiklou.git] / maintenance / eraseArchivedFile.php
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 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script to delete archived (non-current) files from storage.
28 *
29 * @todo Maybe add some simple logging
30 *
31 * @ingroup Maintenance
32 * @since 1.22
33 */
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 );
41 }
42
43 public function execute() {
44 if ( !$this->hasOption( 'delete' ) ) {
45 $this->output( "Use --delete to actually confirm this script\n" );
46 }
47
48 $filekey = $this->getOption( 'filekey' );
49 $filename = $this->getOption( 'filename' );
50
51 if ( $filekey === '*' ) { // all versions by name
52 if ( !strlen( $filename ) ) {
53 $this->error( "Missing --filename parameter.", 1 );
54 }
55 $afile = false;
56 } else { // specified version
57 $dbw = $this->getDB( DB_MASTER );
58 $row = $dbw->selectRow( 'filearchive', '*',
59 [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $filekey ],
60 __METHOD__ );
61 if ( !$row ) {
62 $this->error( "No deleted file exists with key '$filekey'.", 1 );
63 }
64 $filename = $row->fa_name;
65 $afile = ArchivedFile::newFromRow( $row );
66 }
67
68 $file = wfLocalFile( $filename );
69 if ( $file->exists() ) {
70 $this->error( "File '$filename' is still a public file, use the delete form.\n", 1 );
71 }
72
73 $this->output( "Purging all thumbnails for file '$filename'..." );
74 $file->purgeCache();
75 $this->output( "done.\n" );
76
77 if ( $afile instanceof ArchivedFile ) {
78 $this->scrubVersion( $afile );
79 } else {
80 $this->output( "Finding deleted versions of file '$filename'...\n" );
81 $this->scrubAllVersions( $filename );
82 $this->output( "Done\n" );
83 }
84 }
85
86 protected function scrubAllVersions( $name ) {
87 $dbw = $this->getDB( DB_MASTER );
88 $res = $dbw->select( 'filearchive', '*',
89 [ 'fa_name' => $name, 'fa_storage_group' => 'deleted' ],
90 __METHOD__ );
91 foreach ( $res as $row ) {
92 $this->scrubVersion( ArchivedFile::newFromRow( $row ) );
93 }
94 }
95
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" );
106 } else {
107 $this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" );
108 $this->output( print_r( $status->getErrorsArray(), true ) );
109 }
110 } else {
111 $this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" );
112 }
113 }
114 }
115
116 $maintClass = "EraseArchivedFile";
117 require_once RUN_MAINTENANCE_IF_MAIN;