FeedDAO.php 14 KB

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