From: Joshua Rüsweg Date: Wed, 4 Sep 2019 19:11:02 +0000 (+0200) Subject: Clean up reaction counter before processing X-Git-Tag: 5.2.0_Beta_2~75 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=6260430bdc199f1670f66f46f4d417f40e66a0ea;p=GitHub%2FWoltLab%2FWCF.git Clean up reaction counter before processing Fix #3066 --- 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)), + ]; + } } }