MediaWiki REL1_35
eraseArchivedFile.php
Go to the documentation of this file.
1<?php
25
26require_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_MASTER );
62 $fileQuery = ArchivedFile::getQueryInfo();
63 $row = $dbw->selectRow( $fileQuery['tables'], $fileQuery['fields'],
64 [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $filekey ],
65 __METHOD__, [], $fileQuery['joins'] );
66 if ( !$row ) {
67 $this->fatalError( "No deleted file exists with key '$filekey'." );
68 }
69 $filename = $row->fa_name;
70 $afile = ArchivedFile::newFromRow( $row );
71 }
72
73 $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()->newFile( $filename );
74 if ( $file->exists() ) {
75 $this->fatalError( "File '$filename' is still a public file, use the delete form.\n" );
76 }
77
78 $this->output( "Purging all thumbnails for file '$filename'..." );
79 $file->purgeCache();
80 $this->output( "done.\n" );
81
82 if ( $afile instanceof ArchivedFile ) {
83 $this->scrubVersion( $afile );
84 } else {
85 $this->output( "Finding deleted versions of file '$filename'...\n" );
86 $this->scrubAllVersions( $filename );
87 $this->output( "Done\n" );
88 }
89 }
90
91 protected function scrubAllVersions( $name ) {
92 $dbw = $this->getDB( DB_MASTER );
93 $fileQuery = ArchivedFile::getQueryInfo();
94 $res = $dbw->select( $fileQuery['tables'], $fileQuery['fields'],
95 [ 'fa_name' => $name, 'fa_storage_group' => 'deleted' ],
96 __METHOD__, [], $fileQuery['joins'] );
97 foreach ( $res as $row ) {
98 $this->scrubVersion( ArchivedFile::newFromRow( $row ) );
99 }
100 }
101
102 protected function scrubVersion( ArchivedFile $archivedFile ) {
103 $key = $archivedFile->getStorageKey();
104 $name = $archivedFile->getName();
105 $ts = $archivedFile->getTimestamp();
106 $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
107 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
108 if ( $this->hasOption( 'delete' ) ) {
109 $status = $repo->getBackend()->delete( [ 'src' => $path ] );
110 if ( $status->isOK() ) {
111 $this->output( "Deleted version '$key' ($ts) of file '$name'\n" );
112 } else {
113 $this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" );
114 $this->output( print_r( Status::wrap( $status )->getErrorsArray(), true ) );
115 }
116 } else {
117 $this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" );
118 }
119 }
120}
121
122$maintClass = EraseArchivedFile::class;
123require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const RUN_MAINTENANCE_IF_MAIN
Class representing a row of 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 Stable to override.
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...
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
const DB_MASTER
Definition defines.php:29
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42