Undo previous commit. Schema changes are a no-no without permission first :-)
[lhc/web/wiklou.git] / maintenance / deleteArchivedFiles.inc
1 <?php
2
3 /**
4 * Support functions for the deleteArchivedFiles script
5 *
6 * @file
7 * @ingroup Maintenance
8 * @author Aaron Schulz
9 */
10
11 require_once( "$IP/includes/FileStore.php" );
12 require_once( "$IP/includes/filerepo/File.php" );
13
14 function DeleteArchivedFiles( $delete = false ) {
15
16 # Data should come off the master, wrapped in a transaction
17 $dbw = wfGetDB( DB_MASTER );
18
19 $transaction = new FSTransaction();
20 if( !FileStore::lock() ) {
21 wfDebug( __METHOD__.": failed to acquire file store lock, aborting\n" );
22 return false;
23 }
24
25 $tbl_arch = $dbw->tableName( 'filearchive' );
26
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;
33 $id = $row->fa_id;
34
35 $store = FileStore::get( $group );
36 if( $store ) {
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" );
46 } else {
47 echo( "Notice - file '$key' not found in group '$group'\n" );
48 }
49 } else {
50 echo( "Notice - invalid file storage group '$group' for file '$key'\n" );
51 }
52 }
53 echo( "done.\n" );
54
55 $transaction->commit();
56 }