Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 2
n/a
0 / 0
CRAP
n/a
0 / 0
AddMissingLoggingEntries
n/a
0 / 0
n/a
0 / 0
12
n/a
0 / 0
 __construct
n/a
0 / 0
n/a
0 / 0
1
 getUpdateKey
n/a
0 / 0
n/a
0 / 0
1
 doDBUpdates
n/a
0 / 0
n/a
0 / 0
10
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 LoggedUpdateMaintenance;
14use ManualLogEntry;
15use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
16use MediaWiki\Extension\AbuseFilter\Special\SpecialAbuseFilter;
17use MediaWiki\User\User;
18
19/**
20 * @codeCoverageIgnore
21 * No need to test old single-use script.
22 */
23class AddMissingLoggingEntries extends LoggedUpdateMaintenance {
24    public function __construct() {
25        parent::__construct();
26
27        $this->addDescription( 'Add missing logging entries for abusefilter-modify T54919' );
28        $this->addOption( 'dry-run', 'Perform a dry run' );
29        $this->addOption( 'verbose', 'Print a list of affected afh_id' );
30        $this->requireExtension( 'Abuse Filter' );
31    }
32
33    /**
34     * @inheritDoc
35     */
36    public function getUpdateKey() {
37        return 'AddMissingLoggingEntries';
38    }
39
40    /**
41     * @inheritDoc
42     */
43    public function doDBUpdates() {
44        $dryRun = $this->hasOption( 'dry-run' );
45        $logParams = [];
46        $afhRows = [];
47        $db = $this->getDB( DB_REPLICA, 'vslow' );
48
49        $logParamsConcat = $db->buildConcat( [ 'afh_id', $db->addQuotes( "\n" ) ] );
50        $legacyParamsLike = $db->buildLike( $logParamsConcat, $db->anyString() );
51        // Non-legacy entries are a serialized array with 'newId' and 'historyId' keys
52        $newLogParamsLike = $db->buildLike( $db->anyString(), 'historyId', $db->anyString() );
53        $actorQuery = AbuseFilterServices::getActorMigration()->getJoin( 'afh_user' );
54        // Find all entries in abuse_filter_history without logging entry of same timestamp
55        $afhResult = $db->select(
56            [ 'abuse_filter_history', 'logging' ] + $actorQuery['tables'],
57            [ 'afh_id', 'afh_filter', 'afh_timestamp', 'afh_deleted' ] + $actorQuery['fields'],
58            [
59                'log_id IS NULL',
60                "NOT log_params $newLogParamsLike"
61            ],
62            __METHOD__,
63            [],
64            [ 'logging' => [
65                'LEFT JOIN',
66                "afh_timestamp = log_timestamp AND log_params $legacyParamsLike AND log_type = 'abusefilter'"
67            ] ] + $actorQuery['joins']
68        );
69
70        // Because the timestamp matches aren't exact (sometimes a couple of
71        // seconds off), we need to check all our results and ignore those that
72        // do actually have log entries
73        foreach ( $afhResult as $row ) {
74            $logParams[] = $row->afh_id . "\n" . $row->afh_filter;
75            $afhRows[$row->afh_id] = $row;
76        }
77
78        if ( !count( $afhRows ) ) {
79            $this->output( "Nothing to do.\n" );
80            return !$dryRun;
81        }
82
83        $logResult = $this->getDB( DB_REPLICA )->selectFieldValues(
84            'logging',
85            'log_params',
86            [ 'log_type' => 'abusefilter', 'log_params' => $logParams ],
87            __METHOD__
88        );
89
90        foreach ( $logResult as $params ) {
91            // id . "\n" . filter
92            $afhId = explode( "\n", $params, 2 )[0];
93            // Forget this row had any issues - it just has a different timestamp in the log
94            unset( $afhRows[$afhId] );
95        }
96
97        if ( !count( $afhRows ) ) {
98            $this->output( "Nothing to do.\n" );
99            return !$dryRun;
100        }
101
102        if ( $dryRun ) {
103            $msg = count( $afhRows ) . " rows to insert.";
104            if ( $this->hasOption( 'verbose' ) ) {
105                $msg .= " Affected IDs (afh_id):\n" . implode( ', ', array_keys( $afhRows ) );
106            }
107            $this->output( "$msg\n" );
108            return false;
109        }
110
111        $dbw = $this->getDB( DB_PRIMARY );
112
113        $count = 0;
114        foreach ( $afhRows as $row ) {
115            if ( $count % 100 === 0 ) {
116                $this->waitForReplication();
117            }
118            $user = User::newFromAnyId(
119                $row->afh_user ?? null,
120                $row->afh_user_text ?? null,
121                $row->afh_actor ?? null
122            );
123
124            if ( $user === null ) {
125                // This isn't supposed to happen.
126                continue;
127            }
128
129            // This copies the code in FilterStore
130            $logEntry = new ManualLogEntry( 'abusefilter', 'modify' );
131            $logEntry->setPerformer( $user );
132            $logEntry->setTarget( SpecialAbuseFilter::getTitleForSubpage( $row->afh_filter ) );
133            // Use the new format!
134            $logEntry->setParameters( [
135                'historyId' => $row->afh_id,
136                'newId' => $row->afh_filter
137            ] );
138            $logEntry->setTimestamp( $row->afh_timestamp );
139            $logEntry->insert( $dbw );
140
141            $count++;
142        }
143
144        $this->output( "Inserted $count rows.\n" );
145        return true;
146    }
147}
148
149$maintClass = AddMissingLoggingEntries::class;
150require_once RUN_MAINTENANCE_IF_MAIN;