FeedDAO.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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('"' . $this->prefix . 'feed_id_seq"');
  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 = false, $updateCache = true, $mtime = 0) {
  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. if ($mtime <= 0) {
  84. $mtime = time();
  85. }
  86. $values = array(
  87. $mtime,
  88. $inError ? 1 : 0,
  89. $id,
  90. );
  91. $stm = $this->bd->prepare($sql);
  92. if ($stm && $stm->execute($values)) {
  93. return $stm->rowCount();
  94. } else {
  95. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  96. Minz_Log::error('SQL error updateLastUpdate: ' . $info[2]);
  97. return false;
  98. }
  99. }
  100. public function changeCategory($idOldCat, $idNewCat) {
  101. $catDAO = new FreshRSS_CategoryDAO();
  102. $newCat = $catDAO->searchById($idNewCat);
  103. if (!$newCat) {
  104. $newCat = $catDAO->getDefault();
  105. }
  106. $sql = 'UPDATE `' . $this->prefix . 'feed` SET category=? WHERE category=?';
  107. $stm = $this->bd->prepare($sql);
  108. $values = array(
  109. $newCat->id(),
  110. $idOldCat
  111. );
  112. if ($stm && $stm->execute($values)) {
  113. return $stm->rowCount();
  114. } else {
  115. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  116. Minz_Log::error('SQL error changeCategory: ' . $info[2]);
  117. return false;
  118. }
  119. }
  120. public function deleteFeed($id) {
  121. $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE id=?';
  122. $stm = $this->bd->prepare($sql);
  123. $values = array($id);
  124. if ($stm && $stm->execute($values)) {
  125. return $stm->rowCount();
  126. } else {
  127. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  128. Minz_Log::error('SQL error deleteFeed: ' . $info[2]);
  129. return false;
  130. }
  131. }
  132. public function deleteFeedByCategory($id) {
  133. $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE category=?';
  134. $stm = $this->bd->prepare($sql);
  135. $values = array($id);
  136. if ($stm && $stm->execute($values)) {
  137. return $stm->rowCount();
  138. } else {
  139. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  140. Minz_Log::error('SQL error deleteFeedByCategory: ' . $info[2]);
  141. return false;
  142. }
  143. }
  144. public function searchById($id) {
  145. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE id=?';
  146. $stm = $this->bd->prepare($sql);
  147. $values = array($id);
  148. $stm->execute($values);
  149. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  150. $feed = self::daoToFeed($res);
  151. if (isset($feed[$id])) {
  152. return $feed[$id];
  153. } else {
  154. return null;
  155. }
  156. }
  157. public function searchByUrl($url) {
  158. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE url=?';
  159. $stm = $this->bd->prepare($sql);
  160. $values = array($url);
  161. $stm->execute($values);
  162. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  163. $feed = current(self::daoToFeed($res));
  164. if (isset($feed) && $feed !== false) {
  165. return $feed;
  166. } else {
  167. return null;
  168. }
  169. }
  170. public function listFeeds() {
  171. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` ORDER BY name';
  172. $stm = $this->bd->prepare($sql);
  173. $stm->execute();
  174. return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  175. }
  176. public function arrayFeedCategoryNames() { //For API
  177. $sql = 'SELECT f.id, f.name, c.name as c_name FROM `' . $this->prefix . 'feed` f '
  178. . 'INNER JOIN `' . $this->prefix . 'category` c ON c.id = f.category';
  179. $stm = $this->bd->prepare($sql);
  180. $stm->execute();
  181. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  182. $feedCategoryNames = array();
  183. foreach ($res as $line) {
  184. $feedCategoryNames[$line['id']] = array(
  185. 'name' => $line['name'],
  186. 'c_name' => $line['c_name'],
  187. );
  188. }
  189. return $feedCategoryNames;
  190. }
  191. /**
  192. * Use $defaultCacheDuration == -1 to return all feeds, without filtering them by TTL.
  193. */
  194. public function listFeedsOrderUpdate($defaultCacheDuration = 3600) {
  195. $sql = 'SELECT id, url, name, website, `lastUpdate`, `pathEntries`, `httpAuth`, keep_history, ttl '
  196. . 'FROM `' . $this->prefix . 'feed` '
  197. . ($defaultCacheDuration < 0 ? '' : 'WHERE ttl <> -1 AND `lastUpdate` < (' . (time() + 60) . '-(CASE WHEN ttl=-2 THEN ' . intval($defaultCacheDuration) . ' ELSE ttl END)) ')
  198. . 'ORDER BY `lastUpdate`';
  199. $stm = $this->bd->prepare($sql);
  200. if (!($stm && $stm->execute())) {
  201. $sql2 = 'ALTER TABLE `' . $this->prefix . 'feed` ADD COLUMN ttl INT NOT NULL DEFAULT -2'; //v0.7.3
  202. $stm = $this->bd->prepare($sql2);
  203. $stm->execute();
  204. $stm = $this->bd->prepare($sql);
  205. $stm->execute();
  206. }
  207. return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  208. }
  209. public function listByCategory($cat) {
  210. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE category=? ORDER BY name';
  211. $stm = $this->bd->prepare($sql);
  212. $values = array($cat);
  213. $stm->execute($values);
  214. return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  215. }
  216. public function countEntries($id) {
  217. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
  218. $stm = $this->bd->prepare($sql);
  219. $values = array($id);
  220. $stm->execute($values);
  221. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  222. return $res[0]['count'];
  223. }
  224. public function countNotRead($id) {
  225. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND is_read=0';
  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 updateCachedValues() { //For one single feed, call updateLastUpdate($id)
  233. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  234. . 'INNER JOIN ('
  235. . 'SELECT e.id_feed, '
  236. . 'COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS nbUnreads, '
  237. . 'COUNT(e.id) AS nbEntries '
  238. . 'FROM `' . $this->prefix . 'entry` e '
  239. . 'GROUP BY e.id_feed'
  240. . ') x ON x.id_feed=f.id '
  241. . 'SET f.`cache_nbEntries`=x.nbEntries, f.`cache_nbUnreads`=x.nbUnreads';
  242. $stm = $this->bd->prepare($sql);
  243. if ($stm && $stm->execute()) {
  244. return $stm->rowCount();
  245. } else {
  246. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  247. Minz_Log::error('SQL error updateCachedValues: ' . $info[2]);
  248. return false;
  249. }
  250. }
  251. public function truncate($id) {
  252. $sql = 'DELETE FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
  253. $stm = $this->bd->prepare($sql);
  254. $values = array($id);
  255. $this->bd->beginTransaction();
  256. if (!($stm && $stm->execute($values))) {
  257. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  258. Minz_Log::error('SQL error truncate: ' . $info[2]);
  259. $this->bd->rollBack();
  260. return false;
  261. }
  262. $affected = $stm->rowCount();
  263. $sql = 'UPDATE `' . $this->prefix . 'feed` '
  264. . 'SET `cache_nbEntries`=0, `cache_nbUnreads`=0 WHERE id=?';
  265. $values = array($id);
  266. $stm = $this->bd->prepare($sql);
  267. if (!($stm && $stm->execute($values))) {
  268. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  269. Minz_Log::error('SQL error truncate: ' . $info[2]);
  270. $this->bd->rollBack();
  271. return false;
  272. }
  273. $this->bd->commit();
  274. return $affected;
  275. }
  276. public function cleanOldEntries($id, $date_min, $keep = 15) { //Remember to call updateLastUpdate($id) or updateCachedValues() just after
  277. $sql = 'DELETE FROM `' . $this->prefix . 'entry` '
  278. . 'WHERE id_feed=:id_feed AND id<=:id_max '
  279. . 'AND is_favorite=0 ' //Do not remove favourites
  280. . '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
  281. . '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'
  282. $stm = $this->bd->prepare($sql);
  283. if ($stm) {
  284. $id_max = intval($date_min) . '000000';
  285. $stm->bindParam(':id_feed', $id, PDO::PARAM_INT);
  286. $stm->bindParam(':id_max', $id_max, PDO::PARAM_STR);
  287. $stm->bindParam(':keep', $keep, PDO::PARAM_INT);
  288. }
  289. if ($stm && $stm->execute()) {
  290. return $stm->rowCount();
  291. } else {
  292. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  293. Minz_Log::error('SQL error cleanOldEntries: ' . $info[2]);
  294. return false;
  295. }
  296. }
  297. public static function daoToFeed($listDAO, $catID = null) {
  298. $list = array();
  299. if (!is_array($listDAO)) {
  300. $listDAO = array($listDAO);
  301. }
  302. foreach ($listDAO as $key => $dao) {
  303. if (!isset($dao['name'])) {
  304. continue;
  305. }
  306. if (isset($dao['id'])) {
  307. $key = $dao['id'];
  308. }
  309. if ($catID === null) {
  310. $category = isset($dao['category']) ? $dao['category'] : 0;
  311. } else {
  312. $category = $catID ;
  313. }
  314. $myFeed = new FreshRSS_Feed(isset($dao['url']) ? $dao['url'] : '', false);
  315. $myFeed->_category($category);
  316. $myFeed->_name($dao['name']);
  317. $myFeed->_website(isset($dao['website']) ? $dao['website'] : '', false);
  318. $myFeed->_description(isset($dao['description']) ? $dao['description'] : '');
  319. $myFeed->_lastUpdate(isset($dao['lastUpdate']) ? $dao['lastUpdate'] : 0);
  320. $myFeed->_priority(isset($dao['priority']) ? $dao['priority'] : 10);
  321. $myFeed->_pathEntries(isset($dao['pathEntries']) ? $dao['pathEntries'] : '');
  322. $myFeed->_httpAuth(isset($dao['httpAuth']) ? base64_decode($dao['httpAuth']) : '');
  323. $myFeed->_error(isset($dao['error']) ? $dao['error'] : 0);
  324. $myFeed->_keepHistory(isset($dao['keep_history']) ? $dao['keep_history'] : -2);
  325. $myFeed->_ttl(isset($dao['ttl']) ? $dao['ttl'] : -2);
  326. $myFeed->_nbNotRead(isset($dao['cache_nbUnreads']) ? $dao['cache_nbUnreads'] : 0);
  327. $myFeed->_nbEntries(isset($dao['cache_nbEntries']) ? $dao['cache_nbEntries'] : 0);
  328. if (isset($dao['id'])) {
  329. $myFeed->_id($dao['id']);
  330. }
  331. $list[$key] = $myFeed;
  332. }
  333. return $list;
  334. }
  335. }