FeedDAO.php 13 KB

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