FeedDAO.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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] === FreshRSS_DatabaseDAO::ER_BAD_FIELD_ERROR || $errorInfo[0] === FreshRSS_DatabaseDAOPGSQL::UNDEFINED_COLUMN) {
  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. mb_strcut(trim($valuesTmp['name']), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8'),
  53. substr($valuesTmp['website'], 0, 255),
  54. mb_strcut($valuesTmp['description'], 0, 1023, 'UTF-8'),
  55. $valuesTmp['lastUpdate'],
  56. base64_encode($valuesTmp['httpAuth']),
  57. FreshRSS_Feed::KEEP_HISTORY_DEFAULT,
  58. isset($valuesTmp['ttl']) ? intval($valuesTmp['ttl']) : 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. if ($feed->mute() || $feed->ttl() != FreshRSS_Context::$user_conf->ttl_default) {
  90. $values['ttl'] = $feed->ttl() * ($feed->mute() ? -1 : 1);
  91. }
  92. $id = $this->addFeed($values);
  93. if ($id) {
  94. $feed->_id($id);
  95. $feed->faviconPrepare();
  96. }
  97. return $id;
  98. }
  99. return $feed_search->id();
  100. }
  101. public function updateFeed($id, $valuesTmp) {
  102. if (isset($valuesTmp['name'])) {
  103. $valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8');
  104. }
  105. if (isset($valuesTmp['url'])) {
  106. $valuesTmp['url'] = safe_ascii($valuesTmp['url']);
  107. }
  108. if (isset($valuesTmp['website'])) {
  109. $valuesTmp['website'] = safe_ascii($valuesTmp['website']);
  110. }
  111. $set = '';
  112. foreach ($valuesTmp as $key => $v) {
  113. $set .= '`' . $key . '`=?, ';
  114. if ($key === 'httpAuth') {
  115. $valuesTmp[$key] = base64_encode($v);
  116. } elseif ($key === 'attributes') {
  117. $valuesTmp[$key] = json_encode($v);
  118. }
  119. }
  120. $set = substr($set, 0, -2);
  121. $sql = 'UPDATE `' . $this->prefix . 'feed` SET ' . $set . ' WHERE id=?';
  122. $stm = $this->bd->prepare($sql);
  123. foreach ($valuesTmp as $v) {
  124. $values[] = $v;
  125. }
  126. $values[] = $id;
  127. if ($stm && $stm->execute($values)) {
  128. return $stm->rowCount();
  129. } else {
  130. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  131. if ($this->autoUpdateDb($info)) {
  132. return $this->updateFeed($id, $valuesTmp);
  133. }
  134. Minz_Log::error('SQL error updateFeed: ' . $info[2] . ' for feed ' . $id);
  135. return false;
  136. }
  137. }
  138. public function updateFeedAttribute($feed, $key, $value) {
  139. if ($feed instanceof FreshRSS_Feed) {
  140. $feed->_attributes($key, $value);
  141. return $this->updateFeed(
  142. $feed->id(),
  143. array('attributes' => $feed->attributes())
  144. );
  145. }
  146. return false;
  147. }
  148. public function updateLastUpdate($id, $inError = false, $mtime = 0) { //See also updateCachedValue()
  149. $sql = 'UPDATE `' . $this->prefix . 'feed` '
  150. . 'SET `lastUpdate`=?, error=? '
  151. . 'WHERE id=?';
  152. $values = array(
  153. $mtime <= 0 ? time() : $mtime,
  154. $inError ? 1 : 0,
  155. $id,
  156. );
  157. $stm = $this->bd->prepare($sql);
  158. if ($stm && $stm->execute($values)) {
  159. return $stm->rowCount();
  160. } else {
  161. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  162. Minz_Log::error('SQL error updateLastUpdate: ' . $info[2]);
  163. return false;
  164. }
  165. }
  166. public function changeCategory($idOldCat, $idNewCat) {
  167. $catDAO = FreshRSS_Factory::createCategoryDao();
  168. $newCat = $catDAO->searchById($idNewCat);
  169. if (!$newCat) {
  170. $newCat = $catDAO->getDefault();
  171. }
  172. $sql = 'UPDATE `' . $this->prefix . 'feed` SET category=? WHERE category=?';
  173. $stm = $this->bd->prepare($sql);
  174. $values = array(
  175. $newCat->id(),
  176. $idOldCat
  177. );
  178. if ($stm && $stm->execute($values)) {
  179. return $stm->rowCount();
  180. } else {
  181. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  182. Minz_Log::error('SQL error changeCategory: ' . $info[2]);
  183. return false;
  184. }
  185. }
  186. public function deleteFeed($id) {
  187. $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE id=?';
  188. $stm = $this->bd->prepare($sql);
  189. $values = array($id);
  190. if ($stm && $stm->execute($values)) {
  191. return $stm->rowCount();
  192. } else {
  193. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  194. Minz_Log::error('SQL error deleteFeed: ' . $info[2]);
  195. return false;
  196. }
  197. }
  198. public function deleteFeedByCategory($id) {
  199. $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE category=?';
  200. $stm = $this->bd->prepare($sql);
  201. $values = array($id);
  202. if ($stm && $stm->execute($values)) {
  203. return $stm->rowCount();
  204. } else {
  205. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  206. Minz_Log::error('SQL error deleteFeedByCategory: ' . $info[2]);
  207. return false;
  208. }
  209. }
  210. public function searchById($id) {
  211. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE id=?';
  212. $stm = $this->bd->prepare($sql);
  213. $values = array($id);
  214. $stm->execute($values);
  215. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  216. $feed = self::daoToFeed($res);
  217. if (isset($feed[$id])) {
  218. return $feed[$id];
  219. } else {
  220. return null;
  221. }
  222. }
  223. public function searchByUrl($url) {
  224. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE url=?';
  225. $stm = $this->bd->prepare($sql);
  226. $values = array($url);
  227. $stm->execute($values);
  228. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  229. $feed = current(self::daoToFeed($res));
  230. if (isset($feed) && $feed !== false) {
  231. return $feed;
  232. } else {
  233. return null;
  234. }
  235. }
  236. public function listFeedsIds() {
  237. $sql = 'SELECT id FROM `' . $this->prefix . 'feed`';
  238. $stm = $this->bd->prepare($sql);
  239. $stm->execute();
  240. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  241. }
  242. public function listFeeds() {
  243. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` ORDER BY name';
  244. $stm = $this->bd->prepare($sql);
  245. $stm->execute();
  246. return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  247. }
  248. public function arrayFeedCategoryNames() { //For API
  249. $sql = 'SELECT f.id, f.name, c.name as c_name FROM `' . $this->prefix . 'feed` f '
  250. . 'INNER JOIN `' . $this->prefix . 'category` c ON c.id = f.category';
  251. $stm = $this->bd->prepare($sql);
  252. $stm->execute();
  253. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  254. $feedCategoryNames = array();
  255. foreach ($res as $line) {
  256. $feedCategoryNames[$line['id']] = array(
  257. 'name' => $line['name'],
  258. 'c_name' => $line['c_name'],
  259. );
  260. }
  261. return $feedCategoryNames;
  262. }
  263. /**
  264. * Use $defaultCacheDuration == -1 to return all feeds, without filtering them by TTL.
  265. */
  266. public function listFeedsOrderUpdate($defaultCacheDuration = 3600, $limit = 0) {
  267. $this->updateTTL();
  268. $sql = 'SELECT id, url, name, website, `lastUpdate`, `pathEntries`, `httpAuth`, keep_history, ttl, attributes '
  269. . 'FROM `' . $this->prefix . 'feed` '
  270. . ($defaultCacheDuration < 0 ? '' : 'WHERE ttl >= ' . FreshRSS_Feed::TTL_DEFAULT
  271. . ' AND `lastUpdate` < (' . (time() + 60) . '-(CASE WHEN ttl=' . FreshRSS_Feed::TTL_DEFAULT . ' THEN ' . intval($defaultCacheDuration) . ' ELSE ttl END)) ')
  272. . 'ORDER BY `lastUpdate` '
  273. . ($limit < 1 ? '' : 'LIMIT ' . intval($limit));
  274. $stm = $this->bd->prepare($sql);
  275. if ($stm && $stm->execute()) {
  276. return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  277. } else {
  278. $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
  279. if ($this->autoUpdateDb($info)) {
  280. return $this->listFeedsOrderUpdate($defaultCacheDuration);
  281. }
  282. Minz_Log::error('SQL error listFeedsOrderUpdate: ' . $info[2]);
  283. return array();
  284. }
  285. }
  286. public function listByCategory($cat) {
  287. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE category=? ORDER BY name';
  288. $stm = $this->bd->prepare($sql);
  289. $values = array($cat);
  290. $stm->execute($values);
  291. return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
  292. }
  293. public function countEntries($id) {
  294. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
  295. $stm = $this->bd->prepare($sql);
  296. $values = array($id);
  297. $stm->execute($values);
  298. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  299. return $res[0]['count'];
  300. }
  301. public function countNotRead($id) {
  302. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND is_read=0';
  303. $stm = $this->bd->prepare($sql);
  304. $values = array($id);
  305. $stm->execute($values);
  306. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  307. return $res[0]['count'];
  308. }
  309. public function updateCachedValue($id) { //For multiple feeds, call updateCachedValues()
  310. $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
  311. . 'SET `cache_nbEntries`=(SELECT COUNT(e1.id) FROM `' . $this->prefix . 'entry` e1 WHERE e1.id_feed=`' . $this->prefix . 'feed`.id),'
  312. . '`cache_nbUnreads`=(SELECT COUNT(e2.id) FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed=`' . $this->prefix . 'feed`.id AND e2.is_read=0) '
  313. . 'WHERE id=?';
  314. $values = array($id);
  315. $stm = $this->bd->prepare($sql);
  316. if ($stm && $stm->execute($values)) {
  317. return $stm->rowCount();
  318. } else {
  319. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  320. Minz_Log::error('SQL error updateCachedValue: ' . $info[2]);
  321. return false;
  322. }
  323. }
  324. public function updateCachedValues() { //For one single feed, call updateCachedValue($id)
  325. $sql = 'UPDATE `' . $this->prefix . 'feed` '
  326. . 'SET `cache_nbEntries`=(SELECT COUNT(e1.id) FROM `' . $this->prefix . 'entry` e1 WHERE e1.id_feed=`' . $this->prefix . 'feed`.id),'
  327. . '`cache_nbUnreads`=(SELECT COUNT(e2.id) FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed=`' . $this->prefix . 'feed`.id AND e2.is_read=0)';
  328. $stm = $this->bd->prepare($sql);
  329. if ($stm && $stm->execute()) {
  330. return $stm->rowCount();
  331. } else {
  332. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  333. Minz_Log::error('SQL error updateCachedValues: ' . $info[2]);
  334. return false;
  335. }
  336. }
  337. public function truncate($id) {
  338. $sql = 'DELETE FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
  339. $stm = $this->bd->prepare($sql);
  340. $values = array($id);
  341. $this->bd->beginTransaction();
  342. if (!($stm && $stm->execute($values))) {
  343. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  344. Minz_Log::error('SQL error truncate: ' . $info[2]);
  345. $this->bd->rollBack();
  346. return false;
  347. }
  348. $affected = $stm->rowCount();
  349. $sql = 'UPDATE `' . $this->prefix . 'feed` '
  350. . 'SET `cache_nbEntries`=0, `cache_nbUnreads`=0 WHERE id=?';
  351. $values = array($id);
  352. $stm = $this->bd->prepare($sql);
  353. if (!($stm && $stm->execute($values))) {
  354. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  355. Minz_Log::error('SQL error truncate: ' . $info[2]);
  356. $this->bd->rollBack();
  357. return false;
  358. }
  359. $this->bd->commit();
  360. return $affected;
  361. }
  362. public static function daoToFeed($listDAO, $catID = null) {
  363. $list = array();
  364. if (!is_array($listDAO)) {
  365. $listDAO = array($listDAO);
  366. }
  367. foreach ($listDAO as $key => $dao) {
  368. if (!isset($dao['name'])) {
  369. continue;
  370. }
  371. if (isset($dao['id'])) {
  372. $key = $dao['id'];
  373. }
  374. if ($catID === null) {
  375. $category = isset($dao['category']) ? $dao['category'] : 0;
  376. } else {
  377. $category = $catID;
  378. }
  379. $myFeed = new FreshRSS_Feed(isset($dao['url']) ? $dao['url'] : '', false);
  380. $myFeed->_category($category);
  381. $myFeed->_name($dao['name']);
  382. $myFeed->_website(isset($dao['website']) ? $dao['website'] : '', false);
  383. $myFeed->_description(isset($dao['description']) ? $dao['description'] : '');
  384. $myFeed->_lastUpdate(isset($dao['lastUpdate']) ? $dao['lastUpdate'] : 0);
  385. $myFeed->_priority(isset($dao['priority']) ? $dao['priority'] : 10);
  386. $myFeed->_pathEntries(isset($dao['pathEntries']) ? $dao['pathEntries'] : '');
  387. $myFeed->_httpAuth(isset($dao['httpAuth']) ? base64_decode($dao['httpAuth']) : '');
  388. $myFeed->_error(isset($dao['error']) ? $dao['error'] : 0);
  389. $myFeed->_keepHistory(isset($dao['keep_history']) ? $dao['keep_history'] : FreshRSS_Feed::KEEP_HISTORY_DEFAULT);
  390. $myFeed->_ttl(isset($dao['ttl']) ? $dao['ttl'] : FreshRSS_Feed::TTL_DEFAULT);
  391. $myFeed->_attributes('', isset($dao['attributes']) ? $dao['attributes'] : '');
  392. $myFeed->_nbNotRead(isset($dao['cache_nbUnreads']) ? $dao['cache_nbUnreads'] : 0);
  393. $myFeed->_nbEntries(isset($dao['cache_nbEntries']) ? $dao['cache_nbEntries'] : 0);
  394. if (isset($dao['id'])) {
  395. $myFeed->_id($dao['id']);
  396. }
  397. $list[$key] = $myFeed;
  398. }
  399. return $list;
  400. }
  401. public function updateTTL() {
  402. $sql = <<<SQL
  403. UPDATE `{$this->prefix}feed`
  404. SET ttl = :new_value
  405. WHERE ttl = :old_value
  406. SQL;
  407. $stm = $this->bd->prepare($sql);
  408. if (!($stm && $stm->execute(array(':new_value' => FreshRSS_Feed::TTL_DEFAULT, ':old_value' => -2)))) {
  409. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  410. Minz_Log::error('SQL warning updateTTL 1: ' . $info[2] . ' ' . $sql);
  411. $sql2 = 'ALTER TABLE `' . $this->prefix . 'feed` ADD COLUMN ttl INT NOT NULL DEFAULT ' . FreshRSS_Feed::TTL_DEFAULT; //v0.7.3
  412. $stm = $this->bd->prepare($sql2);
  413. if (!($stm && $stm->execute())) {
  414. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  415. Minz_Log::error('SQL error updateTTL 2: ' . $info[2] . ' ' . $sql2);
  416. }
  417. } else {
  418. $stm->execute(array(':new_value' => -3600, ':old_value' => -1));
  419. }
  420. }
  421. }