4
0

FeedDAO.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 addFeedObject($feed) {
  24. // TODO: not sure if we should write this method in DAO since DAO
  25. // should not be aware about feed class
  26. // Add feed only if we don't find it in DB
  27. $feed_search = $this->searchByUrl($feed->url());
  28. if (!$feed_search) {
  29. $values = array(
  30. 'id' => $feed->id(),
  31. 'url' => $feed->url(),
  32. 'category' => $feed->category(),
  33. 'name' => $feed->name(),
  34. 'website' => $feed->website(),
  35. 'description' => $feed->description(),
  36. 'lastUpdate' => 0,
  37. 'httpAuth' => $feed->httpAuth()
  38. );
  39. $id = $this->addFeed($values);
  40. if ($id) {
  41. $feed->_id($id);
  42. $feed->faviconPrepare();
  43. }
  44. return $id;
  45. }
  46. return $feed_search->id();
  47. }
  48. public function updateFeed ($id, $valuesTmp) {
  49. $set = '';
  50. foreach ($valuesTmp as $key => $v) {
  51. $set .= $key . '=?, ';
  52. if ($key == 'httpAuth') {
  53. $valuesTmp[$key] = base64_encode ($v);
  54. }
  55. }
  56. $set = substr ($set, 0, -2);
  57. $sql = 'UPDATE `' . $this->prefix . 'feed` SET ' . $set . ' WHERE id=?';
  58. $stm = $this->bd->prepare ($sql);
  59. foreach ($valuesTmp as $v) {
  60. $values[] = $v;
  61. }
  62. $values[] = $id;
  63. if ($stm && $stm->execute ($values)) {
  64. return $stm->rowCount();
  65. } else {
  66. $info = $stm->errorInfo();
  67. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  68. return false;
  69. }
  70. }
  71. public function updateLastUpdate ($id, $inError = 0, $updateCache = true) {
  72. if ($updateCache) {
  73. $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
  74. . 'SET f.cache_nbEntries=(SELECT COUNT(e1.id) FROM `' . $this->prefix . 'entry` e1 WHERE e1.id_feed=f.id),'
  75. . 'f.cache_nbUnreads=(SELECT COUNT(e2.id) FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed=f.id AND e2.is_read=0),'
  76. . 'lastUpdate=?, error=? '
  77. . 'WHERE f.id=?';
  78. } else {
  79. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  80. . 'SET lastUpdate=?, error=? '
  81. . 'WHERE f.id=?';
  82. }
  83. $values = array (
  84. time(),
  85. $inError,
  86. $id,
  87. );
  88. $stm = $this->bd->prepare ($sql);
  89. if ($stm && $stm->execute ($values)) {
  90. return $stm->rowCount();
  91. } else {
  92. $info = $stm->errorInfo();
  93. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  94. return false;
  95. }
  96. }
  97. public function changeCategory ($idOldCat, $idNewCat) {
  98. $catDAO = new FreshRSS_CategoryDAO ();
  99. $newCat = $catDAO->searchById ($idNewCat);
  100. if (!$newCat) {
  101. $newCat = $catDAO->getDefault ();
  102. }
  103. $sql = 'UPDATE `' . $this->prefix . 'feed` SET category=? WHERE category=?';
  104. $stm = $this->bd->prepare ($sql);
  105. $values = array (
  106. $newCat->id (),
  107. $idOldCat
  108. );
  109. if ($stm && $stm->execute ($values)) {
  110. return $stm->rowCount();
  111. } else {
  112. $info = $stm->errorInfo();
  113. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  114. return false;
  115. }
  116. }
  117. public function deleteFeed ($id) {
  118. /*//For MYISAM (MySQL 5.5-) without FOREIGN KEY
  119. $sql = 'DELETE FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
  120. $stm = $this->bd->prepare ($sql);
  121. $values = array ($id);
  122. if (!($stm && $stm->execute ($values))) {
  123. $info = $stm->errorInfo();
  124. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  125. return false;
  126. }*/
  127. $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE id=?';
  128. $stm = $this->bd->prepare ($sql);
  129. $values = array ($id);
  130. if ($stm && $stm->execute ($values)) {
  131. return $stm->rowCount();
  132. } else {
  133. $info = $stm->errorInfo();
  134. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  135. return false;
  136. }
  137. }
  138. public function deleteFeedByCategory ($id) {
  139. /*//For MYISAM (MySQL 5.5-) without FOREIGN KEY
  140. $sql = 'DELETE FROM `' . $this->prefix . 'entry` e '
  141. . 'INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  142. . 'WHERE f.category=?';
  143. $stm = $this->bd->prepare ($sql);
  144. $values = array ($id);
  145. if (!($stm && $stm->execute ($values))) {
  146. $info = $stm->errorInfo();
  147. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  148. return false;
  149. }*/
  150. $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE category=?';
  151. $stm = $this->bd->prepare ($sql);
  152. $values = array ($id);
  153. if ($stm && $stm->execute ($values)) {
  154. return $stm->rowCount();
  155. } else {
  156. $info = $stm->errorInfo();
  157. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  158. return false;
  159. }
  160. }
  161. public function searchById ($id) {
  162. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE id=?';
  163. $stm = $this->bd->prepare ($sql);
  164. $values = array ($id);
  165. $stm->execute ($values);
  166. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  167. $feed = self::daoToFeed ($res);
  168. if (isset ($feed[$id])) {
  169. return $feed[$id];
  170. } else {
  171. return null;
  172. }
  173. }
  174. public function searchByUrl ($url) {
  175. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE url=?';
  176. $stm = $this->bd->prepare ($sql);
  177. $values = array ($url);
  178. $stm->execute ($values);
  179. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  180. $feed = current (self::daoToFeed ($res));
  181. if (isset ($feed)) {
  182. return $feed;
  183. } else {
  184. return null;
  185. }
  186. }
  187. public function listFeeds () {
  188. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` ORDER BY name';
  189. $stm = $this->bd->prepare ($sql);
  190. $stm->execute ();
  191. return self::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  192. }
  193. public function arrayFeedCategoryNames() { //For API
  194. $sql = 'SELECT f.id, f.name, c.name as c_name FROM `' . $this->prefix . 'feed` f '
  195. . 'INNER JOIN `' . $this->prefix . 'category` c ON c.id = f.category';
  196. $stm = $this->bd->prepare ($sql);
  197. $stm->execute ();
  198. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  199. $feedCategoryNames = array();
  200. foreach ($res as $line) {
  201. $feedCategoryNames[$line['id']] = array(
  202. 'name' => $line['name'],
  203. 'c_name' => $line['c_name'],
  204. );
  205. }
  206. return $feedCategoryNames;
  207. }
  208. public function listFeedsOrderUpdate ($cacheDuration = 1500) {
  209. $sql = 'SELECT id, name, url, lastUpdate, pathEntries, httpAuth, keep_history '
  210. . 'FROM `' . $this->prefix . 'feed` '
  211. . 'WHERE lastUpdate < ' . (time() - intval($cacheDuration))
  212. . ' ORDER BY lastUpdate';
  213. $stm = $this->bd->prepare ($sql);
  214. $stm->execute ();
  215. return self::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  216. }
  217. public function listByCategory ($cat) {
  218. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE category=? ORDER BY name';
  219. $stm = $this->bd->prepare ($sql);
  220. $values = array ($cat);
  221. $stm->execute ($values);
  222. return self::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  223. }
  224. public function countEntries ($id) {
  225. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
  226. $stm = $this->bd->prepare ($sql);
  227. $values = array ($id);
  228. $stm->execute ($values);
  229. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  230. return $res[0]['count'];
  231. }
  232. public function countNotRead ($id) {
  233. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND is_read=0';
  234. $stm = $this->bd->prepare ($sql);
  235. $values = array ($id);
  236. $stm->execute ($values);
  237. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  238. return $res[0]['count'];
  239. }
  240. public function updateCachedValues () { //For one single feed, call updateLastUpdate($id)
  241. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  242. . 'INNER JOIN ('
  243. . 'SELECT e.id_feed, '
  244. . 'COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS nbUnreads, '
  245. . 'COUNT(e.id) AS nbEntries '
  246. . 'FROM `' . $this->prefix . 'entry` e '
  247. . 'GROUP BY e.id_feed'
  248. . ') x ON x.id_feed=f.id '
  249. . 'SET f.cache_nbEntries=x.nbEntries, f.cache_nbUnreads=x.nbUnreads';
  250. $stm = $this->bd->prepare ($sql);
  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 function truncate ($id) {
  260. $sql = 'DELETE e.* FROM `' . $this->prefix . 'entry` e WHERE e.id_feed=?';
  261. $stm = $this->bd->prepare($sql);
  262. $values = array($id);
  263. $this->bd->beginTransaction ();
  264. if (!($stm && $stm->execute ($values))) {
  265. $info = $stm->errorInfo();
  266. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  267. $this->bd->rollBack ();
  268. return false;
  269. }
  270. $affected = $stm->rowCount();
  271. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  272. . 'SET f.cache_nbEntries=0, f.cache_nbUnreads=0 WHERE f.id=?';
  273. $values = array ($id);
  274. $stm = $this->bd->prepare ($sql);
  275. if (!($stm && $stm->execute ($values))) {
  276. $info = $stm->errorInfo();
  277. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  278. $this->bd->rollBack ();
  279. return false;
  280. }
  281. $this->bd->commit ();
  282. return $affected;
  283. }
  284. public function cleanOldEntries ($id, $date_min, $keep = 15) { //Remember to call updateLastUpdate($id) just after
  285. $sql = 'DELETE e.* FROM `' . $this->prefix . 'entry` e '
  286. . 'WHERE e.id_feed = :id_feed AND e.id <= :id_max AND e.is_favorite = 0 AND e.id NOT IN '
  287. . '(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'
  288. $stm = $this->bd->prepare ($sql);
  289. $id_max = intval($date_min) . '000000';
  290. $stm->bindParam(':id_feed', $id, PDO::PARAM_INT);
  291. $stm->bindParam(':id_max', $id_max, PDO::PARAM_INT);
  292. $stm->bindParam(':keep', $keep, PDO::PARAM_INT);
  293. if ($stm && $stm->execute ()) {
  294. return $stm->rowCount();
  295. } else {
  296. $info = $stm->errorInfo();
  297. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  298. return false;
  299. }
  300. }
  301. public static function daoToFeed ($listDAO, $catID = null) {
  302. $list = array ();
  303. if (!is_array ($listDAO)) {
  304. $listDAO = array ($listDAO);
  305. }
  306. foreach ($listDAO as $key => $dao) {
  307. if (!isset ($dao['name'])) {
  308. continue;
  309. }
  310. if (isset ($dao['id'])) {
  311. $key = $dao['id'];
  312. }
  313. if ($catID === null) {
  314. $category = isset($dao['category']) ? $dao['category'] : 0;
  315. } else {
  316. $category = $catID ;
  317. }
  318. $myFeed = new FreshRSS_Feed(isset($dao['url']) ? $dao['url'] : '', false);
  319. $myFeed->_category($category);
  320. $myFeed->_name($dao['name']);
  321. $myFeed->_website(isset($dao['website']) ? $dao['website'] : '', false);
  322. $myFeed->_description(isset($dao['description']) ? $dao['description'] : '');
  323. $myFeed->_lastUpdate(isset($dao['lastUpdate']) ? $dao['lastUpdate'] : 0);
  324. $myFeed->_priority(isset($dao['priority']) ? $dao['priority'] : 10);
  325. $myFeed->_pathEntries(isset($dao['pathEntries']) ? $dao['pathEntries'] : '');
  326. $myFeed->_httpAuth(isset($dao['httpAuth']) ? base64_decode ($dao['httpAuth']) : '');
  327. $myFeed->_error(isset($dao['error']) ? $dao['error'] : 0);
  328. $myFeed->_keepHistory(isset($dao['keep_history']) ? $dao['keep_history'] : -2);
  329. $myFeed->_nbNotRead(isset($dao['cache_nbUnreads']) ? $dao['cache_nbUnreads'] : 0);
  330. $myFeed->_nbEntries(isset($dao['cache_nbEntries']) ? $dao['cache_nbEntries'] : 0);
  331. if (isset ($dao['id'])) {
  332. $myFeed->_id ($dao['id']);
  333. }
  334. $list[$key] = $myFeed;
  335. }
  336. return $list;
  337. }
  338. }