tableName calls moved inside fieldInfoMulti and removed call that existed only for...
[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( dirname(__FILE__) . '/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 $this->addOption( 'force', 'Force deletion of rows from filearchive' );
34 }
35
36 public function execute() {
37 if( !$this->hasOption('delete') ) {
38 $this->output( "Use --delete to actually confirm this script\n" );
39 return;
40 }
41 $force = $this->hasOption( 'force' );
42 # Data should come off the master, wrapped in a transaction
43 $dbw = wfGetDB( DB_MASTER );
44 $dbw->begin();
45 $tbl_arch = $dbw->tableName( 'filearchive' );
46 $repo = RepoGroup::singleton()->getLocalRepo();
47 # Get "active" revisions from the filearchive table
48 $this->output( "Searching for and deleting archived files...\n" );
49 $res = $dbw->query( "SELECT fa_id,fa_storage_group,fa_storage_key FROM $tbl_arch" );
50 $count = 0;
51 foreach( $res as $row ) {
52 $key = $row->fa_storage_key;
53 $group = $row->fa_storage_group;
54 $id = $row->fa_id;
55 $path = $repo->getZonePath( 'deleted' ).'/'.$repo->getDeletedHashPath($key).$key;
56 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
57 // Check if the file is used anywhere...
58 $inuse = $dbw->selectField( 'oldimage', '1',
59 array( 'oi_sha1' => $sha1,
60 'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
61 __METHOD__,
62 array( 'FOR UPDATE' )
63 );
64 if ( $path && file_exists($path) && !$inuse ) {
65 unlink($path); // delete
66 $count++;
67 $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
68 } else {
69 $this->output( "Notice - file '$key' not found in group '$group'\n" );
70 if ( $force ) {
71 $this->output( "Got --force, deleting DB entry\n" );
72 $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
73 }
74 }
75 }
76 $dbw->commit();
77 $this->output( "Done! [$count file(s)]\n" );
78 }
79 }
80
81 $maintClass = "DeleteArchivedFiles";
82 require_once( DO_MAINTENANCE );