(bug 13690) Fix PHP notice on accessing some URLs. parse_url() in some versions...
[mediawiki.git] / maintenance / deleteArchivedFiles.inc
blob98b409ec88f83b2dbfcdea039820485eaf2d1641
1 <?php
3 /**
4  * Support functions for the deleteArchivedFiles script
5  *
6  * @addtogroup Maintenance
7  * @author Aaron Schulz
8  */
10 require_once( "$IP/includes/FileStore.php" );
11 require_once( "$IP/includes/filerepo/File.php" );
13 function DeleteArchivedFiles( $delete = false ) {
15         # Data should come off the master, wrapped in a transaction
16         $dbw = wfGetDB( DB_MASTER );
17         
18         $transaction = new FSTransaction();
19         if( !FileStore::lock() ) {
20                 wfDebug( __METHOD__.": failed to acquire file store lock, aborting\n" );
21                 return false;
22         }
23         
24         $tbl_arch = $dbw->tableName( 'filearchive' );
25         
26         # Get "active" revisions from the filearchive table
27         echo( "Searching for and deleting archived files...\n" );
28         $res = $dbw->query( "SELECT fa_id,fa_storage_group,fa_storage_key FROM $tbl_arch" );
29         while( $row = $dbw->fetchObject( $res ) ) {
30                 $key = $row->fa_storage_key;
31                 $group = $row->fa_storage_group;
32                 $id = $row->fa_id;
33                 
34                 $store = FileStore::get( $group );
35                 if( $store ) {
36                         $path = $store->filePath( $key );
37                         $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
38                         $inuse = $dbw->selectField( 'oldimage', '1',
39                                 array( 'oi_sha1' => $sha1,
40                                         'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
41                                 __METHOD__, array( 'FOR UPDATE' ) );
42                         if ( $path && file_exists($path) && !$inuse ) {
43                                 $transaction->addCommit( FSTransaction::DELETE_FILE, $path );
44                                 $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
45                         } else {
46                                 echo( "Notice - file '$key' not found in group '$group'\n" );
47                         }
48                 } else {
49                         echo( "Notice - invalid file storage group '$group' for file '$key'\n" );
50                 }
51         }
52         echo( "done.\n" );
53         
54         $transaction->commit();