summaryrefslogtreecommitdiff
blob: fa7067d68e0281c555e5140dee7f14ca35f89747 (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
73
74
75
76
77
78
79
80
<?php

$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
	$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";

class BackfillReadBundles extends Maintenance {
	public function __construct() {
		parent::__construct();

		$this->mDescription = "Backfill echo_notification.notification_read_timestamp for bundles";

		$this->setBatchSize( 300 );

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

	public function execute() {
		$dbFactory = MWEchoDbFactory::newFromDefault();
		$dbw = $dbFactory->getEchoDb( DB_MASTER );
		$dbr = $dbFactory->getEchoDb( DB_REPLICA );
		$iterator = new BatchRowIterator(
			$dbr,
			'echo_notification',
			[ 'notification_user', 'notification_event' ],
			$this->mBatchSize
		);
		$iterator->setFetchColumns( [ 'notification_bundle_display_hash', 'notification_read_timestamp' ] );

		$unreadNonBase = $dbr->selectSQLText(
			'echo_notification',
			'notification_bundle_display_hash',
			[
				'notification_bundle_base' => 0,
				'notification_read_timestamp IS NULL',
				"notification_bundle_display_hash <> ''",
			]
		);

		$iterator->addConditions( [
			'notification_bundle_base' => 1,
			'notification_read_timestamp IS NOT NULL',
			"notification_bundle_display_hash IN ( $unreadNonBase )",
		] );

		$processed = 0;
		foreach ( $iterator as $batch ) {
			foreach ( $batch as $row ) {
				$userId = $row->notification_user;
				$displayHash = $row->notification_bundle_display_hash;
				$readTimestamp = $row->notification_read_timestamp;

				$result = $dbw->update(
					'echo_notification',
					[ 'notification_read_timestamp' => $readTimestamp ],
					[
						'notification_user' => $userId,
						'notification_bundle_display_hash' => $displayHash,
						'notification_bundle_base' => 0,
						'notification_read_timestamp IS NULL',
					]
				);

				if ( !$result ) {
					$this->output( "Failed to set read_timestamp on notifications with bundle_display_hash: $displayHash\n" );
				}

				$processed += $dbw->affectedRows();
			}

			$this->output( "Updated $processed notifications.\n" );
			$dbFactory->waitForSlaves();
		}
	}
}

$maintClass = "BackfillReadBundles";
require_once RUN_MAINTENANCE_IF_MAIN;