3 * Scan the logging table and purge 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
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
27 * Maintenance script that scans the deletion log and purges affected files
30 * @ingroup Maintenance
32 class PurgeChangedFiles
extends Maintenance
{
34 * Mapping from type option to log type and actions.
37 private static $typeMappings = array(
39 'upload' => array( 'upload' ),
40 'import' => array( 'upload', 'interwiki' ),
43 'delete' => array( 'delete', 'revision' ),
44 'suppress' => array( 'delete', 'revision' ),
47 'upload' => array( 'overwrite', 'revert' ),
48 'move' => array( 'move', 'move_redir' ),
55 private $startTimestamp;
60 private $endTimestamp;
62 public function __construct() {
63 parent
::__construct();
64 $this->mDescription
= "Scan the logging table and purge files and thumbnails.";
65 $this->addOption( 'starttime', 'Starting timestamp', true, true );
66 $this->addOption( 'endtime', 'Ending timestamp', true, true );
67 $this->addOption( 'type', 'Comma-separated list of types of changes to send purges for (' .
68 implode( ',', array_keys( self
::$typeMappings ) ) . ',all)', false, true );
69 $this->addOption( 'htcp-dest', 'HTCP announcement destination (IP:port)', false, true );
70 $this->addOption( 'dry-run', 'Do not send purge requests' );
71 $this->addOption( 'sleep-per-batch', 'Milliseconds to sleep between batches', false, true );
72 $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
73 $this->setBatchSize( 100 );
76 public function execute() {
77 global $wgHTCPRouting;
79 if ( $this->hasOption( 'htcp-dest' ) ) {
80 $parts = explode( ':', $this->getOption( 'htcp-dest' ) );
81 if ( count( $parts ) < 2 ) {
82 // Add default htcp port
86 // Route all HTCP messages to provided host:port
87 $wgHTCPRouting = array(
88 '' => array( 'host' => $parts[0], 'port' => $parts[1] ),
90 $this->verbose( "HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" );
93 // Find out which actions we should be concerned with
94 $typeOpt = $this->getOption( 'type', 'all' );
95 $validTypes = array_keys( self
::$typeMappings );
96 if ( $typeOpt === 'all' ) {
97 // Convert 'all' to all registered types
98 $typeOpt = implode( ',', $validTypes );
100 $typeList = explode( ',', $typeOpt );
101 foreach ( $typeList as $type ) {
102 if ( !in_array( $type, $validTypes ) ) {
103 $this->error( "\nERROR: Unknown type: {$type}\n" );
104 $this->maybeHelp( true );
108 // Validate the timestamps
109 $dbr = $this->getDB( DB_SLAVE
);
110 $this->startTimestamp
= $dbr->timestamp( $this->getOption( 'starttime' ) );
111 $this->endTimestamp
= $dbr->timestamp( $this->getOption( 'endtime' ) );
113 if ( $this->startTimestamp
> $this->endTimestamp
) {
114 $this->error( "\nERROR: starttime after endtime\n" );
115 $this->maybeHelp( true );
118 // Turn on verbose when dry-run is enabled
119 if ( $this->hasOption( 'dry-run' ) ) {
120 $this->mOptions
['verbose'] = 1;
123 $this->verbose( 'Purging files that were: ' . implode( ', ', $typeList ) . "\n" );
124 foreach ( $typeList as $type ) {
125 $this->verbose( "Checking for {$type} files...\n" );
126 $this->purgeFromLogType( $type );
127 if ( !$this->hasOption( 'dry-run' ) ) {
128 $this->verbose( "...{$type} files purged.\n\n" );
134 * Purge cache and thumbnails for changes of the given type.
136 * @param string $type Type of change to find
138 protected function purgeFromLogType( $type ) {
139 $repo = RepoGroup
::singleton()->getLocalRepo();
140 $dbr = $this->getDB( DB_SLAVE
);
142 foreach ( self
::$typeMappings[$type] as $logType => $logActions ) {
143 $this->verbose( "Scanning for {$logType}/" . implode( ',', $logActions ) . "\n" );
147 array( 'log_title', 'log_timestamp', 'log_params' ),
149 'log_namespace' => NS_FILE
,
150 'log_type' => $logType,
151 'log_action' => $logActions,
152 'log_timestamp >= ' . $dbr->addQuotes( $this->startTimestamp
),
153 'log_timestamp <= ' . $dbr->addQuotes( $this->endTimestamp
),
159 foreach ( $res as $row ) {
160 $file = $repo->newFile( Title
::makeTitle( NS_FILE
, $row->log_title
) );
162 if ( $this->hasOption( 'dry-run' ) ) {
163 $this->verbose( "{$type}[{$row->log_timestamp}]: {$row->log_title}\n" );
167 // Purge current version and any versions in oldimage table
170 if ( $logType === 'delete' ) {
171 // If there is an orphaned storage file... delete it
172 if ( !$file->exists() && $repo->fileExists( $file->getPath() ) ) {
173 $dpath = $this->getDeletedPath( $repo, $file );
174 if ( $repo->fileExists( $dpath ) ) {
175 // Sanity check to avoid data loss
176 $repo->getBackend()->delete( array( 'src' => $file->getPath() ) );
177 $this->verbose( "Deleted orphan file: {$file->getPath()}.\n" );
179 $this->error( "File was not deleted: {$file->getPath()}.\n" );
183 // Purge items from fileachive table (rows are likely here)
184 $this->purgeFromArchiveTable( $repo, $file );
185 } elseif ( $logType === 'move' ) {
186 // Purge the target file as well
188 $params = unserialize( $row->log_params
);
189 if ( isset( $params['4::target'] ) ) {
190 $target = $params['4::target'];
191 $targetFile = $repo->newFile( Title
::makeTitle( NS_FILE
, $target ) );
192 $targetFile->purgeCache();
193 $this->verbose( "Purged file {$target}; move target @{$row->log_timestamp}.\n" );
197 $this->verbose( "Purged file {$row->log_title}; {$type} @{$row->log_timestamp}.\n" );
199 if ( $this->hasOption( 'sleep-per-batch' ) && ++
$bSize > $this->mBatchSize
) {
201 // sleep-per-batch is milliseconds, usleep wants micro seconds.
202 usleep( 1000 * (int)$this->getOption( 'sleep-per-batch' ) );
208 protected function purgeFromArchiveTable( LocalRepo
$repo, LocalFile
$file ) {
209 $dbr = $repo->getSlaveDB();
212 array( 'fa_archive_name' ),
213 array( 'fa_name' => $file->getName() ),
217 foreach ( $res as $row ) {
218 if ( $row->fa_archive_name
=== null ) {
219 // Was not an old version (current version names checked already)
222 $ofile = $repo->newFromArchiveName( $file->getTitle(), $row->fa_archive_name
);
223 // If there is an orphaned storage file still there...delete it
224 if ( !$file->exists() && $repo->fileExists( $ofile->getPath() ) ) {
225 $dpath = $this->getDeletedPath( $repo, $ofile );
226 if ( $repo->fileExists( $dpath ) ) {
227 // Sanity check to avoid data loss
228 $repo->getBackend()->delete( array( 'src' => $ofile->getPath() ) );
229 $this->output( "Deleted orphan file: {$ofile->getPath()}.\n" );
231 $this->error( "File was not deleted: {$ofile->getPath()}.\n" );
234 $file->purgeOldThumbnails( $row->fa_archive_name
);
238 protected function getDeletedPath( LocalRepo
$repo, LocalFile
$file ) {
239 $hash = $repo->getFileSha1( $file->getPath() );
240 $key = "{$hash}.{$file->getExtension()}";
242 return $repo->getDeletedHashPath( $key ) . $key;
246 * Send an output message iff the 'verbose' option has been provided.
248 * @param string $msg Message to output
250 protected function verbose( $msg ) {
251 if ( $this->hasOption( 'verbose' ) ) {
252 $this->output( $msg );
257 $maintClass = "PurgeChangedFiles";
258 require_once RUN_MAINTENANCE_IF_MAIN
;