FeedDAO.php 12 KB

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