4 * Support functions for the deleteArchivedFiles script
11 require_once( "$IP/includes/FileStore.php" );
12 require_once( "$IP/includes/filerepo/File.php" );
14 function DeleteArchivedFiles( $delete = false ) {
16 # Data should come off the master, wrapped in a transaction
17 $dbw = wfGetDB( DB_MASTER );
19 $transaction = new FSTransaction();
20 if( !FileStore::lock() ) {
21 wfDebug( __METHOD__.": failed to acquire file store lock, aborting\n" );
25 $tbl_arch = $dbw->tableName( 'filearchive' );
27 # Get "active" revisions from the filearchive table
28 echo( "Searching for and deleting archived files...\n" );
29 $res = $dbw->query( "SELECT fa_id,fa_storage_group,fa_storage_key FROM $tbl_arch" );
30 while( $row = $dbw->fetchObject( $res ) ) {
31 $key = $row->fa_storage_key;
32 $group = $row->fa_storage_group;
35 $store = FileStore::get( $group );
37 $path = $store->filePath( $key );
38 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
39 $inuse = $dbw->selectField( 'oldimage', '1',
40 array( 'oi_sha1' => $sha1,
41 'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
42 __METHOD__, array( 'FOR UPDATE' ) );
43 if ( $path && file_exists($path) && !$inuse ) {
44 $transaction->addCommit( FSTransaction::DELETE_FILE, $path );
45 $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
47 echo( "Notice - file '$key' not found in group '$group'\n" );
50 echo( "Notice - invalid file storage group '$group' for file '$key'\n" );
55 $transaction->commit();