EntryDAO.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. class FreshRSS_EntryDAO extends Minz_ModelPdo {
  3. public function addEntry ($valuesTmp) {
  4. $sql = 'INSERT INTO `' . $this->prefix . 'entry`(id, guid, title, author, content_bin, link, date, is_read, is_favorite, id_feed, tags) '
  5. . 'VALUES(?, ?, ?, ?, COMPRESS(?), ?, ?, ?, ?, ?, ?)';
  6. $stm = $this->bd->prepare ($sql);
  7. $values = array (
  8. $valuesTmp['id'],
  9. substr($valuesTmp['guid'], 0, 760),
  10. substr($valuesTmp['title'], 0, 255),
  11. substr($valuesTmp['author'], 0, 255),
  12. $valuesTmp['content'],
  13. substr($valuesTmp['link'], 0, 1023),
  14. $valuesTmp['date'],
  15. $valuesTmp['is_read'],
  16. $valuesTmp['is_favorite'],
  17. $valuesTmp['id_feed'],
  18. substr($valuesTmp['tags'], 0, 1023),
  19. );
  20. if ($stm && $stm->execute ($values)) {
  21. return $this->bd->lastInsertId();
  22. } else {
  23. $info = $stm->errorInfo();
  24. if ((int)($info[0] / 1000) !== 23) { //Filter out "SQLSTATE Class code 23: Constraint Violation" because of expected duplicate entries
  25. Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  26. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::ERROR);
  27. } /*else {
  28. Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  29. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::DEBUG);
  30. }*/
  31. return false;
  32. }
  33. }
  34. public function markFavorite ($id, $is_favorite = true) {
  35. $sql = 'UPDATE `' . $this->prefix . 'entry` e '
  36. . 'SET e.is_favorite = ? '
  37. . 'WHERE e.id=?';
  38. $values = array ($is_favorite ? 1 : 0, $id);
  39. $stm = $this->bd->prepare ($sql);
  40. if ($stm && $stm->execute ($values)) {
  41. return $stm->rowCount();
  42. } else {
  43. $info = $stm->errorInfo();
  44. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  45. return false;
  46. }
  47. }
  48. public function markRead ($id, $is_read = true) {
  49. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  50. . 'SET e.is_read = ?,'
  51. . 'f.cache_nbUnreads=f.cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
  52. . 'WHERE e.id=?';
  53. $values = array ($is_read ? 1 : 0, $id);
  54. $stm = $this->bd->prepare ($sql);
  55. if ($stm && $stm->execute ($values)) {
  56. return $stm->rowCount();
  57. } else {
  58. $info = $stm->errorInfo();
  59. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  60. return false;
  61. }
  62. }
  63. public function markReadEntries ($idMax = 0, $favorites = false) {
  64. if ($idMax === 0) {
  65. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  66. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  67. . 'WHERE e.is_read = 0 AND ';
  68. if ($favorites) {
  69. $sql .= 'e.is_favorite = 1';
  70. } else {
  71. $sql .= 'f.priority > 0';
  72. }
  73. $stm = $this->bd->prepare ($sql);
  74. if ($stm && $stm->execute ()) {
  75. return $stm->rowCount();
  76. } else {
  77. $info = $stm->errorInfo();
  78. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  79. return false;
  80. }
  81. } else {
  82. $this->bd->beginTransaction ();
  83. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  84. . 'SET e.is_read = 1 '
  85. . 'WHERE e.is_read = 0 AND e.id <= ? AND ';
  86. if ($favorites) {
  87. $sql .= 'e.is_favorite = 1';
  88. } else {
  89. $sql .= 'f.priority > 0';
  90. }
  91. $values = array ($idMax);
  92. $stm = $this->bd->prepare ($sql);
  93. if (!($stm && $stm->execute ($values))) {
  94. $info = $stm->errorInfo();
  95. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  96. $this->bd->rollBack ();
  97. return false;
  98. }
  99. $affected = $stm->rowCount();
  100. if ($affected > 0) {
  101. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  102. . 'LEFT OUTER JOIN ('
  103. . 'SELECT e.id_feed, '
  104. . 'COUNT(*) AS nbUnreads '
  105. . 'FROM `' . $this->prefix . 'entry` e '
  106. . 'WHERE e.is_read = 0 '
  107. . 'GROUP BY e.id_feed'
  108. . ') x ON x.id_feed=f.id '
  109. . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0)';
  110. $stm = $this->bd->prepare ($sql);
  111. if (!($stm && $stm->execute ())) {
  112. $info = $stm->errorInfo();
  113. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  114. $this->bd->rollBack ();
  115. return false;
  116. }
  117. }
  118. $this->bd->commit ();
  119. return $affected;
  120. }
  121. }
  122. public function markReadCat ($id, $idMax = 0) {
  123. if ($idMax === 0) {
  124. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  125. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  126. . 'WHERE f.category = ? AND e.is_read = 0';
  127. $values = array ($id);
  128. $stm = $this->bd->prepare ($sql);
  129. if ($stm && $stm->execute ($values)) {
  130. return $stm->rowCount();
  131. } else {
  132. $info = $stm->errorInfo();
  133. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  134. return false;
  135. }
  136. } else {
  137. $this->bd->beginTransaction ();
  138. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  139. . 'SET e.is_read = 1 '
  140. . 'WHERE f.category = ? AND e.is_read = 0 AND e.id <= ?';
  141. $values = array ($id, $idMax);
  142. $stm = $this->bd->prepare ($sql);
  143. if (!($stm && $stm->execute ($values))) {
  144. $info = $stm->errorInfo();
  145. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  146. $this->bd->rollBack ();
  147. return false;
  148. }
  149. $affected = $stm->rowCount();
  150. if ($affected > 0) {
  151. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  152. . 'LEFT OUTER JOIN ('
  153. . 'SELECT e.id_feed, '
  154. . 'COUNT(*) AS nbUnreads '
  155. . 'FROM `' . $this->prefix . 'entry` e '
  156. . 'WHERE e.is_read = 0 '
  157. . 'GROUP BY e.id_feed'
  158. . ') x ON x.id_feed=f.id '
  159. . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0) '
  160. . 'WHERE f.category = ?';
  161. $values = array ($id);
  162. $stm = $this->bd->prepare ($sql);
  163. if (!($stm && $stm->execute ($values))) {
  164. $info = $stm->errorInfo();
  165. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  166. $this->bd->rollBack ();
  167. return false;
  168. }
  169. }
  170. $this->bd->commit ();
  171. return $affected;
  172. }
  173. }
  174. public function markReadFeed ($id, $idMax = 0) {
  175. if ($idMax === 0) {
  176. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  177. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  178. . 'WHERE f.id=? AND e.is_read = 0';
  179. $values = array ($id);
  180. $stm = $this->bd->prepare ($sql);
  181. if ($stm && $stm->execute ($values)) {
  182. return $stm->rowCount();
  183. } else {
  184. $info = $stm->errorInfo();
  185. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  186. return false;
  187. }
  188. } else {
  189. $this->bd->beginTransaction ();
  190. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  191. . 'SET e.is_read = 1 '
  192. . 'WHERE f.id=? AND e.is_read = 0 AND e.id <= ?';
  193. $values = array ($id, $idMax);
  194. $stm = $this->bd->prepare ($sql);
  195. if (!($stm && $stm->execute ($values))) {
  196. $info = $stm->errorInfo();
  197. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  198. $this->bd->rollBack ();
  199. return false;
  200. }
  201. $affected = $stm->rowCount();
  202. if ($affected > 0) {
  203. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  204. . 'SET f.cache_nbUnreads=f.cache_nbUnreads-' . $affected
  205. . ' WHERE f.id=?';
  206. $values = array ($id);
  207. $stm = $this->bd->prepare ($sql);
  208. if (!($stm && $stm->execute ($values))) {
  209. $info = $stm->errorInfo();
  210. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  211. $this->bd->rollBack ();
  212. return false;
  213. }
  214. }
  215. $this->bd->commit ();
  216. return $affected;
  217. }
  218. }
  219. public function searchByGuid ($feed_id, $id) {
  220. // un guid est unique pour un flux donné
  221. $sql = 'SELECT id, guid, title, author, UNCOMPRESS(content_bin) AS content, link, date, is_read, is_favorite, id_feed, tags '
  222. . 'FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid=?';
  223. $stm = $this->bd->prepare ($sql);
  224. $values = array (
  225. $feed_id,
  226. $id
  227. );
  228. $stm->execute ($values);
  229. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  230. $entries = HelperEntry::daoToEntry ($res);
  231. return isset ($entries[0]) ? $entries[0] : false;
  232. }
  233. public function searchById ($id) {
  234. $sql = 'SELECT id, guid, title, author, UNCOMPRESS(content_bin) AS content, link, date, is_read, is_favorite, id_feed, tags '
  235. . 'FROM `' . $this->prefix . 'entry` WHERE id=?';
  236. $stm = $this->bd->prepare ($sql);
  237. $values = array ($id);
  238. $stm->execute ($values);
  239. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  240. $entries = HelperEntry::daoToEntry ($res);
  241. return isset ($entries[0]) ? $entries[0] : false;
  242. }
  243. public function listWhere($type = 'a', $id = '', $state = 'all', $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0) {
  244. $where = '';
  245. $values = array();
  246. switch ($type) {
  247. case 'a':
  248. $where .= 'priority > 0 ';
  249. break;
  250. case 's':
  251. $where .= 'is_favorite = 1 ';
  252. break;
  253. case 'c':
  254. $where .= 'category = ? ';
  255. $values[] = intval($id);
  256. break;
  257. case 'f':
  258. $where .= 'id_feed = ? ';
  259. $values[] = intval($id);
  260. break;
  261. default:
  262. throw new FreshRSS_EntriesGetter_Exception ('Bad type in Entry->listByType: [' . $type . ']!');
  263. }
  264. switch ($state) {
  265. case 'all':
  266. break;
  267. case 'not_read':
  268. $where .= 'AND is_read = 0 ';
  269. break;
  270. case 'read':
  271. $where .= 'AND is_read = 1 ';
  272. break;
  273. default:
  274. throw new FreshRSS_EntriesGetter_Exception ('Bad state in Entry->listByType: [' . $state . ']!');
  275. }
  276. switch ($order) {
  277. case 'DESC':
  278. case 'ASC':
  279. break;
  280. default:
  281. throw new FreshRSS_EntriesGetter_Exception ('Bad order in Entry->listByType: [' . $order . ']!');
  282. }
  283. if ($firstId !== '') {
  284. $where .= 'AND e.id ' . ($order === 'DESC' ? '<=' : '>=') . $firstId . ' ';
  285. }
  286. if ($date_min > 0) {
  287. $where .= 'AND (e.id >= ' . $date_min . '000000 OR e.is_favorite = 1 OR f.keep_history = 1) ';
  288. }
  289. $terms = array_unique(explode(' ', trim($filter)));
  290. sort($terms); //Put #tags first
  291. $having = '';
  292. foreach ($terms as $word) {
  293. if (!empty($word)) {
  294. if ($word[0] === '#' && isset($word[1])) {
  295. $having .= 'AND tags LIKE ? ';
  296. $values[] = '%' . $word .'%';
  297. } elseif (!empty($word)) {
  298. $having .= 'AND (e.title LIKE ? OR content LIKE ?) ';
  299. $values[] = '%' . $word .'%';
  300. $values[] = '%' . $word .'%';
  301. }
  302. }
  303. }
  304. $sql = 'SELECT e.id, e.guid, e.title, e.author, UNCOMPRESS(e.content_bin) AS content, e.link, e.date, e.is_read, e.is_favorite, e.id_feed, e.tags '
  305. . 'FROM `' . $this->prefix . 'entry` e '
  306. . 'INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id WHERE ' . $where
  307. . (empty($having) ? '' : 'HAVING' . substr($having, 3))
  308. . 'ORDER BY e.id ' . $order;
  309. if ($limit > 0) {
  310. $sql .= ' LIMIT ' . $limit; //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  311. }
  312. $stm = $this->bd->prepare ($sql);
  313. $stm->execute ($values);
  314. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  315. }
  316. public function listLastGuidsByFeed($id, $n) {
  317. $sql = 'SELECT guid FROM `' . $this->prefix . 'entry` WHERE id_feed=? ORDER BY id DESC LIMIT ' . intval($n);
  318. $stm = $this->bd->prepare ($sql);
  319. $values = array ($id);
  320. $stm->execute ($values);
  321. return $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  322. }
  323. public function countUnreadRead () {
  324. $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'
  325. . ' 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';
  326. $stm = $this->bd->prepare ($sql);
  327. $stm->execute ();
  328. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  329. $all = empty($res[0]) ? 0 : $res[0];
  330. $unread = empty($res[1]) ? 0 : $res[1];
  331. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  332. }
  333. public function count ($minPriority = null) {
  334. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id';
  335. if ($minPriority !== null) {
  336. $sql = ' WHERE priority > ' . intval($minPriority);
  337. }
  338. $stm = $this->bd->prepare ($sql);
  339. $stm->execute ();
  340. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  341. return $res[0];
  342. }
  343. public function countNotRead ($minPriority = null) {
  344. $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';
  345. if ($minPriority !== null) {
  346. $sql = ' AND priority > ' . intval($minPriority);
  347. }
  348. $stm = $this->bd->prepare ($sql);
  349. $stm->execute ();
  350. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  351. return $res[0];
  352. }
  353. public function countUnreadReadFavorites () {
  354. $sql = 'SELECT COUNT(id) FROM `' . $this->prefix . 'entry` WHERE is_favorite=1'
  355. . ' UNION SELECT COUNT(id) FROM `' . $this->prefix . 'entry` WHERE is_favorite=1 AND is_read = 0';
  356. $stm = $this->bd->prepare ($sql);
  357. $stm->execute ();
  358. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  359. $all = empty($res[0]) ? 0 : $res[0];
  360. $unread = empty($res[1]) ? 0 : $res[1];
  361. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  362. }
  363. public function optimizeTable() {
  364. $sql = 'OPTIMIZE TABLE `' . $this->prefix . 'entry`';
  365. $stm = $this->bd->prepare ($sql);
  366. $stm->execute ();
  367. }
  368. }
  369. class HelperEntry {
  370. public static function daoToEntry ($listDAO) {
  371. $list = array ();
  372. if (!is_array ($listDAO)) {
  373. $listDAO = array ($listDAO);
  374. }
  375. foreach ($listDAO as $key => $dao) {
  376. $entry = new FreshRSS_Entry (
  377. $dao['id_feed'],
  378. $dao['guid'],
  379. $dao['title'],
  380. $dao['author'],
  381. $dao['content'],
  382. $dao['link'],
  383. $dao['date'],
  384. $dao['is_read'],
  385. $dao['is_favorite'],
  386. $dao['tags']
  387. );
  388. if (isset ($dao['id'])) {
  389. $entry->_id ($dao['id']);
  390. }
  391. $list[] = $entry;
  392. }
  393. unset ($listDAO);
  394. return $list;
  395. }
  396. }