FeedDAO.php 13 KB

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