CategoryDAO.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_CategoryDAO extends Minz_ModelPdo {
  4. public const DEFAULTCATEGORYID = 1;
  5. public const DEFAULT_CATEGORY_NAME = 'Uncategorized';
  6. public function sqlResetSequence(): bool {
  7. return true; // Nothing to do for MySQL
  8. }
  9. public function resetDefaultCategoryName(): bool {
  10. //FreshRSS 1.15.1
  11. $stm = $this->pdo->prepare('UPDATE `_category` SET name = :name WHERE id = :id');
  12. if ($stm !== false) {
  13. $stm->bindValue(':id', self::DEFAULTCATEGORYID, PDO::PARAM_INT);
  14. $stm->bindValue(':name', self::DEFAULT_CATEGORY_NAME);
  15. }
  16. return $stm !== false && $stm->execute();
  17. }
  18. protected function addColumn(string $name): bool {
  19. if ($this->pdo->inTransaction()) {
  20. $this->pdo->commit();
  21. }
  22. Minz_Log::warning(__METHOD__ . ': ' . $name);
  23. try {
  24. if ($name === 'kind') { //v1.20.0
  25. return $this->pdo->exec('ALTER TABLE `_category` ADD COLUMN kind SMALLINT DEFAULT 0') !== false;
  26. } elseif ($name === 'lastUpdate') { //v1.20.0
  27. return $this->pdo->exec('ALTER TABLE `_category` ADD COLUMN `lastUpdate` BIGINT DEFAULT 0') !== false;
  28. } elseif ($name === 'error') { //v1.20.0
  29. return $this->pdo->exec('ALTER TABLE `_category` ADD COLUMN error SMALLINT DEFAULT 0') !== false;
  30. } elseif ('attributes' === $name) { //v1.15.0
  31. $ok = $this->pdo->exec('ALTER TABLE `_category` ADD COLUMN attributes TEXT') !== false;
  32. /** @var list<array{id:int,url:string,kind:int,category:int,name:string,website:string,lastUpdate:int,
  33. * priority:int,pathEntries:string,httpAuth:string,error:int,keep_history:?int,ttl:int,attributes:string}> $feeds */
  34. $feeds = $this->fetchAssoc('SELECT * FROM `_feed`') ?? [];
  35. $stm = $this->pdo->prepare('UPDATE `_feed` SET attributes = :attributes WHERE id = :id');
  36. if ($stm === false) {
  37. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($this->pdo->errorInfo()));
  38. return false;
  39. }
  40. foreach ($feeds as $feed) {
  41. if (empty($feed['keep_history']) || empty($feed['id'])) {
  42. continue;
  43. }
  44. $keepHistory = $feed['keep_history'];
  45. $attributes = empty($feed['attributes']) ? [] : json_decode($feed['attributes'], true);
  46. if (is_string($attributes)) { //Legacy risk of double-encoding
  47. $attributes = json_decode($attributes, true);
  48. }
  49. if (!is_array($attributes)) {
  50. $attributes = [];
  51. }
  52. $archiving = is_array($attributes['archiving'] ?? null) ? $attributes['archiving'] : [];
  53. if ($keepHistory > 0) {
  54. $archiving['keep_min'] = (int)$keepHistory;
  55. } elseif ($keepHistory == -1) { //Infinite
  56. $archiving['keep_period'] = false;
  57. $archiving['keep_max'] = false;
  58. $archiving['keep_min'] = false;
  59. } else {
  60. continue;
  61. }
  62. $attributes['archiving'] = $archiving;
  63. if (!($stm->bindValue(':id', $feed['id'], PDO::PARAM_INT) &&
  64. $stm->bindValue(':attributes', json_encode($attributes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)) &&
  65. $stm->execute())) {
  66. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($stm->errorInfo()));
  67. }
  68. }
  69. if ($this->pdo->dbType() !== 'sqlite') { //SQLite does not support DROP COLUMN
  70. $this->pdo->exec('ALTER TABLE `_feed` DROP COLUMN keep_history');
  71. } else {
  72. $this->pdo->exec('DROP INDEX IF EXISTS feed_keep_history_index'); //SQLite at least drop index
  73. }
  74. $this->resetDefaultCategoryName();
  75. return $ok;
  76. }
  77. } catch (Exception $e) {
  78. Minz_Log::error(__METHOD__ . ': ' . $e->getMessage());
  79. }
  80. return false;
  81. }
  82. /** @param array{0:string,1:int,2:string} $errorInfo */
  83. protected function autoUpdateDb(array $errorInfo): bool {
  84. if (isset($errorInfo[0])) {
  85. if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_FIELD_ERROR || $errorInfo[0] === FreshRSS_DatabaseDAOPGSQL::UNDEFINED_COLUMN) {
  86. $errorLines = explode("\n", $errorInfo[2], 2); // The relevant column name is on the first line, other lines are noise
  87. if (str_contains($errorLines[0], 'f.')) { // Coming from a feed sub-query
  88. $feedDao = FreshRSS_Factory::createFeedDao();
  89. if ($feedDao->autoUpdateDb($errorInfo)) {
  90. return true;
  91. }
  92. }
  93. foreach (['kind', 'lastUpdate', 'error', 'attributes'] as $column) {
  94. if (str_contains($errorLines[0], $column)) {
  95. return $this->addColumn($column);
  96. }
  97. }
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * @param array{id?:int,name:string,kind?:int,lastUpdate?:int,error?:int|bool,attributes?:string|array<string,mixed>} $valuesTmp
  104. */
  105. public function addCategory(array $valuesTmp): int|false {
  106. if (empty($valuesTmp['id'])) { // Auto-generated ID
  107. $sql = <<<'SQL'
  108. INSERT INTO `_category`(name, kind, attributes)
  109. SELECT * FROM (SELECT :name1 AS name, 1*:kind AS kind, :attributes AS attributes) c2
  110. SQL;
  111. } else {
  112. $sql = <<<'SQL'
  113. INSERT INTO `_category`(id, name, kind, attributes)
  114. SELECT * FROM (SELECT 1*:id AS id, :name1 AS name, 1*:kind AS kind, :attributes AS attributes) c2
  115. SQL;
  116. }
  117. // No tag of the same name
  118. $sql .= "\n" . <<<'SQL'
  119. WHERE NOT EXISTS (SELECT 1 FROM `_tag` WHERE name = :name2)
  120. SQL;
  121. $stm = $this->pdo->prepare($sql);
  122. $valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8');
  123. if (!isset($valuesTmp['attributes'])) {
  124. $valuesTmp['attributes'] = [];
  125. }
  126. if ($stm !== false) {
  127. if (!empty($valuesTmp['id'])) {
  128. $stm->bindValue(':id', $valuesTmp['id'], PDO::PARAM_INT);
  129. }
  130. $stm->bindValue(':name1', $valuesTmp['name'], PDO::PARAM_STR);
  131. $stm->bindValue(':kind', $valuesTmp['kind'] ?? FreshRSS_Category::KIND_NORMAL, PDO::PARAM_INT);
  132. $attributes = is_string($valuesTmp['attributes']) ? $valuesTmp['attributes'] :
  133. json_encode($valuesTmp['attributes'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  134. $stm->bindValue(':attributes', $attributes, PDO::PARAM_STR);
  135. $stm->bindValue(':name2', $valuesTmp['name'], PDO::PARAM_STR);
  136. }
  137. if ($stm !== false && $stm->execute() && $stm->rowCount() > 0) {
  138. if (empty($valuesTmp['id'])) {
  139. // Auto-generated ID
  140. $catId = $this->pdo->lastInsertId('`_category_id_seq`');
  141. return $catId === false ? false : (int)$catId;
  142. }
  143. $this->sqlResetSequence();
  144. return $valuesTmp['id'];
  145. } else {
  146. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  147. /** @var array{0:string,1:int,2:string} $info */
  148. if ($this->autoUpdateDb($info)) {
  149. return $this->addCategory($valuesTmp);
  150. }
  151. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  152. return false;
  153. }
  154. }
  155. public function addCategoryObject(FreshRSS_Category $category): int|false {
  156. $cat = $this->searchByName($category->name());
  157. if ($cat === null) {
  158. $values = [
  159. 'kind' => $category->kind(),
  160. 'name' => $category->name(),
  161. 'attributes' => $category->attributes(),
  162. ];
  163. return $this->addCategory($values);
  164. }
  165. return $cat->id();
  166. }
  167. /**
  168. * @param array{name:string,kind:int,attributes?:array<string,mixed>|mixed|null} $valuesTmp
  169. */
  170. public function updateCategory(int $id, array $valuesTmp): int|false {
  171. // No tag of the same name
  172. $sql = <<<'SQL'
  173. UPDATE `_category` SET name=?, kind=?, attributes=? WHERE id=?
  174. AND NOT EXISTS (SELECT 1 FROM `_tag` WHERE name = ?)
  175. SQL;
  176. $stm = $this->pdo->prepare($sql);
  177. $valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8');
  178. if (empty($valuesTmp['attributes'])) {
  179. $valuesTmp['attributes'] = [];
  180. }
  181. $values = [
  182. $valuesTmp['name'],
  183. $valuesTmp['kind'] ?? FreshRSS_Category::KIND_NORMAL,
  184. is_string($valuesTmp['attributes']) ? $valuesTmp['attributes'] : json_encode($valuesTmp['attributes'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
  185. $id,
  186. $valuesTmp['name'],
  187. ];
  188. if ($stm !== false && $stm->execute($values)) {
  189. return $stm->rowCount();
  190. } else {
  191. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  192. /** @var array{0:string,1:int,2:string} $info */
  193. if ($this->autoUpdateDb($info)) {
  194. return $this->updateCategory($id, $valuesTmp);
  195. }
  196. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  197. return false;
  198. }
  199. }
  200. public function updateLastUpdate(int $id, bool $inError = false, int $mtime = 0): int|false {
  201. $sql = 'UPDATE `_category` SET `lastUpdate`=?, error=? WHERE id=?';
  202. $values = [
  203. $mtime <= 0 ? time() : $mtime,
  204. $inError ? 1 : 0,
  205. $id,
  206. ];
  207. $stm = $this->pdo->prepare($sql);
  208. if ($stm !== false && $stm->execute($values)) {
  209. return $stm->rowCount();
  210. } else {
  211. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  212. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  213. return false;
  214. }
  215. }
  216. public function deleteCategory(int $id): int|false {
  217. $sql = 'DELETE FROM `_category` WHERE id=:id';
  218. $stm = $this->pdo->prepare($sql);
  219. if ($stm !== false && $stm->bindParam(':id', $id, PDO::PARAM_INT) && $stm->execute()) {
  220. return $stm->rowCount();
  221. } else {
  222. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  223. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  224. return false;
  225. }
  226. }
  227. /** @return Traversable<array{id:int,name:string,kind:int,lastUpdate:int,error:int,attributes?:array<string,mixed>}> */
  228. public function selectAll(): Traversable {
  229. $sql = 'SELECT id, name, kind, `lastUpdate`, error, attributes FROM `_category`';
  230. $stm = $this->pdo->query($sql);
  231. if ($stm !== false) {
  232. while (is_array($row = $stm->fetch(PDO::FETCH_ASSOC))) {
  233. /** @var array{id:int,name:string,kind:int,lastUpdate:int,error:int,attributes?:array<string,mixed>} $row */
  234. yield $row;
  235. }
  236. } else {
  237. $info = $this->pdo->errorInfo();
  238. /** @var array{0:string,1:int,2:string} $info */
  239. if ($this->autoUpdateDb($info)) {
  240. yield from $this->selectAll();
  241. } else {
  242. Minz_Log::error(__METHOD__ . ' error: ' . json_encode($info));
  243. }
  244. }
  245. }
  246. public function searchById(int $id): ?FreshRSS_Category {
  247. $sql = 'SELECT * FROM `_category` WHERE id=:id';
  248. $res = $this->fetchAssoc($sql, ['id' => $id]) ?? [];
  249. /** @var list<array{name:string,id:int,kind:int,lastUpdate?:int,error:int,attributes?:string}> $res */
  250. $categories = self::daoToCategories($res);
  251. return reset($categories) ?: null;
  252. }
  253. public function searchByName(string $name): ?FreshRSS_Category {
  254. $sql = 'SELECT * FROM `_category` WHERE name=:name';
  255. $res = $this->fetchAssoc($sql, ['name' => $name]) ?? [];
  256. /** @var list<array{name:string,id:int,kind:int,lastUpdate:int,error:int,attributes:string}> $res */
  257. $categories = self::daoToCategories($res);
  258. return reset($categories) ?: null;
  259. }
  260. /** @return array<int,FreshRSS_Category> where the key is the category ID */
  261. public function listSortedCategories(bool $prePopulateFeeds = true, bool $details = false): array {
  262. $categories = $this->listCategories($prePopulateFeeds, $details);
  263. uasort($categories, static function (FreshRSS_Category $a, FreshRSS_Category $b) {
  264. $aPosition = $a->attributeInt('position');
  265. $bPosition = $b->attributeInt('position');
  266. if ($aPosition === $bPosition) {
  267. return strnatcasecmp($a->name(), $b->name());
  268. } elseif (null === $aPosition) {
  269. return 1;
  270. } elseif (null === $bPosition) {
  271. return -1;
  272. }
  273. return ($aPosition < $bPosition) ? -1 : 1;
  274. });
  275. return $categories;
  276. }
  277. /** @return array<int,FreshRSS_Category> where the key is the category ID */
  278. public function listCategories(bool $prePopulateFeeds = true, bool $details = false): array {
  279. if ($prePopulateFeeds) {
  280. $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, '
  281. . ($details ? 'f.* ' : 'f.id, f.name, f.url, f.kind, f.website, f.priority, f.error, f.attributes, f.`cache_nbEntries`, f.`cache_nbUnreads`, f.ttl ')
  282. . 'FROM `_category` c '
  283. . 'LEFT OUTER JOIN `_feed` f ON f.category=c.id '
  284. . 'GROUP BY f.id, c_id '
  285. . 'ORDER BY c.name, f.name';
  286. $stm = $this->pdo->prepare($sql);
  287. if ($stm !== false && $stm->execute() && ($res = $stm->fetchAll(PDO::FETCH_ASSOC)) !== false) {
  288. /** @var list<array{c_name:string,c_id:int,c_kind:int,c_last_update:int,c_error:int,c_attributes?:string,
  289. * id?:int,name?:string,url?:string,kind?:int,category?:int,website?:string,priority?:int,error?:int,attributes?:string,cache_nbEntries?:int,cache_nbUnreads?:int,ttl?:int}> $res */
  290. return self::daoToCategoriesPrepopulated($res);
  291. } else {
  292. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  293. /** @var array{0:string,1:int,2:string} $info */
  294. if ($this->autoUpdateDb($info)) {
  295. return $this->listCategories($prePopulateFeeds, $details);
  296. }
  297. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  298. return [];
  299. }
  300. } else {
  301. $res = $this->fetchAssoc('SELECT * FROM `_category` ORDER BY name') ?? [];
  302. /** @var list<array{name:string,id:int,kind:int,lastUpdate?:int,error?:int,attributes?:string}> $res */
  303. return empty($res) ? [] : self::daoToCategories($res);
  304. }
  305. }
  306. /** @return array<int,FreshRSS_Category> where the key is the category ID */
  307. public function listCategoriesOrderUpdate(int $defaultCacheDuration = 86400, int $limit = 0): array {
  308. $sql = 'SELECT * FROM `_category` WHERE kind = :kind AND `lastUpdate` < :lu ORDER BY `lastUpdate`'
  309. . ($limit < 1 ? '' : ' LIMIT ' . $limit);
  310. $stm = $this->pdo->prepare($sql);
  311. if ($stm !== false &&
  312. $stm->bindValue(':kind', FreshRSS_Category::KIND_DYNAMIC_OPML, PDO::PARAM_INT) &&
  313. $stm->bindValue(':lu', time() - $defaultCacheDuration, PDO::PARAM_INT) &&
  314. $stm->execute()) {
  315. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  316. /** @var list<array{name:string,id:int,kind:int,lastUpdate:int,error?:int,attributes?:string}> $res */
  317. return self::daoToCategories($res);
  318. } else {
  319. $info = $stm !== false ? $stm->errorInfo() : $this->pdo->errorInfo();
  320. /** @var array{0:string,1:int,2:string} $info */
  321. if ($this->autoUpdateDb($info)) {
  322. return $this->listCategoriesOrderUpdate($defaultCacheDuration, $limit);
  323. }
  324. Minz_Log::warning(__METHOD__ . ' error: ' . $sql . ' : ' . json_encode($info));
  325. return [];
  326. }
  327. }
  328. public function getDefault(): ?FreshRSS_Category {
  329. $sql = 'SELECT * FROM `_category` WHERE id=:id';
  330. $res = $this->fetchAssoc($sql, [':id' => self::DEFAULTCATEGORYID]) ?? [];
  331. /** @var list<array{name:string,id:int,kind:int,lastUpdate?:int,error?:int,attributes?:string}> $res */
  332. $categories = self::daoToCategories($res);
  333. if (isset($categories[self::DEFAULTCATEGORYID])) {
  334. return $categories[self::DEFAULTCATEGORYID];
  335. } else {
  336. if (FreshRSS_Context::$isCli) {
  337. fwrite(STDERR, 'FreshRSS database error: Default category not found!' . "\n");
  338. }
  339. Minz_Log::error('FreshRSS database error: Default category not found!');
  340. return null;
  341. }
  342. }
  343. public function checkDefault(): int|bool {
  344. $def_cat = $this->searchById(self::DEFAULTCATEGORYID);
  345. if ($def_cat == null) {
  346. $cat = new FreshRSS_Category(_t('gen.short.default_category'), self::DEFAULTCATEGORYID);
  347. $sql = 'INSERT INTO `_category`(id, name) VALUES(?, ?)';
  348. $stm = $this->pdo->prepare($sql);
  349. $values = [
  350. $cat->id(),
  351. $cat->name(),
  352. ];
  353. if ($stm !== false && $stm->execute($values)) {
  354. $catId = $this->pdo->lastInsertId('`_category_id_seq`');
  355. $this->sqlResetSequence();
  356. return $catId === false ? false : (int)$catId;
  357. } else {
  358. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  359. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  360. return false;
  361. }
  362. }
  363. return true;
  364. }
  365. public function count(): int {
  366. $sql = 'SELECT COUNT(*) AS count FROM `_category`';
  367. $res = $this->fetchColumn($sql, 0);
  368. return isset($res[0]) ? (int)$res[0] : -1;
  369. }
  370. public function countFeed(int $id): int {
  371. $sql = 'SELECT COUNT(*) AS count FROM `_feed` WHERE category=:id';
  372. $res = $this->fetchColumn($sql, 0, [':id' => $id]);
  373. return isset($res[0]) ? (int)$res[0] : -1;
  374. }
  375. public function countNotRead(int $id, int $minPriority = FreshRSS_Feed::PRIORITY_CATEGORY): int {
  376. $sql = <<<'SQL'
  377. SELECT COUNT(*) AS count FROM `_entry` e
  378. INNER JOIN `_feed` f ON e.id_feed=f.id
  379. WHERE f.category=:id AND e.is_read=0
  380. AND f.priority>=:minPriority
  381. SQL;
  382. $res = $this->fetchColumn($sql, 0, [':id' => $id, ':minPriority' => $minPriority]);
  383. return isset($res[0]) ? (int)$res[0] : -1;
  384. }
  385. /** @return list<string> */
  386. public function listTitles(int $id, int $limit = 0): array {
  387. $sql = <<<'SQL'
  388. SELECT e.title FROM `_entry` e
  389. INNER JOIN `_feed` f ON e.id_feed=f.id
  390. WHERE f.category=:id_category
  391. ORDER BY e.id DESC
  392. SQL;
  393. $sql .= ($limit < 1 ? '' : ' LIMIT ' . intval($limit));
  394. $res = $this->fetchColumn($sql, 0, [':id_category' => $id]) ?? [];
  395. /** @var list<string> $res */
  396. return $res;
  397. }
  398. /**
  399. * @param array<array{c_name:string,c_id:int,c_kind:int,c_last_update:int,c_error:int|bool,c_attributes?:string,
  400. * id?:int,name?:string,url?:string,kind?:int,website?:string,priority?:int,
  401. * error?:int|bool,attributes?:string,cache_nbEntries?:int,cache_nbUnreads?:int,ttl?:int}> $listDAO
  402. * @return array<int,FreshRSS_Category> where the key is the category ID
  403. */
  404. private static function daoToCategoriesPrepopulated(array $listDAO): array {
  405. $list = [];
  406. $previousLine = [];
  407. $feedsDao = [];
  408. $feedDao = FreshRSS_Factory::createFeedDao();
  409. foreach ($listDAO as $line) {
  410. if (!empty($previousLine['c_id']) && $line['c_id'] !== $previousLine['c_id']) {
  411. // End of the current category, we add it to the $list
  412. $cat = new FreshRSS_Category(
  413. $previousLine['c_name'],
  414. $previousLine['c_id'],
  415. $feedDao::daoToFeeds($feedsDao, $previousLine['c_id'])
  416. );
  417. $cat->_kind($previousLine['c_kind']);
  418. $cat->_attributes($previousLine['c_attributes'] ?? '[]');
  419. $list[$cat->id()] = $cat;
  420. $feedsDao = []; //Prepare for next category
  421. }
  422. $previousLine = $line;
  423. $feedsDao[] = $line;
  424. }
  425. // add the last category
  426. if ($previousLine != null) {
  427. $cat = new FreshRSS_Category(
  428. $previousLine['c_name'],
  429. $previousLine['c_id'],
  430. $feedDao::daoToFeeds($feedsDao, $previousLine['c_id'])
  431. );
  432. $cat->_kind($previousLine['c_kind']);
  433. $cat->_lastUpdate($previousLine['c_last_update'] ?? 0);
  434. $cat->_error($previousLine['c_error'] ?? 0);
  435. $cat->_attributes($previousLine['c_attributes'] ?? []);
  436. $list[$cat->id()] = $cat;
  437. }
  438. return $list;
  439. }
  440. /**
  441. * @param array<array{name:string,id:int,kind:int,lastUpdate?:int,error?:int|bool,attributes?:string}> $listDAO
  442. * @return array<int,FreshRSS_Category> where the key is the category ID
  443. */
  444. private static function daoToCategories(array $listDAO): array {
  445. $list = [];
  446. foreach ($listDAO as $dao) {
  447. $cat = new FreshRSS_Category(
  448. $dao['name'],
  449. $dao['id']
  450. );
  451. $cat->_kind($dao['kind']);
  452. $cat->_lastUpdate($dao['lastUpdate'] ?? 0);
  453. $cat->_error($dao['error'] ?? 0);
  454. $cat->_attributes($dao['attributes'] ?? '');
  455. $list[$cat->id()] = $cat;
  456. }
  457. return $list;
  458. }
  459. }