EntryDAO.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <?php
  2. class FreshRSS_EntryDAO extends Minz_ModelPdo {
  3. public function addEntry ($valuesTmp) {
  4. $sql = 'INSERT INTO `' . $this->prefix . 'entry`(id, guid, title, author, content_bin, link, date, is_read, is_favorite, id_feed, tags) '
  5. . 'VALUES(?, ?, ?, ?, COMPRESS(?), ?, ?, ?, ?, ?, ?)';
  6. $stm = $this->bd->prepare ($sql);
  7. $values = array (
  8. $valuesTmp['id'],
  9. substr($valuesTmp['guid'], 0, 760),
  10. substr($valuesTmp['title'], 0, 255),
  11. substr($valuesTmp['author'], 0, 255),
  12. $valuesTmp['content'],
  13. substr($valuesTmp['link'], 0, 1023),
  14. $valuesTmp['date'],
  15. $valuesTmp['is_read'] ? 1 : 0,
  16. $valuesTmp['is_favorite'] ? 1 : 0,
  17. $valuesTmp['id_feed'],
  18. substr($valuesTmp['tags'], 0, 1023),
  19. );
  20. if ($stm && $stm->execute ($values)) {
  21. return $this->bd->lastInsertId();
  22. } else {
  23. $info = $stm->errorInfo();
  24. if ((int)($info[0] / 1000) !== 23) { //Filter out "SQLSTATE Class code 23: Constraint Violation" because of expected duplicate entries
  25. Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  26. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::ERROR);
  27. } /*else {
  28. Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  29. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::DEBUG);
  30. }*/
  31. return false;
  32. }
  33. }
  34. public function markFavorite($ids, $is_favorite = true) {
  35. if (!is_array($ids)) {
  36. $ids = array($ids);
  37. }
  38. $sql = 'UPDATE `' . $this->prefix . 'entry` e '
  39. . 'SET e.is_favorite = ? '
  40. . 'WHERE e.id IN (' . str_repeat('?,', count($ids) - 1). '?)';
  41. $values = array ($is_favorite ? 1 : 0);
  42. $values = array_merge($values, $ids);
  43. $stm = $this->bd->prepare ($sql);
  44. if ($stm && $stm->execute ($values)) {
  45. return $stm->rowCount();
  46. } else {
  47. $info = $stm->errorInfo();
  48. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  49. return false;
  50. }
  51. }
  52. public function markRead($ids, $is_read = true) {
  53. if (is_array($ids)) {
  54. if (count($ids) < 6) { //Speed heuristics
  55. $affected = 0;
  56. foreach ($ids as $id) {
  57. $affected += $this->markRead($id, $is_read);
  58. }
  59. return $affected;
  60. }
  61. $this->bd->beginTransaction();
  62. $sql = 'UPDATE `' . $this->prefix . 'entry` e '
  63. . 'SET e.is_read = ? '
  64. . 'WHERE e.id IN (' . str_repeat('?,', count($ids) - 1). '?)';
  65. $values = array($is_read ? 1 : 0);
  66. $values = array_merge($values, $ids);
  67. $stm = $this->bd->prepare($sql);
  68. if (!($stm && $stm->execute($values))) {
  69. $info = $stm->errorInfo();
  70. Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
  71. $this->bd->rollBack();
  72. return false;
  73. }
  74. $affected = $stm->rowCount();
  75. if ($affected > 0) {
  76. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  77. . 'INNER JOIN ('
  78. . 'SELECT e.id_feed, '
  79. . 'COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS nbUnreads, '
  80. . 'COUNT(e.id) AS nbEntries '
  81. . 'FROM `' . $this->prefix . 'entry` e '
  82. . 'GROUP BY e.id_feed'
  83. . ') x ON x.id_feed=f.id '
  84. . 'SET f.cache_nbEntries=x.nbEntries, f.cache_nbUnreads=x.nbUnreads';
  85. $stm = $this->bd->prepare($sql);
  86. if (!($stm && $stm->execute())) {
  87. $info = $stm->errorInfo();
  88. Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
  89. $this->bd->rollBack();
  90. return false;
  91. }
  92. }
  93. $this->bd->commit();
  94. return $affected;
  95. } else {
  96. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  97. . 'SET e.is_read = ?,'
  98. . 'f.cache_nbUnreads=f.cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
  99. . 'WHERE e.id=?';
  100. $values = array($is_read ? 1 : 0, $ids);
  101. $stm = $this->bd->prepare($sql);
  102. if ($stm && $stm->execute($values)) {
  103. return $stm->rowCount();
  104. } else {
  105. $info = $stm->errorInfo();
  106. Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
  107. return false;
  108. }
  109. }
  110. }
  111. public function markReadEntries ($idMax = 0, $onlyFavorites = false, $priorityMin = 0) {
  112. if ($idMax == 0) {
  113. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  114. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  115. . 'WHERE e.is_read = 0';
  116. if ($onlyFavorites) {
  117. $sql .= ' AND e.is_favorite = 1';
  118. } elseif ($priorityMin >= 0) {
  119. $sql .= ' AND f.priority > ' . intval($priorityMin);
  120. }
  121. $stm = $this->bd->prepare ($sql);
  122. if ($stm && $stm->execute ()) {
  123. return $stm->rowCount();
  124. } else {
  125. $info = $stm->errorInfo();
  126. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  127. return false;
  128. }
  129. } else {
  130. $this->bd->beginTransaction ();
  131. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  132. . 'SET e.is_read = 1 '
  133. . 'WHERE e.is_read = 0 AND e.id <= ?';
  134. if ($onlyFavorites) {
  135. $sql .= ' AND e.is_favorite = 1';
  136. } elseif ($priorityMin >= 0) {
  137. $sql .= ' AND f.priority > ' . intval($priorityMin);
  138. }
  139. $values = array ($idMax);
  140. $stm = $this->bd->prepare ($sql);
  141. if (!($stm && $stm->execute ($values))) {
  142. $info = $stm->errorInfo();
  143. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  144. $this->bd->rollBack ();
  145. return false;
  146. }
  147. $affected = $stm->rowCount();
  148. if ($affected > 0) {
  149. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  150. . 'LEFT OUTER JOIN ('
  151. . 'SELECT e.id_feed, '
  152. . 'COUNT(*) AS nbUnreads '
  153. . 'FROM `' . $this->prefix . 'entry` e '
  154. . 'WHERE e.is_read = 0 '
  155. . 'GROUP BY e.id_feed'
  156. . ') x ON x.id_feed=f.id '
  157. . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0)';
  158. $stm = $this->bd->prepare ($sql);
  159. if (!($stm && $stm->execute ())) {
  160. $info = $stm->errorInfo();
  161. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  162. $this->bd->rollBack ();
  163. return false;
  164. }
  165. }
  166. $this->bd->commit ();
  167. return $affected;
  168. }
  169. }
  170. public function markReadCat ($id, $idMax = 0) {
  171. if ($idMax == 0) {
  172. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  173. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  174. . 'WHERE f.category = ? AND e.is_read = 0';
  175. $values = array ($id);
  176. $stm = $this->bd->prepare ($sql);
  177. if ($stm && $stm->execute ($values)) {
  178. return $stm->rowCount();
  179. } else {
  180. $info = $stm->errorInfo();
  181. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  182. return false;
  183. }
  184. } else {
  185. $this->bd->beginTransaction ();
  186. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  187. . 'SET e.is_read = 1 '
  188. . 'WHERE f.category = ? AND e.is_read = 0 AND e.id <= ?';
  189. $values = array ($id, $idMax);
  190. $stm = $this->bd->prepare ($sql);
  191. if (!($stm && $stm->execute ($values))) {
  192. $info = $stm->errorInfo();
  193. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  194. $this->bd->rollBack ();
  195. return false;
  196. }
  197. $affected = $stm->rowCount();
  198. if ($affected > 0) {
  199. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  200. . 'LEFT OUTER JOIN ('
  201. . 'SELECT e.id_feed, '
  202. . 'COUNT(*) AS nbUnreads '
  203. . 'FROM `' . $this->prefix . 'entry` e '
  204. . 'WHERE e.is_read = 0 '
  205. . 'GROUP BY e.id_feed'
  206. . ') x ON x.id_feed=f.id '
  207. . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0) '
  208. . 'WHERE f.category = ?';
  209. $values = array ($id);
  210. $stm = $this->bd->prepare ($sql);
  211. if (!($stm && $stm->execute ($values))) {
  212. $info = $stm->errorInfo();
  213. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  214. $this->bd->rollBack ();
  215. return false;
  216. }
  217. }
  218. $this->bd->commit ();
  219. return $affected;
  220. }
  221. }
  222. public function markReadCatName($name, $idMax = 0) {
  223. if ($idMax == 0) {
  224. $sql = 'UPDATE `' . $this->prefix . 'entry` e '
  225. . 'INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  226. . 'INNER JOIN `' . $this->prefix . 'category` c ON c.id = f.category '
  227. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  228. . 'WHERE c.name = ?';
  229. $values = array($name);
  230. $stm = $this->bd->prepare($sql);
  231. if ($stm && $stm->execute($values)) {
  232. return $stm->rowCount();
  233. } else {
  234. $info = $stm->errorInfo();
  235. Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
  236. return false;
  237. }
  238. } else {
  239. $this->bd->beginTransaction();
  240. $sql = 'UPDATE `' . $this->prefix . 'entry` e '
  241. . 'INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  242. . 'INNER JOIN `' . $this->prefix . 'category` c ON c.id = f.category '
  243. . 'SET e.is_read = 1 '
  244. . 'WHERE c.name = ? AND e.id <= ?';
  245. $values = array($name, $idMax);
  246. $stm = $this->bd->prepare($sql);
  247. if (!($stm && $stm->execute($values))) {
  248. $info = $stm->errorInfo();
  249. Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
  250. $this->bd->rollBack();
  251. return false;
  252. }
  253. $affected = $stm->rowCount();
  254. if ($affected > 0) {
  255. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  256. . 'LEFT OUTER JOIN ('
  257. . 'SELECT e.id_feed, '
  258. . 'COUNT(*) AS nbUnreads '
  259. . 'FROM `' . $this->prefix . 'entry` e '
  260. . 'WHERE e.is_read = 0 '
  261. . 'GROUP BY e.id_feed'
  262. . ') x ON x.id_feed=f.id '
  263. . 'INNER JOIN `' . $this->prefix . 'category` c ON c.id = f.category '
  264. . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0) '
  265. . 'WHERE c.name = ?';
  266. $values = array($name);
  267. $stm = $this->bd->prepare($sql);
  268. if (!($stm && $stm->execute($values))) {
  269. $info = $stm->errorInfo();
  270. Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
  271. $this->bd->rollBack();
  272. return false;
  273. }
  274. }
  275. $this->bd->commit();
  276. return $affected;
  277. }
  278. }
  279. public function markReadFeed ($id, $idMax = 0) {
  280. if ($idMax == 0) {
  281. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  282. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  283. . 'WHERE f.id=? AND e.is_read = 0';
  284. $values = array ($id);
  285. $stm = $this->bd->prepare ($sql);
  286. if ($stm && $stm->execute ($values)) {
  287. return $stm->rowCount();
  288. } else {
  289. $info = $stm->errorInfo();
  290. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  291. return false;
  292. }
  293. } else {
  294. $this->bd->beginTransaction ();
  295. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  296. . 'SET e.is_read = 1 '
  297. . 'WHERE f.id=? AND e.is_read = 0 AND e.id <= ?';
  298. $values = array ($id, $idMax);
  299. $stm = $this->bd->prepare ($sql);
  300. if (!($stm && $stm->execute ($values))) {
  301. $info = $stm->errorInfo();
  302. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  303. $this->bd->rollBack ();
  304. return false;
  305. }
  306. $affected = $stm->rowCount();
  307. if ($affected > 0) {
  308. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  309. . 'SET f.cache_nbUnreads=f.cache_nbUnreads-' . $affected
  310. . ' WHERE f.id=?';
  311. $values = array ($id);
  312. $stm = $this->bd->prepare ($sql);
  313. if (!($stm && $stm->execute ($values))) {
  314. $info = $stm->errorInfo();
  315. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  316. $this->bd->rollBack ();
  317. return false;
  318. }
  319. }
  320. $this->bd->commit ();
  321. return $affected;
  322. }
  323. }
  324. public function searchByGuid ($feed_id, $id) {
  325. // un guid est unique pour un flux donné
  326. $sql = 'SELECT id, guid, title, author, UNCOMPRESS(content_bin) AS content, link, date, is_read, is_favorite, id_feed, tags '
  327. . 'FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid=?';
  328. $stm = $this->bd->prepare ($sql);
  329. $values = array (
  330. $feed_id,
  331. $id
  332. );
  333. $stm->execute ($values);
  334. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  335. $entries = self::daoToEntry ($res);
  336. return isset ($entries[0]) ? $entries[0] : null;
  337. }
  338. public function searchById ($id) {
  339. $sql = 'SELECT id, guid, title, author, UNCOMPRESS(content_bin) AS content, link, date, is_read, is_favorite, id_feed, tags '
  340. . 'FROM `' . $this->prefix . 'entry` WHERE id=?';
  341. $stm = $this->bd->prepare ($sql);
  342. $values = array ($id);
  343. $stm->execute ($values);
  344. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  345. $entries = self::daoToEntry ($res);
  346. return isset ($entries[0]) ? $entries[0] : null;
  347. }
  348. private function sqlListWhere($type = 'a', $id = '', $state = 'all', $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0, $keepHistoryDefault = 0) {
  349. $where = '';
  350. $joinFeed = false;
  351. $values = array();
  352. switch ($type) {
  353. case 'a':
  354. $where .= 'f.priority > 0 ';
  355. $joinFeed = true;
  356. break;
  357. case 's':
  358. $where .= 'e1.is_favorite = 1 ';
  359. break;
  360. case 'c':
  361. $where .= 'f.category = ? ';
  362. $values[] = intval($id);
  363. $joinFeed = true;
  364. break;
  365. case 'f':
  366. $where .= 'e1.id_feed = ? ';
  367. $values[] = intval($id);
  368. break;
  369. case 'A':
  370. $where .= '1 ';
  371. break;
  372. default:
  373. throw new FreshRSS_EntriesGetter_Exception ('Bad type in Entry->listByType: [' . $type . ']!');
  374. }
  375. switch ($state) {
  376. case 'all':
  377. break;
  378. case 'not_read':
  379. $where .= 'AND e1.is_read = 0 ';
  380. break;
  381. case 'read':
  382. $where .= 'AND e1.is_read = 1 ';
  383. break;
  384. case 'favorite':
  385. $where .= 'AND e1.is_favorite = 1 ';
  386. break;
  387. default:
  388. throw new FreshRSS_EntriesGetter_Exception ('Bad state in Entry->listByType: [' . $state . ']!');
  389. }
  390. switch ($order) {
  391. case 'DESC':
  392. case 'ASC':
  393. break;
  394. default:
  395. throw new FreshRSS_EntriesGetter_Exception ('Bad order in Entry->listByType: [' . $order . ']!');
  396. }
  397. if ($firstId !== '') {
  398. $where .= 'AND e1.id ' . ($order === 'DESC' ? '<=' : '>=') . $firstId . ' ';
  399. }
  400. if (($date_min > 0) && ($type !== 's')) {
  401. $where .= 'AND (e1.id >= ' . $date_min . '000000 OR e1.is_read = 0 OR e1.is_favorite = 1 OR (f.keep_history <> 0';
  402. if (intval($keepHistoryDefault) === 0) {
  403. $where .= ' AND f.keep_history <> -2'; //default
  404. }
  405. $where .= ')) ';
  406. $joinFeed = true;
  407. }
  408. $search = '';
  409. if ($filter !== '') {
  410. $filter = trim($filter);
  411. $filter = addcslashes($filter, '\\%_');
  412. if (stripos($filter, 'intitle:') === 0) {
  413. $filter = substr($filter, strlen('intitle:'));
  414. $intitle = true;
  415. } else {
  416. $intitle = false;
  417. }
  418. if (stripos($filter, 'inurl:') === 0) {
  419. $filter = substr($filter, strlen('inurl:'));
  420. $inurl = true;
  421. } else {
  422. $inurl = false;
  423. }
  424. if (stripos($filter, 'author:') === 0) {
  425. $filter = substr($filter, strlen('author:'));
  426. $author = true;
  427. } else {
  428. $author = false;
  429. }
  430. $terms = array_unique(explode(' ', $filter));
  431. sort($terms); //Put #tags first
  432. foreach ($terms as $word) {
  433. $word = trim($word);
  434. if (strlen($word) > 0) {
  435. if ($intitle) {
  436. $search .= 'AND e1.title LIKE ? ';
  437. $values[] = '%' . $word .'%';
  438. } elseif ($inurl) {
  439. $search .= 'AND CONCAT(e1.link, e1.guid) LIKE ? ';
  440. $values[] = '%' . $word .'%';
  441. } elseif ($author) {
  442. $search .= 'AND e1.author LIKE ? ';
  443. $values[] = '%' . $word .'%';
  444. } else {
  445. if ($word[0] === '#' && isset($word[1])) {
  446. $search .= 'AND e1.tags LIKE ? ';
  447. $values[] = '%' . $word .'%';
  448. } else {
  449. $search .= 'AND CONCAT(e1.title, UNCOMPRESS(e1.content_bin)) LIKE ? ';
  450. $values[] = '%' . $word .'%';
  451. }
  452. }
  453. }
  454. }
  455. }
  456. return array($values,
  457. 'SELECT e1.id FROM `' . $this->prefix . 'entry` e1 '
  458. . ($joinFeed ? 'INNER JOIN `' . $this->prefix . 'feed` f ON e1.id_feed = f.id ' : '')
  459. . 'WHERE ' . $where
  460. . $search
  461. . 'ORDER BY e1.id ' . $order
  462. . ($limit > 0 ? ' LIMIT ' . $limit : '')); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  463. }
  464. public function listWhere($type = 'a', $id = '', $state = 'all', $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0, $keepHistoryDefault = 0) {
  465. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min, $keepHistoryDefault);
  466. $sql = 'SELECT e.id, e.guid, e.title, e.author, UNCOMPRESS(e.content_bin) AS content, e.link, e.date, e.is_read, e.is_favorite, e.id_feed, e.tags '
  467. . 'FROM `' . $this->prefix . 'entry` e '
  468. . 'INNER JOIN ('
  469. . $sql
  470. . ') e2 ON e2.id = e.id '
  471. . 'ORDER BY e.id ' . $order;
  472. $stm = $this->bd->prepare ($sql);
  473. $stm->execute ($values);
  474. return self::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  475. }
  476. public function listIdsWhere($type = 'a', $id = '', $state = 'all', $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0, $keepHistoryDefault = 0) { //For API
  477. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min, $keepHistoryDefault);
  478. $stm = $this->bd->prepare($sql);
  479. $stm->execute($values);
  480. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  481. }
  482. public function listLastGuidsByFeed($id, $n) {
  483. $sql = 'SELECT guid FROM `' . $this->prefix . 'entry` WHERE id_feed=? ORDER BY id DESC LIMIT ' . intval($n);
  484. $stm = $this->bd->prepare ($sql);
  485. $values = array ($id);
  486. $stm->execute ($values);
  487. return $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  488. }
  489. public function countUnreadRead () {
  490. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id WHERE priority > 0'
  491. . ' UNION SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id WHERE priority > 0 AND is_read = 0';
  492. $stm = $this->bd->prepare ($sql);
  493. $stm->execute ();
  494. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  495. $all = empty($res[0]) ? 0 : $res[0];
  496. $unread = empty($res[1]) ? 0 : $res[1];
  497. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  498. }
  499. public function count ($minPriority = null) {
  500. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id';
  501. if ($minPriority !== null) {
  502. $sql = ' WHERE priority > ' . intval($minPriority);
  503. }
  504. $stm = $this->bd->prepare ($sql);
  505. $stm->execute ();
  506. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  507. return $res[0];
  508. }
  509. public function countNotRead ($minPriority = null) {
  510. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id WHERE is_read = 0';
  511. if ($minPriority !== null) {
  512. $sql = ' AND priority > ' . intval($minPriority);
  513. }
  514. $stm = $this->bd->prepare ($sql);
  515. $stm->execute ();
  516. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  517. return $res[0];
  518. }
  519. public function countUnreadReadFavorites () {
  520. $sql = 'SELECT COUNT(id) FROM `' . $this->prefix . 'entry` WHERE is_favorite=1'
  521. . ' UNION SELECT COUNT(id) FROM `' . $this->prefix . 'entry` WHERE is_favorite=1 AND is_read = 0';
  522. $stm = $this->bd->prepare ($sql);
  523. $stm->execute ();
  524. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  525. $all = empty($res[0]) ? 0 : $res[0];
  526. $unread = empty($res[1]) ? 0 : $res[1];
  527. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  528. }
  529. public function optimizeTable() {
  530. $sql = 'OPTIMIZE TABLE `' . $this->prefix . 'entry`';
  531. $stm = $this->bd->prepare ($sql);
  532. $stm->execute ();
  533. }
  534. public static function daoToEntry ($listDAO) {
  535. $list = array ();
  536. if (!is_array ($listDAO)) {
  537. $listDAO = array ($listDAO);
  538. }
  539. foreach ($listDAO as $key => $dao) {
  540. $entry = new FreshRSS_Entry (
  541. $dao['id_feed'],
  542. $dao['guid'],
  543. $dao['title'],
  544. $dao['author'],
  545. $dao['content'],
  546. $dao['link'],
  547. $dao['date'],
  548. $dao['is_read'],
  549. $dao['is_favorite'],
  550. $dao['tags']
  551. );
  552. if (isset ($dao['id'])) {
  553. $entry->_id ($dao['id']);
  554. }
  555. $list[] = $entry;
  556. }
  557. unset ($listDAO);
  558. return $list;
  559. }
  560. }