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 );
16 $tbl_arch = $dbw->tableName( 'filearchive' );
17 $repo = RepoGroup::singleton()->getLocalRepo();
18 # Get "active" revisions from the filearchive table
19 echo( "Searching for and deleting archived files...\n" );
20 $res = $dbw->query( "SELECT fa_id,fa_storage_group,fa_storage_key FROM $tbl_arch" );
22 while( $row = $dbw->fetchObject( $res ) ) {
23 $key = $row->fa_storage_key;
24 $group = $row->fa_storage_group;
26 $path = $repo->getZonePath( 'deleted' ).'/'.$repo->getDeletedHashPath($key).$key;
27 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
28 // Check if the file is used anywhere...
29 $inuse = $dbw->selectField( 'oldimage', '1',
30 array( 'oi_sha1' => $sha1,
31 'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
35 if ( $path && file_exists($path) && !$inuse ) {
36 unlink($path); // delete
38 $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
40 echo( "Notice - file '$key' not found in group '$group'\n" );
43 echo( "Done! [$count file(s)]\n" );