MediaWiki  REL1_22
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             $group = $row->fa_storage_group;
00043             $id = $row->fa_id;
00044             $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
00045             if ( isset( $row->fa_sha1 ) ) {
00046                 $sha1 = $row->fa_sha1;
00047             } else {
00048                 // old row, populate from key
00049                 $sha1 = LocalRepo::getHashFromKey( $key );
00050             }
00051             // Check if the file is used anywhere...
00052             $inuse = $dbw->selectField( 'oldimage', '1',
00053                 array( 'oi_sha1' => $sha1,
00054                 'oi_deleted & ' . File::DELETED_FILE => File::DELETED_FILE ),
00055                 __METHOD__,
00056                 array( 'FOR UPDATE' )
00057             );
00058             if ( $path && $repo->fileExists( $path ) && !$inuse ) {
00059                 if ( $repo->quickPurge( $path ) ) {
00060                     $count++;
00061                     $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
00062                 } else {
00063                     $output->handleOutput( "Unable to remove file $path, skipping\n" );
00064                 }
00065             } else {
00066                 $output->handleOutput( "Notice - file '$key' not found in group '$group'\n" );
00067                 if ( $force ) {
00068                     $output->handleOutput( "Got --force, deleting DB entry\n" );
00069                     $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" );
00070                 }
00071             }
00072         }
00073         $dbw->commit( __METHOD__ );
00074         $output->handleOutput( "Done! [$count file(s)]\n" );
00075     }
00076 }