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) {
$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();
$unread += $tmp_unread + Feeds::_get_cat_unread($line["id"], $owner_uid);
$marked += $tmp_marked + Feeds::_get_cat_marked($line["id"], $owner_uid);
foreach ($cats as $cat) {
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];
@ -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);
$subcribed_feeds = ORM::for_table('ttrss_feeds')
->where('owner_uid', $_SESSION['uid'])
->count();
$pdo = Db::pdo();
$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 = [
array_push($ret, [
"id" => "subscribed-feeds",
"counter" => (int) $subscribed_feeds
];
array_push($ret, $cv);
"counter" => $subcribed_feeds
]);
return $ret;
}

View File

@ -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";
}
}
}