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