X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;ds=sidebyside;f=maintenance%2FdeleteArchivedFiles.inc;h=2eacb73b9e0d4239a4aa657d0be353e96dfb7e87;hb=fdcac385a203a8073626f41792f32d7dfe603aa3;hp=1880ed9c2367d29d747a04c6301e320796fab0ec;hpb=f2f48103b1dbc77ecc0070bfd5c51350c6ebcf85;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/deleteArchivedFiles.inc b/maintenance/deleteArchivedFiles.inc index 1880ed9c23..2eacb73b9e 100644 --- a/maintenance/deleteArchivedFiles.inc +++ b/maintenance/deleteArchivedFiles.inc @@ -1,61 +1,65 @@ -begin(); - - $transaction = new FSTransaction(); - if( !FileStore::lock() ) { - wfDebug( __METHOD__.": failed to acquire file store lock, aborting\n" ); - return false; - } - - $tbl_arch = $dbw->tableName( 'filearchive' ); - - # Get "active" revisions from the filearchive table - echo( "Searching for and deleting archived files...\n" ); - $res = $dbw->query( "SELECT fa_storage_group,fa_storage_key FROM $tbl_arch" ); - while( $row = $dbw->fetchObject( $res ) ) { - $key = $row->fa_storage_key; - $group = $row->fa_storage_group; - - $store = FileStore::get( $group ); - if ( $store ) { - $path = $store->filePath( $key ); - if ( $path && file_exists($path) ) { - $transaction->addCommit( FSTransaction::DELETE_FILE, $path ); - } else { - echo( "Notice - file '$key' not found in group '$group'\n" ); - } - } else { - echo( "Notice - invalid file storage group '$group'\n" ); - } - } - echo( "done.\n" ); - - $transaction->commit(); - - - # Delete as appropriate - echo( "Deleting filearchive rows..." ); - $dbw->query( "TRUNCATE TABLE $tbl_arch" ); - echo( "done.\n" ); - - # This bit's done - # Purge redundant text records - $dbw->commit(); - -} - -?> \ No newline at end of file +begin( __METHOD__ ); + $tbl_arch = $dbw->tableName( 'filearchive' ); + $repo = RepoGroup::singleton()->getLocalRepo(); + # Get "active" revisions from the filearchive table + $output->handleOutput( "Searching for and deleting archived files...\n" ); + $res = $dbw->query( "SELECT fa_id,fa_storage_group,fa_storage_key FROM $tbl_arch" ); + $count = 0; + foreach ( $res as $row ) { + $key = $row->fa_storage_key; + $group = $row->fa_storage_group; + $id = $row->fa_id; + $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key; + $sha1 = substr( $key, 0, strcspn( $key, '.' ) ); + // Check if the file is used anywhere... + $inuse = $dbw->selectField( 'oldimage', '1', + array( 'oi_sha1' => $sha1, + 'oi_deleted & ' . File::DELETED_FILE => File::DELETED_FILE ), + __METHOD__, + array( 'FOR UPDATE' ) + ); + if ( $path && file_exists( $path ) && !$inuse ) { + if( unlink( $path ) ) { // delete + $count++; + $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" ); + } else { + $output->handleOutput( "Unable to remove file $path, skipping\n" ); + } + } else { + $output->handleOutput( "Notice - file '$key' not found in group '$group'\n" ); + if ( $force ) { + $output->handleOutput( "Got --force, deleting DB entry\n" ); + $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" ); + } + } + } + $dbw->commit( __METHOD__ ); + $output->handleOutput( "Done! [$count file(s)]\n" ); + } +} \ No newline at end of file