Bug 34032 - API help header not aligned
[mediawiki.git] / maintenance / deleteArchivedFiles.inc
blob68394b4aadefad5aa49dce3a31e7a7a89ba97c36
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  * @ingroup Maintenance
21  */
23 class DeleteArchivedFilesImplementation {
24         static public function doDelete( $output, $force ) {
25                 # Data should come off the master, wrapped in a transaction
26                 $dbw = wfGetDB( DB_MASTER );
27                 $dbw->begin();
28                 $tbl_arch = $dbw->tableName( 'filearchive' );
29                 $repo = RepoGroup::singleton()->getLocalRepo();
30                 # Get "active" revisions from the filearchive table
31                 $output->handleOutput( "Searching for and deleting archived files...\n" );
32                 $res = $dbw->query( "SELECT fa_id,fa_storage_group,fa_storage_key FROM $tbl_arch" );
33                 $count = 0;
34                 foreach ( $res as $row ) {
35                         $key = $row->fa_storage_key;
36                         $group = $row->fa_storage_group;
37                         $id = $row->fa_id;
38                         $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
39                         $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
40                         // Check if the file is used anywhere...
41                         $inuse = $dbw->selectField( 'oldimage', '1',
42                                 array( 'oi_sha1' => $sha1,
43                                 'oi_deleted & ' . File::DELETED_FILE => File::DELETED_FILE ),
44                                 __METHOD__,
45                                 array( 'FOR UPDATE' )
46                         );
47                         if ( $path && file_exists( $path ) && !$inuse ) {
48                                 if( unlink( $path ) ) { // delete
49                                         $count++;
50                                         $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
51                                 } else {
52                                         $output->handleOutput( "Unable to remove file $path, skipping\n" );
53                                 }
54                         } else {
55                                 $output->handleOutput( "Notice - file '$key' not found in group '$group'\n" );
56                                 if ( $force ) {
57                                         $output->handleOutput( "Got --force, deleting DB entry\n" );
58                                         $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
59                                 }
60                         }
61                 }
62                 $dbw->commit();
63                 $output->handleOutput( "Done! [$count file(s)]\n" );
64         }