use orm in some more places; prevent _get_cat_title from hitting the db for uncategorized

This commit is contained in:
Andrew Dolgov 2021-03-02 20:07:31 +03:00
parent 9ec0732942
commit 6f93c45c28
2 changed files with 40 additions and 56 deletions

View File

@ -21,21 +21,20 @@ class Counters {
); );
} }
static private function get_cat_children($cat_id, $owner_uid) { static private function get_cat_children(int $cat_id, int $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]);
$unread = 0; $unread = 0;
$marked = 0; $marked = 0;
while ($line = $sth->fetch()) { $cats = ORM::for_table('ttrss_feed_categories')
list ($tmp_unread, $tmp_marked) = self::get_cat_children($line["id"], $owner_uid); ->where('owner_uid', $owner_uid)
->where('parent_cat', $cat_id)
->find_many();
$unread += $tmp_unread + Feeds::_get_cat_unread($line["id"], $owner_uid); foreach ($cats as $cat) {
$marked += $tmp_marked + Feeds::_get_cat_marked($line["id"], $owner_uid); list ($tmp_unread, $tmp_marked) = self::get_cat_children($cat->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]; return [$unread, $marked];
@ -178,7 +177,7 @@ class Counters {
$has_img = false; $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) if ((int)date('Y') - (int)date('Y', strtotime($line['last_updated'])) > 2)
$last_updated = ''; $last_updated = '';
@ -200,35 +199,22 @@ class Counters {
return $ret; return $ret;
} }
private static function get_global($global_unread = -1) { private static function get_global() {
$ret = []; $ret = [
[
if ($global_unread == -1) { "id" => "global-unread",
$global_unread = Feeds::_get_global_unread(); "counter" => (int) Feeds::_get_global_unread()
} ]
$cv = [
"id" => "global-unread",
"counter" => (int) $global_unread
]; ];
array_push($ret, $cv); $subcribed_feeds = ORM::for_table('ttrss_feeds')
->where('owner_uid', $_SESSION['uid'])
->count();
$pdo = Db::pdo(); array_push($ret, [
$sth = $pdo->prepare("SELECT COUNT(id) AS fn FROM
ttrss_feeds WHERE owner_uid = ?");
$sth->execute([$_SESSION['uid']]);
$row = $sth->fetch();
$subscribed_feeds = $row["fn"];
$cv = [
"id" => "subscribed-feeds", "id" => "subscribed-feeds",
"counter" => (int) $subscribed_feeds "counter" => $subcribed_feeds
]; ]);
array_push($ret, $cv);
return $ret; return $ret;
} }

View File

@ -1228,7 +1228,7 @@ class Feeds extends Handler_Protected {
return $unread; 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"]; if (!$user_id) $user_id = $_SESSION["uid"];
@ -1244,25 +1244,23 @@ class Feeds extends Handler_Protected {
return $row["count"]; return $row["count"];
} }
static function _get_cat_title($cat_id) { static function _get_cat_title(int $cat_id) {
switch ($cat_id) {
if ($cat_id == -1) { case 0:
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 {
return __("Uncategorized"); 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";
}
} }
} }