summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/AbuseFilter/maintenance/purgeOldLogIPData.php
blob: 1a4b560b7a45feb7f311fc3cca1ac6ba758aaa4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php

if ( getenv( 'MW_INSTALL_PATH' ) ) {
	$IP = getenv( 'MW_INSTALL_PATH' );
} else {
	$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";

class PurgeOldLogIPData extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->mDescription = 'Purge old IP Address data from AbuseFilter logs';
		$this->setBatchSize( 200 );

		$this->requireExtension( 'Abuse Filter' );
	}

	public function execute() {
		global $wgAbuseFilterLogIPMaxAge;

		$this->output( "Purging old IP Address data from abuse_filter_log...\n" );
		$dbw = wfGetDB( DB_MASTER );
		$cutoffUnix = time() - $wgAbuseFilterLogIPMaxAge;

		$count = 0;
		do {
			$ids = $dbw->selectFieldValues(
				'abuse_filter_log',
				'afl_id',
				[
					'afl_ip <> ""',
					"afl_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $cutoffUnix ) )
				],
				__METHOD__,
				[ 'LIMIT' => $this->getBatchSize() ]
			);

			if ( $ids ) {
				$dbw->update(
					'abuse_filter_log',
					[ 'afl_ip' => '' ],
					[ 'afl_id' => $ids ],
					__METHOD__
				);
				$count += $dbw->affectedRows();
				$this->output( "$count\n" );

				wfWaitForSlaves();
			}
		} while ( count( $ids ) >= $this->getBatchSize() );

		$this->output( "$count rows.\n" );

		$this->output( "Done.\n" );
	}

}

$maintClass = "PurgeOldLogIPData";
require_once RUN_MAINTENANCE_IF_MAIN;