MediaWiki  1.34.0
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 === '*' ) { // all versions by name
54  if ( !strlen( $filename ) ) {
55  $this->fatalError( "Missing --filename parameter." );
56  }
57  $afile = false;
58  } else { // specified version
59  $dbw = $this->getDB( DB_MASTER );
60  $fileQuery = ArchivedFile::getQueryInfo();
61  $row = $dbw->selectRow( $fileQuery['tables'], $fileQuery['fields'],
62  [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $filekey ],
63  __METHOD__, [], $fileQuery['joins'] );
64  if ( !$row ) {
65  $this->fatalError( "No deleted file exists with key '$filekey'." );
66  }
67  $filename = $row->fa_name;
68  $afile = ArchivedFile::newFromRow( $row );
69  }
70 
71  $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()->newFile( $filename );
72  if ( $file->exists() ) {
73  $this->fatalError( "File '$filename' is still a public file, use the delete form.\n" );
74  }
75 
76  $this->output( "Purging all thumbnails for file '$filename'..." );
77  $file->purgeCache();
78  $this->output( "done.\n" );
79 
80  if ( $afile instanceof ArchivedFile ) {
81  $this->scrubVersion( $afile );
82  } else {
83  $this->output( "Finding deleted versions of file '$filename'...\n" );
84  $this->scrubAllVersions( $filename );
85  $this->output( "Done\n" );
86  }
87  }
88 
89  protected function scrubAllVersions( $name ) {
90  $dbw = $this->getDB( DB_MASTER );
91  $fileQuery = ArchivedFile::getQueryInfo();
92  $res = $dbw->select( $fileQuery['tables'], $fileQuery['fields'],
93  [ 'fa_name' => $name, 'fa_storage_group' => 'deleted' ],
94  __METHOD__, [], $fileQuery['joins'] );
95  foreach ( $res as $row ) {
96  $this->scrubVersion( ArchivedFile::newFromRow( $row ) );
97  }
98  }
99 
100  protected function scrubVersion( ArchivedFile $archivedFile ) {
101  $key = $archivedFile->getStorageKey();
102  $name = $archivedFile->getName();
103  $ts = $archivedFile->getTimestamp();
104  $repo = RepoGroup::singleton()->getLocalRepo();
105  $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
106  if ( $this->hasOption( 'delete' ) ) {
107  $status = $repo->getBackend()->delete( [ 'src' => $path ] );
108  if ( $status->isOK() ) {
109  $this->output( "Deleted version '$key' ($ts) of file '$name'\n" );
110  } else {
111  $this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" );
112  // @phan-suppress-next-line PhanUndeclaredMethod
113  $this->output( print_r( $status->getErrorsArray(), true ) );
114  }
115  } else {
116  $this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" );
117  }
118  }
119 }
120 
121 $maintClass = EraseArchivedFile::class;
122 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
RepoGroup\singleton
static singleton()
Definition: RepoGroup.php:60
ArchivedFile\getTimestamp
getTimestamp()
Return upload timestamp.
Definition: ArchivedFile.php:469
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:504
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
true
return true
Definition: router.php:92
EraseArchivedFile
Maintenance script to delete archived (non-current) files from storage.
Definition: eraseArchivedFile.php:36
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
ArchivedFile\getQueryInfo
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archivedfile object.
Definition: ArchivedFile.php:228
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
$res
$res
Definition: testCompression.php:52
ArchivedFile\getStorageKey
getStorageKey()
Return the FileStore key (overriding base File class)
Definition: ArchivedFile.php:349
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
$maintClass
$maintClass
Definition: eraseArchivedFile.php:121
DB_MASTER
const DB_MASTER
Definition: defines.php:26
EraseArchivedFile\scrubAllVersions
scrubAllVersions( $name)
Definition: eraseArchivedFile.php:89
ArchivedFile
Class representing a row of the 'filearchive' table.
Definition: ArchivedFile.php:31
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
EraseArchivedFile\scrubVersion
scrubVersion(ArchivedFile $archivedFile)
Definition: eraseArchivedFile.php:100
ArchivedFile\getName
getName()
Return the file name.
Definition: ArchivedFile.php:309
EraseArchivedFile\__construct
__construct()
Default constructor.
Definition: eraseArchivedFile.php:37
$status
return $status
Definition: SyntaxHighlight.php:347
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
$path
$path
Definition: NoLocalSettings.php:25
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
ArchivedFile\newFromRow
static newFromRow( $row)
Loads a file object from the filearchive table.
Definition: ArchivedFile.php:212
EraseArchivedFile\execute
execute()
Do the actual work.
Definition: eraseArchivedFile.php:45