Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 69 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
UserMergeHooks | |
0.00% |
0 / 69 |
|
0.00% |
0 / 3 |
110 | |
0.00% |
0 / 1 |
onUserMergeAccountFields | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
onMergeAccountFromTo | |
0.00% |
0 / 62 |
|
0.00% |
0 / 1 |
72 | |||
onUserMergeAccountDeleteTables | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Notifications; |
4 | |
5 | use MediaWiki\Deferred\DeferredUpdates; |
6 | use MediaWiki\Extension\Notifications\Model\Event; |
7 | use MediaWiki\Extension\UserMerge\Hooks\AccountDeleteTablesHook; |
8 | use MediaWiki\Extension\UserMerge\Hooks\AccountFieldsHook; |
9 | use MediaWiki\Extension\UserMerge\Hooks\MergeAccountFromToHook; |
10 | use MediaWiki\User\User; |
11 | |
12 | class UserMergeHooks implements |
13 | AccountFieldsHook, |
14 | MergeAccountFromToHook, |
15 | AccountDeleteTablesHook |
16 | { |
17 | |
18 | /** |
19 | * For integration with the UserMerge extension. |
20 | * |
21 | * @param array &$updateFields |
22 | */ |
23 | public function onUserMergeAccountFields( array &$updateFields ) { |
24 | // [ tableName, idField, textField ] |
25 | $dbw = DbFactory::newFromDefault()->getEchoDb( DB_PRIMARY ); |
26 | $updateFields[] = [ 'echo_event', 'event_agent_id', 'db' => $dbw ]; |
27 | $updateFields[] = [ 'echo_notification', 'notification_user', 'db' => $dbw, 'options' => [ 'IGNORE' ] ]; |
28 | $updateFields[] = [ 'echo_email_batch', 'eeb_user_id', 'db' => $dbw, 'options' => [ 'IGNORE' ] ]; |
29 | } |
30 | |
31 | public function onMergeAccountFromTo( User &$oldUser, User &$newUser ) { |
32 | $method = __METHOD__; |
33 | DeferredUpdates::addCallableUpdate( static function () use ( $oldUser, $newUser, $method ) { |
34 | if ( $newUser->isRegistered() ) { |
35 | // Select notifications that are now sent to the same user |
36 | $dbw = DbFactory::newFromDefault()->getEchoDb( DB_PRIMARY ); |
37 | $attributeManager = Services::getInstance()->getAttributeManager(); |
38 | $selfIds = $dbw->newSelectQueryBuilder() |
39 | ->select( 'event_id' ) |
40 | ->from( 'echo_notification' ) |
41 | ->join( 'echo_event', null, 'notification_event = event_id' ) |
42 | ->where( [ |
43 | 'notification_user' => $newUser->getId(), |
44 | 'notification_user = event_agent_id', |
45 | $dbw->expr( 'event_type', '!=', $attributeManager->getNotifyAgentEvents() ), |
46 | ] ) |
47 | ->caller( $method ) |
48 | ->fetchFieldValues(); |
49 | |
50 | // Select newer welcome notification(s) |
51 | $welcomeIds = $dbw->newSelectQueryBuilder() |
52 | ->select( 'event_id' ) |
53 | ->from( 'echo_event' ) |
54 | ->join( 'echo_notification', null, 'notification_event = event_id' ) |
55 | ->where( [ |
56 | 'notification_user' => $newUser->getId(), |
57 | 'event_type' => 'welcome', |
58 | ] ) |
59 | ->orderBy( 'notification_timestamp' ) |
60 | ->offset( 1 ) |
61 | ->caller( $method ) |
62 | ->fetchFieldValues(); |
63 | |
64 | // Select newer milestone notifications (per milestone level) |
65 | $counts = []; |
66 | $thankYouIds = []; |
67 | $thankYouRows = $dbw->newSelectQueryBuilder() |
68 | ->select( Event::selectFields() ) |
69 | ->from( 'echo_event' ) |
70 | ->join( 'echo_notification', null, 'notification_event = event_id' ) |
71 | ->where( [ |
72 | 'notification_user' => $newUser->getId(), |
73 | 'event_type' => 'thank-you-edit', |
74 | ] ) |
75 | ->orderBy( 'notification_timestamp' ) |
76 | ->caller( $method ) |
77 | ->fetchResultSet(); |
78 | foreach ( $thankYouRows as $row ) { |
79 | $event = Event::newFromRow( $row ); |
80 | $editCount = $event ? $event->getExtraParam( 'editCount' ) : null; |
81 | if ( $editCount ) { |
82 | if ( isset( $counts[$editCount] ) ) { |
83 | $thankYouIds[] = $row->event_id; |
84 | } else { |
85 | $counts[$editCount] = true; |
86 | } |
87 | } |
88 | } |
89 | |
90 | // Delete notifications |
91 | $ids = array_merge( $selfIds, $welcomeIds, $thankYouIds ); |
92 | if ( $ids !== [] ) { |
93 | $dbw->newDeleteQueryBuilder() |
94 | ->deleteFrom( 'echo_notification' ) |
95 | ->where( [ |
96 | 'notification_user' => $newUser->getId(), |
97 | 'notification_event' => $ids |
98 | ] ) |
99 | ->caller( $method ) |
100 | ->execute(); |
101 | } |
102 | } |
103 | |
104 | NotifUser::newFromUser( $oldUser )->resetNotificationCount(); |
105 | if ( $newUser->isRegistered() ) { |
106 | NotifUser::newFromUser( $newUser )->resetNotificationCount(); |
107 | } |
108 | } ); |
109 | } |
110 | |
111 | public function onUserMergeAccountDeleteTables( array &$tables ) { |
112 | $dbw = DbFactory::newFromDefault()->getEchoDb( DB_PRIMARY ); |
113 | $tables['echo_notification'] = [ 'notification_user', 'db' => $dbw ]; |
114 | $tables['echo_email_batch'] = [ 'eeb_user_id', 'db' => $dbw ]; |
115 | } |
116 | } |