a95643663a19e6855a3251b6a1032c2fb4c781df
[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 if( !$delete ) {
15 echo( "Use --delete to actually confirm this script\n" );
16 return;
17 }
18 # Data should come off the master, wrapped in a transaction
19 $dbw = wfGetDB( DB_MASTER );
20 $dbw->begin();
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" );
26 $count = 0;
27 while( $row = $dbw->fetchObject( $res ) ) {
28 $key = $row->fa_storage_key;
29 $group = $row->fa_storage_group;
30 $id = $row->fa_id;
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 ),
37 __METHOD__,
38 array( 'FOR UPDATE' )
39 );
40 if ( $path && file_exists($path) && !$inuse ) {
41 unlink($path); // delete
42 $count++;
43 $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
44 } else {
45 echo( "Notice - file '$key' not found in group '$group'\n" );
46 }
47 }
48 $dbw->commit();
49 echo( "Done! [$count file(s)]\n" );
50 }