Make sure this commits too
[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/filerepo/File.php" );
12
13 function DeleteArchivedFiles( $delete = false ) {
14 # Data should come off the master, wrapped in a transaction
15 $dbw = wfGetDB( DB_MASTER );
16 $dbw->begin();
17 $tbl_arch = $dbw->tableName( 'filearchive' );
18 $repo = RepoGroup::singleton()->getLocalRepo();
19 # Get "active" revisions from the filearchive table
20 echo( "Searching for and deleting archived files...\n" );
21 $res = $dbw->query( "SELECT fa_id,fa_storage_group,fa_storage_key FROM $tbl_arch" );
22 $count = 0;
23 while( $row = $dbw->fetchObject( $res ) ) {
24 $key = $row->fa_storage_key;
25 $group = $row->fa_storage_group;
26 $id = $row->fa_id;
27 $path = $repo->getZonePath( 'deleted' ).'/'.$repo->getDeletedHashPath($key).$key;
28 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
29 // Check if the file is used anywhere...
30 $inuse = $dbw->selectField( 'oldimage', '1',
31 array( 'oi_sha1' => $sha1,
32 'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
33 __METHOD__,
34 array( 'FOR UPDATE' )
35 );
36 if ( $path && file_exists($path) && !$inuse ) {
37 unlink($path); // delete
38 $count++;
39 $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
40 } else {
41 echo( "Notice - file '$key' not found in group '$group'\n" );
42 }
43 }
44 $dbw->commit();
45 echo( "Done! [$count file(s)]\n" );
46 }