Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.94% covered (success)
93.94%
31 / 33
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
PurgeOldLogIPData
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace MediaWiki\Extension\AbuseFilter\Maintenance;
4
5// @codeCoverageIgnoreStart
6$IP = getenv( 'MW_INSTALL_PATH' );
7if ( $IP === false ) {
8    $IP = __DIR__ . '/../../..';
9}
10require_once "$IP/maintenance/Maintenance.php";
11// @codeCoverageIgnoreEnd
12
13use MediaWiki\Maintenance\Maintenance;
14use MediaWiki\Utils\MWTimestamp;
15
16class 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     * @inheritDoc
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;
68require_once RUN_MAINTENANCE_IF_MAIN;