FeedDAO.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. class FreshRSS_FeedDAO extends Minz_ModelPdo {
  3. public function addFeed ($valuesTmp) {
  4. $sql = 'INSERT INTO `' . $this->prefix . 'feed` (url, category, name, website, description, lastUpdate, priority, httpAuth, error, keep_history) VALUES(?, ?, ?, ?, ?, ?, 10, ?, 0, -2)';
  5. $stm = $this->bd->prepare ($sql);
  6. $values = array (
  7. substr($valuesTmp['url'], 0, 511),
  8. $valuesTmp['category'],
  9. substr($valuesTmp['name'], 0, 255),
  10. substr($valuesTmp['website'], 0, 255),
  11. substr($valuesTmp['description'], 0, 1023),
  12. $valuesTmp['lastUpdate'],
  13. base64_encode ($valuesTmp['httpAuth']),
  14. );
  15. if ($stm && $stm->execute ($values)) {
  16. return $this->bd->lastInsertId();
  17. } else {
  18. $info = $stm->errorInfo();
  19. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  20. return false;
  21. }
  22. }
  23. public function updateFeed ($id, $valuesTmp) {
  24. $set = '';
  25. foreach ($valuesTmp as $key => $v) {
  26. $set .= $key . '=?, ';
  27. if ($key == 'httpAuth') {
  28. $valuesTmp[$key] = base64_encode ($v);
  29. }
  30. }
  31. $set = substr ($set, 0, -2);
  32. $sql = 'UPDATE `' . $this->prefix . 'feed` SET ' . $set . ' WHERE id=?';
  33. $stm = $this->bd->prepare ($sql);
  34. foreach ($valuesTmp as $v) {
  35. $values[] = $v;
  36. }
  37. $values[] = $id;
  38. if ($stm && $stm->execute ($values)) {
  39. return $stm->rowCount();
  40. } else {
  41. $info = $stm->errorInfo();
  42. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  43. return false;
  44. }
  45. }
  46. public function updateLastUpdate ($id, $inError = 0, $updateCache = true) {
  47. if ($updateCache) {
  48. $sql = 'UPDATE `' . $this->prefix . 'feed` f ' //2 sub-requests with FOREIGN KEY(e.id_feed), INDEX(e.is_read) faster than 1 request with GROUP BY or CASE
  49. . 'SET f.cache_nbEntries=(SELECT COUNT(e1.id) FROM `' . $this->prefix . 'entry` e1 WHERE e1.id_feed=f.id),'
  50. . 'f.cache_nbUnreads=(SELECT COUNT(e2.id) FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed=f.id AND e2.is_read=0),'
  51. . 'lastUpdate=?, error=? '
  52. . 'WHERE f.id=?';
  53. } else {
  54. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  55. . 'SET lastUpdate=?, error=? '
  56. . 'WHERE f.id=?';
  57. }
  58. $values = array (
  59. time(),
  60. $inError,
  61. $id,
  62. );
  63. $stm = $this->bd->prepare ($sql);
  64. if ($stm && $stm->execute ($values)) {
  65. return $stm->rowCount();
  66. } else {
  67. $info = $stm->errorInfo();
  68. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  69. return false;
  70. }
  71. }
  72. public function changeCategory ($idOldCat, $idNewCat) {
  73. $catDAO = new FreshRSS_CategoryDAO ();
  74. $newCat = $catDAO->searchById ($idNewCat);
  75. if (!$newCat) {
  76. $newCat = $catDAO->getDefault ();
  77. }
  78. $sql = 'UPDATE `' . $this->prefix . 'feed` SET category=? WHERE category=?';
  79. $stm = $this->bd->prepare ($sql);
  80. $values = array (
  81. $newCat->id (),
  82. $idOldCat
  83. );
  84. if ($stm && $stm->execute ($values)) {
  85. return $stm->rowCount();
  86. } else {
  87. $info = $stm->errorInfo();
  88. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  89. return false;
  90. }
  91. }
  92. public function deleteFeed ($id) {
  93. /*//For MYISAM (MySQL 5.5-) without FOREIGN KEY
  94. $sql = 'DELETE FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
  95. $stm = $this->bd->prepare ($sql);
  96. $values = array ($id);
  97. if (!($stm && $stm->execute ($values))) {
  98. $info = $stm->errorInfo();
  99. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  100. return false;
  101. }*/
  102. $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE id=?';
  103. $stm = $this->bd->prepare ($sql);
  104. $values = array ($id);
  105. if ($stm && $stm->execute ($values)) {
  106. return $stm->rowCount();
  107. } else {
  108. $info = $stm->errorInfo();
  109. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  110. return false;
  111. }
  112. }
  113. public function deleteFeedByCategory ($id) {
  114. /*//For MYISAM (MySQL 5.5-) without FOREIGN KEY
  115. $sql = 'DELETE FROM `' . $this->prefix . 'entry` e '
  116. . 'INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  117. . 'WHERE f.category=?';
  118. $stm = $this->bd->prepare ($sql);
  119. $values = array ($id);
  120. if (!($stm && $stm->execute ($values))) {
  121. $info = $stm->errorInfo();
  122. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  123. return false;
  124. }*/
  125. $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE category=?';
  126. $stm = $this->bd->prepare ($sql);
  127. $values = array ($id);
  128. if ($stm && $stm->execute ($values)) {
  129. return $stm->rowCount();
  130. } else {
  131. $info = $stm->errorInfo();
  132. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  133. return false;
  134. }
  135. }
  136. public function searchById ($id) {
  137. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE id=?';
  138. $stm = $this->bd->prepare ($sql);
  139. $values = array ($id);
  140. $stm->execute ($values);
  141. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  142. $feed = self::daoToFeed ($res);
  143. if (isset ($feed[$id])) {
  144. return $feed[$id];
  145. } else {
  146. return false;
  147. }
  148. }
  149. public function searchByUrl ($url) {
  150. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE url=?';
  151. $stm = $this->bd->prepare ($sql);
  152. $values = array ($url);
  153. $stm->execute ($values);
  154. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  155. $feed = current (self::daoToFeed ($res));
  156. if (isset ($feed)) {
  157. return $feed;
  158. } else {
  159. return false;
  160. }
  161. }
  162. public function listFeeds () {
  163. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` ORDER BY name';
  164. $stm = $this->bd->prepare ($sql);
  165. $stm->execute ();
  166. return self::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  167. }
  168. public function listFeedsOrderUpdate () {
  169. $sql = 'SELECT id, name, url, lastUpdate, pathEntries, httpAuth, keep_history FROM `' . $this->prefix . 'feed` ORDER BY lastUpdate';
  170. $stm = $this->bd->prepare ($sql);
  171. $stm->execute ();
  172. return self::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  173. }
  174. public function listByCategory ($cat) {
  175. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE category=? ORDER BY name';
  176. $stm = $this->bd->prepare ($sql);
  177. $values = array ($cat);
  178. $stm->execute ($values);
  179. return self::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  180. }
  181. public function countEntries ($id) {
  182. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
  183. $stm = $this->bd->prepare ($sql);
  184. $values = array ($id);
  185. $stm->execute ($values);
  186. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  187. return $res[0]['count'];
  188. }
  189. public function countNotRead ($id) {
  190. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND is_read=0';
  191. $stm = $this->bd->prepare ($sql);
  192. $values = array ($id);
  193. $stm->execute ($values);
  194. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  195. return $res[0]['count'];
  196. }
  197. public function updateCachedValues () { //For one single feed, call updateLastUpdate($id)
  198. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  199. . 'INNER JOIN ('
  200. . 'SELECT e.id_feed, '
  201. . 'COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS nbUnreads, '
  202. . 'COUNT(e.id) AS nbEntries '
  203. . 'FROM `' . $this->prefix . 'entry` e '
  204. . 'GROUP BY e.id_feed'
  205. . ') x ON x.id_feed=f.id '
  206. . 'SET f.cache_nbEntries=x.nbEntries, f.cache_nbUnreads=x.nbUnreads';
  207. $stm = $this->bd->prepare ($sql);
  208. $values = array ($feed_id);
  209. if ($stm && $stm->execute ($values)) {
  210. return $stm->rowCount();
  211. } else {
  212. $info = $stm->errorInfo();
  213. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  214. return false;
  215. }
  216. }
  217. public function truncate ($id) {
  218. $sql = 'DELETE e.* FROM `' . $this->prefix . 'entry` e WHERE e.id_feed=?';
  219. $stm = $this->bd->prepare($sql);
  220. $values = array($id);
  221. $this->bd->beginTransaction ();
  222. if (!($stm && $stm->execute ($values))) {
  223. $info = $stm->errorInfo();
  224. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  225. $this->bd->rollBack ();
  226. return false;
  227. }
  228. $affected = $stm->rowCount();
  229. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  230. . 'SET f.cache_nbEntries=0, f.cache_nbUnreads=0 WHERE f.id=?';
  231. $values = array ($id);
  232. $stm = $this->bd->prepare ($sql);
  233. if (!($stm && $stm->execute ($values))) {
  234. $info = $stm->errorInfo();
  235. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  236. $this->bd->rollBack ();
  237. return false;
  238. }
  239. $this->bd->commit ();
  240. return $affected;
  241. }
  242. public function cleanOldEntries ($id, $date_min, $keep = 15) { //Remember to call updateLastUpdate($id) just after
  243. $sql = 'DELETE e.* FROM `' . $this->prefix . 'entry` e '
  244. . 'WHERE e.id_feed = :id_feed AND e.id <= :id_max AND e.is_favorite = 0 AND e.id NOT IN '
  245. . '(SELECT id FROM (SELECT e2.id FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed = :id_feed ORDER BY id DESC LIMIT :keep) keep)'; //Double select because of: MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
  246. $stm = $this->bd->prepare ($sql);
  247. $id_max = intval($date_min) . '000000';
  248. $stm->bindParam(':id_feed', $id, PDO::PARAM_INT);
  249. $stm->bindParam(':id_max', $id_max, PDO::PARAM_INT);
  250. $stm->bindParam(':keep', $keep, PDO::PARAM_INT);
  251. if ($stm && $stm->execute ()) {
  252. return $stm->rowCount();
  253. } else {
  254. $info = $stm->errorInfo();
  255. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  256. return false;
  257. }
  258. }
  259. public static function daoToFeed ($listDAO, $catID = null) {
  260. $list = array ();
  261. if (!is_array ($listDAO)) {
  262. $listDAO = array ($listDAO);
  263. }
  264. foreach ($listDAO as $key => $dao) {
  265. if (!isset ($dao['name'])) {
  266. continue;
  267. }
  268. if (isset ($dao['id'])) {
  269. $key = $dao['id'];
  270. }
  271. if ($catID === null) {
  272. $category = isset($dao['category']) ? $dao['category'] : 0;
  273. } else {
  274. $category = $catID ;
  275. }
  276. $myFeed = new FreshRSS_Feed(isset($dao['url']) ? $dao['url'] : '', false);
  277. $myFeed->_category($category);
  278. $myFeed->_name($dao['name']);
  279. $myFeed->_website(isset($dao['website']) ? $dao['website'] : '', false);
  280. $myFeed->_description(isset($dao['description']) ? $dao['description'] : '');
  281. $myFeed->_lastUpdate(isset($dao['lastUpdate']) ? $dao['lastUpdate'] : 0);
  282. $myFeed->_priority(isset($dao['priority']) ? $dao['priority'] : 10);
  283. $myFeed->_pathEntries(isset($dao['pathEntries']) ? $dao['pathEntries'] : '');
  284. $myFeed->_httpAuth(isset($dao['httpAuth']) ? base64_decode ($dao['httpAuth']) : '');
  285. $myFeed->_error(isset($dao['error']) ? $dao['error'] : 0);
  286. $myFeed->_keepHistory(isset($dao['keep_history']) ? $dao['keep_history'] : -2);
  287. $myFeed->_nbNotRead(isset($dao['cache_nbUnreads']) ? $dao['cache_nbUnreads'] : 0);
  288. $myFeed->_nbEntries(isset($dao['cache_nbEntries']) ? $dao['cache_nbEntries'] : 0);
  289. if (isset ($dao['id'])) {
  290. $myFeed->_id ($dao['id']);
  291. }
  292. $list[$key] = $myFeed;
  293. }
  294. return $list;
  295. }
  296. }