00001 <?php
00002
00026 require_once( dirname(__FILE__) . '/Maintenance.php' );
00027
00028 class DeleteArchivedFiles extends Maintenance {
00029 public function __construct() {
00030 parent::__construct();
00031 $this->mDescription = "Deletes all archived images.";
00032 $this->addOption( 'delete', 'Perform the deletion' );
00033 $this->addOption( 'force', 'Force deletion of rows from filearchive' );
00034 }
00035
00036 public function execute() {
00037 if( !$this->hasOption('delete') ) {
00038 $this->output( "Use --delete to actually confirm this script\n" );
00039 return;
00040 }
00041 $force = $this->hasOption( 'force' );
00042 # Data should come off the master, wrapped in a transaction
00043 $dbw = wfGetDB( DB_MASTER );
00044 $dbw->begin();
00045 $tbl_arch = $dbw->tableName( 'filearchive' );
00046 $repo = RepoGroup::singleton()->getLocalRepo();
00047 # Get "active" revisions from the filearchive table
00048 $this->output( "Searching for and deleting archived files...\n" );
00049 $res = $dbw->query( "SELECT fa_id,fa_storage_group,fa_storage_key FROM $tbl_arch" );
00050 $count = 0;
00051 foreach( $res as $row ) {
00052 $key = $row->fa_storage_key;
00053 $group = $row->fa_storage_group;
00054 $id = $row->fa_id;
00055 $path = $repo->getZonePath( 'deleted' ).'/'.$repo->getDeletedHashPath($key).$key;
00056 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
00057
00058 $inuse = $dbw->selectField( 'oldimage', '1',
00059 array( 'oi_sha1' => $sha1,
00060 'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
00061 __METHOD__,
00062 array( 'FOR UPDATE' )
00063 );
00064 if ( $path && file_exists($path) && !$inuse ) {
00065 unlink($path);
00066 $count++;
00067 $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
00068 } else {
00069 $this->output( "Notice - file '$key' not found in group '$group'\n" );
00070 if ( $force ) {
00071 $this->output( "Got --force, deleting DB entry\n" );
00072 $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
00073 }
00074 }
00075 }
00076 $dbw->commit();
00077 $this->output( "Done! [$count file(s)]\n" );
00078 }
00079 }
00080
00081 $maintClass = "DeleteArchivedFiles";
00082 require_once( DO_MAINTENANCE );