MediaWiki  master
eraseArchivedFile.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
37  public function __construct() {
38  parent::__construct();
39  $this->addDescription( 'Erases traces of deleted files.' );
40  $this->addOption( 'delete', 'Perform the deletion' );
41  $this->addOption( 'filename', 'File name', false, true );
42  $this->addOption( 'filekey', 'File storage key (with extension) or "*"', true, true );
43  }
44 
45  public function execute() {
46  if ( !$this->hasOption( 'delete' ) ) {
47  $this->output( "Use --delete to actually confirm this script\n" );
48  }
49 
50  $filekey = $this->getOption( 'filekey' );
51  $filename = $this->getOption( 'filename' );
52 
53  if ( $filekey === '*' ) {
54  // all versions by name
55  if ( !strlen( $filename ) ) {
56  $this->fatalError( "Missing --filename parameter." );
57  }
58  $afile = false;
59  } else {
60  // specified version
61  $dbw = $this->getDB( DB_PRIMARY );
62  $fileQuery = ArchivedFile::getQueryInfo();
63  $row = $dbw->newSelectQueryBuilder()
64  ->select( $fileQuery['fields'] )
65  ->tables( $fileQuery['tables'] )
66  ->where( [
67  'fa_storage_group' => 'deleted',
68  'fa_storage_key' => $filekey
69  ] )
70  ->joinConds( $fileQuery['joins'] )
71  ->caller( __METHOD__ )
72  ->fetchRow();
73 
74  if ( !$row ) {
75  $this->fatalError( "No deleted file exists with key '$filekey'." );
76  }
77  $filename = $row->fa_name;
78  $afile = ArchivedFile::newFromRow( $row );
79  }
80 
81  $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()->newFile( $filename );
82  if ( $file->exists() ) {
83  $this->fatalError( "File '$filename' is still a public file, use the delete form.\n" );
84  }
85 
86  $this->output( "Purging all thumbnails for file '$filename'..." );
87  $file->purgeCache();
88  $this->output( "done.\n" );
89 
90  if ( $afile instanceof ArchivedFile ) {
91  $this->scrubVersion( $afile );
92  } else {
93  $this->output( "Finding deleted versions of file '$filename'...\n" );
94  $this->scrubAllVersions( $filename );
95  $this->output( "Done\n" );
96  }
97  }
98 
99  protected function scrubAllVersions( $name ) {
100  $dbw = $this->getDB( DB_PRIMARY );
101  $fileQuery = ArchivedFile::getQueryInfo();
102  $res = $dbw->newSelectQueryBuilder()
103  ->select( $fileQuery['fields'] )
104  ->tables( $fileQuery['tables'] )
105  ->where( [
106  'fa_name' => $name,
107  'fa_storage_group' => 'deleted'
108  ] )
109  ->joinConds( $fileQuery['joins'] )
110  ->caller( __METHOD__ )
111  ->fetchResultSet();
112  foreach ( $res as $row ) {
113  $this->scrubVersion( ArchivedFile::newFromRow( $row ) );
114  }
115  }
116 
117  protected function scrubVersion( ArchivedFile $archivedFile ) {
118  $key = $archivedFile->getStorageKey();
119  $name = $archivedFile->getName();
120  $ts = $archivedFile->getTimestamp();
121  $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
122  $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
123  if ( $this->hasOption( 'delete' ) ) {
124  $status = $repo->getBackend()->delete( [ 'src' => $path ] );
125  if ( $status->isOK() ) {
126  $this->output( "Deleted version '$key' ($ts) of file '$name'\n" );
127  } else {
128  $this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" );
129  $this->output( print_r( Status::wrap( $status )->getErrorsArray(), true ) );
130  }
131  } else {
132  $this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" );
133  }
134  }
135 }
136 
137 $maintClass = EraseArchivedFile::class;
138 require_once RUN_MAINTENANCE_IF_MAIN;
Deleted file in the 'filearchive' table.
getTimestamp()
Return upload timestamp.
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archivedfile object.
static newFromRow( $row)
Loads a file object from the filearchive table.
getStorageKey()
Return the FileStore key (overriding base File class)
getName()
Return the file name.
Maintenance script to delete archived (non-current) files from storage.
scrubVersion(ArchivedFile $archivedFile)
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Service locator for MediaWiki core services.
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:62
const DB_PRIMARY
Definition: defines.php:28
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42