summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwn_ <invalid@email.com>2025-05-18 14:51:47 +0000
committerwn_ <invalid@email.com>2025-05-18 14:51:47 +0000
commitcdd48bb1fa67092b2f610ed2b609a9debe97e839 (patch)
treec99f532dad43f32f3ed78433d494099211e71103
parent2fa54cc627cd9a370004a48e1b6430a8a9990846 (diff)
Use ORM in 'Counters::get_feeds()' (and simplify stuff).
-rw-r--r--classes/Counters.php83
1 files changed, 27 insertions, 56 deletions
diff --git a/classes/Counters.php b/classes/Counters.php
index 4f23a7a41..613210ecc 100644
--- a/classes/Counters.php
+++ b/classes/Counters.php
@@ -147,63 +147,34 @@ class Counters {
private static function get_feeds(?array $feed_ids = null): array {
$ret = [];
- $pdo = Db::pdo();
-
- if (is_array($feed_ids)) {
- if (count($feed_ids) == 0)
- return [];
-
- $feed_ids_qmarks = arr_qmarks($feed_ids);
-
- $sth = $pdo->prepare("SELECT f.id,
- f.title,
- SUBSTRING_FOR_DATE(f.last_updated,1,19) AS last_updated,
- f.last_error,
- SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count,
- SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked
- FROM ttrss_feeds f, ttrss_user_entries ue
- WHERE f.id = ue.feed_id AND ue.owner_uid = ? AND f.id IN ($feed_ids_qmarks)
- GROUP BY f.id");
-
- $sth->execute([$_SESSION['uid'], ...$feed_ids]);
- } else {
- $sth = $pdo->prepare("SELECT f.id,
- f.title,
- SUBSTRING_FOR_DATE(f.last_updated,1,19) AS last_updated,
- f.last_error,
- SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count,
- SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked
- FROM ttrss_feeds f, ttrss_user_entries ue
- WHERE f.id = ue.feed_id AND ue.owner_uid = :uid
- GROUP BY f.id");
-
- $sth->execute(["uid" => $_SESSION['uid']]);
- }
-
- while ($line = $sth->fetch()) {
-
- $id = $line["id"];
- $last_updated = TimeHelper::make_local_datetime($line['last_updated']);
-
- if (Feeds::_has_icon($id)) {
- $ts = filemtime(Feeds::_get_icon_file($id));
- } else {
- $ts = 0;
- }
-
- $cv = [
- "id" => $id,
- "updated" => $last_updated,
- "counter" => (int) $line["count"],
- "markedcounter" => (int) $line["count_marked"],
- "ts" => (int) $ts
+ if (is_array($feed_ids) && count($feed_ids) === 0)
+ return $ret;
+
+ $feeds = ORM::for_table('ttrss_feeds')
+ ->table_alias('f')
+ ->select_many('f.id', 'f.title', 'f.last_error')
+ ->select_many_expr([
+ 'count' => 'SUM(CASE WHEN ue.unread THEN 1 ELSE 0 END)',
+ 'count_marked' => 'SUM(CASE WHEN ue.marked THEN 1 ELSE 0 END)',
+ 'last_updated' => 'SUBSTRING_FOR_DATE(f.last_updated,1,19)',
+ ])
+ ->join('ttrss_user_entries', [ 'ue.feed_id', '=', 'f.id'], 'ue')
+ ->where('ue.owner_uid', $_SESSION['uid'])
+ ->group_by('f.id');
+
+ if (is_array($feed_ids))
+ $feeds->where_in('f.id', $feed_ids);
+
+ foreach ($feeds->find_many() as $feed) {
+ $ret[] = [
+ 'id' => $feed->id,
+ 'title' => truncate_string($feed->title, 30),
+ 'error' => $feed->last_error,
+ 'updated' => TimeHelper::make_local_datetime($feed->last_updated),
+ 'counter' => (int) $feed->count,
+ 'markedcounter' => (int) $feed->count_marked,
+ 'ts' => Feeds::_has_icon($feed->id) ? (int) filemtime(Feeds::_get_icon_file($feed->id)) : 0,
];
-
- $cv["error"] = $line["last_error"];
- $cv["title"] = truncate_string($line["title"], 30);
-
- array_push($ret, $cv);
-
}
return $ret;