FeedDAO.php 12 KB

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