SearchResultSet: remove hasResults(), unused
[mediawiki.git] / maintenance / purgeChangedFiles.php
blob1e702dea779b29af871297dfb9e06c7a5f28e1e3
1 <?php
2 /**
3 * Scan the logging table and purge affected files within a timeframe.
5 * @section LICENSE
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
22 * @ingroup Maintenance
25 require_once __DIR__ . '/Maintenance.php';
27 /**
28 * Maintenance script that scans the deletion log and purges affected files
29 * within a timeframe.
31 * @ingroup Maintenance
33 class PurgeChangedFiles extends Maintenance {
34 /**
35 * Mapping from type option to log type and actions.
36 * @var array
38 private static $typeMappings = array(
39 'created' => array(
40 'upload' => array( 'upload' ),
41 'import' => array( 'upload', 'interwiki' ),
43 'deleted' => array(
44 'delete' => array( 'delete', 'revision' ),
45 'suppress' => array( 'delete', 'revision' ),
47 'modified' => array(
48 'upload' => array( 'overwrite', 'revert' ),
49 'move' => array( 'move', 'move_redir' ),
53 /**
54 * @var string
56 private $startTimestamp;
58 /**
59 * @var string
61 private $endTimestamp;
63 public function __construct() {
64 parent::__construct();
65 $this->mDescription = "Scan the logging table and purge files and thumbnails.";
66 $this->addOption( 'starttime', 'Starting timestamp', true, true );
67 $this->addOption( 'endtime', 'Ending timestamp', true, true );
68 $this->addOption( 'type', 'Comma-separated list of types of changes to send purges for (' .
69 implode( ',', array_keys( self::$typeMappings ) ) . ',all)', false, true );
70 $this->addOption( 'htcp-dest', 'HTCP announcement destination (IP:port)', false, true );
71 $this->addOption( 'dry-run', 'Do not send purge requests' );
72 $this->addOption( 'sleep-per-batch', 'Milliseconds to sleep between batches', false, true );
73 $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
74 $this->setBatchSize( 100 );
77 public function execute() {
78 global $wgHTCPRouting;
80 if ( $this->hasOption( 'htcp-dest' ) ) {
81 $parts = explode( ':', $this->getOption( 'htcp-dest' ) );
82 if ( count( $parts ) < 2 ) {
83 // Add default htcp port
84 $parts[] = '4827';
87 // Route all HTCP messages to provided host:port
88 $wgHTCPRouting = array(
89 '' => array( 'host' => $parts[0], 'port' => $parts[1] ),
91 $this->verbose( "HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" );
94 // Find out which actions we should be concerned with
95 $typeOpt = $this->getOption( 'type', 'all' );
96 $validTypes = array_keys( self::$typeMappings );
97 if ( $typeOpt === 'all' ) {
98 // Convert 'all' to all registered types
99 $typeOpt = implode( ',', $validTypes );
101 $typeList = explode( ',', $typeOpt );
102 foreach ( $typeList as $type ) {
103 if ( !in_array( $type, $validTypes ) ) {
104 $this->error( "\nERROR: Unknown type: {$type}\n" );
105 $this->maybeHelp( true );
109 // Validate the timestamps
110 $dbr = $this->getDB( DB_SLAVE );
111 $this->startTimestamp = $dbr->timestamp( $this->getOption( 'starttime' ) );
112 $this->endTimestamp = $dbr->timestamp( $this->getOption( 'endtime' ) );
114 if ( $this->startTimestamp > $this->endTimestamp ) {
115 $this->error( "\nERROR: starttime after endtime\n" );
116 $this->maybeHelp( true );
119 // Turn on verbose when dry-run is enabled
120 if ( $this->hasOption( 'dry-run' ) ) {
121 $this->mOptions['verbose'] = 1;
124 $this->verbose( 'Purging files that were: ' . implode( ', ', $typeList ) . "\n" );
125 foreach ( $typeList as $type ) {
126 $this->verbose( "Checking for {$type} files...\n" );
127 $this->purgeFromLogType( $type );
128 if ( !$this->hasOption( 'dry-run' ) ) {
129 $this->verbose( "...{$type} files purged.\n\n" );
135 * Purge cache and thumbnails for changes of the given type.
137 * @param string $type Type of change to find
139 protected function purgeFromLogType( $type ) {
140 $repo = RepoGroup::singleton()->getLocalRepo();
141 $dbr = $this->getDB( DB_SLAVE );
143 foreach ( self::$typeMappings[$type] as $logType => $logActions ) {
144 $this->verbose( "Scanning for {$logType}/" . implode( ',', $logActions ) . "\n" );
146 $res = $dbr->select(
147 'logging',
148 array( 'log_title', 'log_timestamp', 'log_params' ),
149 array(
150 'log_namespace' => NS_FILE,
151 'log_type' => $logType,
152 'log_action' => $logActions,
153 'log_timestamp >= ' . $dbr->addQuotes( $this->startTimestamp ),
154 'log_timestamp <= ' . $dbr->addQuotes( $this->endTimestamp ),
156 __METHOD__
159 $bSize = 0;
160 foreach ( $res as $row ) {
161 $file = $repo->newFile( Title::makeTitle( NS_FILE, $row->log_title ) );
163 if ( $this->hasOption( 'dry-run' ) ) {
164 $this->verbose( "{$type}[{$row->log_timestamp}]: {$row->log_title}\n" );
165 continue;
168 // Purge current version and any versions in oldimage table
169 $file->purgeCache();
170 $file->purgeHistory();
172 if ( $logType === 'delete' ) {
173 // If there is an orphaned storage file... delete it
174 if ( !$file->exists() && $repo->fileExists( $file->getPath() ) ) {
175 $dpath = $this->getDeletedPath( $repo, $file );
176 if ( $repo->fileExists( $dpath ) ) {
177 // Sanity check to avoid data loss
178 $repo->getBackend()->delete( array( 'src' => $file->getPath() ) );
179 $this->verbose( "Deleted orphan file: {$file->getPath()}.\n" );
180 } else {
181 $this->error( "File was not deleted: {$file->getPath()}.\n" );
185 // Purge items from fileachive table (rows are likely here)
186 $this->purgeFromArchiveTable( $repo, $file );
187 } elseif ( $logType === 'move' ) {
188 // Purge the target file as well
190 $params = unserialize( $row->log_params );
191 if ( isset( $params['4::target'] ) ) {
192 $target = $params['4::target'];
193 $targetFile = $repo->newFile( Title::makeTitle( NS_FILE, $target ) );
194 $targetFile->purgeCache();
195 $targetFile->purgeHistory();
196 $this->verbose( "Purged file {$target}; move target @{$row->log_timestamp}.\n" );
200 $this->verbose( "Purged file {$row->log_title}; {$type} @{$row->log_timestamp}.\n" );
202 if ( $this->hasOption( 'sleep-per-batch' ) && ++$bSize > $this->mBatchSize ) {
203 $bSize = 0;
204 // sleep-per-batch is milliseconds, usleep wants micro seconds.
205 usleep( 1000 * (int)$this->getOption( 'sleep-per-batch' ) );
211 protected function purgeFromArchiveTable( LocalRepo $repo, LocalFile $file ) {
212 $dbr = $repo->getSlaveDB();
213 $res = $dbr->select(
214 'filearchive',
215 array( 'fa_archive_name' ),
216 array( 'fa_name' => $file->getName() ),
217 __METHOD__
220 foreach ( $res as $row ) {
221 if ( $row->fa_archive_name === null ) {
222 // Was not an old version (current version names checked already)
223 continue;
225 $ofile = $repo->newFromArchiveName( $file->getTitle(), $row->fa_archive_name );
226 // If there is an orphaned storage file still there...delete it
227 if ( !$file->exists() && $repo->fileExists( $ofile->getPath() ) ) {
228 $dpath = $this->getDeletedPath( $repo, $ofile );
229 if ( $repo->fileExists( $dpath ) ) {
230 // Sanity check to avoid data loss
231 $repo->getBackend()->delete( array( 'src' => $ofile->getPath() ) );
232 $this->output( "Deleted orphan file: {$ofile->getPath()}.\n" );
233 } else {
234 $this->error( "File was not deleted: {$ofile->getPath()}.\n" );
237 $file->purgeOldThumbnails( $row->fa_archive_name );
241 protected function getDeletedPath( LocalRepo $repo, LocalFile $file ) {
242 $hash = $repo->getFileSha1( $file->getPath() );
243 $key = "{$hash}.{$file->getExtension()}";
245 return $repo->getDeletedHashPath( $key ) . $key;
249 * Send an output message iff the 'verbose' option has been provided.
251 * @param string $msg Message to output
253 protected function verbose( $msg ) {
254 if ( $this->hasOption( 'verbose' ) ) {
255 $this->output( $msg );
260 $maintClass = "PurgeChangedFiles";
261 require_once RUN_MAINTENANCE_IF_MAIN;