Merge maintenance-work branch (now with less errors!):
[lhc/web/wiklou.git] / maintenance / deleteArchivedFiles.php
1 <?php
2
3 /**
4 * Delete archived (non-current) files from the database
5 *
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.
10 *
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.
15 *
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
20 *
21 * @ingroup Maintenance
22 * @author Aaron Schulz
23 * Based on deleteOldRevisions.php by Rob Church
24 */
25
26 require_once( "Maintenance.php" );
27
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' );
33 }
34
35 /**
36 * @todo @fixme FSTransaction/FileStore crap needs removing. Does
37 * not work on trunk
38 */
39 public function execute() {
40 if( !$this->hasOption('delete') ) {
41 $this->output( "Use --delete to actually confirm this script\n" );
42 return;
43 }
44 # Data should come off the master, wrapped in a transaction
45 $dbw = wfGetDB( DB_MASTER );
46 $dbw->begin();
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" );
52 $count = 0;
53 while( $row = $dbw->fetchObject( $res ) ) {
54 $key = $row->fa_storage_key;
55 $group = $row->fa_storage_group;
56 $id = $row->fa_id;
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 ),
63 __METHOD__,
64 array( 'FOR UPDATE' )
65 );
66 if ( $path && file_exists($path) && !$inuse ) {
67 unlink($path); // delete
68 $count++;
69 $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
70 } else {
71 $this->output( "Notice - file '$key' not found in group '$group'\n" );
72 }
73 }
74 $dbw->commit();
75 $this->output( "Done! [$count file(s)]\n" );
76 }
77 }
78
79 $maintClass = "DeleteArchivedFiles";
80 require_once( DO_MAINTENANCE );