From 6260430bdc199f1670f66f46f4d417f40e66a0ea Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joshua=20R=C3=BCsweg?= Date: Wed, 4 Sep 2019 21:11:02 +0200 Subject: [PATCH] Clean up reaction counter before processing Fix #3066 --- .../system/reaction/ReactionHandler.class.php | 44 ++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/wcfsetup/install/files/lib/system/reaction/ReactionHandler.class.php b/wcfsetup/install/files/lib/system/reaction/ReactionHandler.class.php index 926e6a2cb1..226a4ef8fb 100644 --- a/wcfsetup/install/files/lib/system/reaction/ReactionHandler.class.php +++ b/wcfsetup/install/files/lib/system/reaction/ReactionHandler.class.php @@ -429,6 +429,8 @@ class ReactionHandler extends SingletonFactory { $cachedReactions[$reactionType->reactionTypeID] = 1; } + $cachedReactions = self::cleanUpCachedReactions($cachedReactions); + // build update date $updateData = [ 'likes' => $cumulativeLikes, @@ -578,6 +580,8 @@ class ReactionHandler extends SingletonFactory { } } + $cachedReactions = self::cleanUpCachedReactions($cachedReactions); + // build update date $updateData = [ 'likes' => $cumulativeLikes, @@ -735,6 +739,22 @@ class ReactionHandler extends SingletonFactory { return $firstReactionType ? $firstReactionType->reactionTypeID : null; } + /** + * Removes deleted reactions from the reaction counter for the like object table. + * + * @param array $cachedReactions + * @return array + */ + private function cleanUpCachedReactions(array $cachedReactions) { + foreach ($cachedReactions as $reactionTypeID => $count) { + if (self::getReactionTypeByID($reactionTypeID) === null) { + unset($cachedReactions[$reactionTypeID]); + } + } + + return $cachedReactions; + } + /** * @param string|null $cachedReactions * @return array|null @@ -744,17 +764,21 @@ class ReactionHandler extends SingletonFactory { if ($cachedReactions) { $cachedReactions = @unserialize($cachedReactions); - if (is_array($cachedReactions) && !empty($cachedReactions)) { - $allReactions = array_sum($cachedReactions); - - arsort($cachedReactions, SORT_NUMERIC); + if (is_array($cachedReactions)) { + $cachedReactions = self::cleanUpCachedReactions($cachedReactions); - $count = current($cachedReactions); - return [ - 'count' => $count, - 'other' => $allReactions - $count, - 'reaction' => ReactionTypeCache::getInstance()->getReactionTypeByID(key($cachedReactions)), - ]; + if (!empty($cachedReactions)) { + $allReactions = array_sum($cachedReactions); + + arsort($cachedReactions, SORT_NUMERIC); + + $count = current($cachedReactions); + return [ + 'count' => $count, + 'other' => $allReactions - $count, + 'reaction' => ReactionTypeCache::getInstance()->getReactionTypeByID(key($cachedReactions)), + ]; + } } } -- 2.20.1