30require_once __DIR__ .
'/Maintenance.php';
44 private static $typeMappings = [
46 'upload' => [
'upload' ],
47 'import' => [
'upload',
'interwiki' ],
50 'delete' => [
'delete',
'revision' ],
51 'suppress' => [
'delete',
'revision' ],
54 'upload' => [
'overwrite',
'revert' ],
55 'move' => [
'move',
'move_redir' ],
62 private $startTimestamp;
67 private $endTimestamp;
70 parent::__construct();
71 $this->
addDescription(
'Scan the logging table and purge files and thumbnails.' );
72 $this->
addOption(
'starttime',
'Starting timestamp',
true,
true );
73 $this->
addOption(
'endtime',
'Ending timestamp',
true,
true );
74 $this->
addOption(
'type',
'Comma-separated list of types of changes to send purges for (' .
75 implode(
',', array_keys( self::$typeMappings ) ) .
',all)',
false,
true );
76 $this->
addOption(
'htcp-dest',
'HTCP announcement destination (IP:port)',
false,
true );
77 $this->
addOption(
'dry-run',
'Do not send purge requests' );
78 $this->
addOption(
'sleep-per-batch',
'Milliseconds to sleep between batches',
false,
true );
79 $this->
addOption(
'verbose',
'Show more output',
false,
false,
'v' );
87 $parts = explode(
':', $this->
getOption(
'htcp-dest' ), 2 );
88 if ( count( $parts ) < 2 ) {
95 '' => [
'host' => $parts[0],
'port' => $parts[1] ],
97 $this->
verbose(
"HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" );
101 $typeOpt = $this->
getOption(
'type',
'all' );
102 if ( $typeOpt ===
'all' ) {
104 $typeOpt = implode(
',', array_keys( self::$typeMappings ) );
106 $typeList = explode(
',', $typeOpt );
107 foreach ( $typeList as $type ) {
108 if ( !isset( self::$typeMappings[$type] ) ) {
109 $this->
error(
"\nERROR: Unknown type: {$type}\n" );
116 $this->startTimestamp = $dbr->timestamp( $this->
getOption(
'starttime' ) );
117 $this->endTimestamp = $dbr->timestamp( $this->
getOption(
'endtime' ) );
119 if ( $this->startTimestamp > $this->endTimestamp ) {
120 $this->
error(
"\nERROR: starttime after endtime\n" );
129 $this->
verbose(
'Purging files that were: ' . implode(
', ', $typeList ) .
"\n" );
130 foreach ( $typeList as $type ) {
131 $this->
verbose(
"Checking for {$type} files...\n" );
134 $this->
verbose(
"...{$type} files purged.\n\n" );
148 foreach ( self::$typeMappings[$type] as $logType => $logActions ) {
149 $this->
verbose(
"Scanning for {$logType}/" . implode(
',', $logActions ) .
"\n" );
151 $res = $dbr->newSelectQueryBuilder()
152 ->select( [
'log_title',
'log_timestamp',
'log_params' ] )
156 'log_type' => $logType,
157 'log_action' => $logActions,
158 $dbr->expr(
'log_timestamp',
'>=', $this->startTimestamp ),
159 $dbr->expr(
'log_timestamp',
'<=', $this->endTimestamp ),
161 ->caller( __METHOD__ )->fetchResultSet();
164 foreach ( $res as $row ) {
165 $file = $repo->newFile( Title::makeTitle(
NS_FILE, $row->log_title ) );
168 $this->
verbose(
"{$type}[{$row->log_timestamp}]: {$row->log_title}\n" );
175 foreach ( $file->getHistory() as $oldFile ) {
176 $oldFile->purgeCache();
179 if ( $logType ===
'delete' ) {
181 if ( !$file->exists() && $repo->fileExists( $file->getPath() ) ) {
183 if ( $repo->fileExists( $dpath ) ) {
185 $repo->getBackend()->delete( [
'src' => $file->getPath() ] );
186 $this->
verbose(
"Deleted orphan file: {$file->getPath()}.\n" );
188 $this->
error(
"File was not deleted: {$file->getPath()}.\n" );
194 } elseif ( $logType ===
'move' ) {
197 $params = unserialize( $row->log_params );
198 if ( isset( $params[
'4::target'] ) ) {
199 $target = $params[
'4::target'];
200 $targetFile = $repo->newFile( Title::makeTitle(
NS_FILE, $target ) );
201 $targetFile->purgeCache();
202 $this->
verbose(
"Purged file {$target}; move target @{$row->log_timestamp}.\n" );
206 $this->
verbose(
"Purged file {$row->log_title}; {$type} @{$row->log_timestamp}.\n" );
211 usleep( 1000 * (
int)$this->
getOption(
'sleep-per-batch' ) );
219 $res = $dbr->newSelectQueryBuilder()
220 ->select( [
'fa_archive_name' ] )
221 ->from(
'filearchive' )
222 ->where( [
'fa_name' => $file->
getName() ] )
223 ->caller( __METHOD__ )->fetchResultSet();
225 foreach ( $res as $row ) {
226 if ( $row->fa_archive_name ===
null ) {
236 $repo->
getBackend()->delete( [
'src' => $ofile->getPath() ] );
237 $this->
output(
"Deleted orphan file: {$ofile->getPath()}.\n" );
239 $this->
error(
"File was not deleted: {$ofile->getPath()}.\n" );
247 $hash = $repo->getFileSha1( $file->getPath() );
248 $key =
"{$hash}.{$file->getExtension()}";
259 if ( $this->hasOption(
'verbose' ) ) {
260 $this->output( $msg );
267require_once RUN_MAINTENANCE_IF_MAIN;
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.
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.
error( $err, $die=0)
Throw an error to the user.
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.