FeedDAO.php 13 KB

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