Merge "Never display empty sections on Special:Version"
[mediawiki.git] / maintenance / deleteArchivedFiles.inc
blob0c0b34a3334d4d8c52d44f91a198b6a05f1b49bc
1 <?php
2 /**
3  * Core functions for deleteArchivedFiles.php
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  */
24 /**
25  * Core functions for deleteArchivedFiles.php
26  *
27  * @ingroup Maintenance
28  */
29 class DeleteArchivedFilesImplementation {
30         public static function doDelete( $output, $force ) {
31                 # Data should come off the master, wrapped in a transaction
32                 $dbw = wfGetDB( DB_MASTER );
33                 $dbw->begin( __METHOD__ );
34                 $tbl_arch = $dbw->tableName( 'filearchive' );
35                 $repo = RepoGroup::singleton()->getLocalRepo();
36                 # Get "active" revisions from the filearchive table
37                 $output->handleOutput( "Searching for and deleting archived files...\n" );
38                 $res = $dbw->query( "SELECT fa_id,fa_storage_group,fa_storage_key,fa_sha1 FROM $tbl_arch" );
39                 $count = 0;
40                 foreach ( $res as $row ) {
41                         $key = $row->fa_storage_key;
42                         if ( !strlen( $key ) ) {
43                                 $output->handleOutput( "Entry with ID {$row->fa_id} has empty key, skipping\n" );
44                                 continue;
45                         }
46                         $group = $row->fa_storage_group;
47                         $id = $row->fa_id;
48                         $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
49                         if ( isset( $row->fa_sha1 ) ) {
50                                 $sha1 = $row->fa_sha1;
51                         } else {
52                                 // old row, populate from key
53                                 $sha1 = LocalRepo::getHashFromKey( $key );
54                         }
55                         // Check if the file is used anywhere...
56                         $inuse = $dbw->selectField(
57                                 'oldimage',
58                                 '1',
59                                 array(
60                                         'oi_sha1' => $sha1,
61                                         'oi_deleted & ' . File::DELETED_FILE => File::DELETED_FILE
62                                 ),
63                                 __METHOD__,
64                                 array( 'FOR UPDATE' )
65                         );
66                         if ( $path && $repo->fileExists( $path ) && !$inuse ) {
67                                 if ( $repo->quickPurge( $path ) ) {
68                                         $count++;
69                                         $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
70                                 } else {
71                                         $output->handleOutput( "Unable to remove file $path, skipping\n" );
72                                 }
73                         } else {
74                                 $output->handleOutput( "Notice - file '$key' not found in group '$group'\n" );
75                                 if ( $force ) {
76                                         $output->handleOutput( "Got --force, deleting DB entry\n" );
77                                         $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
78                                 }
79                         }
80                 }
81                 $dbw->commit( __METHOD__ );
82                 $output->handleOutput( "Done! [$count file(s)]\n" );
83         }