4 * Support functions for the deleteArchivedFiles script
11 require_once( "$IP/includes/filerepo/File.php" );
13 function DeleteArchivedFiles( $delete = false ) {
14 # Data should come off the master, wrapped in a transaction
15 $dbw = wfGetDB( DB_MASTER );
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" );
23 while( $row = $dbw->fetchObject( $res ) ) {
24 $key = $row->fa_storage_key;
25 $group = $row->fa_storage_group;
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 ),
36 if ( $path && file_exists($path) && !$inuse ) {
37 unlink($path); // delete
39 $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
41 echo( "Notice - file '$key' not found in group '$group'\n" );
45 echo( "Done! [$count file(s)]\n" );