FeedDAO.php 18 KB

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