FeedDAO.php 19 KB

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