26require_once __DIR__ .
'/Maintenance.php';
39 private static $typeMappings = [
41 'upload' => [
'upload' ],
42 'import' => [
'upload',
'interwiki' ],
45 'delete' => [
'delete',
'revision' ],
46 'suppress' => [
'delete',
'revision' ],
49 'upload' => [
'overwrite',
'revert' ],
50 'move' => [
'move',
'move_redir' ],
57 private $startTimestamp;
62 private $endTimestamp;
65 parent::__construct();
66 $this->
addDescription(
'Scan the logging table and purge files and thumbnails.' );
67 $this->
addOption(
'starttime',
'Starting timestamp',
true,
true );
68 $this->
addOption(
'endtime',
'Ending timestamp',
true,
true );
69 $this->
addOption(
'type',
'Comma-separated list of types of changes to send purges for (' .
70 implode(
',', array_keys( self::$typeMappings ) ) .
',all)',
false,
true );
71 $this->
addOption(
'htcp-dest',
'HTCP announcement destination (IP:port)',
false,
true );
72 $this->
addOption(
'dry-run',
'Do not send purge requests' );
73 $this->
addOption(
'sleep-per-batch',
'Milliseconds to sleep between batches',
false,
true );
74 $this->
addOption(
'verbose',
'Show more output',
false,
false,
'v' );
82 $parts = explode(
':', $this->
getOption(
'htcp-dest' ), 2 );
83 if ( count( $parts ) < 2 ) {
90 '' => [
'host' => $parts[0],
'port' => $parts[1] ],
92 $this->
verbose(
"HTCP broadcasts to {$parts[0]}:{$parts[1]}\n" );
96 $typeOpt = $this->
getOption(
'type',
'all' );
97 $validTypes = array_keys( self::$typeMappings );
98 if ( $typeOpt ===
'all' ) {
100 $typeOpt = implode(
',', $validTypes );
102 $typeList = explode(
',', $typeOpt );
103 foreach ( $typeList as
$type ) {
104 if ( !in_array(
$type, $validTypes ) ) {
105 $this->
error(
"\nERROR: Unknown type: {$type}\n" );
112 $this->startTimestamp =
$dbr->timestamp( $this->
getOption(
'starttime' ) );
113 $this->endTimestamp =
$dbr->timestamp( $this->
getOption(
'endtime' ) );
115 if ( $this->startTimestamp > $this->endTimestamp ) {
116 $this->
error(
"\nERROR: starttime after endtime\n" );
122 $this->mOptions[
'verbose'] = 1;
125 $this->
verbose(
'Purging files that were: ' . implode(
', ', $typeList ) .
"\n" );
126 foreach ( $typeList as
$type ) {
127 $this->
verbose(
"Checking for {$type} files...\n" );
130 $this->
verbose(
"...{$type} files purged.\n\n" );
141 $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
144 foreach ( self::$typeMappings[
$type] as $logType => $logActions ) {
145 $this->
verbose(
"Scanning for {$logType}/" . implode(
',', $logActions ) .
"\n" );
149 [
'log_title',
'log_timestamp',
'log_params' ],
152 'log_type' => $logType,
153 'log_action' => $logActions,
154 'log_timestamp >= ' .
$dbr->addQuotes( $this->startTimestamp ),
155 'log_timestamp <= ' .
$dbr->addQuotes( $this->endTimestamp ),
161 foreach (
$res as $row ) {
162 $file = $repo->newFile( Title::makeTitle(
NS_FILE, $row->log_title ) );
165 $this->
verbose(
"{$type}[{$row->log_timestamp}]: {$row->log_title}\n" );
172 foreach (
$file->getHistory() as $oldFile ) {
173 $oldFile->purgeCache();
176 if ( $logType ===
'delete' ) {
178 if ( !
$file->exists() && $repo->fileExists(
$file->getPath() ) ) {
180 if ( $repo->fileExists( $dpath ) ) {
182 $repo->getBackend()->delete( [
'src' =>
$file->getPath() ] );
183 $this->
verbose(
"Deleted orphan file: {$file->getPath()}.\n" );
185 $this->
error(
"File was not deleted: {$file->getPath()}.\n" );
191 } elseif ( $logType ===
'move' ) {
195 if ( isset( $params[
'4::target'] ) ) {
196 $target = $params[
'4::target'];
197 $targetFile = $repo->newFile( Title::makeTitle(
NS_FILE, $target ) );
198 $targetFile->purgeCache();
199 $this->
verbose(
"Purged file {$target}; move target @{$row->log_timestamp}.\n" );
203 $this->
verbose(
"Purged file {$row->log_title}; {$type} @{$row->log_timestamp}.\n" );
208 usleep( 1000 * (
int)$this->
getOption(
'sleep-per-batch' ) );
218 [
'fa_archive_name' ],
219 [
'fa_name' =>
$file->getName() ],
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()}";
264require_once RUN_MAINTENANCE_IF_MAIN;
unserialize( $serialized)
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.
Local file in the wiki's own database.
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...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
maybeHelp( $force=false)
Maybe show the help.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
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.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.