Localisation updates from http://translatewiki.net.
[mediawiki.git] / maintenance / purgeDeletedFiles.php
blob86658c52d48981cea5191e092bd2a36d909dc59e
1 <?php
2 /**
3 * Scan the deletion log and purges affected files within a timeframe.
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Maintenance script that scans the deletion log and purges affected files
28 * within a timeframe.
30 * @ingroup Maintenance
32 class PurgeDeletedFiles extends Maintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "Scan the logging table and purge files that where deleted.";
36 $this->addOption( 'starttime', 'Starting timestamp', false, true );
37 $this->addOption( 'endtime', 'Ending timestamp', false, true );
40 public function execute() {
41 $this->output( "Purging cache and thumbnails for deleted files...\n" );
42 $this->purgeFromLogType( 'delete' );
43 $this->output( "...deleted files purged.\n\n" );
45 $this->output( "Purging cache and thumbnails for suppressed files...\n" );
46 $this->purgeFromLogType( 'suppress' );
47 $this->output( "...suppressed files purged.\n" );
50 protected function purgeFromLogType( $logType ) {
51 $repo = RepoGroup::singleton()->getLocalRepo();
52 $db = $repo->getSlaveDB();
54 $conds = array(
55 'log_namespace' => NS_FILE,
56 'log_type' => $logType,
57 'log_action' => array( 'delete', 'revision' )
59 $start = $this->getOption( 'starttime' );
60 if ( $start ) {
61 $conds[] = 'log_timestamp >= ' . $db->addQuotes( $db->timestamp( $start ) );
63 $end = $this->getOption( 'endtime' );
64 if ( $end ) {
65 $conds[] = 'log_timestamp <= ' . $db->addQuotes( $db->timestamp( $end ) );
68 $res = $db->select( 'logging', array( 'log_title', 'log_timestamp' ), $conds, __METHOD__ );
69 foreach ( $res as $row ) {
70 $file = $repo->newFile( Title::makeTitle( NS_FILE, $row->log_title ) );
71 // If there is an orphaned storage file still there...delete it
72 if ( !$file->exists() && $repo->fileExists( $file->getPath() ) ) {
73 $dpath = $this->getDeletedPath( $repo, $file );
74 if ( $repo->fileExists( $dpath ) ) { // sanity check to avoid data loss
75 $repo->getBackend()->delete( array( 'src' => $file->getPath() ) );
76 $this->output( "Deleted orphan file: {$file->getPath()}.\n" );
77 } else {
78 $this->error( "File was not deleted: {$file->getPath()}.\n" );
81 // Purge current version and any versions in oldimage table
82 $file->purgeCache();
83 $file->purgeHistory();
84 // Purge items from fileachive table (rows are likely here)
85 $this->purgeFromArchiveTable( $repo, $file );
87 $this->output( "Purged file {$row->log_title}; deleted on {$row->log_timestamp}.\n" );
91 protected function purgeFromArchiveTable( LocalRepo $repo, LocalFile $file ) {
92 $db = $repo->getSlaveDB();
93 $res = $db->select( 'filearchive',
94 array( 'fa_archive_name' ),
95 array( 'fa_name' => $file->getName() ),
96 __METHOD__
98 foreach ( $res as $row ) {
99 $ofile = $repo->newFromArchiveName( $file->getTitle(), $row->fa_archive_name );
100 // If there is an orphaned storage file still there...delete it
101 if ( !$file->exists() && $repo->fileExists( $ofile->getPath() ) ) {
102 $dpath = $this->getDeletedPath( $repo, $ofile );
103 if ( $repo->fileExists( $dpath ) ) { // sanity check to avoid data loss
104 $repo->getBackend()->delete( array( 'src' => $ofile->getPath() ) );
105 $this->output( "Deleted orphan file: {$ofile->getPath()}.\n" );
106 } else {
107 $this->error( "File was not deleted: {$ofile->getPath()}.\n" );
110 $file->purgeOldThumbnails( $row->fa_archive_name );
114 protected function getDeletedPath( LocalRepo $repo, LocalFile $file ) {
115 $hash = $repo->getFileSha1( $file->getPath() );
116 $key = "{$hash}.{$file->getExtension()}";
117 return $repo->getDeletedHashPath( $key ) . $key;
121 $maintClass = "PurgeDeletedFiles";
122 require_once RUN_MAINTENANCE_IF_MAIN;