2007-05-12 17:56:09 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Delete archived (non-current) files from the database
|
|
|
|
|
*
|
2012-06-16 20:35:13 +00:00
|
|
|
* Based on deleteOldRevisions.php by Rob Church.
|
|
|
|
|
*
|
2009-08-02 19:35:17 +00:00
|
|
|
* 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
|
|
|
|
|
*
|
2012-06-16 20:35:13 +00:00
|
|
|
* @file
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Maintenance
|
2007-05-12 17:56:09 +00:00
|
|
|
*/
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2007-05-12 17:56:09 +00:00
|
|
|
|
2012-06-16 20:35:13 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script to delete archived (non-current) files from the database.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2009-08-02 19:35:17 +00:00
|
|
|
class DeleteArchivedFiles extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Deletes all archived images.' );
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->addOption( 'delete', 'Perform the deletion' );
|
2010-02-22 00:54:39 +00:00
|
|
|
$this->addOption( 'force', 'Force deletion of rows from filearchive' );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2007-05-12 17:56:09 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function execute() {
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !$this->hasOption( 'delete' ) ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "Use --delete to actually confirm this script\n" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-09-12 05:44:14 +00:00
|
|
|
|
|
|
|
|
# Data should come off the master, wrapped in a transaction
|
2024-01-17 18:53:40 +00:00
|
|
|
$dbw = $this->getPrimaryDB();
|
2015-12-22 08:51:42 +00:00
|
|
|
$this->beginTransaction( $dbw, __METHOD__ );
|
2023-08-31 09:21:12 +00:00
|
|
|
$repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
|
2014-09-12 05:44:14 +00:00
|
|
|
|
|
|
|
|
# Get "active" revisions from the filearchive table
|
|
|
|
|
$this->output( "Searching for and deleting archived files...\n" );
|
2022-07-11 13:51:08 +00:00
|
|
|
$res = $dbw->newSelectQueryBuilder()
|
|
|
|
|
->select( [ 'fa_id', 'fa_storage_group', 'fa_storage_key', 'fa_sha1', 'fa_name' ] )
|
|
|
|
|
->from( 'filearchive' )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->fetchResultSet();
|
2014-09-12 05:44:14 +00:00
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
$key = $row->fa_storage_key;
|
|
|
|
|
if ( !strlen( $key ) ) {
|
|
|
|
|
$this->output( "Entry with ID {$row->fa_id} has empty key, skipping\n" );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-21 05:05:59 +00:00
|
|
|
$file = $repo->newFile( $row->fa_name );
|
2021-11-01 02:43:12 +00:00
|
|
|
$status = $file->acquireFileLock( 10 );
|
|
|
|
|
if ( !$status->isOK() ) {
|
2016-07-21 05:05:59 +00:00
|
|
|
$this->error( "Could not acquire lock on '{$row->fa_name}', skipping\n" );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-12 05:44:14 +00:00
|
|
|
$group = $row->fa_storage_group;
|
|
|
|
|
$id = $row->fa_id;
|
2016-07-21 05:05:59 +00:00
|
|
|
$path = $repo->getZonePath( 'deleted' ) .
|
|
|
|
|
'/' . $repo->getDeletedHashPath( $key ) . $key;
|
2014-09-12 05:44:14 +00:00
|
|
|
if ( isset( $row->fa_sha1 ) ) {
|
|
|
|
|
$sha1 = $row->fa_sha1;
|
|
|
|
|
} else {
|
|
|
|
|
// old row, populate from key
|
|
|
|
|
$sha1 = LocalRepo::getHashFromKey( $key );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if the file is used anywhere...
|
2022-07-11 13:51:08 +00:00
|
|
|
$inuse = (bool)$dbw->newSelectQueryBuilder()
|
|
|
|
|
->select( '1' )
|
|
|
|
|
->from( 'oldimage' )
|
|
|
|
|
->where( [
|
2014-09-12 05:44:14 +00:00
|
|
|
'oi_sha1' => $sha1,
|
|
|
|
|
$dbw->bitAnd( 'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE
|
2022-07-11 13:51:08 +00:00
|
|
|
] )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->forUpdate()
|
|
|
|
|
->fetchField();
|
2014-09-12 05:44:14 +00:00
|
|
|
|
|
|
|
|
$needForce = true;
|
|
|
|
|
if ( !$repo->fileExists( $path ) ) {
|
|
|
|
|
$this->output( "Notice - file '$key' not found in group '$group'\n" );
|
|
|
|
|
} elseif ( $inuse ) {
|
|
|
|
|
$this->output( "Notice - file '$key' is still in use\n" );
|
|
|
|
|
} elseif ( !$repo->quickPurge( $path ) ) {
|
|
|
|
|
$this->output( "Unable to remove file $path, skipping\n" );
|
2021-11-01 02:43:12 +00:00
|
|
|
$file->releaseFileLock();
|
2020-05-19 22:10:27 +00:00
|
|
|
|
|
|
|
|
// don't delete even with --force
|
|
|
|
|
continue;
|
2014-09-12 05:44:14 +00:00
|
|
|
} else {
|
|
|
|
|
$needForce = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $needForce ) {
|
|
|
|
|
if ( $this->hasOption( 'force' ) ) {
|
|
|
|
|
$this->output( "Got --force, deleting DB entry\n" );
|
|
|
|
|
} else {
|
2021-11-01 02:43:12 +00:00
|
|
|
$file->releaseFileLock();
|
2014-09-12 05:44:14 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$count++;
|
2024-01-02 11:59:05 +00:00
|
|
|
$dbw->newDeleteQueryBuilder()
|
|
|
|
|
->deleteFrom( 'filearchive' )
|
|
|
|
|
->where( [ 'fa_id' => $id ] )
|
|
|
|
|
->caller( __METHOD__ )->execute();
|
2021-11-01 02:43:12 +00:00
|
|
|
$file->releaseFileLock();
|
2014-09-12 05:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-22 08:51:42 +00:00
|
|
|
$this->commitTransaction( $dbw, __METHOD__ );
|
2014-09-12 05:44:14 +00:00
|
|
|
$this->output( "Done! [$count file(s)]\n" );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2007-05-12 17:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = DeleteArchivedFiles::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|