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() {
|
|
|
|
$dbFactory = MWEchoDbFactory::newFromDefault();
|
|
|
|
$dbw = $dbFactory->getEchoDb( DB_MASTER );
|
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',
|
|
|
|
$this->mBatchSize
|
|
|
|
);
|
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,
|
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
|
|
|
|
|
|
|
$this->output( "Removing orphaned echo_event rows...\n" );
|
|
|
|
|
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" );
|
2020-06-09 01:09:44 +00:00
|
|
|
$dbFactory->waitForReplicas();
|
2019-04-22 23:01:02 +00:00
|
|
|
}
|
2016-06-06 18:27:23 +00:00
|
|
|
|
2019-04-22 23:01:02 +00:00
|
|
|
$this->output( "Removing any remaining orphaned echo_target_page rows...\n" );
|
|
|
|
$iterator = new BatchRowIterator(
|
|
|
|
$dbr,
|
|
|
|
[ 'echo_target_page', 'echo_event' ],
|
|
|
|
'etp_event',
|
|
|
|
$this->mBatchSize
|
|
|
|
);
|
|
|
|
$iterator->addJoinConditions( [ 'echo_event' => [ 'LEFT JOIN', 'event_id=etp_event' ] ] );
|
|
|
|
$iterator->addConditions( [ 'event_type' => null ] );
|
|
|
|
$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" );
|
2020-06-09 01:09:44 +00:00
|
|
|
$dbFactory->waitForReplicas();
|
2016-06-06 18:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|