44 if ( !$this->hasOption(
'delete' ) ) {
45 $this->output(
"Use --delete to actually confirm this script\n" );
49 # Data should come off the master, wrapped in a transaction
50 $dbw = $this->getPrimaryDB();
51 $this->beginTransaction( $dbw, __METHOD__ );
52 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
54 # Get "active" revisions from the filearchive table
55 $this->output(
"Searching for and deleting archived files...\n" );
56 $res = $dbw->newSelectQueryBuilder()
57 ->select( [
'fa_id',
'fa_storage_group',
'fa_storage_key',
'fa_sha1',
'fa_name' ] )
58 ->from(
'filearchive' )
59 ->caller( __METHOD__ )
63 foreach ( $res as $row ) {
64 $key = $row->fa_storage_key;
65 if ( !strlen( $key ) ) {
66 $this->output(
"Entry with ID {$row->fa_id} has empty key, skipping\n" );
70 $file = $repo->newFile( $row->fa_name );
71 $status = $file->acquireFileLock( 10 );
72 if ( !$status->isOK() ) {
73 $this->error(
"Could not acquire lock on '{$row->fa_name}', skipping\n" );
77 $group = $row->fa_storage_group;
79 $path = $repo->getZonePath(
'deleted' ) .
80 '/' . $repo->getDeletedHashPath( $key ) . $key;
81 if ( isset( $row->fa_sha1 ) ) {
82 $sha1 = $row->fa_sha1;
85 $sha1 = LocalRepo::getHashFromKey( $key );
89 $inuse = (bool)$dbw->newSelectQueryBuilder()
94 $dbw->bitAnd(
'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE
96 ->caller( __METHOD__ )
101 if ( !$repo->fileExists(
$path ) ) {
102 $this->output(
"Notice - file '$key' not found in group '$group'\n" );
103 } elseif ( $inuse ) {
104 $this->output(
"Notice - file '$key' is still in use\n" );
105 } elseif ( !$repo->quickPurge(
$path ) ) {
106 $this->output(
"Unable to remove file $path, skipping\n" );
107 $file->releaseFileLock();
116 if ( $this->hasOption(
'force' ) ) {
117 $this->output(
"Got --force, deleting DB entry\n" );
119 $file->releaseFileLock();
125 $dbw->newDeleteQueryBuilder()
126 ->deleteFrom(
'filearchive' )
127 ->where( [
'fa_id' => $id ] )
128 ->caller( __METHOD__ )->execute();
129 $file->releaseFileLock();
132 $this->commitTransaction( $dbw, __METHOD__ );
133 $this->output(
"Done! [$count file(s)]\n" );