mirror of
https://git.tt-rss.org/git/tt-rss.git
synced 2025-12-13 17:56:34 +00:00
try to calculate counters conditionally based on feed ids
This commit is contained in:
@@ -640,4 +640,20 @@ class Article extends Handler_Protected {
|
||||
return [$article_image, $article_stream, $article_kind];
|
||||
}
|
||||
|
||||
static function _feeds_of(array $article_ids) {
|
||||
$id_qmarks = arr_qmarks($article_ids);
|
||||
|
||||
$sth = DB::pdo()->prepare("SELECT DISTINCT feed_id FROM ttrss_entries e, ttrss_user_entries ue
|
||||
WHERE ue.ref_id = e.id AND id IN ($id_qmarks)");
|
||||
|
||||
$sth->execute($article_ids);
|
||||
|
||||
$rv = [];
|
||||
|
||||
while ($row = $sth->fetch()) {
|
||||
array_push($rv, $row["feed_id"]);
|
||||
}
|
||||
|
||||
return $rv;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,15 @@ class Counters {
|
||||
);
|
||||
}
|
||||
|
||||
static function get_for_feeds($feed_ids) {
|
||||
return array_merge(
|
||||
self::get_global(),
|
||||
self::get_virt(),
|
||||
self::get_labels(),
|
||||
self::get_feeds($feed_ids),
|
||||
self::get_cats(Feeds::_cats_of($feed_ids, $_SESSION["uid"], true)));
|
||||
}
|
||||
|
||||
static private function get_cat_children($cat_id, $owner_uid) {
|
||||
$pdo = Db::pdo();
|
||||
|
||||
@@ -31,7 +40,7 @@ class Counters {
|
||||
return [$unread, $marked];
|
||||
}
|
||||
|
||||
private static function get_cats() {
|
||||
private static function get_cats(array $cat_ids = []) {
|
||||
$ret = [];
|
||||
|
||||
/* Labels category */
|
||||
@@ -43,27 +52,57 @@ class Counters {
|
||||
|
||||
$pdo = Db::pdo();
|
||||
|
||||
$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)
|
||||
WHERE fc.owner_uid = :uid
|
||||
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
|
||||
ue.owner_uid = :uid");
|
||||
if (count($cat_ids) == 0) {
|
||||
$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)
|
||||
WHERE fc.owner_uid = :uid
|
||||
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
|
||||
ue.owner_uid = :uid");
|
||||
|
||||
$sth->execute(["uid" => $_SESSION['uid']]);
|
||||
$sth->execute(["uid" => $_SESSION['uid']]);
|
||||
} else {
|
||||
$cat_ids_qmarks = arr_qmarks($cat_ids);
|
||||
|
||||
$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)
|
||||
WHERE fc.owner_uid = ? AND fc.id IN ($cat_ids_qmarks)
|
||||
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
|
||||
ue.owner_uid = ?");
|
||||
|
||||
$sth->execute(array_merge(
|
||||
[$_SESSION['uid']],
|
||||
$cat_ids,
|
||||
[$_SESSION['uid']]
|
||||
));
|
||||
}
|
||||
|
||||
while ($line = $sth->fetch()) {
|
||||
if ($line["num_children"] > 0) {
|
||||
@@ -83,29 +122,42 @@ class Counters {
|
||||
array_push($ret, $cv);
|
||||
}
|
||||
|
||||
array_push($ret, $cv);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
private static function get_feeds($active_feed = false) {
|
||||
private static function get_feeds(array $feed_ids = []) {
|
||||
|
||||
$ret = [];
|
||||
|
||||
$pdo = Db::pdo();
|
||||
|
||||
$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");
|
||||
if (count($feed_ids) == 0) {
|
||||
$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']]);
|
||||
$sth->execute(["uid" => $_SESSION['uid']]);
|
||||
} else {
|
||||
$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(array_merge([$_SESSION['uid']], $feed_ids));
|
||||
}
|
||||
|
||||
while ($line = $sth->fetch()) {
|
||||
|
||||
|
||||
@@ -1765,7 +1765,7 @@ class Feeds extends Handler_Protected {
|
||||
$sth->execute([$cat, $owner_uid]);
|
||||
|
||||
while ($line = $sth->fetch()) {
|
||||
array_push($rv, $line["parent_cat"]);
|
||||
array_push($rv, (int)$line["parent_cat"]);
|
||||
$rv = array_merge($rv, self::_get_parent_cats($line["parent_cat"], $owner_uid));
|
||||
}
|
||||
|
||||
@@ -1789,6 +1789,30 @@ class Feeds extends Handler_Protected {
|
||||
return $rv;
|
||||
}
|
||||
|
||||
static function _cats_of(array $feeds, int $owner_uid, bool $with_parents = false) {
|
||||
$pdo = Db::pdo();
|
||||
|
||||
$feeds_qmarks = arr_qmarks($feeds);
|
||||
|
||||
$sth = $pdo->prepare("SELECT DISTINCT cat_id FROM ttrss_feeds
|
||||
WHERE id IN ($feeds_qmarks)");
|
||||
$sth->execute($feeds);
|
||||
|
||||
$rv = [];
|
||||
|
||||
if ($row = $sth->fetch()) {
|
||||
array_push($rv, (int)$row["cat_id"]);
|
||||
|
||||
if ($with_parents)
|
||||
$rv = array_merge($rv,
|
||||
self::_get_parent_cats($row["cat_id"], $owner_uid));
|
||||
}
|
||||
|
||||
$rv = array_unique($rv);
|
||||
|
||||
return $rv;
|
||||
}
|
||||
|
||||
static function _cat_of_feed($feed) {
|
||||
$pdo = Db::pdo();
|
||||
|
||||
|
||||
@@ -73,14 +73,21 @@ class RPC extends Handler_Protected {
|
||||
}
|
||||
|
||||
function getAllCounters() {
|
||||
$feed_ids = array_map("intval",
|
||||
explode(",",
|
||||
clean($_REQUEST["feed_ids"])));
|
||||
|
||||
@$seq = (int) $_REQUEST['seq'];
|
||||
|
||||
// @phpstan-ignore-next-line
|
||||
$counters = count($feed_ids) > 0 ? Counters::get_for_feeds($feed_ids) : Counters::get_all();
|
||||
|
||||
$reply = [
|
||||
'counters' => Counters::get_all(),
|
||||
'counters' => $counters,
|
||||
'seq' => $seq
|
||||
];
|
||||
|
||||
if ($seq % 2 == 0)
|
||||
if ($seq % 2)
|
||||
$reply['runtime-info'] = $this->make_runtime_info();
|
||||
|
||||
print json_encode($reply);
|
||||
@@ -88,30 +95,39 @@ class RPC extends Handler_Protected {
|
||||
|
||||
/* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
|
||||
function catchupSelected() {
|
||||
$ids = explode(",", clean($_REQUEST["ids"]));
|
||||
$ids = array_map("intval",
|
||||
explode(",",
|
||||
clean($_REQUEST["ids"])));
|
||||
|
||||
$cmode = (int)clean($_REQUEST["cmode"]);
|
||||
|
||||
Article::_catchup_by_id($ids, $cmode);
|
||||
|
||||
print json_encode(array("message" => "UPDATE_COUNTERS", "ids" => $ids));
|
||||
print json_encode(["message" => "UPDATE_COUNTERS", "feeds" => Article::_feeds_of($ids)]);
|
||||
}
|
||||
|
||||
function markSelected() {
|
||||
$ids = explode(",", clean($_REQUEST["ids"]));
|
||||
$ids = array_map("intval",
|
||||
explode(",",
|
||||
clean($_REQUEST["ids"])));
|
||||
|
||||
$cmode = (int)clean($_REQUEST["cmode"]);
|
||||
|
||||
$this->markArticlesById($ids, $cmode);
|
||||
|
||||
print json_encode(array("message" => "UPDATE_COUNTERS"));
|
||||
print json_encode(["message" => "UPDATE_COUNTERS", "feeds" => Article::_feeds_of($ids)]);
|
||||
}
|
||||
|
||||
function publishSelected() {
|
||||
$ids = explode(",", clean($_REQUEST["ids"]));
|
||||
$ids = array_map("intval",
|
||||
explode(",",
|
||||
clean($_REQUEST["ids"])));
|
||||
|
||||
$cmode = (int)clean($_REQUEST["cmode"]);
|
||||
|
||||
$this->publishArticlesById($ids, $cmode);
|
||||
|
||||
print json_encode(array("message" => "UPDATE_COUNTERS"));
|
||||
print json_encode(["message" => "UPDATE_COUNTERS", "feeds" => Article::_feeds_of($ids)]);
|
||||
}
|
||||
|
||||
function sanityCheck() {
|
||||
|
||||
Reference in New Issue
Block a user