FeedDAO.php 16 KB

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