2019-04-13 00:48:01 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Recompute notification counts for all users.
|
|
|
|
*
|
|
|
|
* @ingroup Maintenance
|
|
|
|
*/
|
2022-11-12 07:19:00 +00:00
|
|
|
|
|
|
|
use MediaWiki\Extension\Notifications\DbFactory;
|
2022-11-13 07:48:43 +00:00
|
|
|
use MediaWiki\Extension\Notifications\NotifUser;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\User;
|
2022-11-12 07:19:00 +00:00
|
|
|
|
2019-04-13 00:48:01 +00:00
|
|
|
require_once getenv( 'MW_INSTALL_PATH' ) !== false
|
|
|
|
? getenv( 'MW_INSTALL_PATH' ) . '/maintenance/Maintenance.php'
|
|
|
|
: __DIR__ . '/../../../maintenance/Maintenance.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maintenance script that recomputes notification counts
|
|
|
|
*
|
|
|
|
* @ingroup Maintenance
|
|
|
|
*/
|
|
|
|
class RecomputeNotifCounts extends Maintenance {
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->addDescription( 'Recompute notification counts for all users.' );
|
2020-06-27 10:05:03 +00:00
|
|
|
$this->addOption( 'user-ids',
|
|
|
|
'Comma-separated list of users to recompute notification counts for', false, true );
|
|
|
|
$this->addOption( 'notif-types',
|
|
|
|
'Recompute counts for all users who have unread notifications of one of these types (comma-separated)',
|
2019-04-13 00:48:01 +00:00
|
|
|
false, true );
|
|
|
|
|
|
|
|
$this->setBatchSize( 500 );
|
|
|
|
|
|
|
|
$this->requireExtension( 'Echo' );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
2022-11-12 07:19:00 +00:00
|
|
|
$dbFactory = DbFactory::newFromDefault();
|
2019-04-13 00:48:01 +00:00
|
|
|
$dbrEcho = $dbFactory->getEchoDb( DB_REPLICA );
|
|
|
|
|
|
|
|
$userIDs = $this->getOption( 'user-ids' );
|
|
|
|
$userIDs = $userIDs ? explode( ',', $userIDs ) : null;
|
|
|
|
$notifTypes = $this->getOption( 'notif-types' );
|
|
|
|
$notifTypes = $notifTypes ? explode( ',', $notifTypes ) : null;
|
|
|
|
|
|
|
|
if ( $userIDs ) {
|
|
|
|
$userIterator = array_chunk( $userIDs, $this->getBatchSize() );
|
|
|
|
} elseif ( $notifTypes ) {
|
|
|
|
$userIterator = new BatchRowIterator(
|
|
|
|
$dbrEcho,
|
|
|
|
[ 'echo_event', 'echo_notification' ],
|
|
|
|
'notification_user',
|
|
|
|
$this->getBatchSize()
|
|
|
|
);
|
|
|
|
$userIterator->addJoinConditions( [
|
|
|
|
'echo_notification' => [ 'INNER JOIN', 'notification_event=event_id' ]
|
|
|
|
] );
|
|
|
|
$userIterator->addConditions( [
|
|
|
|
'event_type' => $notifTypes,
|
|
|
|
'notification_read_timestamp' => null
|
|
|
|
] );
|
|
|
|
$userIterator->addOptions( [
|
|
|
|
'GROUP BY' => 'notification_user'
|
|
|
|
] );
|
2020-09-05 02:02:17 +00:00
|
|
|
$userIterator->setCaller( __METHOD__ );
|
2019-04-13 00:48:01 +00:00
|
|
|
} else {
|
|
|
|
$userQuery = User::getQueryInfo();
|
2024-02-13 18:55:34 +00:00
|
|
|
$userIterator = new BatchRowIterator(
|
|
|
|
$this->getReplicaDB(), $userQuery['tables'], 'user_id', $this->getBatchSize()
|
|
|
|
);
|
2019-04-13 00:48:01 +00:00
|
|
|
$userIterator->setFetchColumns( $userQuery['fields'] );
|
|
|
|
$userIterator->addJoinConditions( $userQuery['joins'] );
|
2020-09-05 02:02:17 +00:00
|
|
|
$userIterator->setCaller( __METHOD__ );
|
2019-04-13 00:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
foreach ( $userIterator as $batch ) {
|
|
|
|
foreach ( $batch as $rowOrID ) {
|
|
|
|
if ( is_object( $rowOrID ) && isset( $rowOrID->user_id ) ) {
|
|
|
|
$user = User::newFromRow( $rowOrID );
|
|
|
|
} else {
|
|
|
|
$user = User::newFromId( is_object( $rowOrID ) ? $rowOrID->notification_user : $rowOrID );
|
|
|
|
}
|
2022-11-13 07:48:43 +00:00
|
|
|
$notifUser = NotifUser::newFromUser( $user );
|
2019-04-13 00:48:01 +00:00
|
|
|
$notifUser->resetNotificationCount();
|
|
|
|
}
|
|
|
|
$count += count( $batch );
|
|
|
|
$this->output( "$count users' counts recomputed.\n" );
|
2023-01-05 21:14:51 +00:00
|
|
|
$this->waitForReplication();
|
2019-04-13 00:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$maintClass = RecomputeNotifCounts::class;
|
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|