MediaWiki  REL1_22
eraseArchivedFile.php
Go to the documentation of this file.
00001 <?php
00025 require_once __DIR__ . '/Maintenance.php';
00026 
00035 class EraseArchivedFile extends Maintenance {
00036     public function __construct() {
00037         parent::__construct();
00038         $this->mDescription = "Erases traces of deleted files.";
00039         $this->addOption( 'delete', 'Perform the deletion' );
00040         $this->addOption( 'filename', 'File name', false, true );
00041         $this->addOption( 'filekey', 'File storage key (with extension) or "*"', true, true );
00042     }
00043 
00044     public function execute() {
00045         if ( !$this->hasOption( 'delete' ) ) {
00046             $this->output( "Use --delete to actually confirm this script\n" );
00047         }
00048 
00049         $filekey = $this->getOption( 'filekey' );
00050         $filename = $this->getOption( 'filename' );
00051 
00052         if ( $filekey === '*' ) { // all versions by name
00053             if ( !strlen( $filename ) ) {
00054                 $this->error( "Missing --filename parameter.", 1 );
00055             }
00056             $afile = false;
00057         } else { // specified version
00058             $dbw = wfGetDB( DB_MASTER );
00059             $row = $dbw->selectRow( 'filearchive', '*',
00060                 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $filekey ),
00061                 __METHOD__ );
00062             if ( !$row ) {
00063                 $this->error( "No deleted file exists with key '$filekey'.", 1 );
00064             }
00065             $filename = $row->fa_name;
00066             $afile = ArchivedFile::newFromRow( $row );
00067         }
00068 
00069         $file = wfLocalFile( $filename );
00070         if ( $file->exists() ) {
00071             $this->error( "File '$filename' is still a public file, use the delete form.\n", 1 );
00072         }
00073 
00074         $this->output( "Purging all thumbnails for file '$filename'..." );
00075         $file->purgeCache();
00076         $file->purgeHistory();
00077         $this->output( "done.\n" );
00078 
00079         if ( $afile instanceof ArchivedFile ) {
00080             $this->scrubVersion( $afile );
00081         } else {
00082             $this->output( "Finding deleted versions of file '$filename'...\n" );
00083             $this->scrubAllVersions( $filename );
00084             $this->output( "Done\n" );
00085         }
00086     }
00087 
00088     protected function scrubAllVersions( $name ) {
00089         $dbw = wfGetDB( DB_MASTER );
00090         $res = $dbw->select( 'filearchive', '*',
00091             array( 'fa_name' => $name, 'fa_storage_group' => 'deleted' ),
00092             __METHOD__ );
00093         foreach ( $res as $row ) {
00094             $this->scrubVersion( ArchivedFile::newFromRow( $row ) );
00095         }
00096     }
00097 
00098     protected function scrubVersion( ArchivedFile $archivedFile ) {
00099         $key = $archivedFile->getStorageKey();
00100         $name = $archivedFile->getName();
00101         $ts = $archivedFile->getTimestamp();
00102         $repo = RepoGroup::singleton()->getLocalRepo();
00103         $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
00104         if ( $this->hasOption( 'delete' ) ) {
00105             $status = $repo->getBackend()->delete( array( 'src' => $path ) );
00106             if ( $status->isOK() ) {
00107                 $this->output( "Deleted version '$key' ($ts) of file '$name'\n" );
00108             } else {
00109                 $this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" );
00110                 $this->output( print_r( $status->getErrorsArray(), true ) );
00111             }
00112         } else {
00113             $this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" );
00114         }
00115     }
00116 }
00117 
00118 $maintClass = "EraseArchivedFile";
00119 require_once RUN_MAINTENANCE_IF_MAIN;