EntryDAO.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 === '' && parent::$sharedDbType === 'mysql') {
  320. $firstId = '9000000000' . '000000'; //MySQL optimization. Tested on MySQL 5.5 with 150k articles
  321. }
  322. if ($firstId !== '') {
  323. $where .= 'AND e1.id ' . ($order === 'DESC' ? '<=' : '>=') . $firstId . ' ';
  324. }
  325. if (($date_min > 0) && ($type !== 's')) {
  326. $where .= 'AND (e1.id >= ' . $date_min . '000000';
  327. if ($showOlderUnreadsorFavorites) { //Lax date constraint
  328. $where .= ' OR e1.is_read=0 OR e1.is_favorite=1 OR (f.keep_history <> 0';
  329. if (intval($keepHistoryDefault) === 0) {
  330. $where .= ' AND f.keep_history <> -2'; //default
  331. }
  332. $where .= ')';
  333. }
  334. $where .= ') ';
  335. $joinFeed = true;
  336. }
  337. $search = '';
  338. if ($filter !== '') {
  339. require_once(LIB_PATH . '/lib_date.php');
  340. $filter = trim($filter);
  341. $filter = addcslashes($filter, '\\%_');
  342. $terms = array_unique(explode(' ', $filter));
  343. //sort($terms); //Put #tags first //TODO: Put the cheapest filters first
  344. foreach ($terms as $word) {
  345. $word = trim($word);
  346. if (stripos($word, 'intitle:') === 0) {
  347. $word = substr($word, strlen('intitle:'));
  348. $search .= 'AND e1.title LIKE ? ';
  349. $values[] = '%' . $word .'%';
  350. } elseif (stripos($word, 'inurl:') === 0) {
  351. $word = substr($word, strlen('inurl:'));
  352. $search .= 'AND CONCAT(e1.link, e1.guid) LIKE ? ';
  353. $values[] = '%' . $word .'%';
  354. } elseif (stripos($word, 'author:') === 0) {
  355. $word = substr($word, strlen('author:'));
  356. $search .= 'AND e1.author LIKE ? ';
  357. $values[] = '%' . $word .'%';
  358. } elseif (stripos($word, 'date:') === 0) {
  359. $word = substr($word, strlen('date:'));
  360. list($minDate, $maxDate) = parseDateInterval($word);
  361. if ($minDate) {
  362. $search .= 'AND e1.id >= ' . $minDate . '000000 ';
  363. }
  364. if ($maxDate) {
  365. $search .= 'AND e1.id <= ' . $maxDate . '000000 ';
  366. }
  367. } elseif (stripos($word, 'pubdate:') === 0) {
  368. $word = substr($word, strlen('pubdate:'));
  369. list($minDate, $maxDate) = parseDateInterval($word);
  370. if ($minDate) {
  371. $search .= 'AND e1.date >= ' . $minDate . ' ';
  372. }
  373. if ($maxDate) {
  374. $search .= 'AND e1.date <= ' . $maxDate . ' ';
  375. }
  376. } else {
  377. if ($word[0] === '#' && isset($word[1])) {
  378. $search .= 'AND e1.tags LIKE ? ';
  379. $values[] = '%' . $word .'%';
  380. } else {
  381. $search .= 'AND ' . $this->sqlconcat('e1.title', $this->isCompressed() ? 'UNCOMPRESS(content_bin)' : 'content') . ' LIKE ? ';
  382. $values[] = '%' . $word .'%';
  383. }
  384. }
  385. }
  386. }
  387. return array($values,
  388. 'SELECT e1.id FROM `' . $this->prefix . 'entry` e1 '
  389. . ($joinFeed ? 'INNER JOIN `' . $this->prefix . 'feed` f ON e1.id_feed=f.id ' : '')
  390. . 'WHERE ' . $where
  391. . $search
  392. . 'ORDER BY e1.id ' . $order
  393. . ($limit > 0 ? ' LIMIT ' . $limit : '')); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  394. }
  395. public function listWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0, $showOlderUnreadsorFavorites = false, $keepHistoryDefault = 0) {
  396. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min, $showOlderUnreadsorFavorites, $keepHistoryDefault);
  397. $sql = 'SELECT e.id, e.guid, e.title, e.author, '
  398. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  399. . ', e.link, e.date, e.is_read, e.is_favorite, e.id_feed, e.tags '
  400. . 'FROM `' . $this->prefix . 'entry` e '
  401. . 'INNER JOIN ('
  402. . $sql
  403. . ') e2 ON e2.id=e.id '
  404. . 'ORDER BY e.id ' . $order;
  405. $stm = $this->bd->prepare($sql);
  406. $stm->execute($values);
  407. return self::daoToEntry($stm->fetchAll(PDO::FETCH_ASSOC));
  408. }
  409. 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
  410. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min, $showOlderUnreadsorFavorites, $keepHistoryDefault);
  411. $stm = $this->bd->prepare($sql);
  412. $stm->execute($values);
  413. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  414. }
  415. public function listLastGuidsByFeed($id, $n) {
  416. $sql = 'SELECT guid FROM `' . $this->prefix . 'entry` WHERE id_feed=? ORDER BY id DESC LIMIT ' . intval($n);
  417. $stm = $this->bd->prepare($sql);
  418. $values = array($id);
  419. $stm->execute($values);
  420. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  421. }
  422. public function countUnreadRead() {
  423. $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'
  424. . ' 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';
  425. $stm = $this->bd->prepare($sql);
  426. $stm->execute();
  427. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  428. $all = empty($res[0]) ? 0 : $res[0];
  429. $unread = empty($res[1]) ? 0 : $res[1];
  430. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  431. }
  432. public function count($minPriority = null) {
  433. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id';
  434. if ($minPriority !== null) {
  435. $sql = ' WHERE priority > ' . intval($minPriority);
  436. }
  437. $stm = $this->bd->prepare($sql);
  438. $stm->execute();
  439. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  440. return $res[0];
  441. }
  442. public function countNotRead($minPriority = null) {
  443. $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';
  444. if ($minPriority !== null) {
  445. $sql = ' AND priority > ' . intval($minPriority);
  446. }
  447. $stm = $this->bd->prepare($sql);
  448. $stm->execute();
  449. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  450. return $res[0];
  451. }
  452. public function countUnreadReadFavorites() {
  453. $sql = 'SELECT c FROM ('
  454. . 'SELECT COUNT(id) AS c, 1 as o FROM `' . $this->prefix . 'entry` WHERE is_favorite=1 '
  455. . 'UNION SELECT COUNT(id) AS c, 2 AS o FROM `' . $this->prefix . 'entry` WHERE is_favorite=1 AND is_read=0'
  456. . ') u ORDER BY o';
  457. $stm = $this->bd->prepare($sql);
  458. $stm->execute();
  459. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  460. $all = empty($res[0]) ? 0 : $res[0];
  461. $unread = empty($res[1]) ? 0 : $res[1];
  462. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  463. }
  464. public function optimizeTable() {
  465. $sql = 'OPTIMIZE TABLE `' . $this->prefix . 'entry`'; //MySQL
  466. $stm = $this->bd->prepare($sql);
  467. $stm->execute();
  468. }
  469. public function size($all = false) {
  470. $db = Minz_Configuration::dataBase();
  471. $sql = 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema=?'; //MySQL
  472. $values = array($db['base']);
  473. if (!$all) {
  474. $sql .= ' AND table_name LIKE ?';
  475. $values[] = $this->prefix . '%';
  476. }
  477. $stm = $this->bd->prepare($sql);
  478. $stm->execute($values);
  479. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  480. return $res[0];
  481. }
  482. public static function daoToEntry($listDAO) {
  483. $list = array();
  484. if (!is_array($listDAO)) {
  485. $listDAO = array($listDAO);
  486. }
  487. foreach ($listDAO as $key => $dao) {
  488. $entry = new FreshRSS_Entry(
  489. $dao['id_feed'],
  490. $dao['guid'],
  491. $dao['title'],
  492. $dao['author'],
  493. $dao['content'],
  494. $dao['link'],
  495. $dao['date'],
  496. $dao['is_read'],
  497. $dao['is_favorite'],
  498. $dao['tags']
  499. );
  500. if (isset($dao['id'])) {
  501. $entry->_id($dao['id']);
  502. }
  503. $list[] = $entry;
  504. }
  505. unset($listDAO);
  506. return $list;
  507. }
  508. }