4 * Support functions for the deleteArchivedFiles script
11 require_once( "$IP/includes/filerepo/File.php" );
13 function DeleteArchivedFiles( $delete = false ) {
15 echo( "Use --delete to actually confirm this script\n" );
18 # Data should come off the master, wrapped in a transaction
19 $dbw = wfGetDB( DB_MASTER );
21 $tbl_arch = $dbw->tableName( 'filearchive' );
22 $repo = RepoGroup::singleton()->getLocalRepo();
23 # Get "active" revisions from the filearchive table
24 echo( "Searching for and deleting archived files...\n" );
25 $res = $dbw->query( "SELECT fa_id,fa_storage_group,fa_storage_key FROM $tbl_arch" );
27 while( $row = $dbw->fetchObject( $res ) ) {
28 $key = $row->fa_storage_key;
29 $group = $row->fa_storage_group;
31 $path = $repo->getZonePath( 'deleted' ).'/'.$repo->getDeletedHashPath($key).$key;
32 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
33 // Check if the file is used anywhere...
34 $inuse = $dbw->selectField( 'oldimage', '1',
35 array( 'oi_sha1' => $sha1,
36 'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
40 if ( $path && file_exists($path) && !$inuse ) {
41 unlink($path); // delete
43 $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
45 echo( "Notice - file '$key' not found in group '$group'\n" );
49 echo( "Done! [$count file(s)]\n" );