|
@@ -245,19 +245,19 @@ SQL;
|
|
|
$sql = 'SELECT * FROM `_category` WHERE id=:id';
|
|
$sql = 'SELECT * FROM `_category` WHERE id=:id';
|
|
|
$res = $this->fetchAssoc($sql, ['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 */
|
|
/** @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 {
|
|
public function searchByName(string $name): ?FreshRSS_Category {
|
|
|
$sql = 'SELECT * FROM `_category` WHERE name=:name';
|
|
$sql = 'SELECT * FROM `_category` WHERE name=:name';
|
|
|
$res = $this->fetchAssoc($sql, ['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 */
|
|
/** @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 {
|
|
public function listSortedCategories(bool $prePopulateFeeds = true, bool $details = false): array {
|
|
|
$categories = $this->listCategories($prePopulateFeeds, $details);
|
|
$categories = $this->listCategories($prePopulateFeeds, $details);
|
|
|
|
|
|
|
@@ -277,7 +277,7 @@ SQL;
|
|
|
return $categories;
|
|
return $categories;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /** @return array<FreshRSS_Category> */
|
|
|
|
|
|
|
+ /** @return array<int,FreshRSS_Category> */
|
|
|
public function listCategories(bool $prePopulateFeeds = true, bool $details = false): array {
|
|
public function listCategories(bool $prePopulateFeeds = true, bool $details = false): array {
|
|
|
if ($prePopulateFeeds) {
|
|
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, '
|
|
$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) ?: [];
|
|
$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,
|
|
/** @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 */
|
|
* '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 {
|
|
} else {
|
|
|
$info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
|
|
$info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
|
|
|
if ($this->autoUpdateDb($info)) {
|
|
if ($this->autoUpdateDb($info)) {
|
|
@@ -305,11 +305,11 @@ SQL;
|
|
|
} else {
|
|
} else {
|
|
|
$res = $this->fetchAssoc('SELECT * FROM `_category` ORDER BY name');
|
|
$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 */
|
|
/** @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 {
|
|
public function listCategoriesOrderUpdate(int $defaultCacheDuration = 86400, int $limit = 0): array {
|
|
|
$sql = 'SELECT * FROM `_category` WHERE kind = :kind AND `lastUpdate` < :lu ORDER BY `lastUpdate`'
|
|
$sql = 'SELECT * FROM `_category` WHERE kind = :kind AND `lastUpdate` < :lu ORDER BY `lastUpdate`'
|
|
|
. ($limit < 1 ? '' : ' LIMIT ' . $limit);
|
|
. ($limit < 1 ? '' : ' LIMIT ' . $limit);
|
|
@@ -318,7 +318,7 @@ SQL;
|
|
|
$stm->bindValue(':kind', FreshRSS_Category::KIND_DYNAMIC_OPML, PDO::PARAM_INT) &&
|
|
$stm->bindValue(':kind', FreshRSS_Category::KIND_DYNAMIC_OPML, PDO::PARAM_INT) &&
|
|
|
$stm->bindValue(':lu', time() - $defaultCacheDuration, PDO::PARAM_INT) &&
|
|
$stm->bindValue(':lu', time() - $defaultCacheDuration, PDO::PARAM_INT) &&
|
|
|
$stm->execute()) {
|
|
$stm->execute()) {
|
|
|
- return self::daoToCategory($stm->fetchAll(PDO::FETCH_ASSOC));
|
|
|
|
|
|
|
+ return self::daoToCategories($stm->fetchAll(PDO::FETCH_ASSOC));
|
|
|
} else {
|
|
} else {
|
|
|
$info = $stm ? $stm->errorInfo() : $this->pdo->errorInfo();
|
|
$info = $stm ? $stm->errorInfo() : $this->pdo->errorInfo();
|
|
|
if ($this->autoUpdateDb($info)) {
|
|
if ($this->autoUpdateDb($info)) {
|
|
@@ -333,9 +333,9 @@ SQL;
|
|
|
$sql = 'SELECT * FROM `_category` WHERE id=:id';
|
|
$sql = 'SELECT * FROM `_category` WHERE id=:id';
|
|
|
$res = $this->fetchAssoc($sql, [':id' => self::DEFAULTCATEGORYID]) ?? [];
|
|
$res = $this->fetchAssoc($sql, [':id' => self::DEFAULTCATEGORYID]) ?? [];
|
|
|
/** @var array<array{'name':string,'id':int,'kind':int,'lastUpdate'?:int,'error'?:int|bool,'attributes'?:string}> $res */
|
|
/** @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 {
|
|
} else {
|
|
|
if (FreshRSS_Context::$isCli) {
|
|
if (FreshRSS_Context::$isCli) {
|
|
|
fwrite(STDERR, 'FreshRSS database error: Default category not found!' . "\n");
|
|
fwrite(STDERR, 'FreshRSS database error: Default category not found!' . "\n");
|
|
@@ -394,41 +394,13 @@ SQL;
|
|
|
return isset($res[0]) ? (int)$res[0] : -1;
|
|
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,
|
|
* @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,
|
|
* 'id'?:int,'name'?:string,'url'?:string,'kind'?:int,'website'?:string,'priority'?:int,
|
|
|
* 'error'?:int|bool,'cache_nbEntries'?:int,'cache_nbUnreads'?:int,'ttl'?:int}> $listDAO
|
|
* 'error'?:int|bool,'cache_nbEntries'?:int,'cache_nbUnreads'?:int,'ttl'?:int}> $listDAO
|
|
|
* @return array<int,FreshRSS_Category>
|
|
* @return array<int,FreshRSS_Category>
|
|
|
*/
|
|
*/
|
|
|
- private static function daoToCategoryPrepopulated(array $listDAO): array {
|
|
|
|
|
|
|
+ private static function daoToCategoriesPrepopulated(array $listDAO): array {
|
|
|
$list = [];
|
|
$list = [];
|
|
|
$previousLine = [];
|
|
$previousLine = [];
|
|
|
$feedsDao = [];
|
|
$feedsDao = [];
|
|
@@ -441,11 +413,11 @@ SQL;
|
|
|
$cat = new FreshRSS_Category(
|
|
$cat = new FreshRSS_Category(
|
|
|
$previousLine['c_name'],
|
|
$previousLine['c_name'],
|
|
|
$previousLine['c_id'],
|
|
$previousLine['c_id'],
|
|
|
- $feedDao::daoToFeed($feedsDao, $previousLine['c_id'])
|
|
|
|
|
|
|
+ $feedDao::daoToFeeds($feedsDao, $previousLine['c_id'])
|
|
|
);
|
|
);
|
|
|
$cat->_kind($previousLine['c_kind']);
|
|
$cat->_kind($previousLine['c_kind']);
|
|
|
$cat->_attributes($previousLine['c_attributes'] ?? '[]');
|
|
$cat->_attributes($previousLine['c_attributes'] ?? '[]');
|
|
|
- $list[(int)$previousLine['c_id']] = $cat;
|
|
|
|
|
|
|
+ $list[$cat->id()] = $cat;
|
|
|
|
|
|
|
|
$feedsDao = []; //Prepare for next category
|
|
$feedsDao = []; //Prepare for next category
|
|
|
}
|
|
}
|
|
@@ -459,13 +431,13 @@ SQL;
|
|
|
$cat = new FreshRSS_Category(
|
|
$cat = new FreshRSS_Category(
|
|
|
$previousLine['c_name'],
|
|
$previousLine['c_name'],
|
|
|
$previousLine['c_id'],
|
|
$previousLine['c_id'],
|
|
|
- $feedDao::daoToFeed($feedsDao, $previousLine['c_id'])
|
|
|
|
|
|
|
+ $feedDao::daoToFeeds($feedsDao, $previousLine['c_id'])
|
|
|
);
|
|
);
|
|
|
$cat->_kind($previousLine['c_kind']);
|
|
$cat->_kind($previousLine['c_kind']);
|
|
|
$cat->_lastUpdate($previousLine['c_last_update'] ?? 0);
|
|
$cat->_lastUpdate($previousLine['c_last_update'] ?? 0);
|
|
|
$cat->_error($previousLine['c_error'] ?? 0);
|
|
$cat->_error($previousLine['c_error'] ?? 0);
|
|
|
$cat->_attributes($previousLine['c_attributes'] ?? []);
|
|
$cat->_attributes($previousLine['c_attributes'] ?? []);
|
|
|
- $list[(int)$previousLine['c_id']] = $cat;
|
|
|
|
|
|
|
+ $list[$cat->id()] = $cat;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return $list;
|
|
return $list;
|
|
@@ -473,11 +445,10 @@ SQL;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @param array<array{'name':string,'id':int,'kind':int,'lastUpdate'?:int,'error'?:int|bool,'attributes'?:string}> $listDAO
|
|
* @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 = [];
|
|
$list = [];
|
|
|
-
|
|
|
|
|
foreach ($listDAO as $dao) {
|
|
foreach ($listDAO as $dao) {
|
|
|
FreshRSS_DatabaseDAO::pdoInt($dao, ['id', 'kind', 'lastUpdate', 'error']);
|
|
FreshRSS_DatabaseDAO::pdoInt($dao, ['id', 'kind', 'lastUpdate', 'error']);
|
|
|
$cat = new FreshRSS_Category(
|
|
$cat = new FreshRSS_Category(
|
|
@@ -488,9 +459,8 @@ SQL;
|
|
|
$cat->_lastUpdate($dao['lastUpdate'] ?? 0);
|
|
$cat->_lastUpdate($dao['lastUpdate'] ?? 0);
|
|
|
$cat->_error($dao['error'] ?? 0);
|
|
$cat->_error($dao['error'] ?? 0);
|
|
|
$cat->_attributes($dao['attributes'] ?? '');
|
|
$cat->_attributes($dao['attributes'] ?? '');
|
|
|
- $list[] = $cat;
|
|
|
|
|
|
|
+ $list[$cat->id()] = $cat;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
return $list;
|
|
return $list;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|