FeedDAO.php 14 KB

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