2013-05-07 00:03:38 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Delete archived (non-current) files from storage
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
|
2023-08-01 21:04:32 +00:00
|
|
|
use MediaWiki\FileRepo\File\FileSelectQueryBuilder;
|
2019-05-14 17:00:34 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2013-08-24 15:06:25 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2013-05-07 00:03:38 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maintenance script to delete archived (non-current) files from storage.
|
|
|
|
|
*
|
2014-07-23 20:04:48 +00:00
|
|
|
* @todo Maybe add some simple logging
|
2013-05-07 00:03:38 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
class EraseArchivedFile extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Erases traces of deleted files.' );
|
2013-05-07 00:03:38 +00:00
|
|
|
$this->addOption( 'delete', 'Perform the deletion' );
|
|
|
|
|
$this->addOption( 'filename', 'File name', false, true );
|
|
|
|
|
$this->addOption( 'filekey', 'File storage key (with extension) or "*"', true, true );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
if ( !$this->hasOption( 'delete' ) ) {
|
|
|
|
|
$this->output( "Use --delete to actually confirm this script\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$filekey = $this->getOption( 'filekey' );
|
|
|
|
|
$filename = $this->getOption( 'filename' );
|
|
|
|
|
|
2020-05-19 22:10:27 +00:00
|
|
|
if ( $filekey === '*' ) {
|
|
|
|
|
// all versions by name
|
2013-05-07 00:03:38 +00:00
|
|
|
if ( !strlen( $filename ) ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Missing --filename parameter." );
|
2013-05-07 00:03:38 +00:00
|
|
|
}
|
|
|
|
|
$afile = false;
|
2020-05-19 22:10:27 +00:00
|
|
|
} else {
|
|
|
|
|
// specified version
|
2024-01-17 18:53:40 +00:00
|
|
|
$dbw = $this->getPrimaryDB();
|
2023-08-01 21:04:32 +00:00
|
|
|
$queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbw );
|
|
|
|
|
$queryBuilder->where( [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $filekey ] );
|
|
|
|
|
$row = $queryBuilder->caller( __METHOD__ )->fetchRow();
|
2022-07-21 09:36:56 +00:00
|
|
|
|
2013-05-07 00:03:38 +00:00
|
|
|
if ( !$row ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "No deleted file exists with key '$filekey'." );
|
2013-05-07 00:03:38 +00:00
|
|
|
}
|
|
|
|
|
$filename = $row->fa_name;
|
|
|
|
|
$afile = ArchivedFile::newFromRow( $row );
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 09:21:12 +00:00
|
|
|
$file = $this->getServiceContainer()->getRepoGroup()->getLocalRepo()->newFile( $filename );
|
2013-05-07 00:03:38 +00:00
|
|
|
if ( $file->exists() ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "File '$filename' is still a public file, use the delete form.\n" );
|
2013-05-07 00:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->output( "Purging all thumbnails for file '$filename'..." );
|
|
|
|
|
$file->purgeCache();
|
|
|
|
|
$this->output( "done.\n" );
|
|
|
|
|
|
|
|
|
|
if ( $afile instanceof ArchivedFile ) {
|
|
|
|
|
$this->scrubVersion( $afile );
|
|
|
|
|
} else {
|
|
|
|
|
$this->output( "Finding deleted versions of file '$filename'...\n" );
|
|
|
|
|
$this->scrubAllVersions( $filename );
|
|
|
|
|
$this->output( "Done\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function scrubAllVersions( $name ) {
|
2024-01-17 18:53:40 +00:00
|
|
|
$dbw = $this->getPrimaryDB();
|
2023-08-01 21:04:32 +00:00
|
|
|
$queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbw );
|
|
|
|
|
$queryBuilder->where( [ 'fa_name' => $name, 'fa_storage_group' => 'deleted' ] );
|
|
|
|
|
$res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
|
2013-05-07 00:03:38 +00:00
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
$this->scrubVersion( ArchivedFile::newFromRow( $row ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function scrubVersion( ArchivedFile $archivedFile ) {
|
|
|
|
|
$key = $archivedFile->getStorageKey();
|
|
|
|
|
$name = $archivedFile->getName();
|
|
|
|
|
$ts = $archivedFile->getTimestamp();
|
2023-08-31 09:21:12 +00:00
|
|
|
$repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
|
2013-05-07 00:03:38 +00:00
|
|
|
$path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
|
|
|
|
|
if ( $this->hasOption( 'delete' ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$status = $repo->getBackend()->delete( [ 'src' => $path ] );
|
2013-05-07 00:03:38 +00:00
|
|
|
if ( $status->isOK() ) {
|
|
|
|
|
$this->output( "Deleted version '$key' ($ts) of file '$name'\n" );
|
|
|
|
|
} else {
|
|
|
|
|
$this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" );
|
Maintenance: Print errors from StatusValue objects in a consistent way
Allow Maintenance::error() and Maintenance::fatalError() to take
StatusValue objects. They now print each error message from the
status on a separate line, in English, ignoring on-wiki message
overrides, as wikitext but after parser function expansion.
Thoughts on the previously commonly used methods:
- $status->getMessage( false, false, 'en' )->text()
Almost the same as the new output, but it allows on-wiki message
overrides, and if there is more than one error, it prefixes each
line with a '*' (like a wikitext list).
- $status->getMessage( false, false, 'en' )->plain()
- $status->getWikiText( false, false, 'en' )
As above, but these forms do not expand parser functions
such as {{GENDER:}}.
- print_r( $status->getErrorsArray(), true )
- print_r( $status->getErrors(), true )
These forms output the message keys instead of the message text,
which is not very human-readable.
The error messages are now always printed using error() rather
than output(), which means they go to STDERR rather than STDOUT
and they're printed even with the --quiet flag.
Change-Id: I5b8e7c7ed2a896a1029f58857a478d3f1b4b0589
2024-06-06 23:50:00 +00:00
|
|
|
$this->error( $status );
|
2013-05-07 00:03:38 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = EraseArchivedFile::class;
|
2013-08-24 15:06:25 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|