Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeleteArchivedFiles
0.00% covered (danger)
0.00%
0 / 67
0.00% covered (danger)
0.00%
0 / 2
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2/**
3 * Delete archived (non-current) files from the database
4 *
5 * Based on deleteOldRevisions.php by Rob Church.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Maintenance
24 */
25
26require_once __DIR__ . '/Maintenance.php';
27
28/**
29 * Maintenance script to delete archived (non-current) files from the database.
30 *
31 * @ingroup Maintenance
32 */
33class DeleteArchivedFiles extends Maintenance {
34    public function __construct() {
35        parent::__construct();
36        $this->addDescription( 'Deletes all archived images.' );
37        $this->addOption( 'delete', 'Perform the deletion' );
38        $this->addOption( 'force', 'Force deletion of rows from filearchive' );
39    }
40
41    public function execute() {
42        if ( !$this->hasOption( 'delete' ) ) {
43            $this->output( "Use --delete to actually confirm this script\n" );
44            return;
45        }
46
47        # Data should come off the master, wrapped in a transaction
48        $dbw = $this->getPrimaryDB();
49        $this->beginTransaction( $dbw, __METHOD__ );
50        $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
51
52        # Get "active" revisions from the filearchive table
53        $this->output( "Searching for and deleting archived files...\n" );
54        $res = $dbw->newSelectQueryBuilder()
55            ->select( [ 'fa_id', 'fa_storage_group', 'fa_storage_key', 'fa_sha1', 'fa_name' ] )
56            ->from( 'filearchive' )
57            ->caller( __METHOD__ )
58            ->fetchResultSet();
59
60        $count = 0;
61        foreach ( $res as $row ) {
62            $key = $row->fa_storage_key;
63            if ( !strlen( $key ) ) {
64                $this->output( "Entry with ID {$row->fa_id} has empty key, skipping\n" );
65                continue;
66            }
67
68            $file = $repo->newFile( $row->fa_name );
69            $status = $file->acquireFileLock( 10 );
70            if ( !$status->isOK() ) {
71                $this->error( "Could not acquire lock on '{$row->fa_name}', skipping\n" );
72                continue;
73            }
74
75            $group = $row->fa_storage_group;
76            $id = $row->fa_id;
77            $path = $repo->getZonePath( 'deleted' ) .
78                '/' . $repo->getDeletedHashPath( $key ) . $key;
79            if ( isset( $row->fa_sha1 ) ) {
80                $sha1 = $row->fa_sha1;
81            } else {
82                // old row, populate from key
83                $sha1 = LocalRepo::getHashFromKey( $key );
84            }
85
86            // Check if the file is used anywhere...
87            $inuse = (bool)$dbw->newSelectQueryBuilder()
88                ->select( '1' )
89                ->from( 'oldimage' )
90                ->where( [
91                    'oi_sha1' => $sha1,
92                    $dbw->bitAnd( 'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE
93                ] )
94                ->caller( __METHOD__ )
95                ->forUpdate()
96                ->fetchField();
97
98            $needForce = true;
99            if ( !$repo->fileExists( $path ) ) {
100                $this->output( "Notice - file '$key' not found in group '$group'\n" );
101            } elseif ( $inuse ) {
102                $this->output( "Notice - file '$key' is still in use\n" );
103            } elseif ( !$repo->quickPurge( $path ) ) {
104                $this->output( "Unable to remove file $path, skipping\n" );
105                $file->releaseFileLock();
106
107                // don't delete even with --force
108                continue;
109            } else {
110                $needForce = false;
111            }
112
113            if ( $needForce ) {
114                if ( $this->hasOption( 'force' ) ) {
115                    $this->output( "Got --force, deleting DB entry\n" );
116                } else {
117                    $file->releaseFileLock();
118                    continue;
119                }
120            }
121
122            $count++;
123            $dbw->newDeleteQueryBuilder()
124                ->deleteFrom( 'filearchive' )
125                ->where( [ 'fa_id' => $id ] )
126                ->caller( __METHOD__ )->execute();
127            $file->releaseFileLock();
128        }
129
130        $this->commitTransaction( $dbw, __METHOD__ );
131        $this->output( "Done! [$count file(s)]\n" );
132    }
133}
134
135$maintClass = DeleteArchivedFiles::class;
136require_once RUN_MAINTENANCE_IF_MAIN;