CategoryDAO.php 16 KB

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