From 6f93c45c2841ad8ca3b5b5eda94407835cb92ade Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 2 Mar 2021 20:07:31 +0300 Subject: [PATCH] use orm in some more places; prevent _get_cat_title from hitting the db for uncategorized --- classes/counters.php | 60 +++++++++++++++++--------------------------- classes/feeds.php | 36 +++++++++++++------------- 2 files changed, 40 insertions(+), 56 deletions(-) diff --git a/classes/counters.php b/classes/counters.php index b4602825c..8a8b8bc71 100644 --- a/classes/counters.php +++ b/classes/counters.php @@ -21,21 +21,20 @@ class Counters { ); } - static private function get_cat_children($cat_id, $owner_uid) { - $pdo = Db::pdo(); - - $sth = $pdo->prepare("SELECT id FROM ttrss_feed_categories WHERE parent_cat = ? - AND owner_uid = ?"); - $sth->execute([$cat_id, $owner_uid]); - + static private function get_cat_children(int $cat_id, int $owner_uid) { $unread = 0; $marked = 0; - while ($line = $sth->fetch()) { - list ($tmp_unread, $tmp_marked) = self::get_cat_children($line["id"], $owner_uid); + $cats = ORM::for_table('ttrss_feed_categories') + ->where('owner_uid', $owner_uid) + ->where('parent_cat', $cat_id) + ->find_many(); + + foreach ($cats as $cat) { + list ($tmp_unread, $tmp_marked) = self::get_cat_children($cat->id, $owner_uid); - $unread += $tmp_unread + Feeds::_get_cat_unread($line["id"], $owner_uid); - $marked += $tmp_marked + Feeds::_get_cat_marked($line["id"], $owner_uid); + $unread += $tmp_unread + Feeds::_get_cat_unread($cat->id, $owner_uid); + $marked += $tmp_marked + Feeds::_get_cat_marked($cat->id, $owner_uid); } return [$unread, $marked]; @@ -178,7 +177,7 @@ class Counters { $has_img = false; } - // hide default un-updated timestamp i.e. 1980-01-01 (?) -fox + // hide default un-updated timestamp i.e. 1970-01-01 (?) -fox if ((int)date('Y') - (int)date('Y', strtotime($line['last_updated'])) > 2) $last_updated = ''; @@ -200,35 +199,22 @@ class Counters { return $ret; } - private static function get_global($global_unread = -1) { - $ret = []; - - if ($global_unread == -1) { - $global_unread = Feeds::_get_global_unread(); - } - - $cv = [ - "id" => "global-unread", - "counter" => (int) $global_unread + private static function get_global() { + $ret = [ + [ + "id" => "global-unread", + "counter" => (int) Feeds::_get_global_unread() + ] ]; - array_push($ret, $cv); - - $pdo = Db::pdo(); - - $sth = $pdo->prepare("SELECT COUNT(id) AS fn FROM - ttrss_feeds WHERE owner_uid = ?"); - $sth->execute([$_SESSION['uid']]); - $row = $sth->fetch(); + $subcribed_feeds = ORM::for_table('ttrss_feeds') + ->where('owner_uid', $_SESSION['uid']) + ->count(); - $subscribed_feeds = $row["fn"]; - - $cv = [ + array_push($ret, [ "id" => "subscribed-feeds", - "counter" => (int) $subscribed_feeds - ]; - - array_push($ret, $cv); + "counter" => $subcribed_feeds + ]); return $ret; } diff --git a/classes/feeds.php b/classes/feeds.php index c67edbc51..1f513d8d4 100755 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -1228,7 +1228,7 @@ class Feeds extends Handler_Protected { return $unread; } - static function _get_global_unread($user_id = false) { + static function _get_global_unread(int $user_id = 0) { if (!$user_id) $user_id = $_SESSION["uid"]; @@ -1244,25 +1244,23 @@ class Feeds extends Handler_Protected { return $row["count"]; } - static function _get_cat_title($cat_id) { - - if ($cat_id == -1) { - return __("Special"); - } else if ($cat_id == -2) { - return __("Labels"); - } else { - - $pdo = Db::pdo(); - - $sth = $pdo->prepare("SELECT title FROM ttrss_feed_categories WHERE - id = ?"); - $sth->execute([$cat_id]); - - if ($row = $sth->fetch()) { - return $row["title"]; - } else { + static function _get_cat_title(int $cat_id) { + switch ($cat_id) { + case 0: return __("Uncategorized"); - } + case -1: + return __("Special"); + case -2: + return __("Labels"); + default: + $cat = ORM::for_table('ttrss_feed_categories') + ->find_one($cat_id); + + if ($cat) { + return $cat->title; + } else { + return "UNKNOWN"; + } } }