2023-08-15 21:29:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\Notifications;
|
|
|
|
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\Deferred\DeferredUpdates;
|
2023-08-15 21:29:54 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
|
|
|
use MediaWiki\Extension\UserMerge\Hooks\AccountDeleteTablesHook;
|
|
|
|
use MediaWiki\Extension\UserMerge\Hooks\AccountFieldsHook;
|
|
|
|
use MediaWiki\Extension\UserMerge\Hooks\MergeAccountFromToHook;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\User;
|
2023-08-15 21:29:54 +00:00
|
|
|
|
2024-05-13 18:33:18 +00:00
|
|
|
class UserMergeHooks implements
|
|
|
|
AccountFieldsHook,
|
|
|
|
MergeAccountFromToHook,
|
|
|
|
AccountDeleteTablesHook
|
|
|
|
{
|
2023-08-15 21:29:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For integration with the UserMerge extension.
|
|
|
|
*
|
|
|
|
* @param array &$updateFields
|
|
|
|
*/
|
|
|
|
public function onUserMergeAccountFields( array &$updateFields ) {
|
2023-08-16 15:46:59 +00:00
|
|
|
// [ tableName, idField, textField ]
|
2022-11-13 07:53:42 +00:00
|
|
|
$dbw = DbFactory::newFromDefault()->getEchoDb( DB_PRIMARY );
|
2023-08-15 21:29:54 +00:00
|
|
|
$updateFields[] = [ 'echo_event', 'event_agent_id', 'db' => $dbw ];
|
|
|
|
$updateFields[] = [ 'echo_notification', 'notification_user', 'db' => $dbw, 'options' => [ 'IGNORE' ] ];
|
|
|
|
$updateFields[] = [ 'echo_email_batch', 'eeb_user_id', 'db' => $dbw, 'options' => [ 'IGNORE' ] ];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onMergeAccountFromTo( User &$oldUser, User &$newUser ) {
|
|
|
|
$method = __METHOD__;
|
|
|
|
DeferredUpdates::addCallableUpdate( static function () use ( $oldUser, $newUser, $method ) {
|
|
|
|
if ( $newUser->isRegistered() ) {
|
|
|
|
// Select notifications that are now sent to the same user
|
2022-11-13 07:53:42 +00:00
|
|
|
$dbw = DbFactory::newFromDefault()->getEchoDb( DB_PRIMARY );
|
|
|
|
$attributeManager = Services::getInstance()->getAttributeManager();
|
2024-04-27 21:37:18 +00:00
|
|
|
$selfIds = $dbw->newSelectQueryBuilder()
|
|
|
|
->select( 'event_id' )
|
|
|
|
->from( 'echo_notification' )
|
|
|
|
->join( 'echo_event', null, 'notification_event = event_id' )
|
|
|
|
->where( [
|
2023-08-15 21:29:54 +00:00
|
|
|
'notification_user' => $newUser->getId(),
|
|
|
|
'notification_user = event_agent_id',
|
2024-04-27 21:37:18 +00:00
|
|
|
$dbw->expr( 'event_type', '!=', $attributeManager->getNotifyAgentEvents() ),
|
|
|
|
] )
|
|
|
|
->caller( $method )
|
|
|
|
->fetchFieldValues();
|
2023-08-15 21:29:54 +00:00
|
|
|
|
|
|
|
// Select newer welcome notification(s)
|
2024-04-27 21:37:18 +00:00
|
|
|
$welcomeIds = $dbw->newSelectQueryBuilder()
|
|
|
|
->select( 'event_id' )
|
|
|
|
->from( 'echo_event' )
|
|
|
|
->join( 'echo_notification', null, 'notification_event = event_id' )
|
|
|
|
->where( [
|
2023-08-15 21:29:54 +00:00
|
|
|
'notification_user' => $newUser->getId(),
|
|
|
|
'event_type' => 'welcome',
|
2024-04-27 21:37:18 +00:00
|
|
|
] )
|
|
|
|
->orderBy( 'notification_timestamp' )
|
|
|
|
->offset( 1 )
|
|
|
|
->caller( $method )
|
|
|
|
->fetchFieldValues();
|
2023-08-15 21:29:54 +00:00
|
|
|
|
|
|
|
// Select newer milestone notifications (per milestone level)
|
|
|
|
$counts = [];
|
|
|
|
$thankYouIds = [];
|
2024-04-27 21:37:18 +00:00
|
|
|
$thankYouRows = $dbw->newSelectQueryBuilder()
|
|
|
|
->select( Event::selectFields() )
|
|
|
|
->from( 'echo_event' )
|
|
|
|
->join( 'echo_notification', null, 'notification_event = event_id' )
|
|
|
|
->where( [
|
2023-08-15 21:29:54 +00:00
|
|
|
'notification_user' => $newUser->getId(),
|
|
|
|
'event_type' => 'thank-you-edit',
|
2024-04-27 21:37:18 +00:00
|
|
|
] )
|
|
|
|
->orderBy( 'notification_timestamp' )
|
|
|
|
->caller( $method )
|
|
|
|
->fetchResultSet();
|
2023-08-15 21:29:54 +00:00
|
|
|
foreach ( $thankYouRows as $row ) {
|
|
|
|
$event = Event::newFromRow( $row );
|
|
|
|
$editCount = $event ? $event->getExtraParam( 'editCount' ) : null;
|
|
|
|
if ( $editCount ) {
|
|
|
|
if ( isset( $counts[$editCount] ) ) {
|
|
|
|
$thankYouIds[] = $row->event_id;
|
|
|
|
} else {
|
|
|
|
$counts[$editCount] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete notifications
|
|
|
|
$ids = array_merge( $selfIds, $welcomeIds, $thankYouIds );
|
|
|
|
if ( $ids !== [] ) {
|
2024-04-12 19:50:43 +00:00
|
|
|
$dbw->newDeleteQueryBuilder()
|
|
|
|
->deleteFrom( 'echo_notification' )
|
|
|
|
->where( [
|
2023-08-15 21:29:54 +00:00
|
|
|
'notification_user' => $newUser->getId(),
|
|
|
|
'notification_event' => $ids
|
2024-04-12 19:50:43 +00:00
|
|
|
] )
|
|
|
|
->caller( $method )
|
|
|
|
->execute();
|
2023-08-15 21:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 07:48:43 +00:00
|
|
|
NotifUser::newFromUser( $oldUser )->resetNotificationCount();
|
2023-08-15 21:29:54 +00:00
|
|
|
if ( $newUser->isRegistered() ) {
|
2022-11-13 07:48:43 +00:00
|
|
|
NotifUser::newFromUser( $newUser )->resetNotificationCount();
|
2023-08-15 21:29:54 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onUserMergeAccountDeleteTables( array &$tables ) {
|
2022-11-13 07:53:42 +00:00
|
|
|
$dbw = DbFactory::newFromDefault()->getEchoDb( DB_PRIMARY );
|
2023-08-15 21:29:54 +00:00
|
|
|
$tables['echo_notification'] = [ 'notification_user', 'db' => $dbw ];
|
|
|
|
$tables['echo_email_batch'] = [ 'eeb_user_id', 'db' => $dbw ];
|
|
|
|
}
|
|
|
|
}
|