4 * Delete archived (non-current) files from the database
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
22 * @author Aaron Schulz
23 * Based on deleteOldRevisions.php by Rob Church
26 require_once( dirname(__FILE__
) . '/Maintenance.php' );
28 class DeleteArchivedFiles
extends Maintenance
{
29 public function __construct() {
30 parent
::__construct();
31 $this->mDescription
= "Deletes all archived images.";
32 $this->addOption( 'delete', 'Perform the deletion' );
36 * @todo @fixme FSTransaction/FileStore crap needs removing. Does
39 public function execute() {
40 if( !$this->hasOption('delete') ) {
41 $this->output( "Use --delete to actually confirm this script\n" );
44 # Data should come off the master, wrapped in a transaction
45 $dbw = wfGetDB( DB_MASTER
);
47 $tbl_arch = $dbw->tableName( 'filearchive' );
48 $repo = RepoGroup
::singleton()->getLocalRepo();
49 # Get "active" revisions from the filearchive table
50 $this->output( "Searching for and deleting archived files...\n" );
51 $res = $dbw->query( "SELECT fa_id,fa_storage_group,fa_storage_key FROM $tbl_arch" );
53 foreach( $res as $row ) {
54 $key = $row->fa_storage_key
;
55 $group = $row->fa_storage_group
;
57 $path = $repo->getZonePath( 'deleted' ).'/'.$repo->getDeletedHashPath($key).$key;
58 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
59 // Check if the file is used anywhere...
60 $inuse = $dbw->selectField( 'oldimage', '1',
61 array( 'oi_sha1' => $sha1,
62 'oi_deleted & '.File
::DELETED_FILE
=> File
::DELETED_FILE
),
66 if ( $path && file_exists($path) && !$inuse ) {
67 unlink($path); // delete
69 $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
71 $this->output( "Notice - file '$key' not found in group '$group'\n" );
75 $this->output( "Done! [$count file(s)]\n" );
79 $maintClass = "DeleteArchivedFiles";
80 require_once( DO_MAINTENANCE
);