| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\Maintenance; |
| 4 |
|
| 5 |
|
| 6 |
$IP = getenv( 'MW_INSTALL_PATH' ); |
| 7 |
if ( $IP === false ) { |
| 8 |
$IP = __DIR__ . '/../../..'; |
| 9 |
} |
| 10 |
require_once "$IP/maintenance/Maintenance.php"; |
| 11 |
|
| 12 |
|
| 13 |
use MediaWiki\Maintenance\Maintenance; |
| 14 |
use MediaWiki\Utils\MWTimestamp; |
| 15 |
|
| 16 |
class PurgeOldLogIPData extends Maintenance { |
| 17 |
public function __construct() { |
| 18 |
parent::__construct(); |
| 19 |
$this->addDescription( 'Purge old IP address data from the abuse_filter_log table' ); |
| 20 |
$this->setBatchSize( 200 ); |
| 21 |
|
| 22 |
$this->requireExtension( 'Abuse Filter' ); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public function execute() { |
| 29 |
$this->output( "Purging old data from abuse_filter_log...\n" ); |
| 30 |
$dbw = $this->getDB( DB_PRIMARY ); |
| 31 |
$cutoffUnix = (int)MWTimestamp::now( TS_UNIX ) - $this->getConfig()->get( 'AbuseFilterLogIPMaxAge' ); |
| 32 |
|
| 33 |
$count = 0; |
| 34 |
do { |
| 35 |
$ids = $dbw->newSelectQueryBuilder() |
| 36 |
->select( 'afl_id' ) |
| 37 |
->from( 'abuse_filter_log' ) |
| 38 |
->where( [ |
| 39 |
$dbw->expr( 'afl_ip', '!=', '' ), |
| 40 |
$dbw->expr( 'afl_timestamp', '<', $dbw->timestamp( $cutoffUnix ) ), |
| 41 |
] ) |
| 42 |
->limit( $this->getBatchSize() ) |
| 43 |
->caller( __METHOD__ ) |
| 44 |
->fetchFieldValues(); |
| 45 |
|
| 46 |
if ( $ids ) { |
| 47 |
$dbw->newUpdateQueryBuilder() |
| 48 |
->update( 'abuse_filter_log' ) |
| 49 |
->set( [ 'afl_ip' => '' ] ) |
| 50 |
->where( [ 'afl_id' => $ids ] ) |
| 51 |
->caller( __METHOD__ ) |
| 52 |
->execute(); |
| 53 |
$count += $dbw->affectedRows(); |
| 54 |
$this->output( "$count\n" ); |
| 55 |
|
| 56 |
$this->waitForReplication(); |
| 57 |
} |
| 58 |
} while ( count( $ids ) >= $this->getBatchSize() ); |
| 59 |
|
| 60 |
$this->output( "$count rows.\n" ); |
| 61 |
|
| 62 |
$this->output( "Done.\n" ); |
| 63 |
} |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
$maintClass = PurgeOldLogIPData::class; |
| 68 |
require_once RUN_MAINTENANCE_IF_MAIN; |