summaryrefslogtreecommitdiff
blob: 432dc05366831ebca21f2f48b98c38c878a9c639 (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
<?php

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

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

		$this->mDescription = "Backfill echo_unread_wikis table";
		$this->addOption( 'rebuild', 'Only recompute already-existing rows' );
		$this->setBatchSize( 300 );
		$this->requireExtension( 'Echo' );
	}

	public function execute() {
		$dbFactory = MWEchoDbFactory::newFromDefault();
		$lookup = CentralIdLookup::factory();

		$rebuild = $this->hasOption( 'rebuild' );
		if ( $rebuild ) {
			$iterator = new BatchRowIterator( $dbFactory->getSharedDb( DB_REPLICA ), 'echo_unread_wikis', 'euw_user', $this->mBatchSize );
			$iterator->addConditions( [ 'euw_wiki' => wfWikiID() ] );
		} else {
			$userQuery = User::getQueryInfo();
			$iterator = new BatchRowIterator(
				wfGetDB( DB_REPLICA ), $userQuery['tables'], 'user_id', $this->mBatchSize
			);
			$iterator->setFetchColumns( $userQuery['fields'] );
			$iterator->addJoinConditions( $userQuery['joins'] );
		}

		$processed = 0;
		foreach ( $iterator as $batch ) {
			foreach ( $batch as $row ) {
				if ( $rebuild ) {
					$user = $lookup->localUserFromCentralId( $row->euw_user, CentralIdLookup::AUDIENCE_RAW );
				} else {
					$user = User::newFromRow( $row );
				}
				if ( !$user ) {
					continue;
				}

				$notifUser = MWEchoNotifUser::newFromUser( $user );
				$uw = EchoUnreadWikis::newFromUser( $user );
				if ( $uw ) {
					$alertCount = $notifUser->getNotificationCount( EchoAttributeManager::ALERT, false );
					$alertUnread = $notifUser->getLastUnreadNotificationTime( EchoAttributeManager::ALERT, false );

					$msgCount = $notifUser->getNotificationCount( EchoAttributeManager::MESSAGE, false );
					$msgUnread = $notifUser->getLastUnreadNotificationTime( EchoAttributeManager::MESSAGE, false );

					if ( ( $alertCount !== 0 && $alertUnread === false ) || ( $msgCount !== 0 && $msgUnread === false ) ) {
						// If there are alerts, there should be an alert timestamp (same for messages).

						// Otherwise, there is a race condition between the two values, indicating there's already
						// just been an updateCount call, so we can skip this user.
						continue;
					}

					$uw->updateCount( wfWikiID(), $alertCount, $alertUnread, $msgCount, $msgUnread );
				}
			}

			$processed += count( $batch );
			$this->output( "Updated $processed users.\n" );
			$dbFactory->waitForSlaves();
		}
	}
}

$maintClass = "BackfillUnreadWikis";
require_once RUN_MAINTENANCE_IF_MAIN;