EntryDAO.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  123. . 'SET is_read=? '
  124. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?)';
  125. $values = array($is_read ? 1 : 0);
  126. $values = array_merge($values, $ids);
  127. $stm = $this->bd->prepare($sql);
  128. if (!($stm && $stm->execute($values))) {
  129. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  130. Minz_Log::record('SQL error markRead: ' . $info[2], Minz_Log::ERROR);
  131. return false;
  132. }
  133. $affected = $stm->rowCount();
  134. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  135. return false;
  136. }
  137. return $affected;
  138. } else {
  139. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id '
  140. . 'SET e.is_read=?,'
  141. . 'f.cache_nbUnreads=f.cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
  142. . 'WHERE e.id=? AND e.is_read=?';
  143. $values = array($is_read ? 1 : 0, $ids, $is_read ? 0 : 1);
  144. $stm = $this->bd->prepare($sql);
  145. if ($stm && $stm->execute($values)) {
  146. return $stm->rowCount();
  147. } else {
  148. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  149. Minz_Log::record('SQL error markRead: ' . $info[2], Minz_Log::ERROR);
  150. return false;
  151. }
  152. }
  153. }
  154. public function markReadEntries($idMax = 0, $onlyFavorites = false, $priorityMin = 0) {
  155. if ($idMax == 0) {
  156. $idMax = time() . '000000';
  157. Minz_Log::record($nb . 'Calling markReadEntries(0) is deprecated!', Minz_Log::DEBUG);
  158. }
  159. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id '
  160. . 'SET e.is_read=1 '
  161. . 'WHERE e.is_read=0 AND e.id <= ?';
  162. if ($onlyFavorites) {
  163. $sql .= ' AND e.is_favorite=1';
  164. } elseif ($priorityMin >= 0) {
  165. $sql .= ' AND f.priority > ' . intval($priorityMin);
  166. }
  167. $values = array($idMax);
  168. $stm = $this->bd->prepare($sql);
  169. if (!($stm && $stm->execute($values))) {
  170. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  171. Minz_Log::record('SQL error markReadEntries: ' . $info[2], Minz_Log::ERROR);
  172. return false;
  173. }
  174. $affected = $stm->rowCount();
  175. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  176. return false;
  177. }
  178. return $affected;
  179. }
  180. public function markReadCat($id, $idMax = 0) {
  181. if ($idMax == 0) {
  182. $idMax = time() . '000000';
  183. Minz_Log::record($nb . 'Calling markReadCat(0) is deprecated!', Minz_Log::DEBUG);
  184. }
  185. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id '
  186. . 'SET e.is_read=1 '
  187. . 'WHERE f.category=? AND e.is_read=0 AND e.id <= ?';
  188. $values = array($id, $idMax);
  189. $stm = $this->bd->prepare($sql);
  190. if (!($stm && $stm->execute($values))) {
  191. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  192. Minz_Log::record('SQL error markReadCat: ' . $info[2], Minz_Log::ERROR);
  193. return false;
  194. }
  195. $affected = $stm->rowCount();
  196. if (($affected > 0) && (!$this->updateCacheUnreads($id, false))) {
  197. return false;
  198. }
  199. return $affected;
  200. }
  201. public function markReadFeed($id, $idMax = 0) {
  202. if ($idMax == 0) {
  203. $idMax = time() . '000000';
  204. Minz_Log::record($nb . 'Calling markReadFeed(0) is deprecated!', Minz_Log::DEBUG);
  205. }
  206. $this->bd->beginTransaction();
  207. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  208. . 'SET is_read=1 '
  209. . 'WHERE id_feed=? AND is_read=0 AND id <= ?';
  210. $values = array($id, $idMax);
  211. $stm = $this->bd->prepare($sql);
  212. if (!($stm && $stm->execute($values))) {
  213. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  214. Minz_Log::record('SQL error markReadFeed: ' . $info[2], Minz_Log::ERROR);
  215. $this->bd->rollBack();
  216. return false;
  217. }
  218. $affected = $stm->rowCount();
  219. if ($affected > 0) {
  220. $sql = 'UPDATE `' . $this->prefix . 'feed` '
  221. . 'SET cache_nbUnreads=cache_nbUnreads-' . $affected
  222. . ' WHERE id=?';
  223. $values = array($id);
  224. $stm = $this->bd->prepare($sql);
  225. if (!($stm && $stm->execute($values))) {
  226. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  227. Minz_Log::record('SQL error markReadFeed: ' . $info[2], Minz_Log::ERROR);
  228. $this->bd->rollBack();
  229. return false;
  230. }
  231. }
  232. $this->bd->commit();
  233. return $affected;
  234. }
  235. public function searchByGuid($feed_id, $id) {
  236. // un guid est unique pour un flux donné
  237. $sql = 'SELECT id, guid, title, author, '
  238. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  239. . ', link, date, is_read, is_favorite, id_feed, tags '
  240. . 'FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid=?';
  241. $stm = $this->bd->prepare($sql);
  242. $values = array(
  243. $feed_id,
  244. $id
  245. );
  246. $stm->execute($values);
  247. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  248. $entries = self::daoToEntry($res);
  249. return isset($entries[0]) ? $entries[0] : null;
  250. }
  251. public function searchById($id) {
  252. $sql = 'SELECT id, guid, title, author, '
  253. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  254. . ', link, date, is_read, is_favorite, id_feed, tags '
  255. . 'FROM `' . $this->prefix . 'entry` WHERE id=?';
  256. $stm = $this->bd->prepare($sql);
  257. $values = array($id);
  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. protected function sqlConcat($s1, $s2) {
  264. return 'CONCAT(' . $s1 . ',' . $s2 . ')'; //MySQL
  265. }
  266. private function sqlListWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0, $showOlderUnreadsorFavorites = false, $keepHistoryDefault = 0) {
  267. if (!$state) {
  268. $state = FreshRSS_Entry::STATE_ALL;
  269. }
  270. $where = '';
  271. $joinFeed = false;
  272. $values = array();
  273. switch ($type) {
  274. case 'a':
  275. $where .= 'f.priority > 0 ';
  276. $joinFeed = true;
  277. break;
  278. case 's': //Deprecated: use $state instead
  279. $where .= 'e1.is_favorite=1 ';
  280. break;
  281. case 'c':
  282. $where .= 'f.category=? ';
  283. $values[] = intval($id);
  284. $joinFeed = true;
  285. break;
  286. case 'f':
  287. $where .= 'e1.id_feed=? ';
  288. $values[] = intval($id);
  289. break;
  290. case 'A':
  291. $where .= '1 ';
  292. break;
  293. default:
  294. throw new FreshRSS_EntriesGetter_Exception('Bad type in Entry->listByType: [' . $type . ']!');
  295. }
  296. if ($state & FreshRSS_Entry::STATE_NOT_READ) {
  297. if (!($state & FreshRSS_Entry::STATE_READ)) {
  298. $where .= 'AND e1.is_read=0 ';
  299. }
  300. }
  301. elseif ($state & FreshRSS_Entry::STATE_READ) {
  302. $where .= 'AND e1.is_read=1 ';
  303. }
  304. if ($state & FreshRSS_Entry::STATE_FAVORITE) {
  305. if (!($state & FreshRSS_Entry::STATE_NOT_FAVORITE)) {
  306. $where .= 'AND e1.is_favorite=1 ';
  307. }
  308. }
  309. elseif ($state & FreshRSS_Entry::STATE_NOT_FAVORITE) {
  310. $where .= 'AND e1.is_favorite=0 ';
  311. }
  312. switch ($order) {
  313. case 'DESC':
  314. case 'ASC':
  315. break;
  316. default:
  317. throw new FreshRSS_EntriesGetter_Exception('Bad order in Entry->listByType: [' . $order . ']!');
  318. }
  319. if ($firstId !== '') {
  320. $where .= 'AND e1.id ' . ($order === 'DESC' ? '<=' : '>=') . $firstId . ' ';
  321. }
  322. if (($date_min > 0) && ($type !== 's')) {
  323. $where .= 'AND (e1.id >= ' . $date_min . '000000';
  324. if ($showOlderUnreadsorFavorites) { //Lax date constraint
  325. $where .= ' OR e1.is_read=0 OR e1.is_favorite=1 OR (f.keep_history <> 0';
  326. if (intval($keepHistoryDefault) === 0) {
  327. $where .= ' AND f.keep_history <> -2'; //default
  328. }
  329. $where .= ')';
  330. }
  331. $where .= ') ';
  332. $joinFeed = true;
  333. }
  334. $search = '';
  335. if ($filter !== '') {
  336. require_once(LIB_PATH . '/lib_date.php');
  337. $filter = trim($filter);
  338. $filter = addcslashes($filter, '\\%_');
  339. $terms = array_unique(explode(' ', $filter));
  340. //sort($terms); //Put #tags first //TODO: Put the cheapest filters first
  341. foreach ($terms as $word) {
  342. $word = trim($word);
  343. if (stripos($word, 'intitle:') === 0) {
  344. $word = substr($word, strlen('intitle:'));
  345. $search .= 'AND e1.title LIKE ? ';
  346. $values[] = '%' . $word .'%';
  347. } elseif (stripos($word, 'inurl:') === 0) {
  348. $word = substr($word, strlen('inurl:'));
  349. $search .= 'AND CONCAT(e1.link, e1.guid) LIKE ? ';
  350. $values[] = '%' . $word .'%';
  351. } elseif (stripos($word, 'author:') === 0) {
  352. $word = substr($word, strlen('author:'));
  353. $search .= 'AND e1.author LIKE ? ';
  354. $values[] = '%' . $word .'%';
  355. } elseif (stripos($word, 'date:') === 0) {
  356. $word = substr($word, strlen('date:'));
  357. list($minDate, $maxDate) = parseDateInterval($word);
  358. if ($minDate) {
  359. $search .= 'AND e1.id >= ' . $minDate . '000000 ';
  360. }
  361. if ($maxDate) {
  362. $search .= 'AND e1.id <= ' . $maxDate . '000000 ';
  363. }
  364. } elseif (stripos($word, 'pubdate:') === 0) {
  365. $word = substr($word, strlen('pubdate:'));
  366. list($minDate, $maxDate) = parseDateInterval($word);
  367. if ($minDate) {
  368. $search .= 'AND e1.date >= ' . $minDate . ' ';
  369. }
  370. if ($maxDate) {
  371. $search .= 'AND e1.date <= ' . $maxDate . ' ';
  372. }
  373. } else {
  374. if ($word[0] === '#' && isset($word[1])) {
  375. $search .= 'AND e1.tags LIKE ? ';
  376. $values[] = '%' . $word .'%';
  377. } else {
  378. $search .= 'AND ' . $this->sqlconcat('e1.title', $this->isCompressed() ? 'UNCOMPRESS(content_bin)' : 'content') . ' LIKE ? ';
  379. $values[] = '%' . $word .'%';
  380. }
  381. }
  382. }
  383. }
  384. return array($values,
  385. 'SELECT e1.id FROM `' . $this->prefix . 'entry` e1 '
  386. . ($joinFeed ? 'INNER JOIN `' . $this->prefix . 'feed` f ON e1.id_feed=f.id ' : '')
  387. . 'WHERE ' . $where
  388. . $search
  389. . 'ORDER BY e1.id ' . $order
  390. . ($limit > 0 ? ' LIMIT ' . $limit : '')); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  391. }
  392. public function listWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0, $showOlderUnreadsorFavorites = false, $keepHistoryDefault = 0) {
  393. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min, $showOlderUnreadsorFavorites, $keepHistoryDefault);
  394. $sql = 'SELECT e.id, e.guid, e.title, e.author, '
  395. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  396. . ', e.link, e.date, e.is_read, e.is_favorite, e.id_feed, e.tags '
  397. . 'FROM `' . $this->prefix . 'entry` e '
  398. . 'INNER JOIN ('
  399. . $sql
  400. . ') e2 ON e2.id=e.id '
  401. . 'ORDER BY e.id ' . $order;
  402. $stm = $this->bd->prepare($sql);
  403. $stm->execute($values);
  404. return self::daoToEntry($stm->fetchAll(PDO::FETCH_ASSOC));
  405. }
  406. 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
  407. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min, $showOlderUnreadsorFavorites, $keepHistoryDefault);
  408. $stm = $this->bd->prepare($sql);
  409. $stm->execute($values);
  410. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  411. }
  412. public function listLastGuidsByFeed($id, $n) {
  413. $sql = 'SELECT guid FROM `' . $this->prefix . 'entry` WHERE id_feed=? ORDER BY id DESC LIMIT ' . intval($n);
  414. $stm = $this->bd->prepare($sql);
  415. $values = array($id);
  416. $stm->execute($values);
  417. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  418. }
  419. public function countUnreadRead() {
  420. $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'
  421. . ' 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';
  422. $stm = $this->bd->prepare($sql);
  423. $stm->execute();
  424. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  425. $all = empty($res[0]) ? 0 : $res[0];
  426. $unread = empty($res[1]) ? 0 : $res[1];
  427. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  428. }
  429. public function count($minPriority = null) {
  430. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id';
  431. if ($minPriority !== null) {
  432. $sql = ' WHERE priority > ' . intval($minPriority);
  433. }
  434. $stm = $this->bd->prepare($sql);
  435. $stm->execute();
  436. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  437. return $res[0];
  438. }
  439. public function countNotRead($minPriority = null) {
  440. $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';
  441. if ($minPriority !== null) {
  442. $sql = ' AND priority > ' . intval($minPriority);
  443. }
  444. $stm = $this->bd->prepare($sql);
  445. $stm->execute();
  446. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  447. return $res[0];
  448. }
  449. public function countUnreadReadFavorites() {
  450. $sql = 'SELECT c FROM ('
  451. . 'SELECT COUNT(id) AS c, 1 as o FROM `' . $this->prefix . 'entry` WHERE is_favorite=1 '
  452. . 'UNION SELECT COUNT(id) AS c, 2 AS o FROM `' . $this->prefix . 'entry` WHERE is_favorite=1 AND is_read=0'
  453. . ') u ORDER BY o';
  454. $stm = $this->bd->prepare($sql);
  455. $stm->execute();
  456. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  457. $all = empty($res[0]) ? 0 : $res[0];
  458. $unread = empty($res[1]) ? 0 : $res[1];
  459. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  460. }
  461. public function optimizeTable() {
  462. $sql = 'OPTIMIZE TABLE `' . $this->prefix . 'entry`'; //MySQL
  463. $stm = $this->bd->prepare($sql);
  464. $stm->execute();
  465. }
  466. public function size($all = false) {
  467. $db = Minz_Configuration::dataBase();
  468. $sql = 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema=?'; //MySQL
  469. $values = array($db['base']);
  470. if (!$all) {
  471. $sql .= ' AND table_name LIKE ?';
  472. $values[] = $this->prefix . '%';
  473. }
  474. $stm = $this->bd->prepare($sql);
  475. $stm->execute($values);
  476. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  477. return $res[0];
  478. }
  479. public static function daoToEntry($listDAO) {
  480. $list = array();
  481. if (!is_array($listDAO)) {
  482. $listDAO = array($listDAO);
  483. }
  484. foreach ($listDAO as $key => $dao) {
  485. $entry = new FreshRSS_Entry(
  486. $dao['id_feed'],
  487. $dao['guid'],
  488. $dao['title'],
  489. $dao['author'],
  490. $dao['content'],
  491. $dao['link'],
  492. $dao['date'],
  493. $dao['is_read'],
  494. $dao['is_favorite'],
  495. $dao['tags']
  496. );
  497. if (isset($dao['id'])) {
  498. $entry->_id($dao['id']);
  499. }
  500. $list[] = $entry;
  501. }
  502. unset($listDAO);
  503. return $list;
  504. }
  505. }