MediaWiki
REL1_20
|
00001 <?php 00029 class DeleteArchivedFilesImplementation { 00030 static public function doDelete( $output, $force ) { 00031 # Data should come off the master, wrapped in a transaction 00032 $dbw = wfGetDB( DB_MASTER ); 00033 $dbw->begin( __METHOD__ ); 00034 $tbl_arch = $dbw->tableName( 'filearchive' ); 00035 $repo = RepoGroup::singleton()->getLocalRepo(); 00036 # Get "active" revisions from the filearchive table 00037 $output->handleOutput( "Searching for and deleting archived files...\n" ); 00038 $res = $dbw->query( "SELECT fa_id,fa_storage_group,fa_storage_key FROM $tbl_arch" ); 00039 $count = 0; 00040 foreach ( $res as $row ) { 00041 $key = $row->fa_storage_key; 00042 $group = $row->fa_storage_group; 00043 $id = $row->fa_id; 00044 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key; 00045 $sha1 = substr( $key, 0, strcspn( $key, '.' ) ); 00046 // Check if the file is used anywhere... 00047 $inuse = $dbw->selectField( 'oldimage', '1', 00048 array( 'oi_sha1' => $sha1, 00049 'oi_deleted & ' . File::DELETED_FILE => File::DELETED_FILE ), 00050 __METHOD__, 00051 array( 'FOR UPDATE' ) 00052 ); 00053 if ( $path && $repo->fileExists( $path ) && !$inuse ) { 00054 if ( $repo->quickPurge( $path ) ) { 00055 $count++; 00056 $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" ); 00057 } else { 00058 $output->handleOutput( "Unable to remove file $path, skipping\n" ); 00059 } 00060 } else { 00061 $output->handleOutput( "Notice - file '$key' not found in group '$group'\n" ); 00062 if ( $force ) { 00063 $output->handleOutput( "Got --force, deleting DB entry\n" ); 00064 $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" ); 00065 } 00066 } 00067 } 00068 $dbw->commit( __METHOD__ ); 00069 $output->handleOutput( "Done! [$count file(s)]\n" ); 00070 } 00071 }