EntryDAO.php 18 KB

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