EntryDAO.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. <?php
  2. class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
  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::error('SQL error addEntry: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  38. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title']);
  39. } /*else {
  40. Minz_Log::debug('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  41. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title']);
  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. /**
  69. * Toggle favorite marker on one or more article
  70. *
  71. * @todo simplify the query by removing the str_repeat. I am pretty sure
  72. * there is an other way to do that.
  73. *
  74. * @param integer|array $ids
  75. * @param boolean $is_favorite
  76. * @return false|integer
  77. */
  78. public function markFavorite($ids, $is_favorite = true) {
  79. if (!is_array($ids)) {
  80. $ids = array($ids);
  81. }
  82. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  83. . 'SET is_favorite=? '
  84. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?)';
  85. $values = array($is_favorite ? 1 : 0);
  86. $values = array_merge($values, $ids);
  87. $stm = $this->bd->prepare($sql);
  88. if ($stm && $stm->execute($values)) {
  89. return $stm->rowCount();
  90. } else {
  91. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  92. Minz_Log::error('SQL error markFavorite: ' . $info[2]);
  93. return false;
  94. }
  95. }
  96. /**
  97. * Update the unread article cache held on every feed details.
  98. * Depending on the parameters, it updates the cache on one feed, on all
  99. * feeds from one category or on all feeds.
  100. *
  101. * @todo It can use the query builder refactoring to build that query
  102. *
  103. * @param false|integer $catId category ID
  104. * @param false|integer $feedId feed ID
  105. * @return boolean
  106. */
  107. protected function updateCacheUnreads($catId = false, $feedId = false) {
  108. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  109. . 'LEFT OUTER JOIN ('
  110. . 'SELECT e.id_feed, '
  111. . 'COUNT(*) AS nbUnreads '
  112. . 'FROM `' . $this->prefix . 'entry` e '
  113. . 'WHERE e.is_read=0 '
  114. . 'GROUP BY e.id_feed'
  115. . ') x ON x.id_feed=f.id '
  116. . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0) '
  117. . 'WHERE 1';
  118. $values = array();
  119. if ($feedId !== false) {
  120. $sql .= ' AND f.id=?';
  121. $values[] = $id;
  122. }
  123. if ($catId !== false) {
  124. $sql .= ' AND f.category=?';
  125. $values[] = $catId;
  126. }
  127. $stm = $this->bd->prepare($sql);
  128. if ($stm && $stm->execute($values)) {
  129. return true;
  130. } else {
  131. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  132. Minz_Log::error('SQL error updateCacheUnreads: ' . $info[2]);
  133. return false;
  134. }
  135. }
  136. /**
  137. * Toggle the read marker on one or more article.
  138. * Then the cache is updated.
  139. *
  140. * @todo change the way the query is build because it seems there is
  141. * unnecessary code in here. For instance, the part with the str_repeat.
  142. * @todo remove code duplication. It seems the code is basically the
  143. * same if it is an array or not.
  144. *
  145. * @param integer|array $ids
  146. * @param boolean $is_read
  147. * @return integer affected rows
  148. */
  149. public function markRead($ids, $is_read = true) {
  150. if (is_array($ids)) { //Many IDs at once (used by API)
  151. if (count($ids) < 6) { //Speed heuristics
  152. $affected = 0;
  153. foreach ($ids as $id) {
  154. $affected += $this->markRead($id, $is_read);
  155. }
  156. return $affected;
  157. }
  158. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  159. . 'SET is_read=? '
  160. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?)';
  161. $values = array($is_read ? 1 : 0);
  162. $values = array_merge($values, $ids);
  163. $stm = $this->bd->prepare($sql);
  164. if (!($stm && $stm->execute($values))) {
  165. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  166. Minz_Log::error('SQL error markRead: ' . $info[2]);
  167. return false;
  168. }
  169. $affected = $stm->rowCount();
  170. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  171. return false;
  172. }
  173. return $affected;
  174. } else {
  175. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id '
  176. . 'SET e.is_read=?,'
  177. . 'f.cache_nbUnreads=f.cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
  178. . 'WHERE e.id=? AND e.is_read=?';
  179. $values = array($is_read ? 1 : 0, $ids, $is_read ? 0 : 1);
  180. $stm = $this->bd->prepare($sql);
  181. if ($stm && $stm->execute($values)) {
  182. return $stm->rowCount();
  183. } else {
  184. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  185. Minz_Log::error('SQL error markRead: ' . $info[2]);
  186. return false;
  187. }
  188. }
  189. }
  190. /**
  191. * Mark all entries as read depending on parameters.
  192. * If $onlyFavorites is true, it is used when the user mark as read in
  193. * the favorite pseudo-category.
  194. * If $priorityMin is greater than 0, it is used when the user mark as
  195. * read in the main feed pseudo-category.
  196. * Then the cache is updated.
  197. *
  198. * If $idMax equals 0, a deprecated debug message is logged
  199. *
  200. * @todo refactor this method along with markReadCat and markReadFeed
  201. * since they are all doing the same thing. I think we need to build a
  202. * tool to generate the query instead of having queries all over the
  203. * place. It will be reused also for the filtering making every thing
  204. * separated.
  205. *
  206. * @param integer $idMax fail safe article ID
  207. * @param boolean $onlyFavorites
  208. * @param integer $priorityMin
  209. * @return integer affected rows
  210. */
  211. public function markReadEntries($idMax = 0, $onlyFavorites = false, $priorityMin = 0) {
  212. if ($idMax == 0) {
  213. $idMax = time() . '000000';
  214. Minz_Log::debug('Calling markReadEntries(0) is deprecated!');
  215. }
  216. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id '
  217. . 'SET e.is_read=1 '
  218. . 'WHERE e.is_read=0 AND e.id <= ?';
  219. if ($onlyFavorites) {
  220. $sql .= ' AND e.is_favorite=1';
  221. } elseif ($priorityMin >= 0) {
  222. $sql .= ' AND f.priority > ' . intval($priorityMin);
  223. }
  224. $values = array($idMax);
  225. $stm = $this->bd->prepare($sql);
  226. if (!($stm && $stm->execute($values))) {
  227. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  228. Minz_Log::error('SQL error markReadEntries: ' . $info[2]);
  229. return false;
  230. }
  231. $affected = $stm->rowCount();
  232. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  233. return false;
  234. }
  235. return $affected;
  236. }
  237. /**
  238. * Mark all the articles in a category as read.
  239. * There is a fail safe to prevent to mark as read articles that are
  240. * loaded during the mark as read action. Then the cache is updated.
  241. *
  242. * If $idMax equals 0, a deprecated debug message is logged
  243. *
  244. * @param integer $id category ID
  245. * @param integer $idMax fail safe article ID
  246. * @return integer affected rows
  247. */
  248. public function markReadCat($id, $idMax = 0) {
  249. if ($idMax == 0) {
  250. $idMax = time() . '000000';
  251. Minz_Log::debug('Calling markReadCat(0) is deprecated!');
  252. }
  253. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id '
  254. . 'SET e.is_read=1 '
  255. . 'WHERE f.category=? AND e.is_read=0 AND e.id <= ?';
  256. $values = array($id, $idMax);
  257. $stm = $this->bd->prepare($sql);
  258. if (!($stm && $stm->execute($values))) {
  259. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  260. Minz_Log::error('SQL error markReadCat: ' . $info[2]);
  261. return false;
  262. }
  263. $affected = $stm->rowCount();
  264. if (($affected > 0) && (!$this->updateCacheUnreads($id, false))) {
  265. return false;
  266. }
  267. return $affected;
  268. }
  269. /**
  270. * Mark all the articles in a feed as read.
  271. * There is a fail safe to prevent to mark as read articles that are
  272. * loaded during the mark as read action. Then the cache is updated.
  273. *
  274. * If $idMax equals 0, a deprecated debug message is logged
  275. *
  276. * @param integer $id feed ID
  277. * @param integer $idMax fail safe article ID
  278. * @return integer affected rows
  279. */
  280. public function markReadFeed($id, $idMax = 0) {
  281. if ($idMax == 0) {
  282. $idMax = time() . '000000';
  283. Minz_Log::debug('Calling markReadFeed(0) is deprecated!');
  284. }
  285. $this->bd->beginTransaction();
  286. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  287. . 'SET is_read=1 '
  288. . 'WHERE id_feed=? AND is_read=0 AND id <= ?';
  289. $values = array($id, $idMax);
  290. $stm = $this->bd->prepare($sql);
  291. if (!($stm && $stm->execute($values))) {
  292. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  293. Minz_Log::error('SQL error markReadFeed: ' . $info[2]);
  294. $this->bd->rollBack();
  295. return false;
  296. }
  297. $affected = $stm->rowCount();
  298. if ($affected > 0) {
  299. $sql = 'UPDATE `' . $this->prefix . 'feed` '
  300. . 'SET cache_nbUnreads=cache_nbUnreads-' . $affected
  301. . ' WHERE id=?';
  302. $values = array($id);
  303. $stm = $this->bd->prepare($sql);
  304. if (!($stm && $stm->execute($values))) {
  305. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  306. Minz_Log::error('SQL error markReadFeed: ' . $info[2]);
  307. $this->bd->rollBack();
  308. return false;
  309. }
  310. }
  311. $this->bd->commit();
  312. return $affected;
  313. }
  314. public function searchByGuid($feed_id, $id) {
  315. // un guid est unique pour un flux donné
  316. $sql = 'SELECT id, guid, title, author, '
  317. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  318. . ', link, date, is_read, is_favorite, id_feed, tags '
  319. . 'FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid=?';
  320. $stm = $this->bd->prepare($sql);
  321. $values = array(
  322. $feed_id,
  323. $id
  324. );
  325. $stm->execute($values);
  326. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  327. $entries = self::daoToEntry($res);
  328. return isset($entries[0]) ? $entries[0] : null;
  329. }
  330. public function searchById($id) {
  331. $sql = 'SELECT id, guid, title, author, '
  332. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  333. . ', link, date, is_read, is_favorite, id_feed, tags '
  334. . 'FROM `' . $this->prefix . 'entry` WHERE id=?';
  335. $stm = $this->bd->prepare($sql);
  336. $values = array($id);
  337. $stm->execute($values);
  338. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  339. $entries = self::daoToEntry($res);
  340. return isset($entries[0]) ? $entries[0] : null;
  341. }
  342. protected function sqlConcat($s1, $s2) {
  343. return 'CONCAT(' . $s1 . ',' . $s2 . ')'; //MySQL
  344. }
  345. private function sqlListWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0) {
  346. if (!$state) {
  347. $state = FreshRSS_Entry::STATE_ALL;
  348. }
  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': //Deprecated: use $state instead
  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. if ($state & FreshRSS_Entry::STATE_NOT_READ) {
  376. if (!($state & FreshRSS_Entry::STATE_READ)) {
  377. $where .= 'AND e1.is_read=0 ';
  378. }
  379. }
  380. elseif ($state & FreshRSS_Entry::STATE_READ) {
  381. $where .= 'AND e1.is_read=1 ';
  382. }
  383. if ($state & FreshRSS_Entry::STATE_FAVORITE) {
  384. if (!($state & FreshRSS_Entry::STATE_NOT_FAVORITE)) {
  385. $where .= 'AND e1.is_favorite=1 ';
  386. }
  387. }
  388. elseif ($state & FreshRSS_Entry::STATE_NOT_FAVORITE) {
  389. $where .= 'AND e1.is_favorite=0 ';
  390. }
  391. switch ($order) {
  392. case 'DESC':
  393. case 'ASC':
  394. break;
  395. default:
  396. throw new FreshRSS_EntriesGetter_Exception('Bad order in Entry->listByType: [' . $order . ']!');
  397. }
  398. /*if ($firstId === '' && parent::$sharedDbType === 'mysql') {
  399. $firstId = $order === 'DESC' ? '9000000000'. '000000' : '0'; //MySQL optimization. TODO: check if this is needed again, after the filtering for old articles has been removed in 0.9-dev
  400. }*/
  401. if ($firstId !== '') {
  402. $where .= 'AND e1.id ' . ($order === 'DESC' ? '<=' : '>=') . $firstId . ' ';
  403. }
  404. if ($date_min > 0) {
  405. $where .= 'AND e1.id >= ' . $date_min . '000000 ';
  406. }
  407. $search = '';
  408. if ($filter !== null) {
  409. if ($filter->getIntitle()) {
  410. $search .= 'AND e1.title LIKE ? ';
  411. $values[] = "%{$filter->getIntitle()}%";
  412. }
  413. if ($filter->getInurl()) {
  414. $search .= 'AND CONCAT(e1.link, e1.guid) LIKE ? ';
  415. $values[] = "%{$filter->getInurl()}%";
  416. }
  417. if ($filter->getAuthor()) {
  418. $search .= 'AND e1.author LIKE ? ';
  419. $values[] = "%{$filter->getAuthor()}%";
  420. }
  421. if ($filter->getMinDate()) {
  422. $search .= 'AND e1.id >= ? ';
  423. $values[] = "{$filter->getMinDate()}000000";
  424. }
  425. if ($filter->getMaxDate()) {
  426. $search .= 'AND e1.id <= ? ';
  427. $values[] = "{$filter->getMaxDate()}000000";
  428. }
  429. if ($filter->getMinPubdate()) {
  430. $search .= 'AND e1.date >= ? ';
  431. $values[] = $filter->getMinPubdate();
  432. }
  433. if ($filter->getMaxPubdate()) {
  434. $search .= 'AND e1.date <= ? ';
  435. $values[] = $filter->getMaxPubdate();
  436. }
  437. if ($filter->getTags()) {
  438. $tags = $filter->getTags();
  439. foreach ($tags as $tag) {
  440. $search .= 'AND e1.tags LIKE ? ';
  441. $values[] = "%{$tag}%";
  442. }
  443. }
  444. if ($filter->getSearch()) {
  445. $search .= 'AND ' . $this->sqlconcat('e1.title', $this->isCompressed() ? 'UNCOMPRESS(content_bin)' : 'content') . ' LIKE ? ';
  446. $values[] = "%{$filter->getSearch()}%";
  447. }
  448. }
  449. return array($values,
  450. 'SELECT e1.id FROM `' . $this->prefix . 'entry` e1 '
  451. . ($joinFeed ? 'INNER JOIN `' . $this->prefix . 'feed` f ON e1.id_feed=f.id ' : '')
  452. . 'WHERE ' . $where
  453. . $search
  454. . 'ORDER BY e1.id ' . $order
  455. . ($limit > 0 ? ' LIMIT ' . $limit : '')); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  456. }
  457. public function listWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0) {
  458. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min);
  459. $sql = 'SELECT e.id, e.guid, e.title, e.author, '
  460. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  461. . ', e.link, e.date, e.is_read, e.is_favorite, e.id_feed, e.tags '
  462. . 'FROM `' . $this->prefix . 'entry` e '
  463. . 'INNER JOIN ('
  464. . $sql
  465. . ') e2 ON e2.id=e.id '
  466. . 'ORDER BY e.id ' . $order;
  467. $stm = $this->bd->prepare($sql);
  468. $stm->execute($values);
  469. return self::daoToEntry($stm->fetchAll(PDO::FETCH_ASSOC));
  470. }
  471. public function listIdsWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0) { //For API
  472. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min);
  473. $stm = $this->bd->prepare($sql);
  474. $stm->execute($values);
  475. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  476. }
  477. public function listLastGuidsByFeed($id, $n) {
  478. $sql = 'SELECT guid FROM `' . $this->prefix . 'entry` WHERE id_feed=? ORDER BY id DESC LIMIT ' . intval($n);
  479. $stm = $this->bd->prepare($sql);
  480. $values = array($id);
  481. $stm->execute($values);
  482. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  483. }
  484. public function countUnreadRead() {
  485. $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'
  486. . ' 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';
  487. $stm = $this->bd->prepare($sql);
  488. $stm->execute();
  489. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  490. $all = empty($res[0]) ? 0 : $res[0];
  491. $unread = empty($res[1]) ? 0 : $res[1];
  492. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  493. }
  494. public function count($minPriority = null) {
  495. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id';
  496. if ($minPriority !== null) {
  497. $sql = ' WHERE priority > ' . intval($minPriority);
  498. }
  499. $stm = $this->bd->prepare($sql);
  500. $stm->execute();
  501. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  502. return $res[0];
  503. }
  504. public function countNotRead($minPriority = null) {
  505. $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';
  506. if ($minPriority !== null) {
  507. $sql = ' AND priority > ' . intval($minPriority);
  508. }
  509. $stm = $this->bd->prepare($sql);
  510. $stm->execute();
  511. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  512. return $res[0];
  513. }
  514. public function countUnreadReadFavorites() {
  515. $sql = 'SELECT c FROM ('
  516. . 'SELECT COUNT(id) AS c, 1 as o FROM `' . $this->prefix . 'entry` WHERE is_favorite=1 '
  517. . 'UNION SELECT COUNT(id) AS c, 2 AS o FROM `' . $this->prefix . 'entry` WHERE is_favorite=1 AND is_read=0'
  518. . ') u ORDER BY o';
  519. $stm = $this->bd->prepare($sql);
  520. $stm->execute();
  521. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  522. $all = empty($res[0]) ? 0 : $res[0];
  523. $unread = empty($res[1]) ? 0 : $res[1];
  524. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  525. }
  526. public function optimizeTable() {
  527. $sql = 'OPTIMIZE TABLE `' . $this->prefix . 'entry`'; //MySQL
  528. $stm = $this->bd->prepare($sql);
  529. $stm->execute();
  530. }
  531. public function size($all = false) {
  532. $db = FreshRSS_Context::$system_conf->db;
  533. $sql = 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema=?'; //MySQL
  534. $values = array($db['base']);
  535. if (!$all) {
  536. $sql .= ' AND table_name LIKE ?';
  537. $values[] = $this->prefix . '%';
  538. }
  539. $stm = $this->bd->prepare($sql);
  540. $stm->execute($values);
  541. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  542. return $res[0];
  543. }
  544. public static function daoToEntry($listDAO) {
  545. $list = array();
  546. if (!is_array($listDAO)) {
  547. $listDAO = array($listDAO);
  548. }
  549. foreach ($listDAO as $key => $dao) {
  550. $entry = new FreshRSS_Entry(
  551. $dao['id_feed'],
  552. $dao['guid'],
  553. $dao['title'],
  554. $dao['author'],
  555. $dao['content'],
  556. $dao['link'],
  557. $dao['date'],
  558. $dao['is_read'],
  559. $dao['is_favorite'],
  560. $dao['tags']
  561. );
  562. if (isset($dao['id'])) {
  563. $entry->_id($dao['id']);
  564. }
  565. $list[] = $entry;
  566. }
  567. unset($listDAO);
  568. return $list;
  569. }
  570. }