MediaWiki  REL1_24
deleteArchivedFiles.inc
Go to the documentation of this file.
00001 <?php
00029 class DeleteArchivedFilesImplementation {
00030     public static 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,fa_sha1 FROM $tbl_arch" );
00039         $count = 0;
00040         foreach ( $res as $row ) {
00041             $key = $row->fa_storage_key;
00042             if ( !strlen( $key ) ) {
00043                 $output->handleOutput( "Entry with ID {$row->fa_id} has empty key, skipping\n" );
00044                 continue;
00045             }
00046             $group = $row->fa_storage_group;
00047             $id = $row->fa_id;
00048             $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
00049             if ( isset( $row->fa_sha1 ) ) {
00050                 $sha1 = $row->fa_sha1;
00051             } else {
00052                 // old row, populate from key
00053                 $sha1 = LocalRepo::getHashFromKey( $key );
00054             }
00055             // Check if the file is used anywhere...
00056             $inuse = $dbw->selectField(
00057                 'oldimage',
00058                 '1',
00059                 array(
00060                     'oi_sha1' => $sha1,
00061                     'oi_deleted & ' . File::DELETED_FILE => File::DELETED_FILE
00062                 ),
00063                 __METHOD__,
00064                 array( 'FOR UPDATE' )
00065             );
00066             if ( $path && $repo->fileExists( $path ) && !$inuse ) {
00067                 if ( $repo->quickPurge( $path ) ) {
00068                     $count++;
00069                     $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
00070                 } else {
00071                     $output->handleOutput( "Unable to remove file $path, skipping\n" );
00072                 }
00073             } else {
00074                 $output->handleOutput( "Notice - file '$key' not found in group '$group'\n" );
00075                 if ( $force ) {
00076                     $output->handleOutput( "Got --force, deleting DB entry\n" );
00077                     $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
00078                 }
00079             }
00080         }
00081         $dbw->commit( __METHOD__ );
00082         $output->handleOutput( "Done! [$count file(s)]\n" );
00083     }
00084 }