MediaWiki  1.23.0
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->mDescription = "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 = wfGetDB( DB_MASTER );
59  $row = $dbw->selectRow( 'filearchive', '*',
60  array( '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  $file->purgeHistory();
77  $this->output( "done.\n" );
78 
79  if ( $afile instanceof ArchivedFile ) {
80  $this->scrubVersion( $afile );
81  } else {
82  $this->output( "Finding deleted versions of file '$filename'...\n" );
83  $this->scrubAllVersions( $filename );
84  $this->output( "Done\n" );
85  }
86  }
87 
88  protected function scrubAllVersions( $name ) {
89  $dbw = wfGetDB( DB_MASTER );
90  $res = $dbw->select( 'filearchive', '*',
91  array( 'fa_name' => $name, 'fa_storage_group' => 'deleted' ),
92  __METHOD__ );
93  foreach ( $res as $row ) {
94  $this->scrubVersion( ArchivedFile::newFromRow( $row ) );
95  }
96  }
97 
98  protected function scrubVersion( ArchivedFile $archivedFile ) {
99  $key = $archivedFile->getStorageKey();
100  $name = $archivedFile->getName();
101  $ts = $archivedFile->getTimestamp();
102  $repo = RepoGroup::singleton()->getLocalRepo();
103  $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
104  if ( $this->hasOption( 'delete' ) ) {
105  $status = $repo->getBackend()->delete( array( 'src' => $path ) );
106  if ( $status->isOK() ) {
107  $this->output( "Deleted version '$key' ($ts) of file '$name'\n" );
108  } else {
109  $this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" );
110  $this->output( print_r( $status->getErrorsArray(), true ) );
111  }
112  } else {
113  $this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" );
114  }
115  }
116 }
117 
118 $maintClass = "EraseArchivedFile";
119 require_once RUN_MAINTENANCE_IF_MAIN;
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:53
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ArchivedFile\getTimestamp
getTimestamp()
Return upload timestamp.
Definition: ArchivedFile.php:413
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
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
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:297
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:1530
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
$maintClass
$maintClass
Definition: eraseArchivedFile.php:118
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
EraseArchivedFile\scrubAllVersions
scrubAllVersions( $name)
Definition: eraseArchivedFile.php:88
ArchivedFile
Class representing a row of the 'filearchive' table.
Definition: ArchivedFile.php:29
EraseArchivedFile\scrubVersion
scrubVersion(ArchivedFile $archivedFile)
Definition: eraseArchivedFile.php:98
ArchivedFile\getName
getName()
Return the file name.
Definition: ArchivedFile.php:261
EraseArchivedFile\__construct
__construct()
Default constructor.
Definition: eraseArchivedFile.php:36
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
$path
$path
Definition: NoLocalSettings.php:35
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\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:181
wfLocalFile
wfLocalFile( $title)
Get an object referring to a locally registered file.
Definition: GlobalFunctions.php:3704
$res
$res
Definition: database.txt:21
ArchivedFile\newFromRow
static newFromRow( $row)
Loads a file object from the filearchive table.
Definition: ArchivedFile.php:179
EraseArchivedFile\execute
execute()
Do the actual work.
Definition: eraseArchivedFile.php:44