16require_once __DIR__ .
'/Maintenance.php';
30 private static $typeMappings = [
32 'upload' => [
'upload' ],
33 'import' => [
'upload',
'interwiki' ],
36 'delete' => [
'delete',
'revision' ],
37 'suppress' => [
'delete',
'revision' ],
40 'upload' => [
'overwrite',
'revert' ],
41 'move' => [
'move',
'move_redir' ],
48 private $startTimestamp;
53 private $endTimestamp;
56 parent::__construct();
57 $this->
addDescription(
'Scan the logging table and purge files and thumbnails.' );
58 $this->
addOption(
'starttime',
'Starting timestamp',
true,
true );
59 $this->
addOption(
'endtime',
'Ending timestamp',
true,
true );
60 $this->
addOption(
'type',
'Comma-separated list of types of changes to send purges for (' .
61 implode(
',', array_keys( self::$typeMappings ) ) .
',all)',
false,
true );
62 $this->
addOption(
'htcp-dest',
'HTCP announcement destination (IP:port)',
false,
true );
63 $this->
addOption(
'dry-run',
'Do not send purge requests' );
64 $this->
addOption(
'sleep-per-batch',
'Milliseconds to sleep between batches',
false,
true );
65 $this->
addOption(
'verbose',
'Show more output',
false,
false,
'v' );
73 $parts = explode(
':', $this->
getOption(
'htcp-dest' ), 2 );
74 if ( count( $parts ) < 2 ) {
81 '' => [
'host' => $parts[0],
'port' => $parts[1] ],
83 $this->
verbose(
"HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" );
87 $typeOpt = $this->
getOption(
'type',
'all' );
88 if ( $typeOpt ===
'all' ) {
90 $typeOpt = implode(
',', array_keys( self::$typeMappings ) );
92 $typeList = explode(
',', $typeOpt );
93 foreach ( $typeList as $type ) {
94 if ( !isset( self::$typeMappings[$type] ) ) {
95 $this->
error(
"\nERROR: Unknown type: {$type}\n" );
102 $this->startTimestamp = $dbr->timestamp( $this->
getOption(
'starttime' ) );
103 $this->endTimestamp = $dbr->timestamp( $this->
getOption(
'endtime' ) );
105 if ( $this->startTimestamp > $this->endTimestamp ) {
106 $this->
error(
"\nERROR: starttime after endtime\n" );
115 $this->
verbose(
'Purging files that were: ' . implode(
', ', $typeList ) .
"\n" );
116 foreach ( $typeList as $type ) {
117 $this->
verbose(
"Checking for {$type} files...\n" );
120 $this->
verbose(
"...{$type} files purged.\n\n" );
134 foreach ( self::$typeMappings[$type] as $logType => $logActions ) {
135 $this->
verbose(
"Scanning for {$logType}/" . implode(
',', $logActions ) .
"\n" );
137 $res = $dbr->newSelectQueryBuilder()
138 ->select( [
'log_title',
'log_timestamp',
'log_params' ] )
142 'log_type' => $logType,
143 'log_action' => $logActions,
144 $dbr->expr(
'log_timestamp',
'>=', $this->startTimestamp ),
145 $dbr->expr(
'log_timestamp',
'<=', $this->endTimestamp ),
147 ->caller( __METHOD__ )->fetchResultSet();
150 foreach ( $res as $row ) {
151 $file = $repo->newFile( Title::makeTitle(
NS_FILE, $row->log_title ) );
154 $this->
verbose(
"{$type}[{$row->log_timestamp}]: {$row->log_title}\n" );
161 foreach ( $file->getHistory() as $oldFile ) {
162 $oldFile->purgeCache();
165 if ( $logType ===
'delete' ) {
167 if ( !$file->exists() && $repo->fileExists( $file->getPath() ) ) {
169 if ( $repo->fileExists( $dpath ) ) {
171 $repo->getBackend()->delete( [
'src' => $file->getPath() ] );
172 $this->
verbose(
"Deleted orphan file: {$file->getPath()}.\n" );
174 $this->
error(
"File was not deleted: {$file->getPath()}.\n" );
180 } elseif ( $logType ===
'move' ) {
183 $params = unserialize( $row->log_params );
184 if ( isset( $params[
'4::target'] ) ) {
185 $target = $params[
'4::target'];
186 $targetFile = $repo->newFile( Title::makeTitle(
NS_FILE, $target ) );
187 $targetFile->purgeCache();
188 $this->
verbose(
"Purged file {$target}; move target @{$row->log_timestamp}.\n" );
192 $this->
verbose(
"Purged file {$row->log_title}; {$type} @{$row->log_timestamp}.\n" );
197 usleep( 1000 * (
int)$this->
getOption(
'sleep-per-batch' ) );
205 $res = $dbr->newSelectQueryBuilder()
206 ->select( [
'fa_archive_name' ] )
207 ->from(
'filearchive' )
208 ->where( [
'fa_name' => $file->
getName() ] )
209 ->caller( __METHOD__ )->fetchResultSet();
211 foreach ( $res as $row ) {
212 if ( $row->fa_archive_name ===
null ) {
222 $repo->
getBackend()->delete( [
'src' => $ofile->getPath() ] );
223 $this->
output(
"Deleted orphan file: {$ofile->getPath()}.\n" );
225 $this->
error(
"File was not deleted: {$ofile->getPath()}.\n" );
233 $hash = $repo->getFileSha1( $file->getPath() );
234 $key =
"{$hash}.{$file->getExtension()}";
245 if ( $this->hasOption(
'verbose' ) ) {
246 $this->output( $msg );
253require_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.
getReplicaDB(string|false $virtualDomain=false)
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.