2016-06-06 18:27:23 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2019-04-12 22:32:56 +00:00
|
|
|
* Remove rows from echo_event that don't have corresponding rows in echo_notification or echo_email_batch.
|
2016-06-06 18:27:23 +00:00
|
|
|
*
|
|
|
|
* @ingroup Maintenance
|
|
|
|
*/
|
2017-06-20 02:41:30 +00:00
|
|
|
require_once getenv( 'MW_INSTALL_PATH' ) !== false
|
2016-06-06 18:27:23 +00:00
|
|
|
? getenv( 'MW_INSTALL_PATH' ) . '/maintenance/Maintenance.php'
|
2017-06-20 02:41:30 +00:00
|
|
|
: __DIR__ . '/../../../maintenance/Maintenance.php';
|
2016-06-06 18:27:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maintenance script that removes orphaned event rows
|
|
|
|
*
|
|
|
|
* @ingroup Maintenance
|
|
|
|
*/
|
|
|
|
class RemoveOrphanedEvents extends LoggedUpdateMaintenance {
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
|
2020-06-27 10:05:03 +00:00
|
|
|
$this->addDescription( "Remove rows from echo_event and echo_target_page that don't have corresponding " .
|
|
|
|
"rows in echo_notification or echo_email_batch" );
|
2016-06-06 18:27:23 +00:00
|
|
|
|
|
|
|
$this->setBatchSize( 500 );
|
2016-12-01 21:34:43 +00:00
|
|
|
|
|
|
|
$this->requireExtension( 'Echo' );
|
2016-06-06 18:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getUpdateKey() {
|
|
|
|
return __CLASS__;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function doDBUpdates() {
|
2022-08-01 14:17:03 +00:00
|
|
|
$startId = 0;
|
|
|
|
$dbFactory = MWEchoDbFactory::newFromDefault();
|
|
|
|
$dbr = $dbFactory->getEchoDb( DB_REPLICA );
|
|
|
|
$maxId = (int)$dbr->newSelectQueryBuilder()
|
|
|
|
->select( 'MAX(event_id)' )
|
|
|
|
->from( 'echo_event' )
|
|
|
|
->fetchField();
|
|
|
|
$eventsProcessedTotal = 0;
|
|
|
|
$targetsProcessedTotal = 0;
|
|
|
|
while ( $startId < $maxId ) {
|
|
|
|
$startId += $this->getBatchSize() * 1000;
|
2022-11-08 16:41:24 +00:00
|
|
|
[ $eventsProcessed, $targetsProcessed ] = $this->doMajorBatch( $startId );
|
2022-08-01 14:17:03 +00:00
|
|
|
$eventsProcessedTotal += $eventsProcessed;
|
|
|
|
$targetsProcessedTotal += $targetsProcessed;
|
|
|
|
}
|
|
|
|
$this->output( "In total, deleted $eventsProcessedTotal orphaned events and " .
|
|
|
|
"$targetsProcessedTotal target_page rows.\n" );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doMajorBatch( $maxId ) {
|
2016-06-06 18:27:23 +00:00
|
|
|
$dbFactory = MWEchoDbFactory::newFromDefault();
|
2021-05-03 07:28:02 +00:00
|
|
|
$dbw = $dbFactory->getEchoDb( DB_PRIMARY );
|
2017-09-24 05:23:47 +00:00
|
|
|
$dbr = $dbFactory->getEchoDb( DB_REPLICA );
|
2016-06-06 18:27:23 +00:00
|
|
|
$iterator = new BatchRowIterator(
|
|
|
|
$dbr,
|
2019-04-12 22:32:56 +00:00
|
|
|
[ 'echo_event', 'echo_notification', 'echo_email_batch' ],
|
2016-06-06 18:27:23 +00:00
|
|
|
'event_id',
|
2020-12-17 18:27:43 +00:00
|
|
|
$this->getBatchSize()
|
2016-06-06 18:27:23 +00:00
|
|
|
);
|
2016-12-05 18:51:07 +00:00
|
|
|
$iterator->addJoinConditions( [
|
2019-04-12 22:32:56 +00:00
|
|
|
'echo_notification' => [ 'LEFT JOIN', 'notification_event=event_id' ],
|
|
|
|
'echo_email_batch' => [ 'LEFT JOIN', 'eeb_event_id=event_id' ],
|
2016-12-05 18:51:07 +00:00
|
|
|
] );
|
|
|
|
$iterator->addConditions( [
|
2019-04-12 22:32:56 +00:00
|
|
|
'notification_user' => null,
|
|
|
|
'eeb_user_id' => null,
|
2022-08-01 14:17:03 +00:00
|
|
|
'event_id < ' . $maxId
|
2016-12-05 18:51:07 +00:00
|
|
|
] );
|
2020-09-05 02:02:17 +00:00
|
|
|
$iterator->setCaller( __METHOD__ );
|
2016-06-06 18:27:23 +00:00
|
|
|
|
2022-08-01 14:17:03 +00:00
|
|
|
$this->output( "Removing orphaned echo_event rows with max event_id of $maxId...\n" );
|
2016-06-06 18:27:23 +00:00
|
|
|
|
2019-04-22 23:01:02 +00:00
|
|
|
$eventsProcessed = 0;
|
|
|
|
$targetsProcessed = 0;
|
2016-06-06 18:27:23 +00:00
|
|
|
foreach ( $iterator as $batch ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$ids = [];
|
2016-06-06 18:27:23 +00:00
|
|
|
foreach ( $batch as $row ) {
|
|
|
|
$ids[] = $row->event_id;
|
|
|
|
}
|
2020-06-07 00:59:39 +00:00
|
|
|
$dbw->delete( 'echo_event', [ 'event_id' => $ids ], __METHOD__ );
|
2019-04-22 23:01:02 +00:00
|
|
|
$eventsProcessed += $dbw->affectedRows();
|
2020-06-07 00:59:39 +00:00
|
|
|
$dbw->delete( 'echo_target_page', [ 'etp_event' => $ids ], __METHOD__ );
|
2019-04-22 23:01:02 +00:00
|
|
|
$targetsProcessed += $dbw->affectedRows();
|
|
|
|
$this->output( "Deleted $eventsProcessed orphaned events and $targetsProcessed target_page rows.\n" );
|
2023-01-05 21:14:51 +00:00
|
|
|
$this->waitForReplication();
|
2019-04-22 23:01:02 +00:00
|
|
|
}
|
2016-06-06 18:27:23 +00:00
|
|
|
|
2022-08-01 14:17:03 +00:00
|
|
|
$this->output( "Removing any remaining orphaned echo_target_page rows with max etp_event of $maxId...\n" );
|
2019-04-22 23:01:02 +00:00
|
|
|
$iterator = new BatchRowIterator(
|
|
|
|
$dbr,
|
|
|
|
[ 'echo_target_page', 'echo_event' ],
|
|
|
|
'etp_event',
|
2020-12-17 18:27:43 +00:00
|
|
|
$this->getBatchSize()
|
2019-04-22 23:01:02 +00:00
|
|
|
);
|
|
|
|
$iterator->addJoinConditions( [ 'echo_event' => [ 'LEFT JOIN', 'event_id=etp_event' ] ] );
|
2022-08-01 14:17:03 +00:00
|
|
|
$iterator->addConditions(
|
|
|
|
[
|
|
|
|
'event_type' => null,
|
|
|
|
'etp_event < ' . $maxId
|
|
|
|
]
|
|
|
|
);
|
2019-04-22 23:01:02 +00:00
|
|
|
$iterator->addOptions( [ 'DISTINCT' ] );
|
2020-09-05 02:02:17 +00:00
|
|
|
$iterator->setCaller( __METHOD__ );
|
2019-04-22 23:01:02 +00:00
|
|
|
|
|
|
|
$processed = 0;
|
|
|
|
foreach ( $iterator as $batch ) {
|
|
|
|
$ids = [];
|
|
|
|
foreach ( $batch as $row ) {
|
|
|
|
$ids[] = $row->etp_event;
|
|
|
|
}
|
2020-06-07 00:59:39 +00:00
|
|
|
$dbw->delete( 'echo_target_page', [ 'etp_event' => $ids ], __METHOD__ );
|
2016-06-06 18:27:23 +00:00
|
|
|
$processed += $dbw->affectedRows();
|
2019-04-22 23:01:02 +00:00
|
|
|
$this->output( "Deleted $processed orphaned target_page rows.\n" );
|
2023-01-05 21:14:51 +00:00
|
|
|
$this->waitForReplication();
|
2016-06-06 18:27:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 14:17:03 +00:00
|
|
|
return [ $eventsProcessed, $targetsProcessed + $processed ];
|
2016-06-06 18:27:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 14:14:15 +00:00
|
|
|
$maintClass = RemoveOrphanedEvents::class;
|
2017-06-20 02:41:30 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|