Skip to content

Commit af5add2

Browse files
committed
Refactor CreateStackableUserNotification to optimize notification updates and support chunked processing
1 parent fbb9ea8 commit af5add2

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

wcfsetup/install/files/lib/command/user/notification/CreateStackableUserNotification.class.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use wcf\data\user\notification\UserNotificationList;
88
use wcf\data\user\User;
99
use wcf\data\user\UserProfile;
10+
use wcf\system\database\util\PreparedStatementConditionBuilder;
1011
use wcf\system\WCF;
1112

1213
/**
@@ -90,33 +91,38 @@ private function createUserNotifications(UserProfile $author, array $notificatio
9091
(notificationID, authorID, time)
9192
VALUES (?, ?, ?)";
9293
$authorStatement = WCF::getDB()->prepare($sql);
93-
$sql = "UPDATE wcf1_user_notification
94-
SET timesTriggered = timesTriggered + ?,
95-
guestTimesTriggered = guestTimesTriggered + ?
96-
WHERE notificationID = ?";
97-
$triggerStatement = WCF::getDB()->prepare($sql);
9894

9995
$authorId = $author->userID;
10096
$isGuestTrigger = $authorId ? 0 : 1;
10197
$now = TIME_NOW;
102-
$notificationIDs = [];
98+
$notificationIDs = \array_map(static function ($notificationData) {
99+
return $notificationData['object']->notificationID;
100+
}, $notifications);
103101

104102
WCF::getDB()->beginTransaction();
105-
foreach ($notifications as $notificationData) {
106-
$notificationID = $notificationData['object']->notificationID;
107-
$notificationIDs[] = $notificationID;
108-
103+
foreach ($notificationIDs as $notificationID) {
109104
$authorStatement->execute([
110105
$notificationID,
111106
$authorId,
112107
$now,
113108
]);
109+
}
110+
111+
$chunks = \array_chunk($notificationIDs, 1_000);
112+
foreach ($chunks as $chunk) {
113+
$conditionBuilder = new PreparedStatementConditionBuilder();
114+
$conditionBuilder->add('notificationID IN (?)', [$chunk]);
114115

115-
$triggerStatement->execute([
116+
$sql = "UPDATE wcf1_user_notification
117+
SET timesTriggered = timesTriggered + ?,
118+
guestTimesTriggered = guestTimesTriggered + ?
119+
" . $conditionBuilder;
120+
$triggerStatement = WCF::getDB()->prepare($sql);
121+
122+
$triggerStatement->execute(\array_merge([
116123
1,
117124
$isGuestTrigger,
118-
$notificationID,
119-
]);
125+
], $conditionBuilder->getParameters()));
120126
}
121127
WCF::getDB()->commitTransaction();
122128

0 commit comments

Comments
 (0)