2015-12-15 15:41:21 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-12 07:19:00 +00:00
|
|
|
use MediaWiki\Extension\Notifications\AttributeManager;
|
|
|
|
use MediaWiki\Extension\Notifications\DbFactory;
|
2022-11-13 07:48:43 +00:00
|
|
|
use MediaWiki\Extension\Notifications\NotifUser;
|
|
|
|
use MediaWiki\Extension\Notifications\UnreadWikis;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\CentralId\CentralIdLookup;
|
|
|
|
use MediaWiki\User\User;
|
2023-04-25 09:53:21 +00:00
|
|
|
use MediaWiki\WikiMap\WikiMap;
|
2021-06-24 19:21:49 +00:00
|
|
|
|
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() {
|
2022-11-12 07:19:00 +00:00
|
|
|
$dbFactory = DbFactory::newFromDefault();
|
2023-11-09 20:16:38 +00:00
|
|
|
$lookup = $this->getServiceContainer()->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(
|
2024-02-13 18:55:34 +00:00
|
|
|
$this->getReplicaDB(), $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;
|
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-12-19 01:16:52 +00:00
|
|
|
$user = $lookup->localUserFromCentralId(
|
2021-06-24 19:21:49 +00:00
|
|
|
$row->euw_user,
|
|
|
|
CentralIdLookup::AUDIENCE_RAW
|
|
|
|
);
|
2021-12-19 01:16:52 +00:00
|
|
|
if ( !$user ) {
|
2021-07-21 19:04:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-06-17 14:45:28 +00:00
|
|
|
} else {
|
|
|
|
$user = User::newFromRow( $row );
|
|
|
|
}
|
2015-12-15 15:41:21 +00:00
|
|
|
|
2022-11-13 07:48:43 +00:00
|
|
|
$notifUser = NotifUser::newFromUser( $user );
|
|
|
|
$uw = UnreadWikis::newFromUser( $user );
|
2016-06-17 13:43:22 +00:00
|
|
|
if ( $uw ) {
|
2022-11-12 07:19:00 +00:00
|
|
|
$alertCount = $notifUser->getNotificationCount( AttributeManager::ALERT, false );
|
|
|
|
$alertUnread = $notifUser->getLastUnreadNotificationTime( AttributeManager::ALERT, false );
|
2016-06-17 13:43:22 +00:00
|
|
|
|
2022-11-12 07:19:00 +00:00
|
|
|
$msgCount = $notifUser->getNotificationCount( AttributeManager::MESSAGE, false );
|
|
|
|
$msgUnread = $notifUser->getLastUnreadNotificationTime( AttributeManager::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" );
|
2023-01-05 21:14:51 +00:00
|
|
|
$this->waitForReplication();
|
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;
|