FeedDAO.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_FeedDAO extends Minz_ModelPdo {
  4. public function sqlResetSequence(): bool {
  5. return true; // Nothing to do for MySQL
  6. }
  7. protected function addColumn(string $name): bool {
  8. if ($this->pdo->inTransaction()) {
  9. $this->pdo->commit();
  10. }
  11. Minz_Log::warning(__METHOD__ . ': ' . $name);
  12. try {
  13. if ($name === 'kind') { //v1.20.0
  14. return $this->pdo->exec('ALTER TABLE `_feed` ADD COLUMN kind SMALLINT DEFAULT 0') !== false;
  15. }
  16. } catch (Exception $e) {
  17. Minz_Log::error(__METHOD__ . ' error: ' . $e->getMessage());
  18. }
  19. return false;
  20. }
  21. /** @param array{0:string,1:int,2:string} $errorInfo */
  22. public function autoUpdateDb(array $errorInfo): bool {
  23. if (isset($errorInfo[0])) {
  24. if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_FIELD_ERROR || $errorInfo[0] === FreshRSS_DatabaseDAOPGSQL::UNDEFINED_COLUMN) {
  25. $errorLines = explode("\n", $errorInfo[2], 2); // The relevant column name is on the first line, other lines are noise
  26. foreach (['kind'] as $column) {
  27. if (str_contains($errorLines[0], $column)) {
  28. return $this->addColumn($column);
  29. }
  30. }
  31. }
  32. }
  33. return false;
  34. }
  35. /**
  36. * @param array{id?:int,url:string,kind:int,category:int,name:string,website:string,description:string,lastUpdate:int,priority?:int,
  37. * pathEntries?:string,httpAuth?:string,error:int|bool,ttl?:int,attributes?:string|array<string|mixed>} $valuesTmp
  38. */
  39. public function addFeed(array $valuesTmp): int|false {
  40. if (empty($valuesTmp['id'])) { // Auto-generated ID
  41. $sql = <<<'SQL'
  42. INSERT INTO `_feed` (url, kind, category, name, website, description, `lastUpdate`, priority, `pathEntries`, `httpAuth`, error, ttl, attributes)
  43. VALUES (:url, :kind, :category, :name, :website, :description, :last_update, :priority, :path_entries, :http_auth, :error, :ttl, :attributes)
  44. SQL;
  45. } else {
  46. $sql = <<<'SQL'
  47. INSERT INTO `_feed` (id, url, kind, category, name, website, description, `lastUpdate`, priority, `pathEntries`, `httpAuth`, error, ttl, attributes)
  48. VALUES (:id, :url, :kind, :category, :name, :website, :description, :last_update, :priority, :path_entries, :http_auth, :error, :ttl, :attributes)
  49. SQL;
  50. }
  51. $stm = $this->pdo->prepare($sql);
  52. if (!isset($valuesTmp['pathEntries'])) {
  53. $valuesTmp['pathEntries'] = '';
  54. }
  55. if (!isset($valuesTmp['attributes'])) {
  56. $valuesTmp['attributes'] = [];
  57. }
  58. $ok = $stm !== false;
  59. if ($ok) {
  60. if (!empty($valuesTmp['id'])) {
  61. $ok &= $stm->bindValue(':id', (int)$valuesTmp['id'], PDO::PARAM_INT);
  62. }
  63. $ok &= $stm->bindValue(':url', safe_ascii($valuesTmp['url']), PDO::PARAM_STR);
  64. $ok &= $stm->bindValue(':kind', $valuesTmp['kind'] ?? FreshRSS_Feed::KIND_RSS, PDO::PARAM_INT);
  65. $ok &= $stm->bindValue(':category', $valuesTmp['category'], PDO::PARAM_INT);
  66. $ok &= $stm->bindValue(':name', mb_strcut(trim($valuesTmp['name']), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8'), PDO::PARAM_STR);
  67. $ok &= $stm->bindValue(':website', safe_ascii($valuesTmp['website']), PDO::PARAM_STR);
  68. $ok &= $stm->bindValue(':description', FreshRSS_SimplePieCustom::sanitizeHTML($valuesTmp['description'], ''), PDO::PARAM_STR);
  69. $ok &= $stm->bindValue(':last_update', $valuesTmp['lastUpdate'], PDO::PARAM_INT);
  70. $ok &= $stm->bindValue(':priority', isset($valuesTmp['priority']) ? (int)$valuesTmp['priority'] : FreshRSS_Feed::PRIORITY_MAIN_STREAM, PDO::PARAM_INT);
  71. $ok &= $stm->bindValue(':path_entries', mb_strcut($valuesTmp['pathEntries'], 0, 4096, 'UTF-8'), PDO::PARAM_STR);
  72. $ok &= $stm->bindValue(':http_auth', base64_encode($valuesTmp['httpAuth'] ?? ''), PDO::PARAM_STR);
  73. $ok &= $stm->bindValue(':error', isset($valuesTmp['error']) ? (int)$valuesTmp['error'] : 0, PDO::PARAM_INT);
  74. $ok &= $stm->bindValue(':ttl', isset($valuesTmp['ttl']) ? (int)$valuesTmp['ttl'] : FreshRSS_Feed::TTL_DEFAULT, PDO::PARAM_INT);
  75. $ok &= $stm->bindValue(':attributes', is_string($valuesTmp['attributes']) ? $valuesTmp['attributes'] :
  76. json_encode($valuesTmp['attributes'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), PDO::PARAM_STR);
  77. }
  78. if ($ok && $stm !== false && $stm->execute()) {
  79. if (empty($valuesTmp['id'])) {
  80. // Auto-generated ID
  81. $feedId = $this->pdo->lastInsertId('`_feed_id_seq`');
  82. return $feedId === false ? false : (int)$feedId;
  83. }
  84. $this->sqlResetSequence();
  85. return $valuesTmp['id'];
  86. } else {
  87. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  88. /** @var array{0:string,1:int,2:string} $info */
  89. if ($this->autoUpdateDb($info)) {
  90. return $this->addFeed($valuesTmp);
  91. }
  92. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  93. return false;
  94. }
  95. }
  96. public function addFeedObject(FreshRSS_Feed $feed): int|false {
  97. // Add feed only if we don’t find it in DB
  98. $feed_search = $this->searchByUrl($feed->url());
  99. if ($feed_search === null) {
  100. $values = [
  101. 'id' => $feed->id(),
  102. 'url' => $feed->url(),
  103. 'kind' => $feed->kind(),
  104. 'category' => $feed->categoryId(),
  105. 'name' => $feed->name(true),
  106. 'website' => $feed->website(),
  107. 'description' => $feed->description(),
  108. 'priority' => $feed->priority(),
  109. 'lastUpdate' => 0,
  110. 'error' => false,
  111. 'pathEntries' => $feed->pathEntries(),
  112. 'httpAuth' => $feed->httpAuth(),
  113. 'ttl' => $feed->ttl(true),
  114. 'attributes' => $feed->attributes(),
  115. ];
  116. $id = $this->addFeed($values);
  117. if ($id) {
  118. $feed->_id($id);
  119. $feed->faviconPrepare();
  120. }
  121. return $id;
  122. } else {
  123. // The feed already exists so make sure it is not muted
  124. $feed->_ttl($feed_search->ttl());
  125. $feed->_mute(false);
  126. // Merge existing and import attributes
  127. $existingAttributes = $feed_search->attributes();
  128. $importAttributes = $feed->attributes();
  129. $mergedAttributes = array_replace_recursive($existingAttributes, $importAttributes);
  130. $mergedAttributes = array_filter($mergedAttributes, 'is_string', ARRAY_FILTER_USE_KEY);
  131. $feed->_attributes($mergedAttributes);
  132. // Update some values of the existing feed using the import
  133. $values = [
  134. 'kind' => $feed->kind(),
  135. 'name' => $feed->name(true),
  136. 'website' => $feed->website(),
  137. 'description' => $feed->description(),
  138. 'pathEntries' => $feed->pathEntries(),
  139. 'ttl' => $feed->ttl(true),
  140. 'attributes' => $feed->attributes(),
  141. ];
  142. if (!$this->updateFeed($feed_search->id(), $values)) {
  143. return false;
  144. }
  145. return $feed_search->id();
  146. }
  147. }
  148. /**
  149. * @param array{'url'?:string,'kind'?:int,'category'?:int,'name'?:string,'website'?:string,'description'?:string,'lastUpdate'?:int,'priority'?:int,
  150. * 'pathEntries'?:string,'httpAuth'?:string,'error'?:int,'ttl'?:int,'attributes'?:string|array<string,mixed>} $valuesTmp $valuesTmp
  151. */
  152. public function updateFeed(int $id, array $valuesTmp): bool {
  153. $values = [];
  154. $originalValues = $valuesTmp;
  155. if (isset($valuesTmp['name'])) {
  156. $valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8');
  157. }
  158. if (isset($valuesTmp['url'])) {
  159. $valuesTmp['url'] = safe_ascii($valuesTmp['url']);
  160. }
  161. if (isset($valuesTmp['website'])) {
  162. $valuesTmp['website'] = safe_ascii($valuesTmp['website']);
  163. }
  164. $set = '';
  165. foreach ($valuesTmp as $key => $v) {
  166. $set .= '`' . $key . '`=?, ';
  167. if ($key === 'httpAuth') {
  168. $valuesTmp[$key] = is_string($v) ? base64_encode($v) : '';
  169. } elseif ($key === 'attributes') {
  170. $valuesTmp[$key] = is_string($valuesTmp[$key]) ? $valuesTmp[$key] : json_encode($valuesTmp[$key], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  171. }
  172. }
  173. $set = substr($set, 0, -2);
  174. $sql = <<<SQL
  175. UPDATE `_feed` SET {$set} WHERE id=?
  176. SQL;
  177. $stm = $this->pdo->prepare($sql);
  178. foreach ($valuesTmp as $v) {
  179. $values[] = $v;
  180. }
  181. $values[] = $id;
  182. if ($stm !== false && $stm->execute($values)) {
  183. return true;
  184. } else {
  185. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  186. /** @var array{0:string,1:int,2:string} $info */
  187. if ($this->autoUpdateDb($info)) {
  188. return $this->updateFeed($id, $originalValues);
  189. }
  190. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info) . ' for feed ' . $id);
  191. return false;
  192. }
  193. }
  194. /**
  195. * @param non-empty-string $key
  196. * @param string|array<mixed>|bool|int|null $value
  197. */
  198. public function updateFeedAttribute(FreshRSS_Feed $feed, string $key, $value): bool {
  199. $feed->_attribute($key, $value);
  200. return $this->updateFeed(
  201. $feed->id(),
  202. ['attributes' => $feed->attributes()]
  203. );
  204. }
  205. /**
  206. * @see updateCachedValues()
  207. */
  208. public function updateLastUpdate(int $id, bool $inError = false, int $mtime = 0): int|false {
  209. $sql = <<<'SQL'
  210. UPDATE `_feed` SET `lastUpdate`=:last_update, error=:error WHERE id=:id
  211. SQL;
  212. $stm = $this->pdo->prepare($sql);
  213. if ($stm !== false &&
  214. $stm->bindValue(':last_update', $mtime <= 0 ? time() : $mtime, PDO::PARAM_INT) &&
  215. $stm->bindValue(':error', $inError ? 1 : 0, PDO::PARAM_INT) &&
  216. $stm->bindValue(':id', $id, PDO::PARAM_INT) &&
  217. $stm->execute()) {
  218. return $stm->rowCount();
  219. } else {
  220. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  221. Minz_Log::warning(__METHOD__ . ' error: ' . $sql . ' : ' . json_encode($info));
  222. return false;
  223. }
  224. }
  225. public function mute(int $id, bool $value = true): int|false {
  226. $sign = $value ? '-' : '';
  227. $sql = <<<SQL
  228. UPDATE `_feed`
  229. SET ttl = {$sign}ABS(ttl)
  230. WHERE id = :id
  231. SQL;
  232. $stm = $this->pdo->prepare($sql);
  233. if ($stm !== false &&
  234. $stm->bindValue(':id', $id, PDO::PARAM_INT) &&
  235. $stm->execute()) {
  236. return $stm->rowCount();
  237. }
  238. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  239. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  240. return false;
  241. }
  242. public function changeCategory(int $idOldCat, int $idNewCat): int|false {
  243. $catDAO = FreshRSS_Factory::createCategoryDao();
  244. $newCat = $catDAO->searchById($idNewCat);
  245. if ($newCat === null) {
  246. $newCat = $catDAO->getDefault();
  247. }
  248. if ($newCat === null) {
  249. return false;
  250. }
  251. $sql = <<<'SQL'
  252. UPDATE `_feed` SET category=:new_category WHERE category=:old_category
  253. SQL;
  254. $stm = $this->pdo->prepare($sql);
  255. if ($stm !== false &&
  256. $stm->bindValue(':new_category', $newCat->id(), PDO::PARAM_INT) &&
  257. $stm->bindValue(':old_category', $idOldCat, PDO::PARAM_INT) &&
  258. $stm->execute()) {
  259. return $stm->rowCount();
  260. } else {
  261. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  262. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  263. return false;
  264. }
  265. }
  266. public function deleteFeed(int $id): int|false {
  267. $sql = <<<'SQL'
  268. DELETE FROM `_feed` WHERE id=:id
  269. SQL;
  270. $stm = $this->pdo->prepare($sql);
  271. if ($stm !== false &&
  272. $stm->bindValue(':id', $id, PDO::PARAM_INT) &&
  273. $stm->execute()) {
  274. return $stm->rowCount();
  275. } else {
  276. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  277. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  278. return false;
  279. }
  280. }
  281. /**
  282. * @param bool|null $muted to include only muted feeds
  283. * @param bool|null $errored to include only errored feeds
  284. */
  285. public function deleteFeedByCategory(int $id, ?bool $muted = null, ?bool $errored = null): int|false {
  286. $sql = <<<'SQL'
  287. DELETE FROM `_feed` WHERE category=:category
  288. SQL;
  289. if ($muted) {
  290. $sql .= "\n" . <<<'SQL'
  291. AND ttl < 0
  292. SQL;
  293. }
  294. if ($errored) {
  295. $sql .= "\n" . <<<'SQL'
  296. AND error <> 0
  297. SQL;
  298. }
  299. $stm = $this->pdo->prepare($sql);
  300. if ($stm !== false &&
  301. $stm->bindValue(':category', $id, PDO::PARAM_INT) &&
  302. $stm->execute()) {
  303. return $stm->rowCount();
  304. } else {
  305. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  306. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  307. return false;
  308. }
  309. }
  310. /** @return Traversable<array{id:int,url:string,kind:int,category:int,name:string,website:string,description:string,lastUpdate:int,priority?:int,
  311. * pathEntries?:string,httpAuth?:string,error:int|bool,ttl?:int,attributes?:string}> */
  312. public function selectAll(): Traversable {
  313. $sql = <<<'SQL'
  314. SELECT id, url, kind, category, name, website, description, `lastUpdate`,
  315. priority, `pathEntries`, `httpAuth`, error, ttl, attributes
  316. FROM `_feed`
  317. SQL;
  318. $stm = $this->pdo->query($sql);
  319. if ($stm !== false) {
  320. while (is_array($row = $stm->fetch(PDO::FETCH_ASSOC))) {
  321. /** @var array{id:int,url:string,kind:int,category:int,name:string,website:string,description:string,lastUpdate:int,priority?:int,
  322. * pathEntries?:string,httpAuth?:string,error:int,ttl?:int,attributes?:string} $row */
  323. yield $row;
  324. }
  325. } else {
  326. $info = $this->pdo->errorInfo();
  327. /** @var array{0:string,1:int,2:string} $info */
  328. if ($this->autoUpdateDb($info)) {
  329. yield from $this->selectAll();
  330. } else {
  331. Minz_Log::error(__METHOD__ . ' error: ' . json_encode($info));
  332. }
  333. }
  334. }
  335. public function searchById(int $id): ?FreshRSS_Feed {
  336. $sql = <<<'SQL'
  337. SELECT * FROM `_feed` WHERE id=:id
  338. SQL;
  339. $res = $this->fetchAssoc($sql, [':id' => $id]);
  340. if (!is_array($res)) {
  341. return null;
  342. }
  343. /** @var list<array{id:int,url:string,kind:int,category:int,name:string,website:string,description:string,lastUpdate:int,priority:int,
  344. * pathEntries:string,httpAuth:string,error:int,ttl:int,attributes?:string,cache_nbUnreads:int,cache_nbEntries:int}> $res */
  345. $feeds = self::daoToFeeds($res);
  346. return $feeds[$id] ?? null;
  347. }
  348. public function searchByUrl(string $url): ?FreshRSS_Feed {
  349. $sql = <<<'SQL'
  350. SELECT * FROM `_feed` WHERE url=:url
  351. SQL;
  352. $res = $this->fetchAssoc($sql, [':url' => $url]);
  353. if (!is_array($res)) {
  354. return null;
  355. }
  356. /** @var list<array{id:int,url:string,kind:int,category:int,name:string,website:string,description:string,lastUpdate:int,priority:int,
  357. * pathEntries:string,httpAuth:string,error:int,ttl:int,attributes?:string,cache_nbUnreads:int,cache_nbEntries:int}> $res */
  358. return empty($res[0]) ? null : (current(self::daoToFeeds($res)) ?: null);
  359. }
  360. /** @return list<int> */
  361. public function listFeedsIds(): array {
  362. $sql = <<<'SQL'
  363. SELECT id FROM `_feed`
  364. SQL;
  365. /** @var list<int> $res */
  366. $res = $this->fetchColumn($sql, 0) ?? [];
  367. return $res;
  368. }
  369. /** @return array<int,FreshRSS_Feed> where the key is the feed ID */
  370. public function listFeeds(): array {
  371. $sql = <<<'SQL'
  372. SELECT * FROM `_feed` ORDER BY name
  373. SQL;
  374. $res = $this->fetchAssoc($sql);
  375. if (!is_array($res)) {
  376. return [];
  377. }
  378. /** @var list<array{id:int,url:string,kind:int,category:int,name:string,website:string,description:string,lastUpdate:int,priority:int,
  379. * pathEntries:string,httpAuth:string,error:int,ttl:int,attributes?:string,cache_nbUnreads:int,cache_nbEntries:int}> $res */
  380. return self::daoToFeeds($res);
  381. }
  382. /** @return array<string,string> */
  383. public function listFeedsNewestItemUsec(?int $id_feed = null): array {
  384. $sql = <<<'SQL'
  385. SELECT id_feed, MAX(id) as newest_item_us FROM `_entry`
  386. SQL;
  387. if ($id_feed === null) {
  388. $sql .= "\n" . <<<'SQL'
  389. GROUP BY id_feed
  390. SQL;
  391. } else {
  392. $sql .= "\n" . <<<SQL
  393. WHERE id_feed=$id_feed
  394. SQL;
  395. }
  396. $res = $this->fetchAssoc($sql);
  397. /** @var list<array{id_feed:int,newest_item_us:string}>|null $res */
  398. if ($res === null) {
  399. return [];
  400. }
  401. $newestItemUsec = [];
  402. foreach ($res as $line) {
  403. $newestItemUsec['f_' . $line['id_feed']] = $line['newest_item_us'];
  404. }
  405. return $newestItemUsec;
  406. }
  407. /**
  408. * @param int $defaultCacheDuration Use -1 to return all feeds, without filtering them by TTL.
  409. * @return array<int,FreshRSS_Feed> where the key is the feed ID
  410. */
  411. public function listFeedsOrderUpdate(int $defaultCacheDuration = 3600, int $limit = 0): array {
  412. $ttlDefault = FreshRSS_Feed::TTL_DEFAULT;
  413. $refreshThreshold = time() + 60;
  414. $sql = <<<SQL
  415. SELECT * FROM `_feed`
  416. SQL;
  417. if ($defaultCacheDuration >= 0) {
  418. $sql .= "\n" . <<<SQL
  419. WHERE ttl >= {$ttlDefault}
  420. AND `lastUpdate` < ({$refreshThreshold}-(CASE WHEN ttl={$ttlDefault} THEN {$defaultCacheDuration} ELSE ttl END))
  421. SQL;
  422. }
  423. $sql .= "\n" . <<<SQL
  424. ORDER BY `lastUpdate`
  425. SQL;
  426. if ($limit > 0) {
  427. $sql .= "\n" . <<<SQL
  428. LIMIT {$limit}
  429. SQL;
  430. }
  431. $stm = $this->pdo->query($sql);
  432. if ($stm !== false && ($res = $stm->fetchAll(PDO::FETCH_ASSOC)) !== false) {
  433. /** @var list<array{id?:int,url?:string,kind?:int,category?:int,name?:string,website?:string,description?:string,lastUpdate?:int,priority?:int,
  434. * pathEntries?:string,httpAuth?:string,error?:int,ttl?:int,attributes?:string,cache_nbUnreads?:int,cache_nbEntries?:int}> $res */
  435. return self::daoToFeeds($res);
  436. } else {
  437. $info = $this->pdo->errorInfo();
  438. /** @var array{0:string,1:int,2:string} $info */
  439. if ($this->autoUpdateDb($info)) {
  440. return $this->listFeedsOrderUpdate($defaultCacheDuration, $limit);
  441. }
  442. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  443. return [];
  444. }
  445. }
  446. /** @return list<string> */
  447. public function listTitles(int $id, int $limit = 0): array {
  448. $sql = <<<SQL
  449. SELECT title FROM `_entry` WHERE id_feed=:id_feed ORDER BY id DESC
  450. SQL;
  451. if ($limit > 0) {
  452. $sql .= "\n" . <<<SQL
  453. LIMIT {$limit}
  454. SQL;
  455. }
  456. $res = $this->fetchColumn($sql, 0, [':id_feed' => $id]) ?? [];
  457. /** @var list<string> $res */
  458. return $res;
  459. }
  460. /**
  461. * @param bool|null $muted to include only muted feeds
  462. * @param bool|null $errored to include only errored feeds
  463. * @return array<int,FreshRSS_Feed> where the key is the feed ID
  464. */
  465. public function listByCategory(int $cat, ?bool $muted = null, ?bool $errored = null): array {
  466. $sql = <<<'SQL'
  467. SELECT * FROM `_feed` WHERE category=:category
  468. SQL;
  469. if ($muted) {
  470. $sql .= "\n" . <<<SQL
  471. AND ttl < 0
  472. SQL;
  473. }
  474. if ($errored) {
  475. $sql .= "\n" . <<<SQL
  476. AND error <> 0
  477. SQL;
  478. }
  479. $res = $this->fetchAssoc($sql, [':category' => $cat]);
  480. if (!is_array($res)) {
  481. return [];
  482. }
  483. /** @var list<array{id:int,url:string,kind:int,category:int,name:string,website:string,description:string,lastUpdate:int,priority:int,
  484. * pathEntries:string,httpAuth:string,error:int,ttl:int,attributes?:string,cache_nbUnreads:int,cache_nbEntries:int}> $res */
  485. $feeds = self::daoToFeeds($res);
  486. uasort($feeds, static fn(FreshRSS_Feed $a, FreshRSS_Feed $b) => strnatcasecmp($a->name(), $b->name()));
  487. return $feeds;
  488. }
  489. public function countEntries(int $id): int {
  490. $sql = <<<'SQL'
  491. SELECT COUNT(*) AS count FROM `_entry` WHERE id_feed=:id_feed
  492. SQL;
  493. return $this->fetchInt($sql, ['id_feed' => $id]) ?? -1;
  494. }
  495. public function countNotRead(int $id): int {
  496. $sql = <<<'SQL'
  497. SELECT COUNT(*) AS count FROM `_entry` WHERE id_feed=:id_feed AND is_read=0
  498. SQL;
  499. return $this->fetchInt($sql, ['id_feed' => $id]) ?? -1;
  500. }
  501. /**
  502. * Update cached values for selected feeds, or all feeds if no feed ID is provided.
  503. */
  504. public function updateCachedValues(int ...$feedIds): int|false {
  505. if (empty($feedIds)) {
  506. $whereFeedIds = 'true';
  507. $whereEntryIdFeeds = 'true';
  508. } else {
  509. $whereFeedIds = 'id IN (' . str_repeat('?,', count($feedIds) - 1) . '?)';
  510. $whereEntryIdFeeds = 'id_feed IN (' . str_repeat('?,', count($feedIds) - 1) . '?)';
  511. }
  512. $sql = <<<SQL
  513. UPDATE `_feed`
  514. LEFT JOIN (
  515. SELECT
  516. id_feed,
  517. COUNT(*) AS total_entries,
  518. SUM(CASE WHEN is_read = 0 THEN 1 ELSE 0 END) AS unread_entries
  519. FROM `_entry`
  520. WHERE $whereEntryIdFeeds
  521. GROUP BY id_feed
  522. ) AS entry_counts ON entry_counts.id_feed = `_feed`.id
  523. SET `cache_nbEntries` = COALESCE(entry_counts.total_entries, 0),
  524. `cache_nbUnreads` = COALESCE(entry_counts.unread_entries, 0)
  525. WHERE $whereFeedIds
  526. SQL;
  527. $stm = $this->pdo->prepare($sql);
  528. if ($stm !== false && $stm->execute(array_merge($feedIds, $feedIds))) {
  529. return $stm->rowCount();
  530. } else {
  531. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  532. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  533. return false;
  534. }
  535. }
  536. /**
  537. * Remember to call updateCachedValues() after calling this function
  538. * @return int|false number of lines affected or false in case of error
  539. */
  540. public function markAsReadMaxUnread(int $id, int $n): int|false {
  541. //Double SELECT for MySQL workaround ERROR 1093 (HY000)
  542. $sql = <<<'SQL'
  543. UPDATE `_entry` SET is_read=1
  544. WHERE id_feed=:id_feed1 AND is_read=0 AND id <= (SELECT e3.id FROM (
  545. SELECT e2.id FROM `_entry` e2
  546. WHERE e2.id_feed=:id_feed2 AND e2.is_read=0
  547. ORDER BY e2.id DESC
  548. LIMIT 1
  549. OFFSET :limit) e3)
  550. SQL;
  551. if (($stm = $this->pdo->prepare($sql)) !== false &&
  552. $stm->bindValue(':id_feed1', $id, PDO::PARAM_INT) &&
  553. $stm->bindValue(':id_feed2', $id, PDO::PARAM_INT) &&
  554. $stm->bindValue(':limit', $n, PDO::PARAM_INT) &&
  555. $stm->execute()) {
  556. return $stm->rowCount();
  557. } else {
  558. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  559. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  560. return false;
  561. }
  562. }
  563. /**
  564. * Remember to call updateCachedValues() after calling this function
  565. * @return int|false number of lines affected or false in case of error
  566. */
  567. public function markAsReadNotSeen(int $id, int $minLastSeen): int|false {
  568. $sql = <<<'SQL'
  569. UPDATE `_entry` SET is_read=1
  570. WHERE id_feed=:id_feed AND is_read=0 AND (`lastSeen` + 10 < :min_last_seen)
  571. SQL;
  572. if (($stm = $this->pdo->prepare($sql)) !== false &&
  573. $stm->bindValue(':id_feed', $id, PDO::PARAM_INT) &&
  574. $stm->bindValue(':min_last_seen', $minLastSeen, PDO::PARAM_INT) &&
  575. $stm->execute()) {
  576. return $stm->rowCount();
  577. } else {
  578. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  579. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  580. return false;
  581. }
  582. }
  583. public function truncate(int $id): int|false {
  584. $sql = <<<'SQL'
  585. DELETE FROM `_entry` WHERE id_feed=:id
  586. SQL;
  587. $stm = $this->pdo->prepare($sql);
  588. $this->pdo->beginTransaction();
  589. if (!($stm !== false &&
  590. $stm->bindValue(':id', $id, PDO::PARAM_INT) &&
  591. $stm->execute())) {
  592. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  593. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  594. $this->pdo->rollBack();
  595. return false;
  596. }
  597. $affected = $stm->rowCount();
  598. $sql = <<<'SQL'
  599. UPDATE `_feed` SET `cache_nbEntries`=0, `cache_nbUnreads`=0, `lastUpdate`=0 WHERE id=:id
  600. SQL;
  601. $stm = $this->pdo->prepare($sql);
  602. if (!($stm !== false &&
  603. $stm->bindValue(':id', $id, PDO::PARAM_INT) &&
  604. $stm->execute())) {
  605. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  606. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  607. $this->pdo->rollBack();
  608. return false;
  609. }
  610. $this->pdo->commit();
  611. return $affected;
  612. }
  613. public function purge(): bool {
  614. $sql = <<<'SQL'
  615. DELETE FROM `_entry`
  616. SQL;
  617. $stm = $this->pdo->prepare($sql);
  618. $this->pdo->beginTransaction();
  619. if ($stm === false || !$stm->execute()) {
  620. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  621. Minz_Log::error('SQL error ' . __METHOD__ . ' A ' . json_encode($info));
  622. $this->pdo->rollBack();
  623. return false;
  624. }
  625. $sql = <<<'SQL'
  626. UPDATE `_feed` SET `cache_nbEntries` = 0, `cache_nbUnreads` = 0
  627. SQL;
  628. $stm = $this->pdo->prepare($sql);
  629. if ($stm === false || !$stm->execute()) {
  630. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  631. Minz_Log::error('SQL error ' . __METHOD__ . ' B ' . json_encode($info));
  632. $this->pdo->rollBack();
  633. return false;
  634. }
  635. return $this->pdo->commit();
  636. }
  637. /**
  638. * @param array<array{id?:int,url?:string,kind?:int,category?:int,name?:string,website?:string,description?:string,lastUpdate?:int,priority?:int,
  639. * pathEntries?:string,httpAuth?:string,error?:int|bool,ttl?:int,attributes?:string,cache_nbUnreads?:int,cache_nbEntries?:int}> $listDAO
  640. * @return array<int,FreshRSS_Feed> where the key is the feed ID
  641. */
  642. public static function daoToFeeds(array $listDAO, ?int $catID = null): array {
  643. $list = [];
  644. foreach ($listDAO as $dao) {
  645. if (!is_string($dao['name'] ?? null)) {
  646. continue;
  647. }
  648. if ($catID === null) {
  649. $category = is_numeric($dao['category'] ?? null) ? (int)$dao['category'] : 0;
  650. } else {
  651. $category = $catID;
  652. }
  653. $myFeed = new FreshRSS_Feed($dao['url'] ?? '', false);
  654. $myFeed->_kind($dao['kind'] ?? FreshRSS_Feed::KIND_RSS);
  655. $myFeed->_categoryId($category);
  656. $myFeed->_name($dao['name']);
  657. $myFeed->_website($dao['website'] ?? '', false);
  658. $myFeed->_description($dao['description'] ?? '');
  659. $myFeed->_lastUpdate($dao['lastUpdate'] ?? 0);
  660. $myFeed->_priority($dao['priority'] ?? 10);
  661. $myFeed->_pathEntries($dao['pathEntries'] ?? '');
  662. $myFeed->_httpAuth(base64_decode($dao['httpAuth'] ?? '', true) ?: '');
  663. $myFeed->_error($dao['error'] ?? 0);
  664. $myFeed->_ttl($dao['ttl'] ?? FreshRSS_Feed::TTL_DEFAULT);
  665. $myFeed->_attributes($dao['attributes'] ?? '');
  666. $myFeed->_nbNotRead($dao['cache_nbUnreads'] ?? -1);
  667. $myFeed->_nbEntries($dao['cache_nbEntries'] ?? -1);
  668. if (isset($dao['id'])) {
  669. $myFeed->_id($dao['id']);
  670. }
  671. $list[$myFeed->id()] = $myFeed;
  672. }
  673. return $list;
  674. }
  675. public function count(): int {
  676. $sql = <<<'SQL'
  677. SELECT COUNT(e.id) AS count FROM `_feed` e
  678. SQL;
  679. return $this->fetchInt($sql) ?? -1;
  680. }
  681. }