FeedDAO.php 15 KB

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