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