CategoryDAO.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. foreach (['kind', 'lastUpdate', 'error', 'attributes'] as $column) {
  87. if (str_contains($errorLines[0], $column)) {
  88. return $this->addColumn($column);
  89. }
  90. }
  91. }
  92. }
  93. return false;
  94. }
  95. /**
  96. * @param array{id?:int,name:string,kind?:int,lastUpdate?:int,error?:int|bool,attributes?:string|array<string,mixed>} $valuesTmp
  97. */
  98. public function addCategory(array $valuesTmp): int|false {
  99. if (empty($valuesTmp['id'])) { // Auto-generated ID
  100. $sql = <<<'SQL'
  101. INSERT INTO `_category`(name, kind, attributes)
  102. SELECT * FROM (SELECT :name1 AS name, 1*:kind AS kind, :attributes AS attributes) c2
  103. SQL;
  104. } else {
  105. $sql = <<<'SQL'
  106. INSERT INTO `_category`(id, name, kind, attributes)
  107. SELECT * FROM (SELECT 1*:id AS id, :name1 AS name, 1*:kind AS kind, :attributes AS attributes) c2
  108. SQL;
  109. }
  110. // No tag of the same name
  111. $sql .= "\n" . <<<'SQL'
  112. WHERE NOT EXISTS (SELECT 1 FROM `_tag` WHERE name = :name2)
  113. SQL;
  114. $stm = $this->pdo->prepare($sql);
  115. $valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8');
  116. if (!isset($valuesTmp['attributes'])) {
  117. $valuesTmp['attributes'] = [];
  118. }
  119. if ($stm !== false) {
  120. if (!empty($valuesTmp['id'])) {
  121. $stm->bindValue(':id', $valuesTmp['id'], PDO::PARAM_INT);
  122. }
  123. $stm->bindValue(':name1', $valuesTmp['name'], PDO::PARAM_STR);
  124. $stm->bindValue(':kind', $valuesTmp['kind'] ?? FreshRSS_Category::KIND_NORMAL, PDO::PARAM_INT);
  125. $attributes = is_string($valuesTmp['attributes']) ? $valuesTmp['attributes'] :
  126. json_encode($valuesTmp['attributes'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  127. $stm->bindValue(':attributes', $attributes, PDO::PARAM_STR);
  128. $stm->bindValue(':name2', $valuesTmp['name'], PDO::PARAM_STR);
  129. }
  130. if ($stm !== false && $stm->execute() && $stm->rowCount() > 0) {
  131. if (empty($valuesTmp['id'])) {
  132. // Auto-generated ID
  133. $catId = $this->pdo->lastInsertId('`_category_id_seq`');
  134. return $catId === false ? false : (int)$catId;
  135. }
  136. $this->sqlResetSequence();
  137. return $valuesTmp['id'];
  138. } else {
  139. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  140. /** @var array{0:string,1:int,2:string} $info */
  141. if ($this->autoUpdateDb($info)) {
  142. return $this->addCategory($valuesTmp);
  143. }
  144. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  145. return false;
  146. }
  147. }
  148. public function addCategoryObject(FreshRSS_Category $category): int|false {
  149. $cat = $this->searchByName($category->name());
  150. if ($cat === null) {
  151. $values = [
  152. 'kind' => $category->kind(),
  153. 'name' => $category->name(),
  154. 'attributes' => $category->attributes(),
  155. ];
  156. return $this->addCategory($values);
  157. }
  158. return $cat->id();
  159. }
  160. /**
  161. * @param array{name:string,kind:int,attributes?:array<string,mixed>|mixed|null} $valuesTmp
  162. */
  163. public function updateCategory(int $id, array $valuesTmp): int|false {
  164. // No tag of the same name
  165. $sql = <<<'SQL'
  166. UPDATE `_category` SET name=?, kind=?, attributes=? WHERE id=?
  167. AND NOT EXISTS (SELECT 1 FROM `_tag` WHERE name = ?)
  168. SQL;
  169. $stm = $this->pdo->prepare($sql);
  170. $valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8');
  171. if (empty($valuesTmp['attributes'])) {
  172. $valuesTmp['attributes'] = [];
  173. }
  174. $values = [
  175. $valuesTmp['name'],
  176. $valuesTmp['kind'] ?? FreshRSS_Category::KIND_NORMAL,
  177. is_string($valuesTmp['attributes']) ? $valuesTmp['attributes'] : json_encode($valuesTmp['attributes'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
  178. $id,
  179. $valuesTmp['name'],
  180. ];
  181. if ($stm !== false && $stm->execute($values)) {
  182. return $stm->rowCount();
  183. } else {
  184. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  185. /** @var array{0:string,1:int,2:string} $info */
  186. if ($this->autoUpdateDb($info)) {
  187. return $this->updateCategory($id, $valuesTmp);
  188. }
  189. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  190. return false;
  191. }
  192. }
  193. public function updateLastUpdate(int $id, bool $inError = false, int $mtime = 0): int|false {
  194. $sql = 'UPDATE `_category` SET `lastUpdate`=?, error=? WHERE id=?';
  195. $values = [
  196. $mtime <= 0 ? time() : $mtime,
  197. $inError ? 1 : 0,
  198. $id,
  199. ];
  200. $stm = $this->pdo->prepare($sql);
  201. if ($stm !== false && $stm->execute($values)) {
  202. return $stm->rowCount();
  203. } else {
  204. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  205. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  206. return false;
  207. }
  208. }
  209. public function deleteCategory(int $id): int|false {
  210. $sql = 'DELETE FROM `_category` WHERE id=:id';
  211. $stm = $this->pdo->prepare($sql);
  212. if ($stm !== false && $stm->bindParam(':id', $id, PDO::PARAM_INT) && $stm->execute()) {
  213. return $stm->rowCount();
  214. } else {
  215. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  216. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  217. return false;
  218. }
  219. }
  220. /** @return Traversable<array{id:int,name:string,kind:int,lastUpdate:int,error:int,attributes?:array<string,mixed>}> */
  221. public function selectAll(): Traversable {
  222. $sql = 'SELECT id, name, kind, `lastUpdate`, error, attributes FROM `_category`';
  223. $stm = $this->pdo->query($sql);
  224. if ($stm !== false) {
  225. while (is_array($row = $stm->fetch(PDO::FETCH_ASSOC))) {
  226. /** @var array{id:int,name:string,kind:int,lastUpdate:int,error:int,attributes?:array<string,mixed>} $row */
  227. yield $row;
  228. }
  229. } else {
  230. $info = $this->pdo->errorInfo();
  231. /** @var array{0:string,1:int,2:string} $info */
  232. if ($this->autoUpdateDb($info)) {
  233. yield from $this->selectAll();
  234. } else {
  235. Minz_Log::error(__METHOD__ . ' error: ' . json_encode($info));
  236. }
  237. }
  238. }
  239. public function searchById(int $id): ?FreshRSS_Category {
  240. $sql = 'SELECT * FROM `_category` WHERE id=:id';
  241. $res = $this->fetchAssoc($sql, ['id' => $id]) ?? [];
  242. /** @var list<array{name:string,id:int,kind:int,lastUpdate?:int,error:int,attributes?:string}> $res */
  243. $categories = self::daoToCategories($res);
  244. return reset($categories) ?: null;
  245. }
  246. public function searchByName(string $name): ?FreshRSS_Category {
  247. $sql = 'SELECT * FROM `_category` WHERE name=:name';
  248. $res = $this->fetchAssoc($sql, ['name' => $name]) ?? [];
  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. /** @return array<int,FreshRSS_Category> where the key is the category ID */
  254. public function listSortedCategories(bool $prePopulateFeeds = true, bool $details = false): array {
  255. $categories = $this->listCategories($prePopulateFeeds, $details);
  256. uasort($categories, static function (FreshRSS_Category $a, FreshRSS_Category $b) {
  257. $aPosition = $a->attributeInt('position');
  258. $bPosition = $b->attributeInt('position');
  259. if ($aPosition === $bPosition) {
  260. return strnatcasecmp($a->name(), $b->name());
  261. } elseif (null === $aPosition) {
  262. return 1;
  263. } elseif (null === $bPosition) {
  264. return -1;
  265. }
  266. return ($aPosition < $bPosition) ? -1 : 1;
  267. });
  268. return $categories;
  269. }
  270. /** @return array<int,FreshRSS_Category> where the key is the category ID */
  271. public function listCategories(bool $prePopulateFeeds = true, bool $details = false): array {
  272. if ($prePopulateFeeds) {
  273. $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, '
  274. . ($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 ')
  275. . 'FROM `_category` c '
  276. . 'LEFT OUTER JOIN `_feed` f ON f.category=c.id '
  277. . 'WHERE f.priority >= :priority '
  278. . 'GROUP BY f.id, c_id '
  279. . 'ORDER BY c.name, f.name';
  280. $stm = $this->pdo->prepare($sql);
  281. $values = [ ':priority' => FreshRSS_Feed::PRIORITY_CATEGORY ];
  282. if ($stm !== false && $stm->execute($values) && ($res = $stm->fetchAll(PDO::FETCH_ASSOC)) !== false) {
  283. /** @var list<array{c_name:string,c_id:int,c_kind:int,c_last_update:int,c_error:int,c_attributes?:string,
  284. * 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 */
  285. return self::daoToCategoriesPrepopulated($res);
  286. } else {
  287. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  288. /** @var array{0:string,1:int,2:string} $info */
  289. if ($this->autoUpdateDb($info)) {
  290. return $this->listCategories($prePopulateFeeds, $details);
  291. }
  292. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  293. return [];
  294. }
  295. } else {
  296. $res = $this->fetchAssoc('SELECT * FROM `_category` ORDER BY name') ?? [];
  297. /** @var list<array{name:string,id:int,kind:int,lastUpdate?:int,error?:int,attributes?:string}> $res */
  298. return empty($res) ? [] : self::daoToCategories($res);
  299. }
  300. }
  301. /** @return array<int,FreshRSS_Category> where the key is the category ID */
  302. public function listCategoriesOrderUpdate(int $defaultCacheDuration = 86400, int $limit = 0): array {
  303. $sql = 'SELECT * FROM `_category` WHERE kind = :kind AND `lastUpdate` < :lu ORDER BY `lastUpdate`'
  304. . ($limit < 1 ? '' : ' LIMIT ' . $limit);
  305. $stm = $this->pdo->prepare($sql);
  306. if ($stm !== false &&
  307. $stm->bindValue(':kind', FreshRSS_Category::KIND_DYNAMIC_OPML, PDO::PARAM_INT) &&
  308. $stm->bindValue(':lu', time() - $defaultCacheDuration, PDO::PARAM_INT) &&
  309. $stm->execute()) {
  310. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  311. /** @var list<array{name:string,id:int,kind:int,lastUpdate:int,error?:int,attributes?:string}> $res */
  312. return self::daoToCategories($res);
  313. } else {
  314. $info = $stm !== false ? $stm->errorInfo() : $this->pdo->errorInfo();
  315. /** @var array{0:string,1:int,2:string} $info */
  316. if ($this->autoUpdateDb($info)) {
  317. return $this->listCategoriesOrderUpdate($defaultCacheDuration, $limit);
  318. }
  319. Minz_Log::warning(__METHOD__ . ' error: ' . $sql . ' : ' . json_encode($info));
  320. return [];
  321. }
  322. }
  323. public function getDefault(): ?FreshRSS_Category {
  324. $sql = 'SELECT * FROM `_category` WHERE id=:id';
  325. $res = $this->fetchAssoc($sql, [':id' => self::DEFAULTCATEGORYID]) ?? [];
  326. /** @var list<array{name:string,id:int,kind:int,lastUpdate?:int,error?:int,attributes?:string}> $res */
  327. $categories = self::daoToCategories($res);
  328. if (isset($categories[self::DEFAULTCATEGORYID])) {
  329. return $categories[self::DEFAULTCATEGORYID];
  330. } else {
  331. if (FreshRSS_Context::$isCli) {
  332. fwrite(STDERR, 'FreshRSS database error: Default category not found!' . "\n");
  333. }
  334. Minz_Log::error('FreshRSS database error: Default category not found!');
  335. return null;
  336. }
  337. }
  338. public function checkDefault(): int|bool {
  339. $def_cat = $this->searchById(self::DEFAULTCATEGORYID);
  340. if ($def_cat == null) {
  341. $cat = new FreshRSS_Category(_t('gen.short.default_category'), self::DEFAULTCATEGORYID);
  342. $sql = 'INSERT INTO `_category`(id, name) VALUES(?, ?)';
  343. $stm = $this->pdo->prepare($sql);
  344. $values = [
  345. $cat->id(),
  346. $cat->name(),
  347. ];
  348. if ($stm !== false && $stm->execute($values)) {
  349. $catId = $this->pdo->lastInsertId('`_category_id_seq`');
  350. $this->sqlResetSequence();
  351. return $catId === false ? false : (int)$catId;
  352. } else {
  353. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  354. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  355. return false;
  356. }
  357. }
  358. return true;
  359. }
  360. public function count(): int {
  361. $sql = 'SELECT COUNT(*) AS count FROM `_category`';
  362. $res = $this->fetchColumn($sql, 0);
  363. return isset($res[0]) ? (int)$res[0] : -1;
  364. }
  365. public function countFeed(int $id): int {
  366. $sql = 'SELECT COUNT(*) AS count FROM `_feed` WHERE category=:id';
  367. $res = $this->fetchColumn($sql, 0, [':id' => $id]);
  368. return isset($res[0]) ? (int)$res[0] : -1;
  369. }
  370. public function countNotRead(int $id): int {
  371. $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';
  372. $res = $this->fetchColumn($sql, 0, [':id' => $id]);
  373. return isset($res[0]) ? (int)$res[0] : -1;
  374. }
  375. /** @return list<string> */
  376. public function listTitles(int $id, int $limit = 0): array {
  377. $sql = <<<'SQL'
  378. SELECT e.title FROM `_entry` e
  379. INNER JOIN `_feed` f ON e.id_feed=f.id
  380. WHERE f.category=:id_category
  381. ORDER BY e.id DESC
  382. SQL;
  383. $sql .= ($limit < 1 ? '' : ' LIMIT ' . intval($limit));
  384. $res = $this->fetchColumn($sql, 0, [':id_category' => $id]) ?? [];
  385. /** @var list<string> $res */
  386. return $res;
  387. }
  388. /**
  389. * @param array<array{c_name:string,c_id:int,c_kind:int,c_last_update:int,c_error:int|bool,c_attributes?:string,
  390. * id?:int,name?:string,url?:string,kind?:int,website?:string,priority?:int,
  391. * error?:int|bool,attributes?:string,cache_nbEntries?:int,cache_nbUnreads?:int,ttl?:int}> $listDAO
  392. * @return array<int,FreshRSS_Category> where the key is the category ID
  393. */
  394. private static function daoToCategoriesPrepopulated(array $listDAO): array {
  395. $list = [];
  396. $previousLine = [];
  397. $feedsDao = [];
  398. $feedDao = FreshRSS_Factory::createFeedDao();
  399. foreach ($listDAO as $line) {
  400. if (!empty($previousLine['c_id']) && $line['c_id'] !== $previousLine['c_id']) {
  401. // End of the current category, we add it to the $list
  402. $cat = new FreshRSS_Category(
  403. $previousLine['c_name'],
  404. $previousLine['c_id'],
  405. $feedDao::daoToFeeds($feedsDao, $previousLine['c_id'])
  406. );
  407. $cat->_kind($previousLine['c_kind']);
  408. $cat->_attributes($previousLine['c_attributes'] ?? '[]');
  409. $list[$cat->id()] = $cat;
  410. $feedsDao = []; //Prepare for next category
  411. }
  412. $previousLine = $line;
  413. $feedsDao[] = $line;
  414. }
  415. // add the last category
  416. if ($previousLine != null) {
  417. $cat = new FreshRSS_Category(
  418. $previousLine['c_name'],
  419. $previousLine['c_id'],
  420. $feedDao::daoToFeeds($feedsDao, $previousLine['c_id'])
  421. );
  422. $cat->_kind($previousLine['c_kind']);
  423. $cat->_lastUpdate($previousLine['c_last_update'] ?? 0);
  424. $cat->_error($previousLine['c_error'] ?? 0);
  425. $cat->_attributes($previousLine['c_attributes'] ?? []);
  426. $list[$cat->id()] = $cat;
  427. }
  428. return $list;
  429. }
  430. /**
  431. * @param array<array{name:string,id:int,kind:int,lastUpdate?:int,error?:int|bool,attributes?:string}> $listDAO
  432. * @return array<int,FreshRSS_Category> where the key is the category ID
  433. */
  434. private static function daoToCategories(array $listDAO): array {
  435. $list = [];
  436. foreach ($listDAO as $dao) {
  437. $cat = new FreshRSS_Category(
  438. $dao['name'],
  439. $dao['id']
  440. );
  441. $cat->_kind($dao['kind']);
  442. $cat->_lastUpdate($dao['lastUpdate'] ?? 0);
  443. $cat->_error($dao['error'] ?? 0);
  444. $cat->_attributes($dao['attributes'] ?? '');
  445. $list[$cat->id()] = $cat;
  446. }
  447. return $list;
  448. }
  449. }