2015-12-15 15:41:21 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-24 19:21:49 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
2015-12-15 15:41:21 +00:00
|
|
|
$IP = getenv( 'MW_INSTALL_PATH' );
|
|
|
|
if ( $IP === false ) {
|
|
|
|
$IP = __DIR__ . '/../../..';
|
|
|
|
}
|
2017-06-20 02:41:30 +00:00
|
|
|
require_once "$IP/maintenance/Maintenance.php";
|
2015-12-15 15:41:21 +00:00
|
|
|
|
|
|
|
class BackfillUnreadWikis extends Maintenance {
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
|
2019-03-27 05:07:48 +00:00
|
|
|
$this->addDescription( "Backfill echo_unread_wikis table" );
|
2016-06-17 14:45:28 +00:00
|
|
|
$this->addOption( 'rebuild', 'Only recompute already-existing rows' );
|
2015-12-15 15:41:21 +00:00
|
|
|
$this->setBatchSize( 300 );
|
2016-12-01 21:34:43 +00:00
|
|
|
$this->requireExtension( 'Echo' );
|
2015-12-15 15:41:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
2016-06-17 14:45:28 +00:00
|
|
|
$dbFactory = MWEchoDbFactory::newFromDefault();
|
2021-06-24 19:21:49 +00:00
|
|
|
$lookup = MediaWikiServices::getInstance()->getCentralIdLookup();
|
2015-12-15 15:41:21 +00:00
|
|
|
|
2016-06-17 14:45:28 +00:00
|
|
|
$rebuild = $this->hasOption( 'rebuild' );
|
|
|
|
if ( $rebuild ) {
|
2020-06-27 10:05:03 +00:00
|
|
|
$iterator = new BatchRowIterator(
|
|
|
|
$dbFactory->getSharedDb( DB_REPLICA ),
|
|
|
|
'echo_unread_wikis',
|
|
|
|
'euw_user',
|
2020-12-17 18:27:43 +00:00
|
|
|
$this->getBatchSize()
|
2020-06-27 10:05:03 +00:00
|
|
|
);
|
2021-12-21 00:47:31 +00:00
|
|
|
$iterator->addConditions( [ 'euw_wiki' => WikiMap::getCurrentWikiId() ] );
|
2016-06-17 14:45:28 +00:00
|
|
|
} else {
|
2017-10-12 19:45:53 +00:00
|
|
|
$userQuery = User::getQueryInfo();
|
|
|
|
$iterator = new BatchRowIterator(
|
2020-12-17 18:27:43 +00:00
|
|
|
wfGetDB( DB_REPLICA ), $userQuery['tables'], 'user_id', $this->getBatchSize()
|
2017-10-12 19:45:53 +00:00
|
|
|
);
|
|
|
|
$iterator->setFetchColumns( $userQuery['fields'] );
|
|
|
|
$iterator->addJoinConditions( $userQuery['joins'] );
|
2016-06-17 14:45:28 +00:00
|
|
|
}
|
2015-12-15 15:41:21 +00:00
|
|
|
|
2020-09-05 02:02:17 +00:00
|
|
|
$iterator->setCaller( __METHOD__ );
|
|
|
|
|
2016-01-19 03:14:34 +00:00
|
|
|
$processed = 0;
|
2021-06-24 19:21:49 +00:00
|
|
|
$userFactory = MediaWikiServices::getInstance()->getUserFactory();
|
2015-12-15 15:41:21 +00:00
|
|
|
foreach ( $iterator as $batch ) {
|
|
|
|
foreach ( $batch as $row ) {
|
2016-06-17 14:45:28 +00:00
|
|
|
if ( $rebuild ) {
|
2021-06-24 19:21:49 +00:00
|
|
|
$userIdentity = $lookup->localUserFromCentralId(
|
|
|
|
$row->euw_user,
|
|
|
|
CentralIdLookup::AUDIENCE_RAW
|
|
|
|
);
|
|
|
|
if ( !$userIdentity ) {
|
2021-07-21 19:04:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-06-24 19:21:49 +00:00
|
|
|
$user = $userFactory->newFromUserIdentity( $userIdentity );
|
2016-06-17 14:45:28 +00:00
|
|
|
} else {
|
|
|
|
$user = User::newFromRow( $row );
|
|
|
|
}
|
2015-12-15 15:41:21 +00:00
|
|
|
|
|
|
|
$notifUser = MWEchoNotifUser::newFromUser( $user );
|
2016-06-17 13:43:22 +00:00
|
|
|
$uw = EchoUnreadWikis::newFromUser( $user );
|
|
|
|
if ( $uw ) {
|
2018-05-31 23:11:57 +00:00
|
|
|
$alertCount = $notifUser->getNotificationCount( EchoAttributeManager::ALERT, false );
|
|
|
|
$alertUnread = $notifUser->getLastUnreadNotificationTime( EchoAttributeManager::ALERT, false );
|
2016-06-17 13:43:22 +00:00
|
|
|
|
2018-05-31 23:11:57 +00:00
|
|
|
$msgCount = $notifUser->getNotificationCount( EchoAttributeManager::MESSAGE, false );
|
|
|
|
$msgUnread = $notifUser->getLastUnreadNotificationTime( EchoAttributeManager::MESSAGE, false );
|
2016-06-17 13:43:22 +00:00
|
|
|
|
2020-06-27 10:05:03 +00:00
|
|
|
if ( ( $alertCount !== 0 && $alertUnread === false ) ||
|
|
|
|
( $msgCount !== 0 && $msgUnread === false )
|
|
|
|
) {
|
2016-07-18 23:46:53 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2021-12-21 00:47:31 +00:00
|
|
|
$uw->updateCount( WikiMap::getCurrentWikiId(), $alertCount, $alertUnread, $msgCount, $msgUnread );
|
2016-06-17 13:43:22 +00:00
|
|
|
}
|
2015-12-15 15:41:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-19 03:14:34 +00:00
|
|
|
$processed += count( $batch );
|
|
|
|
$this->output( "Updated $processed users.\n" );
|
2020-06-09 01:09:44 +00:00
|
|
|
$dbFactory->waitForReplicas();
|
2015-12-15 15:41:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 14:14:15 +00:00
|
|
|
$maintClass = BackfillUnreadWikis::class;
|
2018-03-17 20:55:20 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|