FeedDAO.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. <?php
  2. class FreshRSS_FeedDAO extends Minz_ModelPdo {
  3. protected function addColumn(string $name) {
  4. if ($this->pdo->inTransaction()) {
  5. $this->pdo->commit();
  6. }
  7. Minz_Log::warning(__method__ . ': ' . $name);
  8. try {
  9. if ($name === 'kind') { //v1.20.0
  10. return $this->pdo->exec('ALTER TABLE `_feed` ADD COLUMN kind SMALLINT DEFAULT 0') !== false;
  11. } elseif ($name === 'attributes') { //v1.11.0
  12. return $this->pdo->exec('ALTER TABLE `_feed` ADD COLUMN attributes TEXT') !== false;
  13. }
  14. } catch (Exception $e) {
  15. Minz_Log::error(__method__ . ' error: ' . $e->getMessage());
  16. }
  17. return false;
  18. }
  19. protected function autoUpdateDb(array $errorInfo) {
  20. if (isset($errorInfo[0])) {
  21. if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_FIELD_ERROR || $errorInfo[0] === FreshRSS_DatabaseDAOPGSQL::UNDEFINED_COLUMN) {
  22. $errorLines = explode("\n", $errorInfo[2], 2); // The relevant column name is on the first line, other lines are noise
  23. foreach (['attributes', 'kind'] as $column) {
  24. if (stripos($errorLines[0], $column) !== false) {
  25. return $this->addColumn($column);
  26. }
  27. }
  28. }
  29. }
  30. return false;
  31. }
  32. /** @return int|false */
  33. public function addFeed(array $valuesTmp) {
  34. $sql = 'INSERT INTO `_feed` (url, kind, category, name, website, description, `lastUpdate`, priority, `pathEntries`, `httpAuth`, error, ttl, attributes)
  35. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
  36. $stm = $this->pdo->prepare($sql);
  37. $valuesTmp['url'] = safe_ascii($valuesTmp['url']);
  38. $valuesTmp['website'] = safe_ascii($valuesTmp['website']);
  39. if (!isset($valuesTmp['pathEntries'])) {
  40. $valuesTmp['pathEntries'] = '';
  41. }
  42. if (!isset($valuesTmp['attributes'])) {
  43. $valuesTmp['attributes'] = [];
  44. }
  45. $values = array(
  46. $valuesTmp['url'],
  47. $valuesTmp['kind'] ?? FreshRSS_Feed::KIND_RSS,
  48. $valuesTmp['category'],
  49. mb_strcut(trim($valuesTmp['name']), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8'),
  50. $valuesTmp['website'],
  51. sanitizeHTML($valuesTmp['description'], '', 1023),
  52. $valuesTmp['lastUpdate'],
  53. isset($valuesTmp['priority']) ? intval($valuesTmp['priority']) : FreshRSS_Feed::PRIORITY_MAIN_STREAM,
  54. mb_strcut($valuesTmp['pathEntries'], 0, 511, 'UTF-8'),
  55. base64_encode($valuesTmp['httpAuth']),
  56. isset($valuesTmp['error']) ? intval($valuesTmp['error']) : 0,
  57. isset($valuesTmp['ttl']) ? intval($valuesTmp['ttl']) : FreshRSS_Feed::TTL_DEFAULT,
  58. is_string($valuesTmp['attributes']) ? $valuesTmp['attributes'] : json_encode($valuesTmp['attributes'], JSON_UNESCAPED_SLASHES),
  59. );
  60. if ($stm && $stm->execute($values)) {
  61. return $this->pdo->lastInsertId('`_feed_id_seq`');
  62. } else {
  63. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  64. if ($this->autoUpdateDb($info)) {
  65. return $this->addFeed($valuesTmp);
  66. }
  67. Minz_Log::error('SQL error addFeed: ' . $info[2]);
  68. return false;
  69. }
  70. }
  71. /** @return int|false */
  72. public function addFeedObject(FreshRSS_Feed $feed) {
  73. // Add feed only if we don’t find it in DB
  74. $feed_search = $this->searchByUrl($feed->url());
  75. if (!$feed_search) {
  76. $values = array(
  77. 'id' => $feed->id(),
  78. 'url' => $feed->url(),
  79. 'kind' => $feed->kind(),
  80. 'category' => $feed->categoryId(),
  81. 'name' => $feed->name(true),
  82. 'website' => $feed->website(),
  83. 'description' => $feed->description(),
  84. 'lastUpdate' => 0,
  85. 'pathEntries' => $feed->pathEntries(),
  86. 'httpAuth' => $feed->httpAuth(),
  87. 'ttl' => $feed->ttl(true),
  88. 'attributes' => $feed->attributes(),
  89. );
  90. $id = $this->addFeed($values);
  91. if ($id) {
  92. $feed->_id($id);
  93. $feed->faviconPrepare();
  94. }
  95. return $id;
  96. } else {
  97. // The feed already exists so make sure it is not muted
  98. $feed->_ttl($feed_search->ttl());
  99. $feed->_mute(false);
  100. // Merge existing and import attributes
  101. $existingAttributes = $feed_search->attributes();
  102. $importAttributes = $feed->attributes();
  103. $feed->_attributes('', array_replace_recursive($existingAttributes, $importAttributes));
  104. // Update some values of the existing feed using the import
  105. $values = [
  106. 'kind' => $feed->kind(),
  107. 'name' => $feed->name(true),
  108. 'website' => $feed->website(),
  109. 'description' => $feed->description(),
  110. 'pathEntries' => $feed->pathEntries(),
  111. 'ttl' => $feed->ttl(true),
  112. 'attributes' => $feed->attributes(),
  113. ];
  114. if (!$this->updateFeed($feed_search->id(), $values)) {
  115. return false;
  116. }
  117. return $feed_search->id();
  118. }
  119. }
  120. /** @return int|false */
  121. public function updateFeed(int $id, array $valuesTmp) {
  122. if (isset($valuesTmp['name'])) {
  123. $valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8');
  124. }
  125. if (isset($valuesTmp['url'])) {
  126. $valuesTmp['url'] = safe_ascii($valuesTmp['url']);
  127. }
  128. if (isset($valuesTmp['website'])) {
  129. $valuesTmp['website'] = safe_ascii($valuesTmp['website']);
  130. }
  131. $set = '';
  132. foreach ($valuesTmp as $key => $v) {
  133. $set .= '`' . $key . '`=?, ';
  134. if ($key === 'httpAuth') {
  135. $valuesTmp[$key] = base64_encode($v);
  136. } elseif ($key === 'attributes') {
  137. $valuesTmp[$key] = is_string($valuesTmp[$key]) ? $valuesTmp[$key] : json_encode($valuesTmp[$key], JSON_UNESCAPED_SLASHES);
  138. }
  139. }
  140. $set = substr($set, 0, -2);
  141. $sql = 'UPDATE `_feed` SET ' . $set . ' WHERE id=?';
  142. $stm = $this->pdo->prepare($sql);
  143. foreach ($valuesTmp as $v) {
  144. $values[] = $v;
  145. }
  146. $values[] = $id;
  147. if ($stm && $stm->execute($values)) {
  148. return $stm->rowCount();
  149. } else {
  150. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  151. if ($this->autoUpdateDb($info)) {
  152. return $this->updateFeed($id, $valuesTmp);
  153. }
  154. Minz_Log::error('SQL error updateFeed: ' . $info[2] . ' for feed ' . $id);
  155. return false;
  156. }
  157. }
  158. public function updateFeedAttribute(FreshRSS_Feed $feed, string $key, $value) {
  159. $feed->_attributes($key, $value);
  160. return $this->updateFeed(
  161. $feed->id(),
  162. array('attributes' => $feed->attributes())
  163. );
  164. }
  165. /**
  166. * @see updateCachedValue()
  167. */
  168. public function updateLastUpdate(int $id, bool $inError = false, int $mtime = 0) {
  169. $sql = 'UPDATE `_feed` SET `lastUpdate`=?, error=? WHERE id=?';
  170. $values = array(
  171. $mtime <= 0 ? time() : $mtime,
  172. $inError ? 1 : 0,
  173. $id,
  174. );
  175. $stm = $this->pdo->prepare($sql);
  176. if ($stm && $stm->execute($values)) {
  177. return $stm->rowCount();
  178. } else {
  179. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  180. Minz_Log::warning(__METHOD__ . ' error: ' . $sql . ' : ' . json_encode($info));
  181. return false;
  182. }
  183. }
  184. public function mute(int $id, bool $value = true) {
  185. $sql = 'UPDATE `_feed` SET ttl=' . ($value ? '-' : '') . 'ABS(ttl) WHERE id=' . intval($id);
  186. return $this->pdo->exec($sql);
  187. }
  188. public function changeCategory(int $idOldCat, int $idNewCat) {
  189. $catDAO = FreshRSS_Factory::createCategoryDao();
  190. $newCat = $catDAO->searchById($idNewCat);
  191. if (!$newCat) {
  192. $newCat = $catDAO->getDefault();
  193. }
  194. $sql = 'UPDATE `_feed` SET category=? WHERE category=?';
  195. $stm = $this->pdo->prepare($sql);
  196. $values = array(
  197. $newCat->id(),
  198. $idOldCat
  199. );
  200. if ($stm && $stm->execute($values)) {
  201. return $stm->rowCount();
  202. } else {
  203. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  204. Minz_Log::error('SQL error changeCategory: ' . $info[2]);
  205. return false;
  206. }
  207. }
  208. /** @return int|false */
  209. public function deleteFeed(int $id) {
  210. $sql = 'DELETE FROM `_feed` WHERE id=?';
  211. $stm = $this->pdo->prepare($sql);
  212. $values = array($id);
  213. if ($stm && $stm->execute($values)) {
  214. return $stm->rowCount();
  215. } else {
  216. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  217. Minz_Log::error('SQL error deleteFeed: ' . $info[2]);
  218. return false;
  219. }
  220. }
  221. /**
  222. * @param bool|null $muted to include only muted feeds
  223. * @return int|false
  224. */
  225. public function deleteFeedByCategory(int $id, $muted = null) {
  226. $sql = 'DELETE FROM `_feed` WHERE category=?';
  227. if ($muted) {
  228. $sql .= ' AND ttl < 0';
  229. }
  230. $stm = $this->pdo->prepare($sql);
  231. $values = array($id);
  232. if ($stm && $stm->execute($values)) {
  233. return $stm->rowCount();
  234. } else {
  235. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  236. Minz_Log::error('SQL error deleteFeedByCategory: ' . $info[2]);
  237. return false;
  238. }
  239. }
  240. public function selectAll() {
  241. $sql = <<<'SQL'
  242. SELECT id, url, kind, category, name, website, description, `lastUpdate`,
  243. priority, `pathEntries`, `httpAuth`, error, ttl, attributes
  244. FROM `_feed`
  245. SQL;
  246. $stm = $this->pdo->query($sql);
  247. while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
  248. yield $row;
  249. }
  250. }
  251. public function searchById(int $id): ?FreshRSS_Feed {
  252. $sql = 'SELECT * FROM `_feed` WHERE id=:id';
  253. $stm = $this->pdo->prepare($sql);
  254. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  255. $stm->execute();
  256. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  257. $feed = self::daoToFeed($res);
  258. return $feed[$id] ?? null;
  259. }
  260. /**
  261. * @return FreshRSS_Feed|null
  262. */
  263. public function searchByUrl(string $url) {
  264. $sql = 'SELECT * FROM `_feed` WHERE url=?';
  265. $stm = $this->pdo->prepare($sql);
  266. $values = array($url);
  267. $stm->execute($values);
  268. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  269. $feed = current(self::daoToFeed($res));
  270. return $feed == false ? null : $feed;
  271. }
  272. public function listFeedsIds(): array {
  273. $sql = 'SELECT id FROM `_feed`';
  274. $stm = $this->pdo->query($sql);
  275. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  276. }
  277. /**
  278. * @return array<FreshRSS_Feed>
  279. */
  280. public function listFeeds(): array {
  281. $sql = 'SELECT * FROM `_feed` ORDER BY name';
  282. $stm = $this->pdo->query($sql);
  283. return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  284. }
  285. public function listFeedsNewestItemUsec($id_feed = null) {
  286. $sql = 'SELECT id_feed, MAX(id) as newest_item_us FROM `_entry` ';
  287. if ($id_feed === null) {
  288. $sql .= 'GROUP BY id_feed';
  289. } else {
  290. $sql .= 'WHERE id_feed=' . intval($id_feed);
  291. }
  292. $stm = $this->pdo->query($sql);
  293. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  294. $newestItemUsec = [];
  295. foreach ($res as $line) {
  296. $newestItemUsec['f_' . $line['id_feed']] = $line['newest_item_us'];
  297. }
  298. return $newestItemUsec;
  299. }
  300. /**
  301. * Use $defaultCacheDuration == -1 to return all feeds, without filtering them by TTL.
  302. * @return array<FreshRSS_Feed>
  303. */
  304. public function listFeedsOrderUpdate(int $defaultCacheDuration = 3600, int $limit = 0) {
  305. $this->updateTTL();
  306. $sql = 'SELECT id, url, kind, name, website, `lastUpdate`, `pathEntries`, `httpAuth`, ttl, attributes '
  307. . 'FROM `_feed` '
  308. . ($defaultCacheDuration < 0 ? '' : 'WHERE ttl >= ' . FreshRSS_Feed::TTL_DEFAULT
  309. . ' AND `lastUpdate` < (' . (time() + 60)
  310. . '-(CASE WHEN ttl=' . FreshRSS_Feed::TTL_DEFAULT . ' THEN ' . intval($defaultCacheDuration) . ' ELSE ttl END)) ')
  311. . 'ORDER BY `lastUpdate` '
  312. . ($limit < 1 ? '' : 'LIMIT ' . intval($limit));
  313. $stm = $this->pdo->query($sql);
  314. if ($stm !== false) {
  315. return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  316. } else {
  317. $info = $this->pdo->errorInfo();
  318. if ($this->autoUpdateDb($info)) {
  319. return $this->listFeedsOrderUpdate($defaultCacheDuration, $limit);
  320. }
  321. Minz_Log::error('SQL error listFeedsOrderUpdate: ' . $info[2]);
  322. return array();
  323. }
  324. }
  325. public function listTitles(int $id, int $limit = 0) {
  326. $sql = 'SELECT title FROM `_entry` WHERE id_feed=:id_feed ORDER BY id DESC'
  327. . ($limit < 1 ? '' : ' LIMIT ' . intval($limit));
  328. $stm = $this->pdo->prepare($sql);
  329. $stm->bindParam(':id_feed', $id, PDO::PARAM_INT);
  330. if ($stm && $stm->execute()) {
  331. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  332. }
  333. return false;
  334. }
  335. /**
  336. * @param bool|null $muted to include only muted feeds
  337. * @return array<FreshRSS_Feed>
  338. */
  339. public function listByCategory(int $cat, $muted = null): array {
  340. $sql = 'SELECT * FROM `_feed` WHERE category=?';
  341. if ($muted) {
  342. $sql .= ' AND ttl < 0';
  343. }
  344. $stm = $this->pdo->prepare($sql);
  345. $stm->execute(array($cat));
  346. $feeds = self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  347. usort($feeds, function ($a, $b) {
  348. return strnatcasecmp($a->name(), $b->name());
  349. });
  350. return $feeds;
  351. }
  352. public function countEntries(int $id) {
  353. $sql = 'SELECT COUNT(*) AS count FROM `_entry` WHERE id_feed=?';
  354. $stm = $this->pdo->prepare($sql);
  355. $values = array($id);
  356. $stm->execute($values);
  357. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  358. return $res[0]['count'];
  359. }
  360. public function countNotRead(int $id) {
  361. $sql = 'SELECT COUNT(*) AS count FROM `_entry` WHERE id_feed=? AND is_read=0';
  362. $stm = $this->pdo->prepare($sql);
  363. $values = array($id);
  364. $stm->execute($values);
  365. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  366. return $res[0]['count'];
  367. }
  368. /**
  369. * @return int|false
  370. */
  371. public function updateCachedValues(int $id = 0) {
  372. //2 sub-requests with FOREIGN KEY(e.id_feed), INDEX(e.is_read) faster than 1 request with GROUP BY or CASE
  373. $sql = 'UPDATE `_feed` '
  374. . 'SET `cache_nbEntries`=(SELECT COUNT(e1.id) FROM `_entry` e1 WHERE e1.id_feed=`_feed`.id),'
  375. . '`cache_nbUnreads`=(SELECT COUNT(e2.id) FROM `_entry` e2 WHERE e2.id_feed=`_feed`.id AND e2.is_read=0)'
  376. . ($id != 0 ? ' WHERE id=:id' : '');
  377. $stm = $this->pdo->prepare($sql);
  378. if ($stm && $id != 0) {
  379. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  380. }
  381. if ($stm && $stm->execute()) {
  382. return $stm->rowCount();
  383. } else {
  384. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  385. Minz_Log::error('SQL error updateCachedValue: ' . $info[2]);
  386. return false;
  387. }
  388. }
  389. /**
  390. * Remember to call updateCachedValues() after calling this function
  391. * @return int|false number of lines affected or false in case of error
  392. */
  393. public function keepMaxUnread(int $id, int $n) {
  394. //Double SELECT for MySQL workaround ERROR 1093 (HY000)
  395. $sql = <<<'SQL'
  396. UPDATE `_entry` SET is_read=1
  397. WHERE id_feed=:id_feed1 AND is_read=0 AND id <= (SELECT e3.id FROM (
  398. SELECT e2.id FROM `_entry` e2
  399. WHERE e2.id_feed=:id_feed2 AND e2.is_read=0
  400. ORDER BY e2.id DESC
  401. LIMIT 1
  402. OFFSET :limit) e3)
  403. SQL;
  404. if (($stm = $this->pdo->prepare($sql)) &&
  405. $stm->bindParam(':id_feed1', $id, PDO::PARAM_INT) &&
  406. $stm->bindParam(':id_feed2', $id, PDO::PARAM_INT) &&
  407. $stm->bindParam(':limit', $n, PDO::PARAM_INT) &&
  408. $stm->execute()) {
  409. return $stm->rowCount();
  410. } else {
  411. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  412. Minz_Log::error('SQL error keepMaxUnread: ' . json_encode($info));
  413. return false;
  414. }
  415. }
  416. /**
  417. * Remember to call updateCachedValues() after calling this function
  418. * @return int|false number of lines affected or false in case of error
  419. */
  420. public function markAsReadUponGone(int $id) {
  421. //Double SELECT for MySQL workaround ERROR 1093 (HY000)
  422. $sql = <<<'SQL'
  423. UPDATE `_entry` SET is_read=1
  424. WHERE id_feed=:id_feed1 AND is_read=0 AND `lastSeen` < (SELECT e3.maxlastseen FROM (
  425. SELECT MAX(e2.`lastSeen`) AS maxlastseen FROM `_entry` e2 WHERE e2.id_feed = :id_feed2) e3)
  426. SQL;
  427. if (($stm = $this->pdo->prepare($sql)) &&
  428. $stm->bindParam(':id_feed1', $id, PDO::PARAM_INT) &&
  429. $stm->bindParam(':id_feed2', $id, PDO::PARAM_INT) &&
  430. $stm->execute()) {
  431. return $stm->rowCount();
  432. } else {
  433. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  434. Minz_Log::error('SQL error markAsReadUponGone: ' . json_encode($info));
  435. return false;
  436. }
  437. }
  438. /**
  439. * @return int|false
  440. */
  441. public function truncate(int $id) {
  442. $sql = 'DELETE FROM `_entry` WHERE id_feed=:id';
  443. $stm = $this->pdo->prepare($sql);
  444. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  445. $this->pdo->beginTransaction();
  446. if (!($stm && $stm->execute())) {
  447. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  448. Minz_Log::error('SQL error truncate: ' . $info[2]);
  449. $this->pdo->rollBack();
  450. return false;
  451. }
  452. $affected = $stm->rowCount();
  453. $sql = 'UPDATE `_feed` '
  454. . 'SET `cache_nbEntries`=0, `cache_nbUnreads`=0, `lastUpdate`=0 WHERE id=:id';
  455. $stm = $this->pdo->prepare($sql);
  456. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  457. if (!($stm && $stm->execute())) {
  458. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  459. Minz_Log::error('SQL error truncate: ' . $info[2]);
  460. $this->pdo->rollBack();
  461. return false;
  462. }
  463. $this->pdo->commit();
  464. return $affected;
  465. }
  466. public function purge() {
  467. $sql = 'DELETE FROM `_entry`';
  468. $stm = $this->pdo->prepare($sql);
  469. $this->pdo->beginTransaction();
  470. if (!($stm && $stm->execute())) {
  471. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  472. Minz_Log::error('SQL error truncate: ' . $info[2]);
  473. $this->pdo->rollBack();
  474. return false;
  475. }
  476. $sql = 'UPDATE `_feed` SET `cache_nbEntries` = 0, `cache_nbUnreads` = 0';
  477. $stm = $this->pdo->prepare($sql);
  478. if (!($stm && $stm->execute())) {
  479. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  480. Minz_Log::error('SQL error truncate: ' . $info[2]);
  481. $this->pdo->rollBack();
  482. return false;
  483. }
  484. $this->pdo->commit();
  485. }
  486. /**
  487. * @return array<FreshRSS_Feed>
  488. */
  489. public static function daoToFeed($listDAO, $catID = null): array {
  490. $list = array();
  491. if (!is_array($listDAO)) {
  492. $listDAO = array($listDAO);
  493. }
  494. foreach ($listDAO as $key => $dao) {
  495. if (!isset($dao['name'])) {
  496. continue;
  497. }
  498. if (isset($dao['id'])) {
  499. $key = $dao['id'];
  500. }
  501. if ($catID === null) {
  502. $category = isset($dao['category']) ? $dao['category'] : 0;
  503. } else {
  504. $category = $catID;
  505. }
  506. $myFeed = new FreshRSS_Feed($dao['url'] ?? '', false);
  507. $myFeed->_kind($dao['kind'] ?? FreshRSS_Feed::KIND_RSS);
  508. $myFeed->_categoryId($category);
  509. $myFeed->_name($dao['name']);
  510. $myFeed->_website($dao['website'] ?? '', false);
  511. $myFeed->_description($dao['description'] ?? '');
  512. $myFeed->_lastUpdate($dao['lastUpdate'] ?? 0);
  513. $myFeed->_priority($dao['priority'] ?? 10);
  514. $myFeed->_pathEntries($dao['pathEntries'] ?? '');
  515. $myFeed->_httpAuth(base64_decode($dao['httpAuth'] ?? ''));
  516. $myFeed->_error($dao['error'] ?? 0);
  517. $myFeed->_ttl($dao['ttl'] ?? FreshRSS_Feed::TTL_DEFAULT);
  518. $myFeed->_attributes('', $dao['attributes'] ?? '');
  519. $myFeed->_nbNotRead($dao['cache_nbUnreads'] ?? 0);
  520. $myFeed->_nbEntries($dao['cache_nbEntries'] ?? 0);
  521. if (isset($dao['id'])) {
  522. $myFeed->_id($dao['id']);
  523. }
  524. $list[$key] = $myFeed;
  525. }
  526. return $list;
  527. }
  528. public function updateTTL() {
  529. $sql = 'UPDATE `_feed` SET ttl=:new_value WHERE ttl=:old_value';
  530. $stm = $this->pdo->prepare($sql);
  531. if (!($stm && $stm->execute(array(':new_value' => FreshRSS_Feed::TTL_DEFAULT, ':old_value' => -2)))) {
  532. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  533. Minz_Log::error('SQL warning updateTTL 1: ' . $info[2] . ' ' . $sql);
  534. $sql2 = 'ALTER TABLE `_feed` ADD COLUMN ttl INT NOT NULL DEFAULT ' . FreshRSS_Feed::TTL_DEFAULT; //v0.7.3
  535. $stm = $this->pdo->query($sql2);
  536. if ($stm === false) {
  537. $info = $this->pdo->errorInfo();
  538. Minz_Log::error('SQL error updateTTL 2: ' . $info[2] . ' ' . $sql2);
  539. }
  540. } else {
  541. $stm->execute(array(':new_value' => -3600, ':old_value' => -1));
  542. }
  543. }
  544. /**
  545. * @return int|false
  546. */
  547. public function count() {
  548. $sql = 'SELECT COUNT(e.id) AS count FROM `_feed` e';
  549. $stm = $this->pdo->query($sql);
  550. if ($stm == false) {
  551. return false;
  552. }
  553. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  554. return isset($res[0]) ? $res[0] : 0;
  555. }
  556. }