Debian Codegraph @

extensions/Echo/maintenance/removeOrphanedEvents.php

raw symbols SCIP
1 <?php
2 /**
3 * Remove rows from echo_event that don't have corresponding rows in echo_notification or echo_email_batch.
4 *
5 * @ingroup Maintenance
6 */
7
8 use MediaWiki\Extension\Notifications\DbFactory;
9 use MediaWiki\Maintenance\LoggedUpdateMaintenance;
10
11 require_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 orphaned event rows
17 *
18 * @ingroup Maintenance
19 */
20 class RemoveOrphanedEvents extends LoggedUpdateMaintenance {
21
22 public function __construct() {
23 parent::__construct();
24
25 $this->addDescription( "Remove rows from echo_event and echo_target_page that don't have corresponding " .
26 "rows in echo_notification or echo_email_batch" );
27
28 $this->setBatchSize( 500 );
29
30 $this->requireExtension( 'Echo' );
31 }
32
33 public function getUpdateKey() {
34 return __CLASS__;
35 }
36
37 public function doDBUpdates() {
38 $startId = 0;
39 $dbFactory = DbFactory::newFromDefault();
40 $dbr = $dbFactory->getEchoDb( DB_REPLICA );
41 $maxId = (int)$dbr->newSelectQueryBuilder()
42 ->select( 'MAX(event_id)' )
43 ->from( 'echo_event' )
44 ->caller( __METHOD__ )
45 ->fetchField();
46 $eventsProcessedTotal = 0;
47 $targetsProcessedTotal = 0;
48 while ( $startId < $maxId ) {
49 $startId += $this->getBatchSize() * 1000;
50 [ $eventsProcessed, $targetsProcessed ] = $this->doMajorBatch( $startId );
51 $eventsProcessedTotal += $eventsProcessed;
52 $targetsProcessedTotal += $targetsProcessed;
53 }
54 $this->output( "In total, deleted $eventsProcessedTotal orphaned events and " .
55 "$targetsProcessedTotal target_page rows.\n" );
56
57 return true;
58 }
59
60 private function doMajorBatch( $maxId ) {
61 $dbFactory = DbFactory::newFromDefault();
62 $dbw = $dbFactory->getEchoDb( DB_PRIMARY );
63 $dbr = $dbFactory->getEchoDb( DB_REPLICA );
64 $iterator = new BatchRowIterator(
65 $dbr,
66 [ 'echo_event', 'echo_notification', 'echo_email_batch' ],
67 'event_id',
68 $this->getBatchSize()
69 );
70 $iterator->addJoinConditions( [
71 'echo_notification' => [ 'LEFT JOIN', 'notification_event=event_id' ],
72 'echo_email_batch' => [ 'LEFT JOIN', 'eeb_event_id=event_id' ],
73 ] );
74 $iterator->addConditions( [
75 'notification_user' => null,
76 'eeb_user_id' => null,
77 $dbr->expr( 'event_id', '<', $maxId ),
78 ] );
79 $iterator->setCaller( __METHOD__ );
80
81 $this->output( "Removing orphaned echo_event rows with max event_id of $maxId...\n" );
82
83 $eventsProcessed = 0;
84 $targetsProcessed = 0;
85 foreach ( $iterator as $batch ) {
86 $ids = [];
87 foreach ( $batch as $row ) {
88 $ids[] = $row->event_id;
89 }
90 $dbw->newDeleteQueryBuilder()
91 ->deleteFrom( 'echo_event' )
92 ->where( [ 'event_id' => $ids ] )
93 ->caller( __METHOD__ )
94 ->execute();
95 $eventsProcessed += $dbw->affectedRows();
96 $dbw->newDeleteQueryBuilder()
97 ->deleteFrom( 'echo_target_page' )
98 ->where( [ 'etp_event' => $ids ] )
99 ->caller( __METHOD__ )
100 ->execute();
101 $targetsProcessed += $dbw->affectedRows();
102 $this->output( "Deleted $eventsProcessed orphaned events and $targetsProcessed target_page rows.\n" );
103 $this->waitForReplication();
104 }
105
106 $this->output( "Removing any remaining orphaned echo_target_page rows with max etp_event of $maxId...\n" );
107 $iterator = new BatchRowIterator(
108 $dbr,
109 [ 'echo_target_page', 'echo_event' ],
110 'etp_event',
111 $this->getBatchSize()
112 );
113 $iterator->addJoinConditions( [ 'echo_event' => [ 'LEFT JOIN', 'event_id=etp_event' ] ] );
114 $iterator->addConditions(
115 [
116 'event_type' => null,
117 $dbr->expr( 'etp_event', '<', $maxId ),
118 ]
119 );
120 $iterator->addOptions( [ 'DISTINCT' ] );
121 $iterator->setCaller( __METHOD__ );
122
123 $processed = 0;
124 foreach ( $iterator as $batch ) {
125 $ids = [];
126 foreach ( $batch as $row ) {
127 $ids[] = $row->etp_event;
128 }
129 $dbw->newDeleteQueryBuilder()
130 ->deleteFrom( 'echo_target_page' )
131 ->where( [ 'etp_event' => $ids ] )
132 ->caller( __METHOD__ )
133 ->execute();
134 $processed += $dbw->affectedRows();
135 $this->output( "Deleted $processed orphaned target_page rows.\n" );
136 $this->waitForReplication();
137 }
138
139 return [ $eventsProcessed, $targetsProcessed + $processed ];
140 }
141 }
142
143 $maintClass = RemoveOrphanedEvents::class;
144 require_once RUN_MAINTENANCE_IF_MAIN;