CategoryDAO.php 15 KB

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