getCategoryCounters: properly calculate counters for child subcategory entries

getCategoryUnread: cleanup
This commit is contained in:
Andrew Dolgov 2020-01-25 09:57:28 +03:00
parent deefa901ab
commit a6ced36189
2 changed files with 15 additions and 35 deletions

View File

@ -48,7 +48,7 @@ class Counters {
while ($line = $sth->fetch()) { while ($line = $sth->fetch()) {
if ($line["num_children"] > 0) { if ($line["num_children"] > 0) {
$child_counter = Feeds::getCategoryChildrenUnread($line["cat_id"], $_SESSION["uid"]); $child_counter = Feeds::getCategoryChildrenUnread($line["id"], $_SESSION["uid"]);
} else { } else {
$child_counter = 0; $child_counter = 0;
} }

View File

@ -1298,46 +1298,26 @@ class Feeds extends Handler_Protected {
if ($cat >= 0) { if ($cat >= 0) {
if (!$cat) $cat = null; $sth = $pdo->prepare("SELECT SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS unread
$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
FROM ttrss_user_entries FROM ttrss_user_entries
WHERE unread = true AND ($match_part) WHERE feed_id IN (SELECT id FROM ttrss_feeds
AND owner_uid = ?"); WHERE (cat_id = :cat OR (:cat IS NULL AND cat_id IS NULL))
$sth->execute([$owner_uid]); 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) { } else if ($cat == -1) {
return 0; return 0;
} else if ($cat == -2) { } else if ($cat == -2) {
$sth = $pdo->prepare("SELECT COUNT(unread) AS unread FROM $sth = $pdo->prepare("SELECT SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS unread FROM
ttrss_user_entries, ttrss_user_labels2 ttrss_user_entries ue, ttrss_user_labels2 l
WHERE article_id = ref_id AND unread = true WHERE article_id = ref_id AND
AND ttrss_user_entries.owner_uid = ?"); ue.owner_uid = :uid");
$sth->execute([$owner_uid]); $sth->execute(["uid" => $owner_uid]);
$row = $sth->fetch(); $row = $sth->fetch();
return $row["unread"]; return $row["unread"];
@ -1357,7 +1337,7 @@ class Feeds extends Handler_Protected {
$unread = 0; $unread = 0;
while ($line = $sth->fetch()) { 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); $unread += Feeds::getCategoryChildrenUnread($line["id"], $owner_uid);
} }