28require_once __DIR__ .
'/Maintenance.php';
42 private static $typeMappings = [
44 'upload' => [
'upload' ],
45 'import' => [
'upload',
'interwiki' ],
48 'delete' => [
'delete',
'revision' ],
49 'suppress' => [
'delete',
'revision' ],
52 'upload' => [
'overwrite',
'revert' ],
53 'move' => [
'move',
'move_redir' ],
60 private $startTimestamp;
65 private $endTimestamp;
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' );
85 $parts = explode(
':', $this->
getOption(
'htcp-dest' ), 2 );
86 if ( count( $parts ) < 2 ) {
93 '' => [
'host' => $parts[0],
'port' => $parts[1] ],
95 $this->
verbose(
"HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" );
99 $typeOpt = $this->
getOption(
'type',
'all' );
100 if ( $typeOpt ===
'all' ) {
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" );
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" );
127 $this->
verbose(
'Purging files that were: ' . implode(
', ', $typeList ) .
"\n" );
128 foreach ( $typeList as $type ) {
129 $this->
verbose(
"Checking for {$type} files...\n" );
132 $this->
verbose(
"...{$type} files purged.\n\n" );
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' ] )
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();
162 foreach ( $res as $row ) {
163 $file = $repo->newFile( Title::makeTitle(
NS_FILE, $row->log_title ) );
166 $this->
verbose(
"{$type}[{$row->log_timestamp}]: {$row->log_title}\n" );
173 foreach ( $file->getHistory() as $oldFile ) {
174 $oldFile->purgeCache();
177 if ( $logType ===
'delete' ) {
179 if ( !$file->exists() && $repo->fileExists( $file->getPath() ) ) {
181 if ( $repo->fileExists( $dpath ) ) {
183 $repo->getBackend()->delete( [
'src' => $file->getPath() ] );
184 $this->
verbose(
"Deleted orphan file: {$file->getPath()}.\n" );
186 $this->
error(
"File was not deleted: {$file->getPath()}.\n" );
192 } elseif ( $logType ===
'move' ) {
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" );
209 usleep( 1000 * (
int)$this->
getOption(
'sleep-per-batch' ) );
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 ) {
234 $repo->
getBackend()->delete( [
'src' => $ofile->getPath() ] );
235 $this->
output(
"Deleted orphan file: {$ofile->getPath()}.\n" );
237 $this->
error(
"File was not deleted: {$ofile->getPath()}.\n" );
246 $key =
"{$hash}.{$file->getExtension()}";
265require_once RUN_MAINTENANCE_IF_MAIN;
array $params
The job parameters.
fileExists( $file)
Checks existence of a file.
getFileSha1( $virtualUrl)
Get the sha1 (base 36) of a file with a given virtual URL/storage path.
getDeletedHashPath( $key)
Get a relative path for a deletion archive key, e.g.
getBackend()
Get the file backend instance.
getPath()
Return the storage path to the file.
getName()
Return the name of this file.
getTitle()
Return the associated title object.
Local file in the wiki's own database.
exists()
canRender inherited
purgeOldThumbnails( $archiveName)
Delete cached transformed files for an archived version only.
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
getReplicaDB()
Get a connection to the replica DB.
newFromArchiveName( $title, $archiveName)
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.