MediaWiki  1.29.1
eraseArchivedFile.php
Go to the documentation of this file.
1 <?php
25 require_once __DIR__ . '/Maintenance.php';
26 
36  public function __construct() {
37  parent::__construct();
38  $this->addDescription( 'Erases traces of deleted files.' );
39  $this->addOption( 'delete', 'Perform the deletion' );
40  $this->addOption( 'filename', 'File name', false, true );
41  $this->addOption( 'filekey', 'File storage key (with extension) or "*"', true, true );
42  }
43 
44  public function execute() {
45  if ( !$this->hasOption( 'delete' ) ) {
46  $this->output( "Use --delete to actually confirm this script\n" );
47  }
48 
49  $filekey = $this->getOption( 'filekey' );
50  $filename = $this->getOption( 'filename' );
51 
52  if ( $filekey === '*' ) { // all versions by name
53  if ( !strlen( $filename ) ) {
54  $this->error( "Missing --filename parameter.", 1 );
55  }
56  $afile = false;
57  } else { // specified version
58  $dbw = $this->getDB( DB_MASTER );
59  $row = $dbw->selectRow( 'filearchive', '*',
60  [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $filekey ],
61  __METHOD__ );
62  if ( !$row ) {
63  $this->error( "No deleted file exists with key '$filekey'.", 1 );
64  }
65  $filename = $row->fa_name;
66  $afile = ArchivedFile::newFromRow( $row );
67  }
68 
69  $file = wfLocalFile( $filename );
70  if ( $file->exists() ) {
71  $this->error( "File '$filename' is still a public file, use the delete form.\n", 1 );
72  }
73 
74  $this->output( "Purging all thumbnails for file '$filename'..." );
75  $file->purgeCache();
76  $this->output( "done.\n" );
77 
78  if ( $afile instanceof ArchivedFile ) {
79  $this->scrubVersion( $afile );
80  } else {
81  $this->output( "Finding deleted versions of file '$filename'...\n" );
82  $this->scrubAllVersions( $filename );
83  $this->output( "Done\n" );
84  }
85  }
86 
87  protected function scrubAllVersions( $name ) {
88  $dbw = $this->getDB( DB_MASTER );
89  $res = $dbw->select( 'filearchive', '*',
90  [ 'fa_name' => $name, 'fa_storage_group' => 'deleted' ],
91  __METHOD__ );
92  foreach ( $res as $row ) {
93  $this->scrubVersion( ArchivedFile::newFromRow( $row ) );
94  }
95  }
96 
97  protected function scrubVersion( ArchivedFile $archivedFile ) {
98  $key = $archivedFile->getStorageKey();
99  $name = $archivedFile->getName();
100  $ts = $archivedFile->getTimestamp();
101  $repo = RepoGroup::singleton()->getLocalRepo();
102  $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
103  if ( $this->hasOption( 'delete' ) ) {
104  $status = $repo->getBackend()->delete( [ 'src' => $path ] );
105  if ( $status->isOK() ) {
106  $this->output( "Deleted version '$key' ($ts) of file '$name'\n" );
107  } else {
108  $this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" );
109  $this->output( print_r( $status->getErrorsArray(), true ) );
110  }
111  } else {
112  $this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" );
113  }
114  }
115 }
116 
117 $maintClass = "EraseArchivedFile";
118 require_once RUN_MAINTENANCE_IF_MAIN;
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:59
ArchivedFile\getTimestamp
getTimestamp()
Return upload timestamp.
Definition: ArchivedFile.php:455
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1049
EraseArchivedFile
Maintenance script to delete archived (non-current) files from storage.
Definition: eraseArchivedFile.php:35
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$res
$res
Definition: database.txt:21
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
ArchivedFile\getStorageKey
getStorageKey()
Return the FileStore key (overriding base File class)
Definition: ArchivedFile.php:337
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
$maintClass
$maintClass
Definition: eraseArchivedFile.php:117
DB_MASTER
const DB_MASTER
Definition: defines.php:26
EraseArchivedFile\scrubAllVersions
scrubAllVersions( $name)
Definition: eraseArchivedFile.php:87
ArchivedFile
Class representing a row of the 'filearchive' table.
Definition: ArchivedFile.php:29
EraseArchivedFile\scrubVersion
scrubVersion(ArchivedFile $archivedFile)
Definition: eraseArchivedFile.php:97
ArchivedFile\getName
getName()
Return the file name.
Definition: ArchivedFile.php:297
EraseArchivedFile\__construct
__construct()
Default constructor.
Definition: eraseArchivedFile.php:36
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:250
$path
$path
Definition: NoLocalSettings.php:26
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1251
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1956
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:392
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:236
wfLocalFile
wfLocalFile( $title)
Get an object referring to a locally registered file.
Definition: GlobalFunctions.php:3112
ArchivedFile\newFromRow
static newFromRow( $row)
Loads a file object from the filearchive table.
Definition: ArchivedFile.php:209
EraseArchivedFile\execute
execute()
Do the actual work.
Definition: eraseArchivedFile.php:44