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 if ( $typeOpt ===
'all' ) {
99 $typeOpt = implode(
',', array_keys( self::$typeMappings ) );
101 $typeList = explode(
',', $typeOpt );
102 foreach ( $typeList as $type ) {
103 if ( !isset( self::$typeMappings[$type] ) ) {
104 $this->
error(
"\nERROR: Unknown type: {$type}\n" );
111 $this->startTimestamp = $dbr->timestamp( $this->
getOption(
'starttime' ) );
112 $this->endTimestamp = $dbr->timestamp( $this->
getOption(
'endtime' ) );
114 if ( $this->startTimestamp > $this->endTimestamp ) {
115 $this->
error(
"\nERROR: starttime after endtime\n" );
121 $this->mOptions[
'verbose'] = 1;
124 $this->
verbose(
'Purging files that were: ' . implode(
', ', $typeList ) .
"\n" );
125 foreach ( $typeList as $type ) {
126 $this->
verbose(
"Checking for {$type} files...\n" );
129 $this->
verbose(
"...{$type} files purged.\n\n" );
143 foreach ( self::$typeMappings[$type] as $logType => $logActions ) {
144 $this->
verbose(
"Scanning for {$logType}/" . implode(
',', $logActions ) .
"\n" );
146 $res = $dbr->newSelectQueryBuilder()
147 ->select( [
'log_title',
'log_timestamp',
'log_params' ] )
151 'log_type' => $logType,
152 'log_action' => $logActions,
153 $dbr->buildComparison(
'>=', [
'log_timestamp' => $this->startTimestamp ] ),
154 $dbr->buildComparison(
'<=', [
'log_timestamp' => $this->endTimestamp ] ),
156 ->caller( __METHOD__ )->fetchResultSet();
159 foreach ( $res as $row ) {
160 $file = $repo->newFile( Title::makeTitle(
NS_FILE, $row->log_title ) );
163 $this->
verbose(
"{$type}[{$row->log_timestamp}]: {$row->log_title}\n" );
170 foreach (
$file->getHistory() as $oldFile ) {
171 $oldFile->purgeCache();
174 if ( $logType ===
'delete' ) {
176 if ( !
$file->exists() && $repo->fileExists(
$file->getPath() ) ) {
178 if ( $repo->fileExists( $dpath ) ) {
180 $repo->getBackend()->delete( [
'src' =>
$file->getPath() ] );
181 $this->
verbose(
"Deleted orphan file: {$file->getPath()}.\n" );
183 $this->
error(
"File was not deleted: {$file->getPath()}.\n" );
189 } elseif ( $logType ===
'move' ) {
192 $params = unserialize( $row->log_params );
193 if ( isset( $params[
'4::target'] ) ) {
194 $target = $params[
'4::target'];
195 $targetFile = $repo->newFile( Title::makeTitle(
NS_FILE, $target ) );
196 $targetFile->purgeCache();
197 $this->
verbose(
"Purged file {$target}; move target @{$row->log_timestamp}.\n" );
201 $this->
verbose(
"Purged file {$row->log_title}; {$type} @{$row->log_timestamp}.\n" );
206 usleep( 1000 * (
int)$this->
getOption(
'sleep-per-batch' ) );
214 $res = $dbr->newSelectQueryBuilder()
215 ->select( [
'fa_archive_name' ] )
216 ->from(
'filearchive' )
217 ->where( [
'fa_name' =>
$file->getName() ] )
218 ->caller( __METHOD__ )->fetchResultSet();
220 foreach ( $res as $row ) {
221 if ( $row->fa_archive_name ===
null ) {
231 $repo->
getBackend()->delete( [
'src' => $ofile->getPath() ] );
232 $this->
output(
"Deleted orphan file: {$ofile->getPath()}.\n" );
234 $this->
error(
"File was not deleted: {$ofile->getPath()}.\n" );
243 $key =
"{$hash}.{$file->getExtension()}";
261require_once RUN_MAINTENANCE_IF_MAIN;
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.
getServiceContainer()
Returns the main service container.
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.