aboutsummaryrefslogtreecommitdiff
path: root/app/Models
diff options
context:
space:
mode:
Diffstat (limited to 'app/Models')
-rw-r--r--app/Models/DatabaseDAO.php9
-rw-r--r--app/Models/EntryDAO.php93
-rw-r--r--app/Models/View.php4
3 files changed, 39 insertions, 67 deletions
diff --git a/app/Models/DatabaseDAO.php b/app/Models/DatabaseDAO.php
index b34c0fc66..1a6a824e5 100644
--- a/app/Models/DatabaseDAO.php
+++ b/app/Models/DatabaseDAO.php
@@ -347,13 +347,8 @@ SQL;
//SQLite is the only one with database-level optimization, instead of at table level.
$this->optimize();
}
- } else {
- if ($databaseDAO->exits()) {
- $nbEntries = $entryDAO->countUnreadRead();
- if (isset($nbEntries['all']) && $nbEntries['all'] > 0) {
- $error = 'Error: Destination database already contains some entries!';
- }
- }
+ } elseif ($databaseDAO->exits() && $entryDAO->count() > 0) {
+ $error = 'Error: Destination database already contains some entries!';
}
break;
default:
diff --git a/app/Models/EntryDAO.php b/app/Models/EntryDAO.php
index b4f7451c7..eb800ff1e 100644
--- a/app/Models/EntryDAO.php
+++ b/app/Models/EntryDAO.php
@@ -1733,29 +1733,35 @@ SQL;
}
}
- /** @return array<string,int> */
- public function countUnreadRead(): array {
+ /** @return array{all:int,unread:int,read:int,favorites:int} */
+ public function countAsStates(?int $minPriority = null): array {
+ $values = [];
$sql = <<<'SQL'
-SELECT COUNT(e.id) AS count FROM `_entry` e
- INNER JOIN `_feed` f ON e.id_feed=f.id
- WHERE f.priority > 0
-UNION
-SELECT COUNT(e.id) AS count FROM `_entry` e
- INNER JOIN `_feed` f ON e.id_feed=f.id
- WHERE f.priority > 0 AND e.is_read=0
-SQL;
- $res = $this->fetchColumn($sql, 0);
- if ($res === null) {
- return ['all' => -1, 'unread' => -1, 'read' => -1];
+ SELECT
+ COUNT(*) AS total,
+ COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS unread,
+ COUNT(CASE WHEN e.is_favorite = 1 THEN 1 END) AS favorites
+ FROM `_entry` e
+ SQL;
+ if ($minPriority !== null) {
+ $sql .= <<<'SQL'
+ INNER JOIN `_feed` f ON e.id_feed = f.id
+ WHERE f.priority > :priority
+ SQL;
+ $values[':priority'] = $minPriority;
}
- rsort($res);
- $all = (int)($res[0] ?? 0);
- $unread = (int)($res[1] ?? 0);
- return ['all' => $all, 'unread' => $unread, 'read' => $all - $unread];
+ $res = $this->fetchAssoc($sql, $values);
+ if ($res === null || !isset($res[0])) {
+ return ['all' => -1, 'unread' => -1, 'read' => -1, 'favorites' => -1];
+ }
+ $all = (int)($res[0]['total'] ?? 0);
+ $unread = (int)($res[0]['unread'] ?? 0);
+ $favorites = (int)($res[0]['favorites'] ?? 0);
+ return ['all' => $all, 'unread' => $unread, 'read' => $all - $unread, 'favorites' => $favorites];
}
public function count(?int $minPriority = null): int {
- $sql = 'SELECT COUNT(e.id) AS count FROM `_entry` e';
+ $sql = 'SELECT COUNT(*) AS count FROM `_entry` e';
$values = [];
if ($minPriority !== null) {
$sql .= ' INNER JOIN `_feed` f ON e.id_feed=f.id';
@@ -1766,51 +1772,22 @@ SQL;
return isset($res[0]) ? (int)($res[0]) : -1;
}
- public function countNotRead(?int $minPriority = null): int {
- $sql = 'SELECT COUNT(e.id) AS count FROM `_entry` e';
- if ($minPriority !== null) {
- $sql .= ' INNER JOIN `_feed` f ON e.id_feed=f.id';
- }
- $sql .= ' WHERE e.is_read=0';
- $values = [];
- if ($minPriority !== null) {
- $sql .= ' AND f.priority > :priority';
- $values[':priority'] = $minPriority;
- }
- $res = $this->fetchColumn($sql, 0, $values);
- return isset($res[0]) ? (int)($res[0]) : -1;
- }
-
/** @return array{'all':int,'read':int,'unread':int} */
public function countUnreadReadFavorites(): array {
$sql = <<<'SQL'
-SELECT c FROM (
- SELECT COUNT(e1.id) AS c, 1 AS o
- FROM `_entry` AS e1
- JOIN `_feed` AS f1 ON e1.id_feed = f1.id
- WHERE e1.is_favorite = 1
- AND f1.priority >= :priority1
- UNION
- SELECT COUNT(e2.id) AS c, 2 AS o
- FROM `_entry` AS e2
- JOIN `_feed` AS f2 ON e2.id_feed = f2.id
- WHERE e2.is_favorite = 1
- AND e2.is_read = 0 AND f2.priority >= :priority2
- ) u
-ORDER BY o
-SQL;
- //Binding a value more than once is not standard and does not work with native prepared statements (e.g. MySQL) https://bugs.php.net/bug.php?id=40417
- $res = $this->fetchColumn($sql, 0, [
- ':priority1' => FreshRSS_Feed::PRIORITY_CATEGORY,
- ':priority2' => FreshRSS_Feed::PRIORITY_CATEGORY,
- ]);
- if ($res === null) {
+ SELECT
+ COUNT(*) AS total,
+ COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS unread
+ FROM `_entry` e
+ JOIN `_feed` f ON e.id_feed = f.id
+ WHERE e.is_favorite = 1 AND f.priority > :priority
+ SQL;
+ $res = $this->fetchAssoc($sql, [':priority' => FreshRSS_Feed::PRIORITY_HIDDEN]);
+ if ($res === null || !isset($res[0])) {
return ['all' => -1, 'unread' => -1, 'read' => -1];
}
-
- rsort($res);
- $all = (int)($res[0] ?? 0);
- $unread = (int)($res[1] ?? 0);
+ $all = (int)($res[0]['total'] ?? 0);
+ $unread = (int)($res[0]['unread'] ?? 0);
return ['all' => $all, 'unread' => $unread, 'read' => $all - $unread];
}
}
diff --git a/app/Models/View.php b/app/Models/View.php
index b7970b57e..d1d5d0d8e 100644
--- a/app/Models/View.php
+++ b/app/Models/View.php
@@ -44,12 +44,12 @@ class FreshRSS_View extends Minz_View {
public bool $signalError;
// Manage users
- /** @var array{feed_count:int,article_count:int,database_size:int,language:string,mail_login:string,enabled:bool,is_admin:bool,last_user_activity:string,is_default:bool} */
+ /** @var array{feed_count:?int,article_count:?int,database_size:?int,language:string,mail_login:string,enabled:bool,is_admin:bool,last_user_activity:string,is_default:bool} */
public array $details;
public bool $disable_aside;
public bool $show_email_field;
public string $username;
- /** @var array<array{language:string,enabled:bool,is_admin:bool,enabled:bool,article_count:int,database_size:int,last_user_activity:string,mail_login:string,feed_count:int,is_default:bool}> */
+ /** @var array<array{language:string,enabled:bool,is_admin:bool,enabled:bool,article_count:?int,database_size:?int,last_user_activity:string,mail_login:string,feed_count:?int,is_default:bool}> */
public array $users;
// Updates