FeedDAO.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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, int $mtime = 0): int|false {
  209. $sql = <<<'SQL'
  210. UPDATE `_feed` SET `lastUpdate`=:last_update, error=0 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(':id', $id, PDO::PARAM_INT) &&
  216. $stm->execute()) {
  217. return $stm->rowCount();
  218. } else {
  219. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  220. Minz_Log::warning(__METHOD__ . ' error: ' . $sql . ' : ' . json_encode($info));
  221. return false;
  222. }
  223. }
  224. public function updateLastError(int $id, ?int $mtime = null): int|false {
  225. $sql = <<<'SQL'
  226. UPDATE `_feed` SET error=:last_update WHERE id=:id
  227. SQL;
  228. $stm = $this->pdo->prepare($sql);
  229. if ($stm !== false &&
  230. $stm->bindValue(':last_update', $mtime === null || $mtime < 0 ? time() : $mtime, PDO::PARAM_INT) &&
  231. $stm->bindValue(':id', $id, PDO::PARAM_INT) &&
  232. $stm->execute()) {
  233. return $stm->rowCount();
  234. } else {
  235. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  236. Minz_Log::warning(__METHOD__ . ' error: ' . $sql . ' : ' . json_encode($info));
  237. return false;
  238. }
  239. }
  240. public function mute(int $id, bool $value = true): int|false {
  241. $sign = $value ? '-' : '';
  242. $sql = <<<SQL
  243. UPDATE `_feed`
  244. SET ttl = {$sign}ABS(ttl)
  245. WHERE id = :id
  246. SQL;
  247. $stm = $this->pdo->prepare($sql);
  248. if ($stm !== false &&
  249. $stm->bindValue(':id', $id, PDO::PARAM_INT) &&
  250. $stm->execute()) {
  251. return $stm->rowCount();
  252. }
  253. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  254. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  255. return false;
  256. }
  257. public function changeCategory(int $idOldCat, int $idNewCat): int|false {
  258. $catDAO = FreshRSS_Factory::createCategoryDao();
  259. $newCat = $catDAO->searchById($idNewCat);
  260. if ($newCat === null) {
  261. $newCat = $catDAO->getDefault();
  262. }
  263. if ($newCat === null) {
  264. return false;
  265. }
  266. $sql = <<<'SQL'
  267. UPDATE `_feed` SET category=:new_category WHERE category=:old_category
  268. SQL;
  269. $stm = $this->pdo->prepare($sql);
  270. if ($stm !== false &&
  271. $stm->bindValue(':new_category', $newCat->id(), PDO::PARAM_INT) &&
  272. $stm->bindValue(':old_category', $idOldCat, 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. public function deleteFeed(int $id): int|false {
  282. $sql = <<<'SQL'
  283. DELETE FROM `_feed` WHERE id=:id
  284. SQL;
  285. $stm = $this->pdo->prepare($sql);
  286. if ($stm !== false &&
  287. $stm->bindValue(':id', $id, PDO::PARAM_INT) &&
  288. $stm->execute()) {
  289. return $stm->rowCount();
  290. } else {
  291. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  292. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  293. return false;
  294. }
  295. }
  296. /**
  297. * @param bool|null $muted to include only muted feeds
  298. * @param bool|null $errored to include only errored feeds
  299. */
  300. public function deleteFeedByCategory(int $id, ?bool $muted = null, ?bool $errored = null): int|false {
  301. $sql = <<<'SQL'
  302. DELETE FROM `_feed` WHERE category=:category
  303. SQL;
  304. if ($muted) {
  305. $sql .= "\n" . <<<'SQL'
  306. AND ttl < 0
  307. SQL;
  308. }
  309. if ($errored) {
  310. $sql .= "\n" . <<<'SQL'
  311. AND error <> 0
  312. SQL;
  313. }
  314. $stm = $this->pdo->prepare($sql);
  315. if ($stm !== false &&
  316. $stm->bindValue(':category', $id, PDO::PARAM_INT) &&
  317. $stm->execute()) {
  318. return $stm->rowCount();
  319. } else {
  320. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  321. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  322. return false;
  323. }
  324. }
  325. /** @return Traversable<array{id:int,url:string,kind:int,category:int,name:string,website:string,description:string,lastUpdate:int,priority?:int,
  326. * pathEntries?:string,httpAuth?:string,error:int|bool,ttl?:int,attributes?:string}> */
  327. public function selectAll(): Traversable {
  328. $sql = <<<'SQL'
  329. SELECT id, url, kind, category, name, website, description, `lastUpdate`,
  330. priority, `pathEntries`, `httpAuth`, error, ttl, attributes
  331. FROM `_feed`
  332. SQL;
  333. $stm = $this->pdo->query($sql);
  334. if ($stm !== false) {
  335. while (is_array($row = $stm->fetch(PDO::FETCH_ASSOC))) {
  336. /** @var array{id:int,url:string,kind:int,category:int,name:string,website:string,description:string,lastUpdate:int,priority?:int,
  337. * pathEntries?:string,httpAuth?:string,error:int,ttl?:int,attributes?:string} $row */
  338. yield $row;
  339. }
  340. } else {
  341. $info = $this->pdo->errorInfo();
  342. /** @var array{0:string,1:int,2:string} $info */
  343. if ($this->autoUpdateDb($info)) {
  344. yield from $this->selectAll();
  345. } else {
  346. Minz_Log::error(__METHOD__ . ' error: ' . json_encode($info));
  347. }
  348. }
  349. }
  350. public function searchById(int $id): ?FreshRSS_Feed {
  351. $sql = <<<'SQL'
  352. SELECT * FROM `_feed` WHERE id=:id
  353. SQL;
  354. $res = $this->fetchAssoc($sql, [':id' => $id]);
  355. if (!is_array($res)) {
  356. return null;
  357. }
  358. /** @var list<array{id:int,url:string,kind:int,category:int,name:string,website:string,description:string,lastUpdate:int,priority:int,
  359. * pathEntries:string,httpAuth:string,error:int,ttl:int,attributes?:string,cache_nbUnreads:int,cache_nbEntries:int}> $res */
  360. $feeds = self::daoToFeeds($res);
  361. return $feeds[$id] ?? null;
  362. }
  363. public function searchByUrl(string $url): ?FreshRSS_Feed {
  364. $sql = <<<'SQL'
  365. SELECT * FROM `_feed` WHERE url=:url
  366. SQL;
  367. $res = $this->fetchAssoc($sql, [':url' => $url]);
  368. if (!is_array($res)) {
  369. return null;
  370. }
  371. /** @var list<array{id:int,url:string,kind:int,category:int,name:string,website:string,description:string,lastUpdate:int,priority:int,
  372. * pathEntries:string,httpAuth:string,error:int,ttl:int,attributes?:string,cache_nbUnreads:int,cache_nbEntries:int}> $res */
  373. return empty($res[0]) ? null : (current(self::daoToFeeds($res)) ?: null);
  374. }
  375. /** @return list<int> */
  376. public function listFeedsIds(): array {
  377. $sql = <<<'SQL'
  378. SELECT id FROM `_feed`
  379. SQL;
  380. /** @var list<int> $res */
  381. $res = $this->fetchColumn($sql, 0) ?? [];
  382. return $res;
  383. }
  384. /** @return array<int,FreshRSS_Feed> where the key is the feed ID */
  385. public function listFeeds(): array {
  386. $sql = <<<'SQL'
  387. SELECT * FROM `_feed` ORDER BY name
  388. SQL;
  389. $res = $this->fetchAssoc($sql);
  390. if (!is_array($res)) {
  391. return [];
  392. }
  393. /** @var list<array{id:int,url:string,kind:int,category:int,name:string,website:string,description:string,lastUpdate:int,priority:int,
  394. * pathEntries:string,httpAuth:string,error:int,ttl:int,attributes?:string,cache_nbUnreads:int,cache_nbEntries:int}> $res */
  395. return self::daoToFeeds($res);
  396. }
  397. /** @return array<string,string> */
  398. public function listFeedsNewestItemUsec(?int $id_feed = null): array {
  399. $sql = <<<'SQL'
  400. SELECT id_feed, MAX(id) as newest_item_us FROM `_entry`
  401. SQL;
  402. if ($id_feed === null) {
  403. $sql .= "\n" . <<<'SQL'
  404. GROUP BY id_feed
  405. SQL;
  406. } else {
  407. $sql .= "\n" . <<<SQL
  408. WHERE id_feed=$id_feed
  409. SQL;
  410. }
  411. $res = $this->fetchAssoc($sql);
  412. /** @var list<array{id_feed:int,newest_item_us:string}>|null $res */
  413. if ($res === null) {
  414. return [];
  415. }
  416. $newestItemUsec = [];
  417. foreach ($res as $line) {
  418. $newestItemUsec['f_' . $line['id_feed']] = $line['newest_item_us'];
  419. }
  420. return $newestItemUsec;
  421. }
  422. /**
  423. * @param int $defaultCacheDuration Use -1 to return all feeds, without filtering them by TTL.
  424. * @return array<int,FreshRSS_Feed> where the key is the feed ID
  425. */
  426. public function listFeedsOrderUpdate(int $defaultCacheDuration = 3600, int $limit = 0): array {
  427. $ttlDefault = FreshRSS_Feed::TTL_DEFAULT;
  428. $refreshThreshold = time() + 60;
  429. $lastAttemptExpression = '(CASE WHEN error > `lastUpdate` THEN error ELSE `lastUpdate` END)';
  430. $sql = <<<SQL
  431. SELECT * FROM `_feed`
  432. SQL;
  433. if ($defaultCacheDuration >= 0) {
  434. $sql .= "\n" . <<<SQL
  435. WHERE ttl >= {$ttlDefault}
  436. AND {$lastAttemptExpression} < ({$refreshThreshold}-(CASE WHEN ttl={$ttlDefault} THEN {$defaultCacheDuration} ELSE ttl END))
  437. SQL;
  438. }
  439. $sql .= "\n" . <<<SQL
  440. ORDER BY {$lastAttemptExpression} ASC
  441. SQL;
  442. if ($limit > 0) {
  443. $sql .= "\n" . <<<SQL
  444. LIMIT {$limit}
  445. SQL;
  446. }
  447. $stm = $this->pdo->query($sql);
  448. if ($stm !== false && ($res = $stm->fetchAll(PDO::FETCH_ASSOC)) !== false) {
  449. /** @var list<array{id?:int,url?:string,kind?:int,category?:int,name?:string,website?:string,description?:string,lastUpdate?:int,priority?:int,
  450. * pathEntries?:string,httpAuth?:string,error?:int,ttl?:int,attributes?:string,cache_nbUnreads?:int,cache_nbEntries?:int}> $res */
  451. return self::daoToFeeds($res);
  452. } else {
  453. $info = $this->pdo->errorInfo();
  454. /** @var array{0:string,1:int,2:string} $info */
  455. if ($this->autoUpdateDb($info)) {
  456. return $this->listFeedsOrderUpdate($defaultCacheDuration, $limit);
  457. }
  458. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  459. return [];
  460. }
  461. }
  462. /** @return list<string> */
  463. public function listTitles(int $id, int $limit = 0): array {
  464. $sql = <<<SQL
  465. SELECT title FROM `_entry` WHERE id_feed=:id_feed ORDER BY id DESC
  466. SQL;
  467. if ($limit > 0) {
  468. $sql .= "\n" . <<<SQL
  469. LIMIT {$limit}
  470. SQL;
  471. }
  472. $res = $this->fetchColumn($sql, 0, [':id_feed' => $id]) ?? [];
  473. /** @var list<string> $res */
  474. return $res;
  475. }
  476. /**
  477. * @param bool|null $muted to include only muted feeds
  478. * @param bool|null $errored to include only errored feeds
  479. * @return array<int,FreshRSS_Feed> where the key is the feed ID
  480. */
  481. public function listByCategory(int $cat, ?bool $muted = null, ?bool $errored = null): array {
  482. $sql = <<<'SQL'
  483. SELECT * FROM `_feed` WHERE category=:category
  484. SQL;
  485. if ($muted) {
  486. $sql .= "\n" . <<<SQL
  487. AND ttl < 0
  488. SQL;
  489. }
  490. if ($errored) {
  491. $sql .= "\n" . <<<SQL
  492. AND error <> 0
  493. SQL;
  494. }
  495. $res = $this->fetchAssoc($sql, [':category' => $cat]);
  496. if (!is_array($res)) {
  497. return [];
  498. }
  499. /** @var list<array{id:int,url:string,kind:int,category:int,name:string,website:string,description:string,lastUpdate:int,priority:int,
  500. * pathEntries:string,httpAuth:string,error:int,ttl:int,attributes?:string,cache_nbUnreads:int,cache_nbEntries:int}> $res */
  501. $feeds = self::daoToFeeds($res);
  502. uasort($feeds, static fn(FreshRSS_Feed $a, FreshRSS_Feed $b) => strnatcasecmp($a->name(), $b->name()));
  503. return $feeds;
  504. }
  505. public function countEntries(int $id): int {
  506. $sql = <<<'SQL'
  507. SELECT COUNT(*) AS count FROM `_entry` WHERE id_feed=:id_feed
  508. SQL;
  509. return $this->fetchInt($sql, ['id_feed' => $id]) ?? -1;
  510. }
  511. public function countNotRead(int $id): int {
  512. $sql = <<<'SQL'
  513. SELECT COUNT(*) AS count FROM `_entry` WHERE id_feed=:id_feed AND is_read=0
  514. SQL;
  515. return $this->fetchInt($sql, ['id_feed' => $id]) ?? -1;
  516. }
  517. /**
  518. * Update cached values for selected feeds, or all feeds if no feed ID is provided.
  519. */
  520. public function updateCachedValues(int ...$feedIds): int|false {
  521. if (empty($feedIds)) {
  522. $whereFeedIds = 'true';
  523. $whereEntryIdFeeds = 'true';
  524. } else {
  525. $whereFeedIds = 'id IN (' . str_repeat('?,', count($feedIds) - 1) . '?)';
  526. $whereEntryIdFeeds = 'id_feed IN (' . str_repeat('?,', count($feedIds) - 1) . '?)';
  527. }
  528. $sql = <<<SQL
  529. UPDATE `_feed`
  530. LEFT JOIN (
  531. SELECT
  532. id_feed,
  533. COUNT(*) AS total_entries,
  534. SUM(CASE WHEN is_read = 0 THEN 1 ELSE 0 END) AS unread_entries
  535. FROM `_entry`
  536. WHERE $whereEntryIdFeeds
  537. GROUP BY id_feed
  538. ) AS entry_counts ON entry_counts.id_feed = `_feed`.id
  539. SET `cache_nbEntries` = COALESCE(entry_counts.total_entries, 0),
  540. `cache_nbUnreads` = COALESCE(entry_counts.unread_entries, 0)
  541. WHERE $whereFeedIds
  542. SQL;
  543. $stm = $this->pdo->prepare($sql);
  544. if ($stm !== false && $stm->execute(array_merge($feedIds, $feedIds))) {
  545. return $stm->rowCount();
  546. } else {
  547. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  548. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  549. return false;
  550. }
  551. }
  552. /**
  553. * Remember to call updateCachedValues() after calling this function
  554. * @return int|false number of lines affected or false in case of error
  555. */
  556. public function markAsReadMaxUnread(int $id, int $n): int|false {
  557. //Double SELECT for MySQL workaround ERROR 1093 (HY000)
  558. $sql = <<<'SQL'
  559. UPDATE `_entry` SET is_read=1
  560. WHERE id_feed=:id_feed1 AND is_read=0 AND id <= (SELECT e3.id FROM (
  561. SELECT e2.id FROM `_entry` e2
  562. WHERE e2.id_feed=:id_feed2 AND e2.is_read=0
  563. ORDER BY e2.id DESC
  564. LIMIT 1
  565. OFFSET :limit) e3)
  566. SQL;
  567. if (($stm = $this->pdo->prepare($sql)) !== false &&
  568. $stm->bindValue(':id_feed1', $id, PDO::PARAM_INT) &&
  569. $stm->bindValue(':id_feed2', $id, PDO::PARAM_INT) &&
  570. $stm->bindValue(':limit', $n, PDO::PARAM_INT) &&
  571. $stm->execute()) {
  572. return $stm->rowCount();
  573. } else {
  574. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  575. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  576. return false;
  577. }
  578. }
  579. /**
  580. * Remember to call updateCachedValues() after calling this function
  581. * @return int|false number of lines affected or false in case of error
  582. */
  583. public function markAsReadNotSeen(int $id, int $minLastSeen): int|false {
  584. $sql = <<<'SQL'
  585. UPDATE `_entry` SET is_read=1
  586. WHERE id_feed=:id_feed AND is_read=0 AND (`lastSeen` + 10 < :min_last_seen)
  587. SQL;
  588. if (($stm = $this->pdo->prepare($sql)) !== false &&
  589. $stm->bindValue(':id_feed', $id, PDO::PARAM_INT) &&
  590. $stm->bindValue(':min_last_seen', $minLastSeen, PDO::PARAM_INT) &&
  591. $stm->execute()) {
  592. return $stm->rowCount();
  593. } else {
  594. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  595. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  596. return false;
  597. }
  598. }
  599. public function truncate(int $id): int|false {
  600. $sql = <<<'SQL'
  601. DELETE FROM `_entry` WHERE id_feed=:id
  602. SQL;
  603. $stm = $this->pdo->prepare($sql);
  604. $this->pdo->beginTransaction();
  605. if (!($stm !== false &&
  606. $stm->bindValue(':id', $id, PDO::PARAM_INT) &&
  607. $stm->execute())) {
  608. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  609. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  610. $this->pdo->rollBack();
  611. return false;
  612. }
  613. $affected = $stm->rowCount();
  614. $sql = <<<'SQL'
  615. UPDATE `_feed` SET `cache_nbEntries`=0, `cache_nbUnreads`=0, `lastUpdate`=0 WHERE id=:id
  616. SQL;
  617. $stm = $this->pdo->prepare($sql);
  618. if (!($stm !== false &&
  619. $stm->bindValue(':id', $id, PDO::PARAM_INT) &&
  620. $stm->execute())) {
  621. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  622. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  623. $this->pdo->rollBack();
  624. return false;
  625. }
  626. $this->pdo->commit();
  627. return $affected;
  628. }
  629. public function purge(): bool {
  630. $sql = <<<'SQL'
  631. DELETE FROM `_entry`
  632. SQL;
  633. $stm = $this->pdo->prepare($sql);
  634. $this->pdo->beginTransaction();
  635. if ($stm === false || !$stm->execute()) {
  636. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  637. Minz_Log::error('SQL error ' . __METHOD__ . ' A ' . json_encode($info));
  638. $this->pdo->rollBack();
  639. return false;
  640. }
  641. $sql = <<<'SQL'
  642. UPDATE `_feed` SET `cache_nbEntries` = 0, `cache_nbUnreads` = 0
  643. SQL;
  644. $stm = $this->pdo->prepare($sql);
  645. if ($stm === false || !$stm->execute()) {
  646. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  647. Minz_Log::error('SQL error ' . __METHOD__ . ' B ' . json_encode($info));
  648. $this->pdo->rollBack();
  649. return false;
  650. }
  651. return $this->pdo->commit();
  652. }
  653. /**
  654. * @param array<array{id?:int,url?:string,kind?:int,category?:int,name?:string,website?:string,description?:string,lastUpdate?:int,priority?:int,
  655. * pathEntries?:string,httpAuth?:string,error?:int|bool,ttl?:int,attributes?:string,cache_nbUnreads?:int,cache_nbEntries?:int}> $listDAO
  656. * @return array<int,FreshRSS_Feed> where the key is the feed ID
  657. */
  658. public static function daoToFeeds(array $listDAO, ?int $catID = null): array {
  659. $list = [];
  660. foreach ($listDAO as $dao) {
  661. if (!is_string($dao['name'] ?? null)) {
  662. continue;
  663. }
  664. if ($catID === null) {
  665. $category = is_numeric($dao['category'] ?? null) ? (int)$dao['category'] : 0;
  666. } else {
  667. $category = $catID;
  668. }
  669. $myFeed = new FreshRSS_Feed($dao['url'] ?? '', false);
  670. $myFeed->_kind($dao['kind'] ?? FreshRSS_Feed::KIND_RSS);
  671. $myFeed->_categoryId($category);
  672. $myFeed->_name($dao['name']);
  673. $myFeed->_website($dao['website'] ?? '', false);
  674. $myFeed->_description($dao['description'] ?? '');
  675. $myFeed->_lastUpdate($dao['lastUpdate'] ?? 0);
  676. $myFeed->_priority($dao['priority'] ?? 10);
  677. $myFeed->_pathEntries($dao['pathEntries'] ?? '');
  678. $myFeed->_httpAuth(base64_decode($dao['httpAuth'] ?? '', true) ?: '');
  679. $myFeed->_error($dao['error'] ?? 0);
  680. $myFeed->_ttl($dao['ttl'] ?? FreshRSS_Feed::TTL_DEFAULT);
  681. $myFeed->_attributes($dao['attributes'] ?? '');
  682. $myFeed->_nbNotRead($dao['cache_nbUnreads'] ?? -1);
  683. $myFeed->_nbEntries($dao['cache_nbEntries'] ?? -1);
  684. if (isset($dao['id'])) {
  685. $myFeed->_id($dao['id']);
  686. }
  687. $list[$myFeed->id()] = $myFeed;
  688. }
  689. return $list;
  690. }
  691. public function count(): int {
  692. $sql = <<<'SQL'
  693. SELECT COUNT(e.id) AS count FROM `_feed` e
  694. SQL;
  695. return $this->fetchInt($sql) ?? -1;
  696. }
  697. }