FeedDAO.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
  3. public function addFeed($valuesTmp) {
  4. $sql = 'INSERT INTO `' . $this->prefix . 'feed` (url, category, name, website, description, lastUpdate, priority, httpAuth, error, keep_history, ttl) VALUES(?, ?, ?, ?, ?, ?, 10, ?, 0, -2, -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 == null ? array(2 => 'syntax error') : $stm->errorInfo();
  19. Minz_Log::error('SQL error addFeed: ' . $info[2]);
  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 == null ? array(2 => 'syntax error') : $stm->errorInfo();
  67. Minz_Log::error('SQL error updateFeed: ' . $info[2]);
  68. return false;
  69. }
  70. }
  71. public function updateLastUpdate($id, $inError = 0, $updateCache = true) {
  72. if ($updateCache) {
  73. $sql = 'UPDATE `' . $this->prefix . 'feed` ' //2 sub-requests with FOREIGN KEY(e.id_feed), INDEX(e.is_read) faster than 1 request with GROUP BY or CASE
  74. . 'SET cache_nbEntries=(SELECT COUNT(e1.id) FROM `' . $this->prefix . 'entry` e1 WHERE e1.id_feed=`' . $this->prefix . 'feed`.id),'
  75. . 'cache_nbUnreads=(SELECT COUNT(e2.id) FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed=`' . $this->prefix . 'feed`.id AND e2.is_read=0),'
  76. . 'lastUpdate=?, error=? '
  77. . 'WHERE id=?';
  78. } else {
  79. $sql = 'UPDATE `' . $this->prefix . 'feed` '
  80. . 'SET lastUpdate=?, error=? '
  81. . 'WHERE 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 == null ? array(2 => 'syntax error') : $stm->errorInfo();
  93. Minz_Log::error('SQL error updateLastUpdate: ' . $info[2]);
  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 == null ? array(2 => 'syntax error') : $stm->errorInfo();
  113. Minz_Log::error('SQL error changeCategory: ' . $info[2]);
  114. return false;
  115. }
  116. }
  117. public function deleteFeed($id) {
  118. $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE id=?';
  119. $stm = $this->bd->prepare($sql);
  120. $values = array($id);
  121. if ($stm && $stm->execute($values)) {
  122. return $stm->rowCount();
  123. } else {
  124. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  125. Minz_Log::error('SQL error deleteFeed: ' . $info[2]);
  126. return false;
  127. }
  128. }
  129. public function deleteFeedByCategory($id) {
  130. $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE category=?';
  131. $stm = $this->bd->prepare($sql);
  132. $values = array($id);
  133. if ($stm && $stm->execute($values)) {
  134. return $stm->rowCount();
  135. } else {
  136. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  137. Minz_Log::error('SQL error deleteFeedByCategory: ' . $info[2]);
  138. return false;
  139. }
  140. }
  141. public function searchById($id) {
  142. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE id=?';
  143. $stm = $this->bd->prepare($sql);
  144. $values = array($id);
  145. $stm->execute($values);
  146. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  147. $feed = self::daoToFeed($res);
  148. if (isset($feed[$id])) {
  149. return $feed[$id];
  150. } else {
  151. return null;
  152. }
  153. }
  154. public function searchByUrl($url) {
  155. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE url=?';
  156. $stm = $this->bd->prepare($sql);
  157. $values = array($url);
  158. $stm->execute($values);
  159. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  160. $feed = current(self::daoToFeed($res));
  161. if (isset($feed) && $feed !== false) {
  162. return $feed;
  163. } else {
  164. return null;
  165. }
  166. }
  167. public function listFeeds() {
  168. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` ORDER BY name';
  169. $stm = $this->bd->prepare($sql);
  170. $stm->execute();
  171. return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  172. }
  173. public function arrayFeedCategoryNames() { //For API
  174. $sql = 'SELECT f.id, f.name, c.name as c_name FROM `' . $this->prefix . 'feed` f '
  175. . 'INNER JOIN `' . $this->prefix . 'category` c ON c.id = f.category';
  176. $stm = $this->bd->prepare($sql);
  177. $stm->execute();
  178. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  179. $feedCategoryNames = array();
  180. foreach ($res as $line) {
  181. $feedCategoryNames[$line['id']] = array(
  182. 'name' => $line['name'],
  183. 'c_name' => $line['c_name'],
  184. );
  185. }
  186. return $feedCategoryNames;
  187. }
  188. public function listFeedsOrderUpdate($defaultCacheDuration = 3600) {
  189. if ($defaultCacheDuration < 0) {
  190. $defaultCacheDuration = 2147483647;
  191. }
  192. $sql = 'SELECT id, url, name, website, lastUpdate, pathEntries, httpAuth, keep_history, ttl '
  193. . 'FROM `' . $this->prefix . 'feed` '
  194. . 'WHERE ttl <> -1 AND lastUpdate < (' . (time() + 60) . '-(CASE WHEN ttl=-2 THEN ' . intval($defaultCacheDuration) . ' ELSE ttl END)) '
  195. . 'ORDER BY lastUpdate';
  196. $stm = $this->bd->prepare($sql);
  197. if (!($stm && $stm->execute())) {
  198. $sql2 = 'ALTER TABLE `' . $this->prefix . 'feed` ADD COLUMN ttl INT NOT NULL DEFAULT -2'; //v0.7.3
  199. $stm = $this->bd->prepare($sql2);
  200. $stm->execute();
  201. $stm = $this->bd->prepare($sql);
  202. $stm->execute();
  203. }
  204. return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  205. }
  206. public function listByCategory($cat) {
  207. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE category=? ORDER BY name';
  208. $stm = $this->bd->prepare($sql);
  209. $values = array($cat);
  210. $stm->execute($values);
  211. return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  212. }
  213. public function countEntries($id) {
  214. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
  215. $stm = $this->bd->prepare($sql);
  216. $values = array($id);
  217. $stm->execute($values);
  218. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  219. return $res[0]['count'];
  220. }
  221. public function countNotRead($id) {
  222. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND is_read=0';
  223. $stm = $this->bd->prepare($sql);
  224. $values = array($id);
  225. $stm->execute($values);
  226. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  227. return $res[0]['count'];
  228. }
  229. public function updateCachedValues() { //For one single feed, call updateLastUpdate($id)
  230. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  231. . 'INNER JOIN ('
  232. . 'SELECT e.id_feed, '
  233. . 'COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS nbUnreads, '
  234. . 'COUNT(e.id) AS nbEntries '
  235. . 'FROM `' . $this->prefix . 'entry` e '
  236. . 'GROUP BY e.id_feed'
  237. . ') x ON x.id_feed=f.id '
  238. . 'SET f.cache_nbEntries=x.nbEntries, f.cache_nbUnreads=x.nbUnreads';
  239. $stm = $this->bd->prepare($sql);
  240. if ($stm && $stm->execute()) {
  241. return $stm->rowCount();
  242. } else {
  243. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  244. Minz_Log::error('SQL error updateCachedValues: ' . $info[2]);
  245. return false;
  246. }
  247. }
  248. public function truncate($id) {
  249. $sql = 'DELETE FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
  250. $stm = $this->bd->prepare($sql);
  251. $values = array($id);
  252. $this->bd->beginTransaction();
  253. if (!($stm && $stm->execute($values))) {
  254. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  255. Minz_Log::error('SQL error truncate: ' . $info[2]);
  256. $this->bd->rollBack();
  257. return false;
  258. }
  259. $affected = $stm->rowCount();
  260. $sql = 'UPDATE `' . $this->prefix . 'feed` '
  261. . 'SET cache_nbEntries=0, cache_nbUnreads=0 WHERE id=?';
  262. $values = array($id);
  263. $stm = $this->bd->prepare($sql);
  264. if (!($stm && $stm->execute($values))) {
  265. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  266. Minz_Log::error('SQL error truncate: ' . $info[2]);
  267. $this->bd->rollBack();
  268. return false;
  269. }
  270. $this->bd->commit();
  271. return $affected;
  272. }
  273. public function cleanOldEntries($id, $date_min, $keep = 15) { //Remember to call updateLastUpdate($id) or updateCachedValues() just after
  274. $sql = 'DELETE FROM `' . $this->prefix . 'entry` '
  275. . 'WHERE id_feed=:id_feed AND id<=:id_max '
  276. . 'AND is_favorite=0 ' //Do not remove favourites
  277. . 'AND lastSeen < (SELECT maxLastSeen FROM (SELECT (MAX(e3.lastSeen)-99) AS maxLastSeen FROM `' . $this->prefix . 'entry` e3 WHERE e3.id_feed=:id_feed) recent) ' //Do not remove the most newly seen articles, plus a few seconds of tolerance
  278. . 'AND id NOT IN (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: MySQL doesn't support 'LIMIT & IN/ALL/ANY/SOME subquery'
  279. $stm = $this->bd->prepare($sql);
  280. if ($stm) {
  281. $id_max = intval($date_min) . '000000';
  282. $stm->bindParam(':id_feed', $id, PDO::PARAM_INT);
  283. $stm->bindParam(':id_max', $id_max, PDO::PARAM_STR);
  284. $stm->bindParam(':keep', $keep, PDO::PARAM_INT);
  285. }
  286. if ($stm && $stm->execute()) {
  287. return $stm->rowCount();
  288. } else {
  289. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  290. Minz_Log::error('SQL error cleanOldEntries: ' . $info[2]);
  291. return false;
  292. }
  293. }
  294. public static function daoToFeed($listDAO, $catID = null) {
  295. $list = array();
  296. if (!is_array($listDAO)) {
  297. $listDAO = array($listDAO);
  298. }
  299. foreach ($listDAO as $key => $dao) {
  300. if (!isset($dao['name'])) {
  301. continue;
  302. }
  303. if (isset($dao['id'])) {
  304. $key = $dao['id'];
  305. }
  306. if ($catID === null) {
  307. $category = isset($dao['category']) ? $dao['category'] : 0;
  308. } else {
  309. $category = $catID ;
  310. }
  311. $myFeed = new FreshRSS_Feed(isset($dao['url']) ? $dao['url'] : '', false);
  312. $myFeed->_category($category);
  313. $myFeed->_name($dao['name']);
  314. $myFeed->_website(isset($dao['website']) ? $dao['website'] : '', false);
  315. $myFeed->_description(isset($dao['description']) ? $dao['description'] : '');
  316. $myFeed->_lastUpdate(isset($dao['lastUpdate']) ? $dao['lastUpdate'] : 0);
  317. $myFeed->_priority(isset($dao['priority']) ? $dao['priority'] : 10);
  318. $myFeed->_pathEntries(isset($dao['pathEntries']) ? $dao['pathEntries'] : '');
  319. $myFeed->_httpAuth(isset($dao['httpAuth']) ? base64_decode($dao['httpAuth']) : '');
  320. $myFeed->_error(isset($dao['error']) ? $dao['error'] : 0);
  321. $myFeed->_keepHistory(isset($dao['keep_history']) ? $dao['keep_history'] : -2);
  322. $myFeed->_ttl(isset($dao['ttl']) ? $dao['ttl'] : -2);
  323. $myFeed->_nbNotRead(isset($dao['cache_nbUnreads']) ? $dao['cache_nbUnreads'] : 0);
  324. $myFeed->_nbEntries(isset($dao['cache_nbEntries']) ? $dao['cache_nbEntries'] : 0);
  325. if (isset($dao['id'])) {
  326. $myFeed->_id($dao['id']);
  327. }
  328. $list[$key] = $myFeed;
  329. }
  330. return $list;
  331. }
  332. }