aboutsummaryrefslogtreecommitdiff
path: root/app/Models
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2024-07-21 14:54:34 +0200
committerGravatar GitHub <noreply@github.com> 2024-07-21 14:54:34 +0200
commit0eeac4a669e46fb6521ae1765b23656c9afc43de (patch)
tree7c5a08b80b628f855b11c8850aa993c0b509feb1 /app/Models
parent47b5e40e7240b5ccb0d0b6c54e0a958243689c83 (diff)
Revisit keepMaxUnreads (#6632)
* Revisit keepMaxUnreads Again, follow-up of https://github.com/FreshRSS/FreshRSS/pull/5905 fix https://github.com/FreshRSS/FreshRSS/issues/6620 * Refactoring to address buggy cases * Fix minor test
Diffstat (limited to 'app/Models')
-rw-r--r--app/Models/EntryDAO.php43
-rw-r--r--app/Models/Feed.php7
-rw-r--r--app/Models/FeedDAO.php23
3 files changed, 20 insertions, 53 deletions
diff --git a/app/Models/EntryDAO.php b/app/Models/EntryDAO.php
index 0b289eb41..ec627dda1 100644
--- a/app/Models/EntryDAO.php
+++ b/app/Models/EntryDAO.php
@@ -274,45 +274,14 @@ SQL;
}
/**
- * Count the number of new entries in the temporary table (which have not yet been committed), grouped by read / unread.
- * @return array{'all':int,'unread':int,'read':int}
+ * Count the number of new entries in the temporary table (which have not yet been committed).
*/
- public function countNewEntries(): array {
+ public function countNewEntries(): int {
$sql = <<<'SQL'
- SELECT is_read, COUNT(id) AS nb_entries FROM `_entrytmp`
- GROUP BY is_read
+ SELECT COUNT(id) AS nb_entries FROM `_entrytmp`
SQL;
- $lines = $this->fetchAssoc($sql) ?? [];
- $nbRead = 0;
- $nbUnread = 0;
- foreach ($lines as $line) {
- if (empty($line['is_read'])) {
- $nbUnread = (int)($line['nb_entries'] ?? 0);
- } else {
- $nbRead = (int)($line['nb_entries'] ?? 0);
- }
- }
- return ['all' => $nbRead + $nbUnread, 'unread' => $nbUnread, 'read' => $nbRead];
- }
-
- /**
- * Count the number of new unread entries in the temporary table (which have not yet been committed), grouped by feed ID.
- * @return array<int,int>
- */
- public function newUnreadEntriesPerFeed(): array {
- $sql = <<<'SQL'
- SELECT id_feed, COUNT(id) AS nb_entries FROM `_entrytmp`
- WHERE is_read = 0
- GROUP BY id_feed
- SQL;
- $lines = $this->fetchAssoc($sql) ?? [];
- $result = [];
- foreach ($lines as $line) {
- if (!empty($line['id_feed'])) {
- $result[(int)$line['id_feed']] = (int)($line['nb_entries'] ?? 0);
- }
- }
- return $result;
+ $res = $this->fetchColumn($sql, 0);
+ return isset($res[0]) ? (int)$res[0] : -1;
}
/**
@@ -651,7 +620,7 @@ SQL;
}
/**
- * Remember to call updateCachedValue($id_feed) or updateCachedValues() just after.
+ * Remember to call updateCachedValues($id_feed) or updateCachedValues() just after.
* @param array<string,bool|int|string> $options
* @return int|false
*/
diff --git a/app/Models/Feed.php b/app/Models/Feed.php
index 0274ded0a..e9d031a82 100644
--- a/app/Models/Feed.php
+++ b/app/Models/Feed.php
@@ -835,15 +835,12 @@ class FreshRSS_Feed extends Minz_Model {
}
$feedDAO = FreshRSS_Factory::createFeedDao();
$affected = $feedDAO->markAsReadMaxUnread($this->id(), $keepMaxUnread);
- if ($affected > 0) {
- Minz_Log::debug(__METHOD__ . " $affected items [" . $this->url(false) . ']');
- }
return $affected;
}
/**
* Applies the *mark as read upon gone* policy, if enabled.
- * Remember to call `updateCachedValue($id_feed)` or `updateCachedValues()` just after.
+ * Remember to call `updateCachedValues($id_feed)` or `updateCachedValues()` just after.
* @return int|false the number of lines affected, or false if not applicable
*/
public function markAsReadUponGone(bool $upstreamIsEmpty, int $maxTimestamp = 0) {
@@ -871,7 +868,7 @@ class FreshRSS_Feed extends Minz_Model {
}
/**
- * Remember to call `updateCachedValue($id_feed)` or `updateCachedValues()` just after
+ * Remember to call `updateCachedValues($id_feed)` or `updateCachedValues()` just after
* @return int|false
*/
public function cleanOldEntries() {
diff --git a/app/Models/FeedDAO.php b/app/Models/FeedDAO.php
index c0b2e9e7a..4a75e3dea 100644
--- a/app/Models/FeedDAO.php
+++ b/app/Models/FeedDAO.php
@@ -203,7 +203,7 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
/**
* @return int|false
- * @see updateCachedValue()
+ * @see updateCachedValues()
*/
public function updateLastUpdate(int $id, bool $inError = false, int $mtime = 0) {
$sql = 'UPDATE `_feed` SET `lastUpdate`=?, error=? WHERE id=?';
@@ -453,20 +453,21 @@ SQL;
}
/**
+ * Update cached values for selected feeds, or all feeds if no feed ID is provided.
* @return int|false
*/
- public function updateCachedValues(int $id = 0) {
+ public function updateCachedValues(int ...$feedIds) {
//2 sub-requests with FOREIGN KEY(e.id_feed), INDEX(e.is_read) faster than 1 request with GROUP BY or CASE
- $sql = 'UPDATE `_feed` '
- . 'SET `cache_nbEntries`=(SELECT COUNT(e1.id) FROM `_entry` e1 WHERE e1.id_feed=`_feed`.id),'
- . '`cache_nbUnreads`=(SELECT COUNT(e2.id) FROM `_entry` e2 WHERE e2.id_feed=`_feed`.id AND e2.is_read=0)'
- . ($id != 0 ? ' WHERE id=:id' : '');
- $stm = $this->pdo->prepare($sql);
- if ($stm !== false && $id != 0) {
- $stm->bindParam(':id', $id, PDO::PARAM_INT);
+ $sql = <<<SQL
+UPDATE `_feed`
+SET `cache_nbEntries`=(SELECT COUNT(e1.id) FROM `_entry` e1 WHERE e1.id_feed=`_feed`.id),
+ `cache_nbUnreads`=(SELECT COUNT(e2.id) FROM `_entry` e2 WHERE e2.id_feed=`_feed`.id AND e2.is_read=0)
+SQL;
+ if (count($feedIds) > 0) {
+ $sql .= ' WHERE id IN (' . str_repeat('?,', count($feedIds) - 1). '?)';
}
-
- if ($stm !== false && $stm->execute()) {
+ $stm = $this->pdo->prepare($sql);
+ if ($stm !== false && $stm->execute($feedIds)) {
return $stm->rowCount();
} else {
$info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();