Merge "docs: Fix typo"
[mediawiki.git] / maintenance / eraseArchivedFile.php
blob4134f536af71b2bb4f588b2e678d56cfa7a8c9be
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
24 use MediaWiki\FileRepo\File\FileSelectQueryBuilder;
25 use MediaWiki\Maintenance\Maintenance;
27 // @codeCoverageIgnoreStart
28 require_once __DIR__ . '/Maintenance.php';
29 // @codeCoverageIgnoreEnd
31 /**
32 * Maintenance script to delete archived (non-current) files from storage.
34 * @todo Maybe add some simple logging
36 * @ingroup Maintenance
37 * @since 1.22
39 class EraseArchivedFile extends Maintenance {
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Erases traces of deleted files.' );
43 $this->addOption( 'delete', 'Perform the deletion' );
44 $this->addOption( 'filename', 'File name', false, true );
45 $this->addOption( 'filekey', 'File storage key (with extension) or "*"', true, true );
48 public function execute() {
49 if ( !$this->hasOption( 'delete' ) ) {
50 $this->output( "Use --delete to actually confirm this script\n" );
53 $filekey = $this->getOption( 'filekey' );
54 $filename = $this->getOption( 'filename' );
56 if ( $filekey === '*' ) {
57 // all versions by name
58 if ( !strlen( $filename ) ) {
59 $this->fatalError( "Missing --filename parameter." );
61 $afile = false;
62 } else {
63 // specified version
64 $dbw = $this->getPrimaryDB();
65 $queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbw );
66 $queryBuilder->where( [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $filekey ] );
67 $row = $queryBuilder->caller( __METHOD__ )->fetchRow();
69 if ( !$row ) {
70 $this->fatalError( "No deleted file exists with key '$filekey'." );
72 $filename = $row->fa_name;
73 $afile = ArchivedFile::newFromRow( $row );
76 $file = $this->getServiceContainer()->getRepoGroup()->getLocalRepo()->newFile( $filename );
77 if ( $file->exists() ) {
78 $this->fatalError( "File '$filename' is still a public file, use the delete form.\n" );
81 $this->output( "Purging all thumbnails for file '$filename'..." );
82 $file->purgeCache();
83 $this->output( "done.\n" );
85 if ( $afile instanceof ArchivedFile ) {
86 $this->scrubVersion( $afile );
87 } else {
88 $this->output( "Finding deleted versions of file '$filename'...\n" );
89 $this->scrubAllVersions( $filename );
90 $this->output( "Done\n" );
94 protected function scrubAllVersions( $name ) {
95 $dbw = $this->getPrimaryDB();
96 $queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbw );
97 $queryBuilder->where( [ 'fa_name' => $name, 'fa_storage_group' => 'deleted' ] );
98 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
99 foreach ( $res as $row ) {
100 $this->scrubVersion( ArchivedFile::newFromRow( $row ) );
104 protected function scrubVersion( ArchivedFile $archivedFile ) {
105 $key = $archivedFile->getStorageKey();
106 $name = $archivedFile->getName();
107 $ts = $archivedFile->getTimestamp();
108 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
109 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
110 if ( $this->hasOption( 'delete' ) ) {
111 $status = $repo->getBackend()->delete( [ 'src' => $path ] );
112 if ( $status->isOK() ) {
113 $this->output( "Deleted version '$key' ($ts) of file '$name'\n" );
114 } else {
115 $this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" );
116 $this->error( $status );
118 } else {
119 $this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" );
124 // @codeCoverageIgnoreStart
125 $maintClass = EraseArchivedFile::class;
126 require_once RUN_MAINTENANCE_IF_MAIN;
127 // @codeCoverageIgnoreEnd