4
0

EntryDAO.php 19 KB

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