From a6ced361890ab44e049369ac63a74b63f8820b7e Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sat, 25 Jan 2020 09:57:28 +0300 Subject: [PATCH] getCategoryCounters: properly calculate counters for child subcategory entries getCategoryUnread: cleanup --- classes/counters.php | 2 +- classes/feeds.php | 48 +++++++++++++------------------------------- 2 files changed, 15 insertions(+), 35 deletions(-) diff --git a/classes/counters.php b/classes/counters.php index a0e39ea52..b65f0adeb 100644 --- a/classes/counters.php +++ b/classes/counters.php @@ -48,7 +48,7 @@ class Counters { while ($line = $sth->fetch()) { if ($line["num_children"] > 0) { - $child_counter = Feeds::getCategoryChildrenUnread($line["cat_id"], $_SESSION["uid"]); + $child_counter = Feeds::getCategoryChildrenUnread($line["id"], $_SESSION["uid"]); } else { $child_counter = 0; } diff --git a/classes/feeds.php b/classes/feeds.php index d24fcdee9..c6a2cf66b 100755 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -1298,46 +1298,26 @@ class Feeds extends Handler_Protected { if ($cat >= 0) { - if (!$cat) $cat = null; - - $sth = $pdo->prepare("SELECT id FROM ttrss_feeds - WHERE (cat_id = :cat OR (:cat IS NULL AND cat_id IS NULL)) - AND owner_uid = :uid"); - - $sth->execute([":cat" => $cat, ":uid" => $owner_uid]); - - $cat_feeds = array(); - while ($line = $sth->fetch()) { - array_push($cat_feeds, "feed_id = " . (int)$line["id"]); - } - - if (count($cat_feeds) == 0) return 0; - - $match_part = implode(" OR ", $cat_feeds); - - $sth = $pdo->prepare("SELECT COUNT(int_id) AS unread + $sth = $pdo->prepare("SELECT SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS unread FROM ttrss_user_entries - WHERE unread = true AND ($match_part) - AND owner_uid = ?"); - $sth->execute([$owner_uid]); + WHERE feed_id IN (SELECT id FROM ttrss_feeds + WHERE (cat_id = :cat OR (:cat IS NULL AND cat_id IS NULL)) + AND owner_uid = :uid) + AND owner_uid = :uid"); + $sth->execute(["cat" => $cat ? $cat : null, "uid" => $owner_uid]); + $row = $sth->fetch(); - $unread = 0; + return $row["unread"]; - # this needs to be rewritten - while ($line = $sth->fetch()) { - $unread += $line["unread"]; - } - - return $unread; } else if ($cat == -1) { return 0; } else if ($cat == -2) { - $sth = $pdo->prepare("SELECT COUNT(unread) AS unread FROM - ttrss_user_entries, ttrss_user_labels2 - WHERE article_id = ref_id AND unread = true - AND ttrss_user_entries.owner_uid = ?"); - $sth->execute([$owner_uid]); + $sth = $pdo->prepare("SELECT SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS unread FROM + ttrss_user_entries ue, ttrss_user_labels2 l + WHERE article_id = ref_id AND + ue.owner_uid = :uid"); + $sth->execute(["uid" => $owner_uid]); $row = $sth->fetch(); return $row["unread"]; @@ -1357,7 +1337,7 @@ class Feeds extends Handler_Protected { $unread = 0; while ($line = $sth->fetch()) { - $unread += Feeds::getCategoryUnread($line["id"], $owner_uid); + $unread += Feeds::getCategoryUnread($line["id"], $owner_uid) + $unread += Feeds::getCategoryChildrenUnread($line["id"], $owner_uid); }