CategoryDAO.php 18 KB

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