Escape message 'version-poweredby-others' on Special:Version
[lhc/web/wiklou.git] / maintenance / eraseArchivedFile.php
1 <?php
2 /**
3 * Delete archived (non-current) files from storage
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 * @author Aaron Schulz
23 */
24
25 require_once __DIR__ . '/Maintenance.php';
26
27 /**
28 * Maintenance script to delete archived (non-current) files from storage.
29 *
30 * @todo Maybe add some simple logging
31 *
32 * @ingroup Maintenance
33 * @since 1.22
34 */
35 class EraseArchivedFile extends Maintenance {
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;