Add ability to override mb_strtoupper in Language::ucfirst
[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->fatalError( "Missing --filename parameter." );
54 }
55 $afile = false;
56 } else { // specified version
57 $dbw = $this->getDB( DB_MASTER );
58 $fileQuery = ArchivedFile::getQueryInfo();
59 $row = $dbw->selectRow( $fileQuery['tables'], $fileQuery['fields'],
60 [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $filekey ],
61 __METHOD__, [], $fileQuery['joins'] );
62 if ( !$row ) {
63 $this->fatalError( "No deleted file exists with key '$filekey'." );
64 }
65 $filename = $row->fa_name;
66 $afile = ArchivedFile::newFromRow( $row );
67 }
68
69 $file = wfLocalFile( $filename );
70 if ( $file->exists() ) {
71 $this->fatalError( "File '$filename' is still a public file, use the delete form.\n" );
72 }
73
74 $this->output( "Purging all thumbnails for file '$filename'..." );
75 $file->purgeCache();
76 $this->output( "done.\n" );
77
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" );
84 }
85 }
86
87 protected function scrubAllVersions( $name ) {
88 $dbw = $this->getDB( DB_MASTER );
89 $fileQuery = ArchivedFile::getQueryInfo();
90 $res = $dbw->select( $fileQuery['tables'], $fileQuery['fields'],
91 [ 'fa_name' => $name, 'fa_storage_group' => 'deleted' ],
92 __METHOD__, [], $fileQuery['joins'] );
93 foreach ( $res as $row ) {
94 $this->scrubVersion( ArchivedFile::newFromRow( $row ) );
95 }
96 }
97
98 protected function scrubVersion( ArchivedFile $archivedFile ) {
99 $key = $archivedFile->getStorageKey();
100 $name = $archivedFile->getName();
101 $ts = $archivedFile->getTimestamp();
102 $repo = RepoGroup::singleton()->getLocalRepo();
103 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
104 if ( $this->hasOption( 'delete' ) ) {
105 $status = $repo->getBackend()->delete( [ 'src' => $path ] );
106 if ( $status->isOK() ) {
107 $this->output( "Deleted version '$key' ($ts) of file '$name'\n" );
108 } else {
109 $this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" );
110 $this->output( print_r( $status->getErrorsArray(), true ) );
111 }
112 } else {
113 $this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" );
114 }
115 }
116 }
117
118 $maintClass = EraseArchivedFile::class;
119 require_once RUN_MAINTENANCE_IF_MAIN;