FeedDAO.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. <?php
  2. class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
  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. /**
  252. * @return FreshRSS_Feed|null
  253. */
  254. public function searchById($id) {
  255. $sql = 'SELECT * FROM `_feed` WHERE id=:id';
  256. $stm = $this->pdo->prepare($sql);
  257. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  258. $stm->execute();
  259. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  260. $feed = self::daoToFeed($res);
  261. return $feed[$id] ?? null;
  262. }
  263. /**
  264. * @return FreshRSS_Feed|null
  265. */
  266. public function searchByUrl(string $url) {
  267. $sql = 'SELECT * FROM `_feed` WHERE url=?';
  268. $stm = $this->pdo->prepare($sql);
  269. $values = array($url);
  270. $stm->execute($values);
  271. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  272. $feed = current(self::daoToFeed($res));
  273. return $feed == false ? null : $feed;
  274. }
  275. public function listFeedsIds(): array {
  276. $sql = 'SELECT id FROM `_feed`';
  277. $stm = $this->pdo->query($sql);
  278. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  279. }
  280. /**
  281. * @return array<FreshRSS_Feed>
  282. */
  283. public function listFeeds(): array {
  284. $sql = 'SELECT * FROM `_feed` ORDER BY name';
  285. $stm = $this->pdo->query($sql);
  286. return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  287. }
  288. public function listFeedsNewestItemUsec($id_feed = null) {
  289. $sql = 'SELECT id_feed, MAX(id) as newest_item_us FROM `_entry` ';
  290. if ($id_feed === null) {
  291. $sql .= 'GROUP BY id_feed';
  292. } else {
  293. $sql .= 'WHERE id_feed=' . intval($id_feed);
  294. }
  295. $stm = $this->pdo->query($sql);
  296. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  297. $newestItemUsec = [];
  298. foreach ($res as $line) {
  299. $newestItemUsec['f_' . $line['id_feed']] = $line['newest_item_us'];
  300. }
  301. return $newestItemUsec;
  302. }
  303. /**
  304. * Use $defaultCacheDuration == -1 to return all feeds, without filtering them by TTL.
  305. * @return array<FreshRSS_Feed>
  306. */
  307. public function listFeedsOrderUpdate(int $defaultCacheDuration = 3600, int $limit = 0) {
  308. $this->updateTTL();
  309. $sql = 'SELECT id, url, kind, name, website, `lastUpdate`, `pathEntries`, `httpAuth`, ttl, attributes '
  310. . 'FROM `_feed` '
  311. . ($defaultCacheDuration < 0 ? '' : 'WHERE ttl >= ' . FreshRSS_Feed::TTL_DEFAULT
  312. . ' AND `lastUpdate` < (' . (time() + 60)
  313. . '-(CASE WHEN ttl=' . FreshRSS_Feed::TTL_DEFAULT . ' THEN ' . intval($defaultCacheDuration) . ' ELSE ttl END)) ')
  314. . 'ORDER BY `lastUpdate` '
  315. . ($limit < 1 ? '' : 'LIMIT ' . intval($limit));
  316. $stm = $this->pdo->query($sql);
  317. if ($stm !== false) {
  318. return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  319. } else {
  320. $info = $this->pdo->errorInfo();
  321. if ($this->autoUpdateDb($info)) {
  322. return $this->listFeedsOrderUpdate($defaultCacheDuration, $limit);
  323. }
  324. Minz_Log::error('SQL error listFeedsOrderUpdate: ' . $info[2]);
  325. return array();
  326. }
  327. }
  328. public function listTitles(int $id, int $limit = 0) {
  329. $sql = 'SELECT title FROM `_entry` WHERE id_feed=:id_feed ORDER BY id DESC'
  330. . ($limit < 1 ? '' : ' LIMIT ' . intval($limit));
  331. $stm = $this->pdo->prepare($sql);
  332. $stm->bindParam(':id_feed', $id, PDO::PARAM_INT);
  333. if ($stm && $stm->execute()) {
  334. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  335. }
  336. return false;
  337. }
  338. /**
  339. * @param bool|null $muted to include only muted feeds
  340. * @return array<FreshRSS_Feed>
  341. */
  342. public function listByCategory(int $cat, $muted = null): array {
  343. $sql = 'SELECT * FROM `_feed` WHERE category=?';
  344. if ($muted) {
  345. $sql .= ' AND ttl < 0';
  346. }
  347. $stm = $this->pdo->prepare($sql);
  348. $stm->execute(array($cat));
  349. $feeds = self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  350. usort($feeds, function ($a, $b) {
  351. return strnatcasecmp($a->name(), $b->name());
  352. });
  353. return $feeds;
  354. }
  355. public function countEntries(int $id) {
  356. $sql = 'SELECT COUNT(*) AS count FROM `_entry` WHERE id_feed=?';
  357. $stm = $this->pdo->prepare($sql);
  358. $values = array($id);
  359. $stm->execute($values);
  360. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  361. return $res[0]['count'];
  362. }
  363. public function countNotRead(int $id) {
  364. $sql = 'SELECT COUNT(*) AS count FROM `_entry` WHERE id_feed=? AND is_read=0';
  365. $stm = $this->pdo->prepare($sql);
  366. $values = array($id);
  367. $stm->execute($values);
  368. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  369. return $res[0]['count'];
  370. }
  371. /**
  372. * @return int|false
  373. */
  374. public function updateCachedValues(int $id = 0) {
  375. //2 sub-requests with FOREIGN KEY(e.id_feed), INDEX(e.is_read) faster than 1 request with GROUP BY or CASE
  376. $sql = 'UPDATE `_feed` '
  377. . 'SET `cache_nbEntries`=(SELECT COUNT(e1.id) FROM `_entry` e1 WHERE e1.id_feed=`_feed`.id),'
  378. . '`cache_nbUnreads`=(SELECT COUNT(e2.id) FROM `_entry` e2 WHERE e2.id_feed=`_feed`.id AND e2.is_read=0)'
  379. . ($id != 0 ? ' WHERE id=:id' : '');
  380. $stm = $this->pdo->prepare($sql);
  381. if ($id != 0) {
  382. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  383. }
  384. if ($stm && $stm->execute()) {
  385. return $stm->rowCount();
  386. } else {
  387. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  388. Minz_Log::error('SQL error updateCachedValue: ' . $info[2]);
  389. return false;
  390. }
  391. }
  392. /**
  393. * Remember to call updateCachedValues() after calling this function
  394. * @return int|false number of lines affected or false in case of error
  395. */
  396. public function keepMaxUnread(int $id, int $n) {
  397. //Double SELECT for MySQL workaround ERROR 1093 (HY000)
  398. $sql = <<<'SQL'
  399. UPDATE `_entry` SET is_read=1
  400. WHERE id_feed=:id_feed1 AND is_read=0 AND id <= (SELECT e3.id FROM (
  401. SELECT e2.id FROM `_entry` e2
  402. WHERE e2.id_feed=:id_feed2 AND e2.is_read=0
  403. ORDER BY e2.id DESC
  404. LIMIT 1
  405. OFFSET :limit) e3)
  406. SQL;
  407. if (($stm = $this->pdo->prepare($sql)) &&
  408. $stm->bindParam(':id_feed1', $id, PDO::PARAM_INT) &&
  409. $stm->bindParam(':id_feed2', $id, PDO::PARAM_INT) &&
  410. $stm->bindParam(':limit', $n, PDO::PARAM_INT) &&
  411. $stm->execute()) {
  412. return $stm->rowCount();
  413. } else {
  414. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  415. Minz_Log::error('SQL error keepMaxUnread: ' . json_encode($info));
  416. return false;
  417. }
  418. }
  419. /**
  420. * Remember to call updateCachedValues() after calling this function
  421. * @return int|false number of lines affected or false in case of error
  422. */
  423. public function markAsReadUponGone(int $id) {
  424. //Double SELECT for MySQL workaround ERROR 1093 (HY000)
  425. $sql = <<<'SQL'
  426. UPDATE `_entry` SET is_read=1
  427. WHERE id_feed=:id_feed1 AND is_read=0 AND `lastSeen` < (SELECT e3.maxlastseen FROM (
  428. SELECT MAX(e2.`lastSeen`) AS maxlastseen FROM `_entry` e2 WHERE e2.id_feed = :id_feed2) e3)
  429. SQL;
  430. if (($stm = $this->pdo->prepare($sql)) &&
  431. $stm->bindParam(':id_feed1', $id, PDO::PARAM_INT) &&
  432. $stm->bindParam(':id_feed2', $id, PDO::PARAM_INT) &&
  433. $stm->execute()) {
  434. return $stm->rowCount();
  435. } else {
  436. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  437. Minz_Log::error('SQL error markAsReadUponGone: ' . json_encode($info));
  438. return false;
  439. }
  440. }
  441. /**
  442. * @return int|false
  443. */
  444. public function truncate(int $id) {
  445. $sql = 'DELETE FROM `_entry` WHERE id_feed=:id';
  446. $stm = $this->pdo->prepare($sql);
  447. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  448. $this->pdo->beginTransaction();
  449. if (!($stm && $stm->execute())) {
  450. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  451. Minz_Log::error('SQL error truncate: ' . $info[2]);
  452. $this->pdo->rollBack();
  453. return false;
  454. }
  455. $affected = $stm->rowCount();
  456. $sql = 'UPDATE `_feed` '
  457. . 'SET `cache_nbEntries`=0, `cache_nbUnreads`=0, `lastUpdate`=0 WHERE id=:id';
  458. $stm = $this->pdo->prepare($sql);
  459. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  460. if (!($stm && $stm->execute())) {
  461. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  462. Minz_Log::error('SQL error truncate: ' . $info[2]);
  463. $this->pdo->rollBack();
  464. return false;
  465. }
  466. $this->pdo->commit();
  467. return $affected;
  468. }
  469. public function purge() {
  470. $sql = 'DELETE FROM `_entry`';
  471. $stm = $this->pdo->prepare($sql);
  472. $this->pdo->beginTransaction();
  473. if (!($stm && $stm->execute())) {
  474. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  475. Minz_Log::error('SQL error truncate: ' . $info[2]);
  476. $this->pdo->rollBack();
  477. return false;
  478. }
  479. $sql = 'UPDATE `_feed` SET `cache_nbEntries` = 0, `cache_nbUnreads` = 0';
  480. $stm = $this->pdo->prepare($sql);
  481. if (!($stm && $stm->execute())) {
  482. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  483. Minz_Log::error('SQL error truncate: ' . $info[2]);
  484. $this->pdo->rollBack();
  485. return false;
  486. }
  487. $this->pdo->commit();
  488. }
  489. /**
  490. * @return array<FreshRSS_Feed>
  491. */
  492. public static function daoToFeed($listDAO, $catID = null): array {
  493. $list = array();
  494. if (!is_array($listDAO)) {
  495. $listDAO = array($listDAO);
  496. }
  497. foreach ($listDAO as $key => $dao) {
  498. if (!isset($dao['name'])) {
  499. continue;
  500. }
  501. if (isset($dao['id'])) {
  502. $key = $dao['id'];
  503. }
  504. if ($catID === null) {
  505. $category = isset($dao['category']) ? $dao['category'] : 0;
  506. } else {
  507. $category = $catID;
  508. }
  509. $myFeed = new FreshRSS_Feed($dao['url'] ?? '', false);
  510. $myFeed->_kind($dao['kind'] ?? FreshRSS_Feed::KIND_RSS);
  511. $myFeed->_categoryId($category);
  512. $myFeed->_name($dao['name']);
  513. $myFeed->_website($dao['website'] ?? '', false);
  514. $myFeed->_description($dao['description'] ?? '');
  515. $myFeed->_lastUpdate($dao['lastUpdate'] ?? 0);
  516. $myFeed->_priority($dao['priority'] ?? 10);
  517. $myFeed->_pathEntries($dao['pathEntries'] ?? '');
  518. $myFeed->_httpAuth(base64_decode($dao['httpAuth'] ?? ''));
  519. $myFeed->_error($dao['error'] ?? 0);
  520. $myFeed->_ttl($dao['ttl'] ?? FreshRSS_Feed::TTL_DEFAULT);
  521. $myFeed->_attributes('', $dao['attributes'] ?? '');
  522. $myFeed->_nbNotRead($dao['cache_nbUnreads'] ?? 0);
  523. $myFeed->_nbEntries($dao['cache_nbEntries'] ?? 0);
  524. if (isset($dao['id'])) {
  525. $myFeed->_id($dao['id']);
  526. }
  527. $list[$key] = $myFeed;
  528. }
  529. return $list;
  530. }
  531. public function updateTTL() {
  532. $sql = 'UPDATE `_feed` SET ttl=:new_value WHERE ttl=:old_value';
  533. $stm = $this->pdo->prepare($sql);
  534. if (!($stm && $stm->execute(array(':new_value' => FreshRSS_Feed::TTL_DEFAULT, ':old_value' => -2)))) {
  535. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  536. Minz_Log::error('SQL warning updateTTL 1: ' . $info[2] . ' ' . $sql);
  537. $sql2 = 'ALTER TABLE `_feed` ADD COLUMN ttl INT NOT NULL DEFAULT ' . FreshRSS_Feed::TTL_DEFAULT; //v0.7.3
  538. $stm = $this->pdo->query($sql2);
  539. if ($stm === false) {
  540. $info = $this->pdo->errorInfo();
  541. Minz_Log::error('SQL error updateTTL 2: ' . $info[2] . ' ' . $sql2);
  542. }
  543. } else {
  544. $stm->execute(array(':new_value' => -3600, ':old_value' => -1));
  545. }
  546. }
  547. /**
  548. * @return int|false
  549. */
  550. public function count() {
  551. $sql = 'SELECT COUNT(e.id) AS count FROM `_feed` e';
  552. $stm = $this->pdo->query($sql);
  553. if ($stm == false) {
  554. return false;
  555. }
  556. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  557. return isset($res[0]) ? $res[0] : 0;
  558. }
  559. }