FeedDAO.php 18 KB

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