FeedDAO.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. <?php
  2. class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
  3. protected function addColumn($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($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($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) {
  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($id, $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($feed, $key, $value) {
  148. if ($feed instanceof FreshRSS_Feed) {
  149. $feed->_attributes($key, $value);
  150. return $this->updateFeed(
  151. $feed->id(),
  152. array('attributes' => $feed->attributes())
  153. );
  154. }
  155. return false;
  156. }
  157. /**
  158. * @see updateCachedValue()
  159. */
  160. public function updateLastUpdate($id, $inError = false, $mtime = 0) {
  161. $sql = 'UPDATE `_feed` SET `lastUpdate`=?, error=? WHERE id=?';
  162. $values = array(
  163. $mtime <= 0 ? time() : $mtime,
  164. $inError ? 1 : 0,
  165. $id,
  166. );
  167. $stm = $this->pdo->prepare($sql);
  168. if ($stm && $stm->execute($values)) {
  169. return $stm->rowCount();
  170. } else {
  171. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  172. Minz_Log::error('SQL error updateLastUpdate: ' . $info[2]);
  173. return false;
  174. }
  175. }
  176. public function mute($id, $value = true) {
  177. $sql = 'UPDATE `_feed` SET ttl=' . ($value ? '-' : '') . 'ABS(ttl) WHERE id=' . intval($id);
  178. return $this->pdo->exec($sql);
  179. }
  180. public function changeCategory($idOldCat, $idNewCat) {
  181. $catDAO = FreshRSS_Factory::createCategoryDao();
  182. $newCat = $catDAO->searchById($idNewCat);
  183. if (!$newCat) {
  184. $newCat = $catDAO->getDefault();
  185. }
  186. $sql = 'UPDATE `_feed` SET category=? WHERE category=?';
  187. $stm = $this->pdo->prepare($sql);
  188. $values = array(
  189. $newCat->id(),
  190. $idOldCat
  191. );
  192. if ($stm && $stm->execute($values)) {
  193. return $stm->rowCount();
  194. } else {
  195. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  196. Minz_Log::error('SQL error changeCategory: ' . $info[2]);
  197. return false;
  198. }
  199. }
  200. public function deleteFeed($id) {
  201. $sql = 'DELETE FROM `_feed` WHERE id=?';
  202. $stm = $this->pdo->prepare($sql);
  203. $values = array($id);
  204. if ($stm && $stm->execute($values)) {
  205. return $stm->rowCount();
  206. } else {
  207. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  208. Minz_Log::error('SQL error deleteFeed: ' . $info[2]);
  209. return false;
  210. }
  211. }
  212. public function deleteFeedByCategory($id) {
  213. $sql = 'DELETE FROM `_feed` WHERE category=?';
  214. $stm = $this->pdo->prepare($sql);
  215. $values = array($id);
  216. if ($stm && $stm->execute($values)) {
  217. return $stm->rowCount();
  218. } else {
  219. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  220. Minz_Log::error('SQL error deleteFeedByCategory: ' . $info[2]);
  221. return false;
  222. }
  223. }
  224. public function selectAll() {
  225. $sql = <<<'SQL'
  226. SELECT id, url, category, name, website, description, `lastUpdate`,
  227. priority, `pathEntries`, `httpAuth`, error, ttl, attributes
  228. FROM `_feed`
  229. SQL;
  230. $stm = $this->pdo->query($sql);
  231. while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
  232. yield $row;
  233. }
  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. if (isset($feed[$id])) {
  243. return $feed[$id];
  244. } else {
  245. return null;
  246. }
  247. }
  248. public function searchByUrl($url) {
  249. $sql = 'SELECT * FROM `_feed` WHERE url=?';
  250. $stm = $this->pdo->prepare($sql);
  251. $values = array($url);
  252. $stm->execute($values);
  253. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  254. $feed = current(self::daoToFeed($res));
  255. if (isset($feed) && $feed !== false) {
  256. return $feed;
  257. } else {
  258. return null;
  259. }
  260. }
  261. public function listFeedsIds() {
  262. $sql = 'SELECT id FROM `_feed`';
  263. $stm = $this->pdo->query($sql);
  264. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  265. }
  266. public function listFeeds() {
  267. $sql = 'SELECT * FROM `_feed` ORDER BY name';
  268. $stm = $this->pdo->query($sql);
  269. return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  270. }
  271. public function listFeedsNewestItemUsec($id_feed = null) {
  272. $sql = 'SELECT id_feed, MAX(id) as newest_item_us FROM `_entry` ';
  273. if ($id_feed === null) {
  274. $sql .= 'GROUP BY id_feed';
  275. } else {
  276. $sql .= 'WHERE id_feed=' . intval($id_feed);
  277. }
  278. $stm = $this->pdo->query($sql);
  279. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  280. $newestItemUsec = [];
  281. foreach ($res as $line) {
  282. $newestItemUsec['f_' . $line['id_feed']] = $line['newest_item_us'];
  283. }
  284. return $newestItemUsec;
  285. }
  286. /**
  287. * For API
  288. */
  289. public function arrayFeedCategoryNames() {
  290. $sql = <<<'SQL'
  291. SELECT f.id, f.name, c.name as c_name FROM `_feed` f
  292. INNER JOIN `_category` c ON c.id = f.category
  293. SQL;
  294. $stm = $this->pdo->query($sql);
  295. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  296. $feedCategoryNames = array();
  297. foreach ($res as $line) {
  298. $feedCategoryNames[$line['id']] = array(
  299. 'name' => $line['name'],
  300. 'c_name' => $line['c_name'],
  301. );
  302. }
  303. return $feedCategoryNames;
  304. }
  305. /**
  306. * Use $defaultCacheDuration == -1 to return all feeds, without filtering them by TTL.
  307. */
  308. public function listFeedsOrderUpdate($defaultCacheDuration = 3600, $limit = 0) {
  309. $this->updateTTL();
  310. $sql = 'SELECT id, url, name, website, `lastUpdate`, `pathEntries`, `httpAuth`, ttl, attributes '
  311. . 'FROM `_feed` '
  312. . ($defaultCacheDuration < 0 ? '' : 'WHERE ttl >= ' . FreshRSS_Feed::TTL_DEFAULT
  313. . ' AND `lastUpdate` < (' . (time() + 60)
  314. . '-(CASE WHEN ttl=' . FreshRSS_Feed::TTL_DEFAULT . ' THEN ' . intval($defaultCacheDuration) . ' ELSE ttl END)) ')
  315. . 'ORDER BY `lastUpdate` '
  316. . ($limit < 1 ? '' : 'LIMIT ' . intval($limit));
  317. $stm = $this->pdo->query($sql);
  318. if ($stm !== false) {
  319. return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  320. } else {
  321. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  322. if ($this->autoUpdateDb($info)) {
  323. return $this->listFeedsOrderUpdate($defaultCacheDuration);
  324. }
  325. Minz_Log::error('SQL error listFeedsOrderUpdate: ' . $info[2]);
  326. return array();
  327. }
  328. }
  329. public function listTitles($id, $limit = null) {
  330. $sql = 'SELECT title FROM `_entry` WHERE id_feed=:id_feed ORDER BY id DESC'
  331. . ($limit < 1 ? '' : ' LIMIT ' . intval($limit));
  332. $stm = $this->pdo->prepare($sql);
  333. $stm->bindParam(':id_feed', $id, PDO::PARAM_INT);
  334. if ($stm && $stm->execute()) {
  335. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  336. }
  337. return false;
  338. }
  339. public function listByCategory($cat) {
  340. $sql = 'SELECT * FROM `_feed` WHERE category=?';
  341. $stm = $this->pdo->prepare($sql);
  342. $stm->execute(array($cat));
  343. $feeds = self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  344. usort($feeds, function ($a, $b) {
  345. return strnatcasecmp($a->name(), $b->name());
  346. });
  347. return $feeds;
  348. }
  349. public function countEntries($id) {
  350. $sql = 'SELECT COUNT(*) AS count FROM `_entry` WHERE id_feed=?';
  351. $stm = $this->pdo->prepare($sql);
  352. $values = array($id);
  353. $stm->execute($values);
  354. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  355. return $res[0]['count'];
  356. }
  357. public function countNotRead($id) {
  358. $sql = 'SELECT COUNT(*) AS count FROM `_entry` WHERE id_feed=? AND is_read=0';
  359. $stm = $this->pdo->prepare($sql);
  360. $values = array($id);
  361. $stm->execute($values);
  362. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  363. return $res[0]['count'];
  364. }
  365. public function updateCachedValues($id = null) {
  366. //2 sub-requests with FOREIGN KEY(e.id_feed), INDEX(e.is_read) faster than 1 request with GROUP BY or CASE
  367. $sql = 'UPDATE `_feed` '
  368. . 'SET `cache_nbEntries`=(SELECT COUNT(e1.id) FROM `_entry` e1 WHERE e1.id_feed=`_feed`.id),'
  369. . '`cache_nbUnreads`=(SELECT COUNT(e2.id) FROM `_entry` e2 WHERE e2.id_feed=`_feed`.id AND e2.is_read=0)'
  370. . ($id != null ? ' WHERE id=:id' : '');
  371. $stm = $this->pdo->prepare($sql);
  372. if ($id != null) {
  373. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  374. }
  375. if ($stm && $stm->execute()) {
  376. return $stm->rowCount();
  377. } else {
  378. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  379. Minz_Log::error('SQL error updateCachedValue: ' . $info[2]);
  380. return false;
  381. }
  382. }
  383. public function keepMaxUnread($id, $n) {
  384. //Double SELECT for MySQL workaround ERROR 1093 (HY000)
  385. $sql = <<<'SQL'
  386. UPDATE `_entry` SET is_read=1
  387. WHERE id_feed=:id_feed1 AND is_read=0 AND id <= (SELECT e3.id FROM (
  388. SELECT e2.id FROM `_entry` e2
  389. WHERE e2.id_feed=:id_feed2 AND e2.is_read=0
  390. ORDER BY e2.id DESC
  391. LIMIT 1
  392. OFFSET :limit) e3)
  393. SQL;
  394. $stm = $this->pdo->prepare($sql);
  395. $stm->bindParam(':id_feed1', $id, PDO::PARAM_INT);
  396. $stm->bindParam(':id_feed2', $id, PDO::PARAM_INT);
  397. $stm->bindParam(':limit', $n, PDO::PARAM_INT);
  398. if (!$stm || !$stm->execute()) {
  399. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  400. Minz_Log::error('SQL error keepMaxUnread: ' . json_encode($info));
  401. return false;
  402. }
  403. $affected = $stm->rowCount();
  404. if ($affected > 0) {
  405. $sql = 'UPDATE `_feed` '
  406. . 'SET `cache_nbUnreads`=`cache_nbUnreads`-' . $affected
  407. . ' WHERE id=:id';
  408. $stm = $this->pdo->prepare($sql);
  409. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  410. if (!($stm && $stm->execute())) {
  411. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  412. Minz_Log::error('SQL error keepMaxUnread cache: ' . json_encode($info));
  413. return false;
  414. }
  415. }
  416. return $affected;
  417. }
  418. public function truncate($id) {
  419. $sql = 'DELETE FROM `_entry` WHERE id_feed=:id';
  420. $stm = $this->pdo->prepare($sql);
  421. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  422. $this->pdo->beginTransaction();
  423. if (!($stm && $stm->execute())) {
  424. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  425. Minz_Log::error('SQL error truncate: ' . $info[2]);
  426. $this->pdo->rollBack();
  427. return false;
  428. }
  429. $affected = $stm->rowCount();
  430. $sql = 'UPDATE `_feed` '
  431. . 'SET `cache_nbEntries`=0, `cache_nbUnreads`=0, `lastUpdate`=0 WHERE id=:id';
  432. $stm = $this->pdo->prepare($sql);
  433. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  434. if (!($stm && $stm->execute())) {
  435. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  436. Minz_Log::error('SQL error truncate: ' . $info[2]);
  437. $this->pdo->rollBack();
  438. return false;
  439. }
  440. $this->pdo->commit();
  441. return $affected;
  442. }
  443. public function purge() {
  444. $sql = 'DELETE FROM `_entry`';
  445. $stm = $this->pdo->prepare($sql);
  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. $sql = 'UPDATE `_feed` SET `cache_nbEntries` = 0, `cache_nbUnreads` = 0';
  454. $stm = $this->pdo->prepare($sql);
  455. if (!($stm && $stm->execute())) {
  456. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  457. Minz_Log::error('SQL error truncate: ' . $info[2]);
  458. $this->pdo->rollBack();
  459. return false;
  460. }
  461. $this->pdo->commit();
  462. }
  463. public static function daoToFeed($listDAO, $catID = null) {
  464. $list = array();
  465. if (!is_array($listDAO)) {
  466. $listDAO = array($listDAO);
  467. }
  468. foreach ($listDAO as $key => $dao) {
  469. if (!isset($dao['name'])) {
  470. continue;
  471. }
  472. if (isset($dao['id'])) {
  473. $key = $dao['id'];
  474. }
  475. if ($catID === null) {
  476. $category = isset($dao['category']) ? $dao['category'] : 0;
  477. } else {
  478. $category = $catID;
  479. }
  480. $myFeed = new FreshRSS_Feed(isset($dao['url']) ? $dao['url'] : '', false);
  481. $myFeed->_category($category);
  482. $myFeed->_name($dao['name']);
  483. $myFeed->_website(isset($dao['website']) ? $dao['website'] : '', false);
  484. $myFeed->_description(isset($dao['description']) ? $dao['description'] : '');
  485. $myFeed->_lastUpdate(isset($dao['lastUpdate']) ? $dao['lastUpdate'] : 0);
  486. $myFeed->_priority(isset($dao['priority']) ? $dao['priority'] : 10);
  487. $myFeed->_pathEntries(isset($dao['pathEntries']) ? $dao['pathEntries'] : '');
  488. $myFeed->_httpAuth(isset($dao['httpAuth']) ? base64_decode($dao['httpAuth']) : '');
  489. $myFeed->_error(isset($dao['error']) ? $dao['error'] : 0);
  490. $myFeed->_ttl(isset($dao['ttl']) ? $dao['ttl'] : FreshRSS_Feed::TTL_DEFAULT);
  491. $myFeed->_attributes('', isset($dao['attributes']) ? $dao['attributes'] : '');
  492. $myFeed->_nbNotRead(isset($dao['cache_nbUnreads']) ? $dao['cache_nbUnreads'] : 0);
  493. $myFeed->_nbEntries(isset($dao['cache_nbEntries']) ? $dao['cache_nbEntries'] : 0);
  494. if (isset($dao['id'])) {
  495. $myFeed->_id($dao['id']);
  496. }
  497. $list[$key] = $myFeed;
  498. }
  499. return $list;
  500. }
  501. public function updateTTL() {
  502. $sql = 'UPDATE `_feed` SET ttl=:new_value WHERE ttl=:old_value';
  503. $stm = $this->pdo->prepare($sql);
  504. if (!($stm && $stm->execute(array(':new_value' => FreshRSS_Feed::TTL_DEFAULT, ':old_value' => -2)))) {
  505. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  506. Minz_Log::error('SQL warning updateTTL 1: ' . $info[2] . ' ' . $sql);
  507. $sql2 = 'ALTER TABLE `_feed` ADD COLUMN ttl INT NOT NULL DEFAULT ' . FreshRSS_Feed::TTL_DEFAULT; //v0.7.3
  508. $stm = $this->pdo->query($sql2);
  509. if ($stm === false) {
  510. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  511. Minz_Log::error('SQL error updateTTL 2: ' . $info[2] . ' ' . $sql2);
  512. }
  513. } else {
  514. $stm->execute(array(':new_value' => -3600, ':old_value' => -1));
  515. }
  516. }
  517. public function count() {
  518. $sql = 'SELECT COUNT(e.id) AS count FROM `_feed` e';
  519. $stm = $this->pdo->query($sql);
  520. if ($stm == false) {
  521. return false;
  522. }
  523. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  524. return isset($res[0]) ? $res[0] : 0;
  525. }
  526. }