summaryrefslogtreecommitdiff
blob: 585ddc27ed98ff1c0bb7e5af5dc50d5d5884e315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
 * Remove rows from echo_event that don't have corresponding rows in echo_notification.
 *
 * @ingroup Maintenance
 */
require_once getenv( 'MW_INSTALL_PATH' ) !== false
	? getenv( 'MW_INSTALL_PATH' ) . '/maintenance/Maintenance.php'
	: __DIR__ . '/../../../maintenance/Maintenance.php';

/**
 * Maintenance script that removes orphaned event rows
 *
 * @ingroup Maintenance
 */
class RemoveOrphanedEvents extends LoggedUpdateMaintenance {

	public function __construct() {
		parent::__construct();

		$this->mDescription = "Remove rows from echo_event that don't have corresponding rows in echo_notification";

		$this->setBatchSize( 500 );

		$this->requireExtension( 'Echo' );
	}

	public function getUpdateKey() {
		return __CLASS__;
	}

	public function doDBUpdates() {
		$dbFactory = MWEchoDbFactory::newFromDefault();
		$dbw = $dbFactory->getEchoDb( DB_MASTER );
		$dbr = $dbFactory->getEchoDb( DB_REPLICA );
		$iterator = new BatchRowIterator(
			$dbr,
			[ 'echo_event', 'echo_notification' ],
			'event_id',
			$this->mBatchSize
		);
		$iterator->addJoinConditions( [
			'echo_notification' => [ 'LEFT JOIN', 'notification_event=event_id' ]
		] );
		$iterator->addConditions( [
			'notification_user' => null
		] );

		$this->output( "Removing orphaned echo_event rows...\n" );

		$processed = 0;
		foreach ( $iterator as $batch ) {
			$ids = [];
			foreach ( $batch as $row ) {
				$ids[] = $row->event_id;
			}
			$dbw->delete(
				'echo_event',
				[ 'event_id' => $ids ]

			);
			$processed += $dbw->affectedRows();
			$this->output( "Deleted $processed orphaned rows.\n" );
			$dbFactory->waitForSlaves();
		}

		return true;
	}
}

$maintClass = 'RemoveOrphanedEvents';
require_once RUN_MAINTENANCE_IF_MAIN;