diff options
| author | 2024-02-26 09:01:03 +0100 | |
|---|---|---|
| committer | 2024-02-26 09:01:03 +0100 | |
| commit | 39cc1c11ec596176e842cc98e6a54337e3c04d7e (patch) | |
| tree | dab89beb80268acb5e4bd58dfc55297bd30a8486 /app/Models/CategoryDAO.php | |
| parent | 25166c218be4e1ce1cb098de274a231b623d527e (diff) | |
New feature: shareable user query (#6052)
* New feature: shareable user query
Share the output of a user query by RSS / HTML / OPML with other people through unique URLs.
Replaces the global admin token, which was the only option (but unsafe) to share RSS outputs with other people.
Also add a new HTML output for people without an RSS reader.
fix https://github.com/FreshRSS/FreshRSS/issues/3066#issuecomment-648977890
fix https://github.com/FreshRSS/FreshRSS/issues/3178#issuecomment-769435504
* Remove unused method
* Fix token saving
* Implement HTML view
* Update i18n for master token
* Revert i18n get_favorite
* Fix missing i18n for user queries from before this PR
* Remove irrelevant tests
* Add link to RSS version
* Fix getGet
* Fix getState
* Fix getSearch
* Alternative getSearch
* Default getOrder
* Explicit default state
* Fix test
* Add OPML sharing
* Remove many redundant SQL queries from original implementation of user queries
* Fix article tags
* Use default user settings
* Prepare public search
* Fixes
* Allow user search on article tags
* Implement user search
* Revert filter bug
* Revert wrong SQL left outer join change
* Implement checkboxes
* Safe check of OPML
* Fix label
* Remove RSS button to favour new sharing method
That sharing button was using a global admin token
* First version of HTTP 304
* Disallow some recusrivity
fix https://github.com/FreshRSS/FreshRSS/issues/6086
* Draft of nav
* Minor httpConditional
* Add support for offset for pagination
* Fix offset pagination
* Fix explicit order ASC
* Add documentation
* Help links i18n
* Note about deprecated master token
* Typo
* Doc about format
Diffstat (limited to 'app/Models/CategoryDAO.php')
| -rw-r--r-- | app/Models/CategoryDAO.php | 72 |
1 files changed, 21 insertions, 51 deletions
diff --git a/app/Models/CategoryDAO.php b/app/Models/CategoryDAO.php index 8ea8090b8..90c3db30d 100644 --- a/app/Models/CategoryDAO.php +++ b/app/Models/CategoryDAO.php @@ -245,19 +245,19 @@ SQL; $sql = 'SELECT * FROM `_category` WHERE id=:id'; $res = $this->fetchAssoc($sql, ['id' => $id]) ?? []; /** @var array<array{'name':string,'id':int,'kind':int,'lastUpdate':int,'error':int|bool,'attributes':string}> $res */ - $cat = self::daoToCategory($res); - return $cat[0] ?? null; + $categories = self::daoToCategories($res); + return reset($categories) ?: null; } public function searchByName(string $name): ?FreshRSS_Category { $sql = 'SELECT * FROM `_category` WHERE name=:name'; $res = $this->fetchAssoc($sql, ['name' => $name]) ?? []; /** @var array<array{'name':string,'id':int,'kind':int,'lastUpdate':int,'error':int|bool,'attributes':string}> $res */ - $cat = self::daoToCategory($res); - return $cat[0] ?? null; + $categories = self::daoToCategories($res); + return reset($categories) ?: null; } - /** @return array<FreshRSS_Category> */ + /** @return array<int,FreshRSS_Category> */ public function listSortedCategories(bool $prePopulateFeeds = true, bool $details = false): array { $categories = $this->listCategories($prePopulateFeeds, $details); @@ -277,7 +277,7 @@ SQL; return $categories; } - /** @return array<FreshRSS_Category> */ + /** @return array<int,FreshRSS_Category> */ public function listCategories(bool $prePopulateFeeds = true, bool $details = false): array { if ($prePopulateFeeds) { $sql = 'SELECT c.id AS c_id, c.name AS c_name, c.kind AS c_kind, c.`lastUpdate` AS c_last_update, c.error AS c_error, c.attributes AS c_attributes, ' @@ -293,7 +293,7 @@ SQL; $res = $stm->fetchAll(PDO::FETCH_ASSOC) ?: []; /** @var array<array{'c_name':string,'c_id':int,'c_kind':int,'c_last_update':int,'c_error':int|bool,'c_attributes'?:string, * 'id'?:int,'name'?:string,'url'?:string,'kind'?:int,'category'?:int,'website'?:string,'priority'?:int,'error'?:int|bool,'cache_nbEntries'?:int,'cache_nbUnreads'?:int,'ttl'?:int}> $res */ - return self::daoToCategoryPrepopulated($res); + return self::daoToCategoriesPrepopulated($res); } else { $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo(); if ($this->autoUpdateDb($info)) { @@ -305,11 +305,11 @@ SQL; } else { $res = $this->fetchAssoc('SELECT * FROM `_category` ORDER BY name'); /** @var array<array{'name':string,'id':int,'kind':int,'lastUpdate'?:int,'error'?:int|bool,'attributes'?:string}> $res */ - return $res == null ? [] : self::daoToCategory($res); + return empty($res) ? [] : self::daoToCategories($res); } } - /** @return array<FreshRSS_Category> */ + /** @return array<int,FreshRSS_Category> */ public function listCategoriesOrderUpdate(int $defaultCacheDuration = 86400, int $limit = 0): array { $sql = 'SELECT * FROM `_category` WHERE kind = :kind AND `lastUpdate` < :lu ORDER BY `lastUpdate`' . ($limit < 1 ? '' : ' LIMIT ' . $limit); @@ -318,7 +318,7 @@ SQL; $stm->bindValue(':kind', FreshRSS_Category::KIND_DYNAMIC_OPML, PDO::PARAM_INT) && $stm->bindValue(':lu', time() - $defaultCacheDuration, PDO::PARAM_INT) && $stm->execute()) { - return self::daoToCategory($stm->fetchAll(PDO::FETCH_ASSOC)); + return self::daoToCategories($stm->fetchAll(PDO::FETCH_ASSOC)); } else { $info = $stm ? $stm->errorInfo() : $this->pdo->errorInfo(); if ($this->autoUpdateDb($info)) { @@ -333,9 +333,9 @@ SQL; $sql = 'SELECT * FROM `_category` WHERE id=:id'; $res = $this->fetchAssoc($sql, [':id' => self::DEFAULTCATEGORYID]) ?? []; /** @var array<array{'name':string,'id':int,'kind':int,'lastUpdate'?:int,'error'?:int|bool,'attributes'?:string}> $res */ - $cat = self::daoToCategory($res); - if (isset($cat[0])) { - return $cat[0]; + $categories = self::daoToCategories($res); + if (isset($categories[self::DEFAULTCATEGORYID])) { + return $categories[self::DEFAULTCATEGORYID]; } else { if (FreshRSS_Context::$isCli) { fwrite(STDERR, 'FreshRSS database error: Default category not found!' . "\n"); @@ -394,41 +394,13 @@ SQL; return isset($res[0]) ? (int)$res[0] : -1; } - /** @param array<FreshRSS_Category> $categories */ - public static function findFeed(array $categories, int $feed_id): ?FreshRSS_Feed { - foreach ($categories as $category) { - foreach ($category->feeds() as $feed) { - if ($feed->id() === $feed_id) { - $feed->_category($category); // Should already be done; just to be safe - return $feed; - } - } - } - return null; - } - - /** - * @param array<FreshRSS_Category> $categories - */ - public static function countUnread(array $categories, int $minPriority = 0): int { - $n = 0; - foreach ($categories as $category) { - foreach ($category->feeds() as $feed) { - if ($feed->priority() >= $minPriority) { - $n += $feed->nbNotRead(); - } - } - } - return $n; - } - /** * @param array<array{'c_name':string,'c_id':int,'c_kind':int,'c_last_update':int,'c_error':int|bool,'c_attributes'?:string, * 'id'?:int,'name'?:string,'url'?:string,'kind'?:int,'website'?:string,'priority'?:int, * 'error'?:int|bool,'cache_nbEntries'?:int,'cache_nbUnreads'?:int,'ttl'?:int}> $listDAO * @return array<int,FreshRSS_Category> */ - private static function daoToCategoryPrepopulated(array $listDAO): array { + private static function daoToCategoriesPrepopulated(array $listDAO): array { $list = []; $previousLine = []; $feedsDao = []; @@ -441,11 +413,11 @@ SQL; $cat = new FreshRSS_Category( $previousLine['c_name'], $previousLine['c_id'], - $feedDao::daoToFeed($feedsDao, $previousLine['c_id']) + $feedDao::daoToFeeds($feedsDao, $previousLine['c_id']) ); $cat->_kind($previousLine['c_kind']); $cat->_attributes($previousLine['c_attributes'] ?? '[]'); - $list[(int)$previousLine['c_id']] = $cat; + $list[$cat->id()] = $cat; $feedsDao = []; //Prepare for next category } @@ -459,13 +431,13 @@ SQL; $cat = new FreshRSS_Category( $previousLine['c_name'], $previousLine['c_id'], - $feedDao::daoToFeed($feedsDao, $previousLine['c_id']) + $feedDao::daoToFeeds($feedsDao, $previousLine['c_id']) ); $cat->_kind($previousLine['c_kind']); $cat->_lastUpdate($previousLine['c_last_update'] ?? 0); $cat->_error($previousLine['c_error'] ?? 0); $cat->_attributes($previousLine['c_attributes'] ?? []); - $list[(int)$previousLine['c_id']] = $cat; + $list[$cat->id()] = $cat; } return $list; @@ -473,11 +445,10 @@ SQL; /** * @param array<array{'name':string,'id':int,'kind':int,'lastUpdate'?:int,'error'?:int|bool,'attributes'?:string}> $listDAO - * @return array<FreshRSS_Category> + * @return array<int,FreshRSS_Category> */ - private static function daoToCategory(array $listDAO): array { + private static function daoToCategories(array $listDAO): array { $list = []; - foreach ($listDAO as $dao) { FreshRSS_DatabaseDAO::pdoInt($dao, ['id', 'kind', 'lastUpdate', 'error']); $cat = new FreshRSS_Category( @@ -488,9 +459,8 @@ SQL; $cat->_lastUpdate($dao['lastUpdate'] ?? 0); $cat->_error($dao['error'] ?? 0); $cat->_attributes($dao['attributes'] ?? ''); - $list[] = $cat; + $list[$cat->id()] = $cat; } - return $list; } } |
