Debian Codegraph @

maintenance/eraseArchivedFile.php

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