MediaWiki 1.44.0
eraseArchivedFile.php
Go to the documentation of this file.
1<?php
27
28// @codeCoverageIgnoreStart
29require_once __DIR__ . '/Maintenance.php';
30// @codeCoverageIgnoreEnd
31
41 public function __construct() {
42 parent::__construct();
43 $this->addDescription( 'Erases traces of deleted files.' );
44 $this->addOption( 'delete', 'Perform the deletion' );
45 $this->addOption( 'filename', 'File name', false, true );
46 $this->addOption( 'filekey', 'File storage key (with extension) or "*"', true, true );
47 }
48
49 public function execute() {
50 if ( !$this->hasOption( 'delete' ) ) {
51 $this->output( "Use --delete to actually confirm this script\n" );
52 }
53
54 $filekey = $this->getOption( 'filekey' );
55 $filename = $this->getOption( 'filename' );
56
57 if ( $filekey === '*' ) {
58 // all versions by name
59 if ( $filename === null || $filename === '' ) {
60 $this->fatalError( "Missing --filename parameter." );
61 }
62 $afile = false;
63 } else {
64 // specified version
65 $dbw = $this->getPrimaryDB();
66 $queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbw );
67 $queryBuilder->where( [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $filekey ] );
68 $row = $queryBuilder->caller( __METHOD__ )->fetchRow();
69
70 if ( !$row ) {
71 $this->fatalError( "No deleted file exists with key '$filekey'." );
72 }
73 $filename = $row->fa_name;
74 $afile = ArchivedFile::newFromRow( $row );
75 }
76
77 $file = $this->getServiceContainer()->getRepoGroup()->getLocalRepo()->newFile( $filename );
78 if ( $file->exists() ) {
79 $this->fatalError( "File '$filename' is still a public file, use the delete form.\n" );
80 }
81
82 $this->output( "Purging all thumbnails for file '$filename'..." );
83 $file->purgeCache();
84 $this->output( "done.\n" );
85
86 if ( $afile instanceof ArchivedFile ) {
87 $this->scrubVersion( $afile );
88 } else {
89 $this->output( "Finding deleted versions of file '$filename'...\n" );
90 $this->scrubAllVersions( $filename );
91 $this->output( "Done\n" );
92 }
93 }
94
95 protected function scrubAllVersions( string $name ) {
96 $dbw = $this->getPrimaryDB();
97 $queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbw );
98 $queryBuilder->where( [ 'fa_name' => $name, 'fa_storage_group' => 'deleted' ] );
99 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
100 foreach ( $res as $row ) {
101 $this->scrubVersion( ArchivedFile::newFromRow( $row ) );
102 }
103 }
104
105 protected function scrubVersion( ArchivedFile $archivedFile ) {
106 $key = $archivedFile->getStorageKey();
107 $name = $archivedFile->getName();
108 $ts = $archivedFile->getTimestamp();
109 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
110 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
111 if ( $this->hasOption( 'delete' ) ) {
112 $status = $repo->getBackend()->delete( [ 'src' => $path ] );
113 if ( $status->isOK() ) {
114 $this->output( "Deleted version '$key' ($ts) of file '$name'\n" );
115 } else {
116 $this->output( "Failed to delete version '$key' ($ts) of file '$name'\n" );
117 $this->error( $status );
118 }
119 } else {
120 $this->output( "Would delete version '{$key}' ({$ts}) of file '$name'\n" );
121 }
122 }
123}
124
125// @codeCoverageIgnoreStart
126$maintClass = EraseArchivedFile::class;
127require_once RUN_MAINTENANCE_IF_MAIN;
128// @codeCoverageIgnoreEnd
Maintenance script to delete archived (non-current) files from storage.
scrubVersion(ArchivedFile $archivedFile)
scrubAllVersions(string $name)
execute()
Do the actual work.
__construct()
Default constructor.
Deleted file in the 'filearchive' table.
getTimestamp()
Return upload timestamp.
getStorageKey()
Return the FileStore key (overriding base File class)
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.