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