From 8218e2aa2330da15465acf2a9a62b83d5ad589c0 Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Tue, 15 Aug 2023 23:29:54 +0200 Subject: [PATCH] Use HookHandlers for UserMerge hook Bug: T315938 Depends-On: Ib78dae49854863af1a37a00636737c94694776ae Change-Id: I948ef262b9c6f546244e64a62775068bfce79b69 --- .phan/config.php | 6 +- extension.json | 9 ++- includes/Hooks.php | 102 ------------------------------ includes/UserMergeHooks.php | 121 ++++++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 107 deletions(-) create mode 100644 includes/UserMergeHooks.php diff --git a/.phan/config.php b/.phan/config.php index c3247bd93..ce3b30d1c 100644 --- a/.phan/config.php +++ b/.phan/config.php @@ -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', ] ); diff --git a/extension.json b/extension.json index 2280db198..a604b5878 100644 --- a/extension.json +++ b/extension.json @@ -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": { diff --git a/includes/Hooks.php b/includes/Hooks.php index c91a54203..77eb32a35 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -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 * diff --git a/includes/UserMergeHooks.php b/includes/UserMergeHooks.php new file mode 100644 index 000000000..c3341ff5c --- /dev/null +++ b/includes/UserMergeHooks.php @@ -0,0 +1,121 @@ +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 ]; + } +}