2017-05-05 08:54:31 +00:00
|
|
|
<?php
|
|
|
|
class Counters {
|
|
|
|
|
2021-11-11 12:26:30 +00:00
|
|
|
/**
|
|
|
|
* @return array<int, array<string, int|string>>
|
|
|
|
*/
|
|
|
|
static function get_all(): array {
|
2021-02-15 14:01:05 +00:00
|
|
|
return array_merge(
|
|
|
|
self::get_global(),
|
|
|
|
self::get_virt(),
|
2021-02-15 13:00:54 +00:00
|
|
|
self::get_labels(),
|
|
|
|
self::get_feeds(),
|
2021-02-15 14:01:05 +00:00
|
|
|
self::get_cats()
|
|
|
|
);
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 12:26:30 +00:00
|
|
|
/**
|
|
|
|
* @param array<int> $feed_ids
|
|
|
|
* @param array<int> $label_ids
|
|
|
|
* @return array<int, array<string, int|string>>
|
|
|
|
*/
|
|
|
|
static function get_conditional(array $feed_ids = null, array $label_ids = null): array {
|
2021-02-24 06:47:26 +00:00
|
|
|
return array_merge(
|
|
|
|
self::get_global(),
|
|
|
|
self::get_virt(),
|
2021-02-24 12:07:31 +00:00
|
|
|
self::get_labels($label_ids),
|
2021-02-24 06:47:26 +00:00
|
|
|
self::get_feeds($feed_ids),
|
2021-02-24 12:07:31 +00:00
|
|
|
self::get_cats(is_array($feed_ids) ? Feeds::_cats_of($feed_ids, $_SESSION["uid"], true) : null)
|
|
|
|
);
|
2021-02-24 06:47:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 12:26:30 +00:00
|
|
|
/**
|
|
|
|
* @return array<int, int>
|
|
|
|
*/
|
|
|
|
static private function get_cat_children(int $cat_id, int $owner_uid): array {
|
2020-02-20 12:54:40 +00:00
|
|
|
$unread = 0;
|
|
|
|
$marked = 0;
|
|
|
|
|
2021-03-02 17:07:31 +00:00
|
|
|
$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);
|
2020-02-20 12:54:40 +00:00
|
|
|
|
2021-03-02 17:07:31 +00:00
|
|
|
$unread += $tmp_unread + Feeds::_get_cat_unread($cat->id, $owner_uid);
|
|
|
|
$marked += $tmp_marked + Feeds::_get_cat_marked($cat->id, $owner_uid);
|
2020-02-20 12:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return [$unread, $marked];
|
|
|
|
}
|
|
|
|
|
2021-11-11 12:26:30 +00:00
|
|
|
/**
|
|
|
|
* @param array<int> $cat_ids
|
|
|
|
* @return array<int, array<string, int|string>>
|
|
|
|
*/
|
|
|
|
private static function get_cats(array $cat_ids = null): array {
|
2020-01-24 11:25:31 +00:00
|
|
|
$ret = [];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
|
|
|
/* Labels category */
|
|
|
|
|
|
|
|
$cv = array("id" => -2, "kind" => "cat",
|
2021-02-15 12:43:07 +00:00
|
|
|
"counter" => Feeds::_get_cat_unread(-2));
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
array_push($ret, $cv);
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-09-17 06:18:03 +00:00
|
|
|
$pdo = Db::pdo();
|
2017-12-01 14:47:29 +00:00
|
|
|
|
2021-02-24 12:07:31 +00:00
|
|
|
if (is_array($cat_ids)) {
|
|
|
|
if (count($cat_ids) == 0)
|
|
|
|
return [];
|
|
|
|
|
|
|
|
$cat_ids_qmarks = arr_qmarks($cat_ids);
|
|
|
|
|
2021-02-24 06:47:26 +00:00
|
|
|
$sth = $pdo->prepare("SELECT fc.id,
|
|
|
|
SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count,
|
|
|
|
SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked,
|
|
|
|
(SELECT COUNT(id) FROM ttrss_feed_categories fcc
|
|
|
|
WHERE fcc.parent_cat = fc.id) AS num_children
|
|
|
|
FROM ttrss_feed_categories fc
|
|
|
|
LEFT JOIN ttrss_feeds f ON (f.cat_id = fc.id)
|
|
|
|
LEFT JOIN ttrss_user_entries ue ON (ue.feed_id = f.id)
|
2021-02-24 12:07:31 +00:00
|
|
|
WHERE fc.owner_uid = ? AND fc.id IN ($cat_ids_qmarks)
|
2021-02-24 06:47:26 +00:00
|
|
|
GROUP BY fc.id
|
|
|
|
UNION
|
|
|
|
SELECT 0,
|
|
|
|
SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count,
|
|
|
|
SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked,
|
|
|
|
0
|
|
|
|
FROM ttrss_feeds f, ttrss_user_entries ue
|
|
|
|
WHERE f.cat_id IS NULL AND
|
|
|
|
ue.feed_id = f.id AND
|
2021-02-24 12:07:31 +00:00
|
|
|
ue.owner_uid = ?");
|
2021-02-24 06:47:26 +00:00
|
|
|
|
2021-02-24 12:07:31 +00:00
|
|
|
$sth->execute(array_merge(
|
|
|
|
[$_SESSION['uid']],
|
|
|
|
$cat_ids,
|
|
|
|
[$_SESSION['uid']]
|
|
|
|
));
|
2021-02-24 06:47:26 +00:00
|
|
|
|
2021-02-24 12:07:31 +00:00
|
|
|
} else {
|
2021-02-24 06:47:26 +00:00
|
|
|
$sth = $pdo->prepare("SELECT fc.id,
|
|
|
|
SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count,
|
|
|
|
SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked,
|
|
|
|
(SELECT COUNT(id) FROM ttrss_feed_categories fcc
|
|
|
|
WHERE fcc.parent_cat = fc.id) AS num_children
|
|
|
|
FROM ttrss_feed_categories fc
|
|
|
|
LEFT JOIN ttrss_feeds f ON (f.cat_id = fc.id)
|
|
|
|
LEFT JOIN ttrss_user_entries ue ON (ue.feed_id = f.id)
|
2021-02-24 12:07:31 +00:00
|
|
|
WHERE fc.owner_uid = :uid
|
2021-02-24 06:47:26 +00:00
|
|
|
GROUP BY fc.id
|
|
|
|
UNION
|
|
|
|
SELECT 0,
|
|
|
|
SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count,
|
|
|
|
SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked,
|
|
|
|
0
|
|
|
|
FROM ttrss_feeds f, ttrss_user_entries ue
|
|
|
|
WHERE f.cat_id IS NULL AND
|
|
|
|
ue.feed_id = f.id AND
|
2021-02-24 12:07:31 +00:00
|
|
|
ue.owner_uid = :uid");
|
2021-02-24 06:47:26 +00:00
|
|
|
|
2021-02-24 12:07:31 +00:00
|
|
|
$sth->execute(["uid" => $_SESSION['uid']]);
|
2021-02-24 06:47:26 +00:00
|
|
|
}
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2017-12-01 14:47:29 +00:00
|
|
|
while ($line = $sth->fetch()) {
|
2017-05-05 08:54:31 +00:00
|
|
|
if ($line["num_children"] > 0) {
|
2021-02-15 13:14:48 +00:00
|
|
|
list ($child_counter, $child_marked_counter) = self::get_cat_children($line["id"], $_SESSION["uid"]);
|
2017-05-05 08:54:31 +00:00
|
|
|
} else {
|
|
|
|
$child_counter = 0;
|
2020-02-20 12:54:40 +00:00
|
|
|
$child_marked_counter = 0;
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
$cv = [
|
|
|
|
"id" => (int)$line["id"],
|
|
|
|
"kind" => "cat",
|
2020-02-20 12:54:40 +00:00
|
|
|
"markedcounter" => (int) $line["count_marked"] + $child_marked_counter,
|
2020-01-24 11:25:31 +00:00
|
|
|
"counter" => (int) $line["count"] + $child_counter
|
|
|
|
];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
array_push($ret, $cv);
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2021-11-11 12:26:30 +00:00
|
|
|
/**
|
|
|
|
* @param array<int> $feed_ids
|
|
|
|
* @return array<int, array<string, int|string>>
|
|
|
|
*/
|
|
|
|
private static function get_feeds(array $feed_ids = null): array {
|
2020-01-24 11:25:31 +00:00
|
|
|
|
|
|
|
$ret = [];
|
|
|
|
|
|
|
|
$pdo = Db::pdo();
|
|
|
|
|
2021-02-24 12:07:31 +00:00
|
|
|
if (is_array($feed_ids)) {
|
|
|
|
if (count($feed_ids) == 0)
|
|
|
|
return [];
|
|
|
|
|
|
|
|
$feed_ids_qmarks = arr_qmarks($feed_ids);
|
|
|
|
|
2021-02-24 06:47:26 +00:00
|
|
|
$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
|
2021-02-24 12:07:31 +00:00
|
|
|
WHERE f.id = ue.feed_id AND ue.owner_uid = ? AND f.id IN ($feed_ids_qmarks)
|
2021-02-24 06:47:26 +00:00
|
|
|
GROUP BY f.id");
|
|
|
|
|
2021-02-24 12:07:31 +00:00
|
|
|
$sth->execute(array_merge([$_SESSION['uid']], $feed_ids));
|
2021-02-24 06:47:26 +00:00
|
|
|
} 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
|
2021-02-24 12:07:31 +00:00
|
|
|
WHERE f.id = ue.feed_id AND ue.owner_uid = :uid
|
2021-02-24 06:47:26 +00:00
|
|
|
GROUP BY f.id");
|
|
|
|
|
2021-02-24 12:07:31 +00:00
|
|
|
$sth->execute(["uid" => $_SESSION['uid']]);
|
2021-02-24 06:47:26 +00:00
|
|
|
}
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
while ($line = $sth->fetch()) {
|
|
|
|
|
|
|
|
$id = $line["id"];
|
2020-09-23 10:04:26 +00:00
|
|
|
$last_updated = TimeHelper::make_local_datetime($line['last_updated'], false);
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2021-02-15 12:43:07 +00:00
|
|
|
if (Feeds::_has_icon($id)) {
|
|
|
|
$has_img = filemtime(Feeds::_get_icon_file($id));
|
2020-01-24 11:25:31 +00:00
|
|
|
} else {
|
|
|
|
$has_img = false;
|
|
|
|
}
|
|
|
|
|
2021-03-02 17:07:31 +00:00
|
|
|
// hide default un-updated timestamp i.e. 1970-01-01 (?) -fox
|
2021-02-06 14:56:47 +00:00
|
|
|
if ((int)date('Y') - (int)date('Y', strtotime($line['last_updated'])) > 2)
|
2020-01-24 11:25:31 +00:00
|
|
|
$last_updated = '';
|
|
|
|
|
|
|
|
$cv = [
|
|
|
|
"id" => $id,
|
|
|
|
"updated" => $last_updated,
|
|
|
|
"counter" => (int) $line["count"],
|
|
|
|
"markedcounter" => (int) $line["count_marked"],
|
|
|
|
"has_img" => (int) $has_img
|
|
|
|
];
|
|
|
|
|
2021-02-24 10:25:26 +00:00
|
|
|
$cv["error"] = $line["last_error"];
|
|
|
|
$cv["title"] = truncate_string($line["title"], 30);
|
2020-01-24 11:25:31 +00:00
|
|
|
|
|
|
|
array_push($ret, $cv);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 12:26:30 +00:00
|
|
|
/**
|
|
|
|
* @return array<int, array<string, int|string>>
|
|
|
|
*/
|
|
|
|
private static function get_global(): array {
|
2021-03-02 17:07:31 +00:00
|
|
|
$ret = [
|
|
|
|
[
|
|
|
|
"id" => "global-unread",
|
|
|
|
"counter" => (int) Feeds::_get_global_unread()
|
|
|
|
]
|
2020-01-24 11:25:31 +00:00
|
|
|
];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2021-03-02 17:07:31 +00:00
|
|
|
$subcribed_feeds = ORM::for_table('ttrss_feeds')
|
|
|
|
->where('owner_uid', $_SESSION['uid'])
|
|
|
|
->count();
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2021-03-02 17:07:31 +00:00
|
|
|
array_push($ret, [
|
2020-01-24 11:25:31 +00:00
|
|
|
"id" => "subscribed-feeds",
|
2021-03-02 17:07:31 +00:00
|
|
|
"counter" => $subcribed_feeds
|
|
|
|
]);
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
return $ret;
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 12:26:30 +00:00
|
|
|
/**
|
|
|
|
* @return array<int, array<string, int|string>>
|
|
|
|
*/
|
|
|
|
private static function get_virt(): array {
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
$ret = [];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
|
|
|
for ($i = 0; $i >= -4; $i--) {
|
|
|
|
|
2022-02-20 08:04:15 +00:00
|
|
|
$count = Feeds::_get_counters($i);
|
2017-05-05 08:54:31 +00:00
|
|
|
|
|
|
|
if ($i == 0 || $i == -1 || $i == -2)
|
2021-02-15 12:43:07 +00:00
|
|
|
$auxctr = Feeds::_get_counters($i, false);
|
2017-05-05 08:54:31 +00:00
|
|
|
else
|
|
|
|
$auxctr = 0;
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
$cv = [
|
|
|
|
"id" => $i,
|
2017-05-05 08:54:31 +00:00
|
|
|
"counter" => (int) $count,
|
2020-01-24 11:25:31 +00:00
|
|
|
"auxcounter" => (int) $auxctr
|
|
|
|
];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-02-20 11:14:45 +00:00
|
|
|
if ($i == -1)
|
|
|
|
$cv["markedcounter"] = $auxctr;
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
array_push($ret, $cv);
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$feeds = PluginHost::getInstance()->get_feeds(-1);
|
|
|
|
|
|
|
|
if (is_array($feeds)) {
|
|
|
|
foreach ($feeds as $feed) {
|
2021-11-15 05:19:44 +00:00
|
|
|
/** @var IVirtualFeed $feed['sender'] */
|
|
|
|
|
2021-11-15 04:11:29 +00:00
|
|
|
if (!implements_interface($feed['sender'], 'IVirtualFeed'))
|
2021-11-12 05:04:55 +00:00
|
|
|
continue;
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
$cv = [
|
|
|
|
"id" => PluginHost::pfeed_to_feed_id($feed['id']),
|
|
|
|
"counter" => $feed['sender']->get_unread($feed['id'])
|
|
|
|
];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
|
|
|
if (method_exists($feed['sender'], 'get_total'))
|
|
|
|
$cv["auxcounter"] = $feed['sender']->get_total($feed['id']);
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
array_push($ret, $cv);
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
return $ret;
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 12:26:30 +00:00
|
|
|
/**
|
|
|
|
* @param array<int> $label_ids
|
|
|
|
* @return array<int, array<string, int|string>>
|
|
|
|
*/
|
|
|
|
static function get_labels(array $label_ids = null): array {
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
$ret = [];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2017-12-01 14:47:29 +00:00
|
|
|
$pdo = Db::pdo();
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2021-02-24 12:07:31 +00:00
|
|
|
if (is_array($label_ids)) {
|
|
|
|
if (count($label_ids) == 0)
|
|
|
|
return [];
|
|
|
|
|
|
|
|
$label_ids_qmarks = arr_qmarks($label_ids);
|
|
|
|
|
|
|
|
$sth = $pdo->prepare("SELECT id,
|
|
|
|
caption,
|
|
|
|
SUM(CASE WHEN u1.unread = true THEN 1 ELSE 0 END) AS count_unread,
|
|
|
|
SUM(CASE WHEN u1.marked = true THEN 1 ELSE 0 END) AS count_marked,
|
|
|
|
COUNT(u1.unread) AS total
|
|
|
|
FROM ttrss_labels2 LEFT JOIN ttrss_user_labels2 ON
|
|
|
|
(ttrss_labels2.id = label_id)
|
|
|
|
LEFT JOIN ttrss_user_entries AS u1 ON u1.ref_id = article_id AND u1.owner_uid = ?
|
|
|
|
WHERE ttrss_labels2.owner_uid = ? AND ttrss_labels2.id IN ($label_ids_qmarks)
|
|
|
|
GROUP BY ttrss_labels2.id, ttrss_labels2.caption");
|
|
|
|
$sth->execute(array_merge([$_SESSION["uid"], $_SESSION["uid"]], $label_ids));
|
|
|
|
} else {
|
|
|
|
$sth = $pdo->prepare("SELECT id,
|
|
|
|
caption,
|
|
|
|
SUM(CASE WHEN u1.unread = true THEN 1 ELSE 0 END) AS count_unread,
|
|
|
|
SUM(CASE WHEN u1.marked = true THEN 1 ELSE 0 END) AS count_marked,
|
|
|
|
COUNT(u1.unread) AS total
|
|
|
|
FROM ttrss_labels2 LEFT JOIN ttrss_user_labels2 ON
|
|
|
|
(ttrss_labels2.id = label_id)
|
|
|
|
LEFT JOIN ttrss_user_entries AS u1 ON u1.ref_id = article_id AND u1.owner_uid = :uid
|
|
|
|
WHERE ttrss_labels2.owner_uid = :uid
|
|
|
|
GROUP BY ttrss_labels2.id, ttrss_labels2.caption");
|
|
|
|
$sth->execute([":uid" => $_SESSION['uid']]);
|
|
|
|
}
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2017-12-01 14:47:29 +00:00
|
|
|
while ($line = $sth->fetch()) {
|
2017-05-05 08:54:31 +00:00
|
|
|
|
|
|
|
$id = Labels::label_to_feed_id($line["id"]);
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
$cv = [
|
|
|
|
"id" => $id,
|
2020-02-20 11:14:45 +00:00
|
|
|
"counter" => (int) $line["count_unread"],
|
|
|
|
"auxcounter" => (int) $line["total"],
|
2021-02-24 12:07:31 +00:00
|
|
|
"markedcounter" => (int) $line["count_marked"],
|
|
|
|
"description" => $line["caption"]
|
2020-01-24 11:25:31 +00:00
|
|
|
];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
array_push($ret, $cv);
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
return $ret;
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
2020-01-24 11:25:31 +00:00
|
|
|
}
|