Merge "docs: Fix typo"
[mediawiki.git] / maintenance / purgeChangedFiles.php
blobd06b06610fa751e90fcdadd6c99b9d271ae0186a
1 <?php
2 /**
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
20 * @file
21 * @ingroup Maintenance
24 use MediaWiki\Maintenance\Maintenance;
25 use MediaWiki\Title\Title;
27 // @codeCoverageIgnoreStart
28 require_once __DIR__ . '/Maintenance.php';
29 // @codeCoverageIgnoreEnd
31 /**
32 * Maintenance script that scans the deletion log and purges affected files
33 * within a timeframe.
35 * @ingroup Maintenance
37 class PurgeChangedFiles extends Maintenance {
38 /**
39 * Mapping from type option to log type and actions.
40 * @var array
42 private static $typeMappings = [
43 'created' => [
44 'upload' => [ 'upload' ],
45 'import' => [ 'upload', 'interwiki' ],
47 'deleted' => [
48 'delete' => [ 'delete', 'revision' ],
49 'suppress' => [ 'delete', 'revision' ],
51 'modified' => [
52 'upload' => [ 'overwrite', 'revert' ],
53 'move' => [ 'move', 'move_redir' ],
57 /**
58 * @var string
60 private $startTimestamp;
62 /**
63 * @var string
65 private $endTimestamp;
67 public function __construct() {
68 parent::__construct();
69 $this->addDescription( 'Scan the logging table and purge files and thumbnails.' );
70 $this->addOption( 'starttime', 'Starting timestamp', true, true );
71 $this->addOption( 'endtime', 'Ending timestamp', true, true );
72 $this->addOption( 'type', 'Comma-separated list of types of changes to send purges for (' .
73 implode( ',', array_keys( self::$typeMappings ) ) . ',all)', false, true );
74 $this->addOption( 'htcp-dest', 'HTCP announcement destination (IP:port)', false, true );
75 $this->addOption( 'dry-run', 'Do not send purge requests' );
76 $this->addOption( 'sleep-per-batch', 'Milliseconds to sleep between batches', false, true );
77 $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
78 $this->setBatchSize( 100 );
81 public function execute() {
82 global $wgHTCPRouting;
84 if ( $this->hasOption( 'htcp-dest' ) ) {
85 $parts = explode( ':', $this->getOption( 'htcp-dest' ), 2 );
86 if ( count( $parts ) < 2 ) {
87 // Add default htcp port
88 $parts[] = '4827';
91 // Route all HTCP messages to provided host:port
92 $wgHTCPRouting = [
93 '' => [ 'host' => $parts[0], 'port' => $parts[1] ],
95 $this->verbose( "HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" );
98 // Find out which actions we should be concerned with
99 $typeOpt = $this->getOption( 'type', 'all' );
100 if ( $typeOpt === 'all' ) {
101 // Convert 'all' to all registered types
102 $typeOpt = implode( ',', array_keys( self::$typeMappings ) );
104 $typeList = explode( ',', $typeOpt );
105 foreach ( $typeList as $type ) {
106 if ( !isset( self::$typeMappings[$type] ) ) {
107 $this->error( "\nERROR: Unknown type: {$type}\n" );
108 $this->maybeHelp( true );
112 // Validate the timestamps
113 $dbr = $this->getReplicaDB();
114 $this->startTimestamp = $dbr->timestamp( $this->getOption( 'starttime' ) );
115 $this->endTimestamp = $dbr->timestamp( $this->getOption( 'endtime' ) );
117 if ( $this->startTimestamp > $this->endTimestamp ) {
118 $this->error( "\nERROR: starttime after endtime\n" );
119 $this->maybeHelp( true );
122 // Turn on verbose when dry-run is enabled
123 if ( $this->hasOption( 'dry-run' ) ) {
124 $this->setOption( 'verbose', 1 );
127 $this->verbose( 'Purging files that were: ' . implode( ', ', $typeList ) . "\n" );
128 foreach ( $typeList as $type ) {
129 $this->verbose( "Checking for {$type} files...\n" );
130 $this->purgeFromLogType( $type );
131 if ( !$this->hasOption( 'dry-run' ) ) {
132 $this->verbose( "...{$type} files purged.\n\n" );
138 * Purge cache and thumbnails for changes of the given type.
140 * @param string $type Type of change to find
142 protected function purgeFromLogType( $type ) {
143 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
144 $dbr = $this->getReplicaDB();
146 foreach ( self::$typeMappings[$type] as $logType => $logActions ) {
147 $this->verbose( "Scanning for {$logType}/" . implode( ',', $logActions ) . "\n" );
149 $res = $dbr->newSelectQueryBuilder()
150 ->select( [ 'log_title', 'log_timestamp', 'log_params' ] )
151 ->from( 'logging' )
152 ->where( [
153 'log_namespace' => NS_FILE,
154 'log_type' => $logType,
155 'log_action' => $logActions,
156 $dbr->expr( 'log_timestamp', '>=', $this->startTimestamp ),
157 $dbr->expr( 'log_timestamp', '<=', $this->endTimestamp ),
159 ->caller( __METHOD__ )->fetchResultSet();
161 $bSize = 0;
162 foreach ( $res as $row ) {
163 $file = $repo->newFile( Title::makeTitle( NS_FILE, $row->log_title ) );
165 if ( $this->hasOption( 'dry-run' ) ) {
166 $this->verbose( "{$type}[{$row->log_timestamp}]: {$row->log_title}\n" );
167 continue;
170 // Purge current version and its thumbnails
171 $file->purgeCache();
172 // Purge the old versions and their thumbnails
173 foreach ( $file->getHistory() as $oldFile ) {
174 $oldFile->purgeCache();
177 if ( $logType === 'delete' ) {
178 // If there is an orphaned storage file... delete it
179 if ( !$file->exists() && $repo->fileExists( $file->getPath() ) ) {
180 $dpath = $this->getDeletedPath( $repo, $file );
181 if ( $repo->fileExists( $dpath ) ) {
182 // Check to avoid data loss
183 $repo->getBackend()->delete( [ 'src' => $file->getPath() ] );
184 $this->verbose( "Deleted orphan file: {$file->getPath()}.\n" );
185 } else {
186 $this->error( "File was not deleted: {$file->getPath()}.\n" );
190 // Purge items from fileachive table (rows are likely here)
191 $this->purgeFromArchiveTable( $repo, $file );
192 } elseif ( $logType === 'move' ) {
193 // Purge the target file as well
195 $params = unserialize( $row->log_params );
196 if ( isset( $params['4::target'] ) ) {
197 $target = $params['4::target'];
198 $targetFile = $repo->newFile( Title::makeTitle( NS_FILE, $target ) );
199 $targetFile->purgeCache();
200 $this->verbose( "Purged file {$target}; move target @{$row->log_timestamp}.\n" );
204 $this->verbose( "Purged file {$row->log_title}; {$type} @{$row->log_timestamp}.\n" );
206 if ( $this->hasOption( 'sleep-per-batch' ) && ++$bSize > $this->getBatchSize() ) {
207 $bSize = 0;
208 // sleep-per-batch is milliseconds, usleep wants micro seconds.
209 usleep( 1000 * (int)$this->getOption( 'sleep-per-batch' ) );
215 protected function purgeFromArchiveTable( LocalRepo $repo, LocalFile $file ) {
216 $dbr = $repo->getReplicaDB();
217 $res = $dbr->newSelectQueryBuilder()
218 ->select( [ 'fa_archive_name' ] )
219 ->from( 'filearchive' )
220 ->where( [ 'fa_name' => $file->getName() ] )
221 ->caller( __METHOD__ )->fetchResultSet();
223 foreach ( $res as $row ) {
224 if ( $row->fa_archive_name === null ) {
225 // Was not an old version (current version names checked already)
226 continue;
228 $ofile = $repo->newFromArchiveName( $file->getTitle(), $row->fa_archive_name );
229 // If there is an orphaned storage file still there...delete it
230 if ( !$file->exists() && $repo->fileExists( $ofile->getPath() ) ) {
231 $dpath = $this->getDeletedPath( $repo, $ofile );
232 if ( $repo->fileExists( $dpath ) ) {
233 // Check to avoid data loss
234 $repo->getBackend()->delete( [ 'src' => $ofile->getPath() ] );
235 $this->output( "Deleted orphan file: {$ofile->getPath()}.\n" );
236 } else {
237 $this->error( "File was not deleted: {$ofile->getPath()}.\n" );
240 $file->purgeOldThumbnails( $row->fa_archive_name );
244 protected function getDeletedPath( LocalRepo $repo, LocalFile $file ) {
245 $hash = $repo->getFileSha1( $file->getPath() );
246 $key = "{$hash}.{$file->getExtension()}";
248 return $repo->getDeletedHashPath( $key ) . $key;
252 * Send an output message iff the 'verbose' option has been provided.
254 * @param string $msg Message to output
256 protected function verbose( $msg ) {
257 if ( $this->hasOption( 'verbose' ) ) {
258 $this->output( $msg );
263 // @codeCoverageIgnoreStart
264 $maintClass = PurgeChangedFiles::class;
265 require_once RUN_MAINTENANCE_IF_MAIN;
266 // @codeCoverageIgnoreEnd