Use HookHandlers for UserMerge hook

Bug: T315938
Depends-On: Ib78dae49854863af1a37a00636737c94694776ae
Change-Id: I948ef262b9c6f546244e64a62775068bfce79b69
This commit is contained in:
Umherirrender 2023-08-15 23:29:54 +02:00 committed by Jforrester
parent 078788f411
commit 8218e2aa23
4 changed files with 131 additions and 107 deletions

View file

@ -6,7 +6,8 @@ $cfg['directory_list'] = array_merge(
$cfg['directory_list'],
[
'../../extensions/EventLogging',
'../../extensions/CentralAuth'
'../../extensions/CentralAuth',
'../../extensions/UserMerge',
]
);
@ -14,7 +15,8 @@ $cfg['exclude_analysis_directory_list'] = array_merge(
$cfg['exclude_analysis_directory_list'],
[
'../../extensions/EventLogging',
'../../extensions/CentralAuth'
'../../extensions/CentralAuth',
'../../extensions/UserMerge',
]
);

View file

@ -447,9 +447,9 @@
"OutputPageCheckLastModified": "main",
"ArticleDeleteComplete": "main",
"ArticleUndelete": "main",
"UserMergeAccountFields": "MediaWiki\\Extension\\Notifications\\Hooks::onUserMergeAccountFields",
"MergeAccountFromTo": "MediaWiki\\Extension\\Notifications\\Hooks::onMergeAccountFromTo",
"UserMergeAccountDeleteTables": "MediaWiki\\Extension\\Notifications\\Hooks::onUserMergeAccountDeleteTables",
"UserMergeAccountFields": "usermerge",
"MergeAccountFromTo": "usermerge",
"UserMergeAccountDeleteTables": "usermerge",
"EchoGetBundleRules": "MediaWiki\\Extension\\Notifications\\Hooks::onEchoGetBundleRules",
"EchoAbortEmailNotification": "MediaWiki\\Extension\\Notifications\\Hooks::onEchoAbortEmailNotification",
"PageSaveComplete": "main",
@ -475,6 +475,9 @@
},
"schema": {
"class": "MediaWiki\\Extension\\Notifications\\SchemaHooks"
},
"usermerge": {
"class": "MediaWiki\\Extension\\Notifications\\UserMergeHooks"
}
},
"config": {

View file

@ -66,7 +66,6 @@ use MediaWiki\User\Options\Hook\LoadUserOptionsHook;
use MediaWiki\User\Options\Hook\SaveUserOptionsHook;
use MediaWiki\User\UserIdentity;
use MediaWiki\WikiMap\WikiMap;
use MWEchoDbFactory;
use MWEchoNotifUser;
use OutputPage;
use RecentChange;
@ -1458,107 +1457,6 @@ class Hooks implements
] );
}
/**
* For integration with the UserMerge extension.
*
* @param array &$updateFields
*/
public static function onUserMergeAccountFields( &$updateFields ) {
// array( tableName, idField, textField )
$dbw = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_PRIMARY );
$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 static 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
$dbw = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_PRIMARY );
$attributeManager = EchoServices::getInstance()->getAttributeManager();
$selfIds = $dbw->selectFieldValues(
[ 'echo_notification', 'echo_event' ],
'event_id',
[
'notification_user' => $newUser->getId(),
'notification_event = event_id',
'notification_user = event_agent_id',
'event_type NOT IN (' . $dbw->makeList( $attributeManager->getNotifyAgentEvents() ) . ')'
],
$method
) ?: [];
// Select newer welcome notification(s)
$welcomeIds = $dbw->selectFieldValues(
[ 'echo_notification', 'echo_event' ],
'event_id',
[
'notification_user' => $newUser->getId(),
'notification_event = event_id',
'event_type' => 'welcome',
],
$method,
[
'ORDER BY' => 'notification_timestamp ASC',
'OFFSET' => 1,
]
) ?: [];
// Select newer milestone notifications (per milestone level)
$counts = [];
$thankYouIds = [];
$thankYouRows = $dbw->select(
[ 'echo_notification', 'echo_event' ],
Event::selectFields(),
[
'notification_user' => $newUser->getId(),
'notification_event = event_id',
'event_type' => 'thank-you-edit',
],
$method,
[ 'ORDER BY' => 'notification_timestamp ASC' ]
) ?: [];
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 !== [] ) {
$dbw->delete(
'echo_notification',
[
'notification_user' => $newUser->getId(),
'notification_event' => $ids
],
$method
);
}
}
MWEchoNotifUser::newFromUser( $oldUser )->resetNotificationCount();
if ( $newUser->isRegistered() ) {
MWEchoNotifUser::newFromUser( $newUser )->resetNotificationCount();
}
} );
}
public static function onUserMergeAccountDeleteTables( &$tables ) {
$dbw = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_PRIMARY );
$tables['echo_notification'] = [ 'notification_user', 'db' => $dbw ];
$tables['echo_email_batch'] = [ 'eeb_user_id', 'db' => $dbw ];
}
/**
* Sets custom login message for redirect from notification page
*

121
includes/UserMergeHooks.php Normal file
View file

@ -0,0 +1,121 @@
<?php
namespace MediaWiki\Extension\Notifications;
use DeferredUpdates;
use EchoServices;
use MediaWiki\Extension\Notifications\Model\Event;
use MediaWiki\Extension\UserMerge\Hooks\AccountDeleteTablesHook;
use MediaWiki\Extension\UserMerge\Hooks\AccountFieldsHook;
use MediaWiki\Extension\UserMerge\Hooks\MergeAccountFromToHook;
use MWEchoDbFactory;
use MWEchoNotifUser;
use User;
class UserMergeHooks implements
AccountFieldsHook,
MergeAccountFromToHook,
AccountDeleteTablesHook
{
/**
* For integration with the UserMerge extension.
*
* @param array &$updateFields
*/
public function onUserMergeAccountFields( array &$updateFields ) {
// array( tableName, idField, textField )
$dbw = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_PRIMARY );
$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
$dbw = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_PRIMARY );
$attributeManager = EchoServices::getInstance()->getAttributeManager();
$selfIds = $dbw->selectFieldValues(
[ 'echo_notification', 'echo_event' ],
'event_id',
[
'notification_user' => $newUser->getId(),
'notification_event = event_id',
'notification_user = event_agent_id',
'event_type NOT IN (' . $dbw->makeList( $attributeManager->getNotifyAgentEvents() ) . ')'
],
$method
) ?: [];
// Select newer welcome notification(s)
$welcomeIds = $dbw->selectFieldValues(
[ 'echo_notification', 'echo_event' ],
'event_id',
[
'notification_user' => $newUser->getId(),
'notification_event = event_id',
'event_type' => 'welcome',
],
$method,
[
'ORDER BY' => 'notification_timestamp ASC',
'OFFSET' => 1,
]
) ?: [];
// Select newer milestone notifications (per milestone level)
$counts = [];
$thankYouIds = [];
$thankYouRows = $dbw->select(
[ 'echo_notification', 'echo_event' ],
Event::selectFields(),
[
'notification_user' => $newUser->getId(),
'notification_event = event_id',
'event_type' => 'thank-you-edit',
],
$method,
[ 'ORDER BY' => 'notification_timestamp ASC' ]
) ?: [];
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 !== [] ) {
$dbw->delete(
'echo_notification',
[
'notification_user' => $newUser->getId(),
'notification_event' => $ids
],
$method
);
}
}
MWEchoNotifUser::newFromUser( $oldUser )->resetNotificationCount();
if ( $newUser->isRegistered() ) {
MWEchoNotifUser::newFromUser( $newUser )->resetNotificationCount();
}
} );
}
public function onUserMergeAccountDeleteTables( array &$tables ) {
$dbw = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_PRIMARY );
$tables['echo_notification'] = [ 'notification_user', 'db' => $dbw ];
$tables['echo_email_batch'] = [ 'eeb_user_id', 'db' => $dbw ];
}
}