Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
RemoveInvalidNotification
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * Remove invalid events from echo_event and echo_notification
4 *
5 * @ingroup Maintenance
6 */
7
8use MediaWiki\Extension\Notifications\DbFactory;
9use MediaWiki\Maintenance\Maintenance;
10
11require_once getenv( 'MW_INSTALL_PATH' ) !== false
12    ? getenv( 'MW_INSTALL_PATH' ) . '/maintenance/Maintenance.php'
13    : __DIR__ . '/../../../maintenance/Maintenance.php';
14
15/**
16 * Maintenance script that removes invalid notifications
17 *
18 * @ingroup Maintenance
19 */
20class RemoveInvalidNotification extends Maintenance {
21
22    /** @var string[] */
23    protected $invalidEventType = [ 'article-linked' ];
24
25    public function __construct() {
26        parent::__construct();
27
28        $this->addDescription( "Removes invalid notifications from the database." );
29        $this->setBatchSize( 500 );
30        $this->requireExtension( 'Echo' );
31    }
32
33    public function execute() {
34        $lbFactory = DbFactory::newFromDefault();
35        if ( !$this->invalidEventType ) {
36            $this->output( "There is nothing to process\n" );
37
38            return;
39        }
40
41        $dbw = $lbFactory->getEchoDb( DB_PRIMARY );
42        $dbr = $lbFactory->getEchoDb( DB_REPLICA );
43
44        $batchSize = $this->getBatchSize();
45        $count = $batchSize;
46
47        while ( $count == $batchSize ) {
48            $res = $dbr->newSelectQueryBuilder()
49                ->select( 'event_id' )
50                ->from( 'echo_event' )
51                ->where( [
52                    'event_type' => $this->invalidEventType,
53                ] )
54                ->limit( $batchSize )
55                ->caller( __METHOD__ )
56                ->fetchResultSet();
57
58            $event = [];
59            $count = 0;
60            foreach ( $res as $row ) {
61                // @phan-suppress-next-line PhanPossiblyUndeclaredVariable
62                if ( !in_array( $row->event_id, $event ) ) {
63                    $event[] = $row->event_id;
64                }
65                $count++;
66            }
67
68            if ( $event ) {
69                $this->beginTransaction( $dbw, __METHOD__ );
70
71                $dbw->newDeleteQueryBuilder()
72                    ->deleteFrom( 'echo_event' )
73                    ->where( [ 'event_id' => $event ] )
74                    ->caller( __METHOD__ )
75                    ->execute();
76                $dbw->newDeleteQueryBuilder()
77                    ->deleteFrom( 'echo_notification' )
78                    ->where( [ 'notification_event' => $event ] )
79                    ->caller( __METHOD__ )
80                    ->execute();
81
82                $this->commitTransaction( $dbw, __METHOD__ );
83
84                $this->output( "processing " . count( $event ) . " invalid events\n" );
85                $this->waitForReplication();
86            }
87
88            // Cleanup is not necessary for
89            // 1. echo_email_batch, invalid notification is removed during the cron
90        }
91    }
92}
93
94$maintClass = RemoveInvalidNotification::class;
95require_once RUN_MAINTENANCE_IF_MAIN;