17require_once __DIR__ .
'/Maintenance.php';
31 private static $typeMappings = [
33 'upload' => [
'upload' ],
34 'import' => [
'upload',
'interwiki' ],
37 'delete' => [
'delete',
'revision' ],
38 'suppress' => [
'delete',
'revision' ],
41 'upload' => [
'overwrite',
'revert' ],
42 'move' => [
'move',
'move_redir' ],
49 private $startTimestamp;
54 private $endTimestamp;
57 parent::__construct();
58 $this->
addDescription(
'Scan the logging table and purge files and thumbnails.' );
59 $this->
addOption(
'starttime',
'Starting timestamp',
true,
true );
60 $this->
addOption(
'endtime',
'Ending timestamp',
true,
true );
61 $this->
addOption(
'type',
'Comma-separated list of types of changes to send purges for (' .
62 implode(
',', array_keys( self::$typeMappings ) ) .
',all)',
false,
true );
63 $this->
addOption(
'htcp-dest',
'HTCP announcement destination (IP:port)',
false,
true );
64 $this->
addOption(
'dry-run',
'Do not send purge requests' );
65 $this->
addOption(
'sleep-per-batch',
'Milliseconds to sleep between batches',
false,
true );
66 $this->
addOption(
'verbose',
'Show more output',
false,
false,
'v' );
74 $parts = explode(
':', $this->
getOption(
'htcp-dest' ), 2 );
75 if ( count( $parts ) < 2 ) {
82 '' => [
'host' => $parts[0],
'port' => $parts[1] ],
84 $this->
verbose(
"HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" );
88 $typeOpt = $this->
getOption(
'type',
'all' );
89 if ( $typeOpt ===
'all' ) {
91 $typeOpt = implode(
',', array_keys( self::$typeMappings ) );
93 $typeList = explode(
',', $typeOpt );
94 foreach ( $typeList as $type ) {
95 if ( !isset( self::$typeMappings[$type] ) ) {
96 $this->
error(
"\nERROR: Unknown type: {$type}\n" );
103 $this->startTimestamp = $dbr->timestamp( $this->
getOption(
'starttime' ) );
104 $this->endTimestamp = $dbr->timestamp( $this->
getOption(
'endtime' ) );
106 if ( $this->startTimestamp > $this->endTimestamp ) {
107 $this->
error(
"\nERROR: starttime after endtime\n" );
116 $this->
verbose(
'Purging files that were: ' . implode(
', ', $typeList ) .
"\n" );
117 foreach ( $typeList as $type ) {
118 $this->
verbose(
"Checking for {$type} files...\n" );
121 $this->
verbose(
"...{$type} files purged.\n\n" );
135 foreach ( self::$typeMappings[$type] as $logType => $logActions ) {
136 $this->
verbose(
"Scanning for {$logType}/" . implode(
',', $logActions ) .
"\n" );
138 $res = $dbr->newSelectQueryBuilder()
139 ->select( [
'log_title',
'log_timestamp',
'log_params' ] )
143 'log_type' => $logType,
144 'log_action' => $logActions,
145 $dbr->expr(
'log_timestamp',
'>=', $this->startTimestamp ),
146 $dbr->expr(
'log_timestamp',
'<=', $this->endTimestamp ),
148 ->caller( __METHOD__ )->fetchResultSet();
151 foreach ( $res as $row ) {
152 $file = $repo->newFile( Title::makeTitle(
NS_FILE, $row->log_title ) );
155 $this->
verbose(
"{$type}[{$row->log_timestamp}]: {$row->log_title}\n" );
162 foreach ( $file->getHistory() as $oldFile ) {
163 $oldFile->purgeCache();
166 if ( $logType ===
'delete' ) {
168 if ( !$file->exists() && $repo->fileExists( $file->getPath() ) ) {
170 if ( $repo->fileExists( $dpath ) ) {
172 $repo->getBackend()->delete( [
'src' => $file->getPath() ] );
173 $this->
verbose(
"Deleted orphan file: {$file->getPath()}.\n" );
175 $this->
error(
"File was not deleted: {$file->getPath()}.\n" );
181 } elseif ( $logType ===
'move' ) {
184 $params = LogEntryBase::extractParams( $row->log_params,
null );
185 if ( isset( $params[
'4::target'] ) ) {
186 $target = $params[
'4::target'];
187 $targetFile = $repo->newFile( Title::makeTitle(
NS_FILE, $target ) );
188 $targetFile->purgeCache();
189 $this->
verbose(
"Purged file {$target}; move target @{$row->log_timestamp}.\n" );
193 $this->
verbose(
"Purged file {$row->log_title}; {$type} @{$row->log_timestamp}.\n" );
198 usleep( 1000 * (
int)$this->
getOption(
'sleep-per-batch' ) );
206 $res = $dbr->newSelectQueryBuilder()
207 ->select( [
'fa_archive_name' ] )
208 ->from(
'filearchive' )
209 ->where( [
'fa_name' => $file->
getName() ] )
210 ->caller( __METHOD__ )->fetchResultSet();
212 foreach ( $res as $row ) {
213 if ( $row->fa_archive_name ===
null ) {
223 $repo->
getBackend()->delete( [
'src' => $ofile->getPath() ] );
224 $this->
output(
"Deleted orphan file: {$ofile->getPath()}.\n" );
226 $this->
error(
"File was not deleted: {$ofile->getPath()}.\n" );
234 $hash = $repo->getFileSha1( $file->getPath() );
235 $key =
"{$hash}.{$file->getExtension()}";
246 if ( $this->hasOption(
'verbose' ) ) {
247 $this->output( $msg );
254require_once RUN_MAINTENANCE_IF_MAIN;
Extends the LogEntry Interface with some basic functionality.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
error( $err)
Throw an error to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
setOption(string $name, $value)
Programmatically set the value of the given option.
getOption( $name, $default=null)
Get an option, or return the default.
getReplicaDB(string|false $virtualDomain=false)
getServiceContainer()
Returns the main service container.
maybeHelp( $force=false)
Maybe show the help.
addDescription( $text)
Set the description text.
Maintenance script that scans the deletion log and purges affected files within a timeframe.
execute()
Do the actual work.
__construct()
Default constructor.
purgeFromLogType( $type)
Purge cache and thumbnails for changes of the given type.
getDeletedPath(LocalRepo $repo, LocalFile $file)
purgeFromArchiveTable(LocalRepo $repo, LocalFile $file)
verbose( $msg)
Send an output message iff the 'verbose' option has been provided.
$wgHTCPRouting
Config variable stub for the HTCPRouting setting, for use by phpdoc and IDEs.