EntryDAO.php 19 KB

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