EntryDAO.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. <?php
  2. class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
  3. public function isCompressed() {
  4. return parent::$sharedDbType !== 'sqlite';
  5. }
  6. protected function autoAddColumn($errorInfo) {
  7. if (isset($errorInfo[0])) {
  8. if ($errorInfo[0] == '42S22') { //ER_BAD_FIELD_ERROR
  9. $hasTransaction = false;
  10. try {
  11. $stm = null;
  12. if (stripos($errorInfo[2], 'lastSeen') !== false) { //v1.2
  13. if (!$this->bd->inTransaction()) {
  14. $this->bd->beginTransaction();
  15. $hasTransaction = true;
  16. }
  17. $stm = $this->bd->prepare('ALTER TABLE `' . $this->prefix . 'entry` ADD COLUMN lastSeen INT(11) NOT NULL');
  18. if ($stm && $stm->execute()) {
  19. $stm = $this->bd->prepare('CREATE INDEX entry_lastSeen_index ON `' . $this->prefix . 'entry`(`lastSeen`);'); //"IF NOT EXISTS" does not exist in MySQL 5.7
  20. if ($stm && $stm->execute()) {
  21. if ($hasTransaction) {
  22. $this->bd->commit();
  23. }
  24. return true;
  25. }
  26. }
  27. if ($hasTransaction) {
  28. $this->bd->rollBack();
  29. }
  30. } elseif (stripos($errorInfo[2], 'hash') !== false) { //v1.2
  31. $stm = $this->bd->prepare('ALTER TABLE `' . $this->prefix . 'entry` ADD COLUMN hash BINARY(16) NOT NULL');
  32. return $stm && $stm->execute();
  33. }
  34. } catch (Exception $e) {
  35. Minz_Log::debug('FreshRSS_EntryDAO::autoAddColumn error: ' . $e->getMessage());
  36. if ($hasTransaction) {
  37. $this->bd->rollBack();
  38. }
  39. }
  40. }
  41. }
  42. return false;
  43. }
  44. private $addEntryPrepared = null;
  45. public function addEntry($valuesTmp) {
  46. if ($this->addEntryPrepared === null) {
  47. $sql = 'INSERT INTO `' . $this->prefix . 'entry` (id, guid, title, author, '
  48. . ($this->isCompressed() ? 'content_bin' : 'content')
  49. . ', link, date, lastSeen, hash, is_read, is_favorite, id_feed, tags) '
  50. . 'VALUES(?, ?, ?, ?, '
  51. . ($this->isCompressed() ? 'COMPRESS(?)' : '?')
  52. . ', ?, ?, ?, X?, ?, ?, ?, ?)';
  53. $this->addEntryPrepared = $this->bd->prepare($sql);
  54. }
  55. $values = array(
  56. $valuesTmp['id'],
  57. substr($valuesTmp['guid'], 0, 760),
  58. substr($valuesTmp['title'], 0, 255),
  59. substr($valuesTmp['author'], 0, 255),
  60. $valuesTmp['content'],
  61. substr($valuesTmp['link'], 0, 1023),
  62. $valuesTmp['date'],
  63. time(),
  64. $valuesTmp['hash'],
  65. $valuesTmp['is_read'] ? 1 : 0,
  66. $valuesTmp['is_favorite'] ? 1 : 0,
  67. $valuesTmp['id_feed'],
  68. substr($valuesTmp['tags'], 0, 1023),
  69. );
  70. if ($this->addEntryPrepared && $this->addEntryPrepared->execute($values)) {
  71. return $this->bd->lastInsertId();
  72. } else {
  73. $info = $this->addEntryPrepared == null ? array(2 => 'syntax error') : $this->addEntryPrepared->errorInfo();
  74. if ($this->autoAddColumn($info)) {
  75. return $this->addEntry($valuesTmp);
  76. } elseif ((int)($info[0] / 1000) !== 23) { //Filter out "SQLSTATE Class code 23: Constraint Violation" because of expected duplicate entries
  77. Minz_Log::error('SQL error addEntry: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  78. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title']);
  79. }
  80. return false;
  81. }
  82. }
  83. private $updateEntryPrepared = null;
  84. public function updateEntry($valuesTmp) {
  85. if ($this->updateEntryPrepared === null) {
  86. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  87. . 'SET title=?, author=?, '
  88. . ($this->isCompressed() ? 'content_bin=COMPRESS(?)' : 'content=?')
  89. . ', link=?, date=?, lastSeen=?, hash=X?, is_read=?, tags=? '
  90. . 'WHERE id_feed=? AND guid=?';
  91. $this->updateEntryPrepared = $this->bd->prepare($sql);
  92. }
  93. $values = array(
  94. substr($valuesTmp['title'], 0, 255),
  95. substr($valuesTmp['author'], 0, 255),
  96. $valuesTmp['content'],
  97. substr($valuesTmp['link'], 0, 1023),
  98. $valuesTmp['date'],
  99. time(),
  100. $valuesTmp['hash'],
  101. $valuesTmp['is_read'] ? 1 : 0,
  102. substr($valuesTmp['tags'], 0, 1023),
  103. $valuesTmp['id_feed'],
  104. substr($valuesTmp['guid'], 0, 760),
  105. );
  106. if ($this->updateEntryPrepared && $this->updateEntryPrepared->execute($values)) {
  107. return $this->bd->lastInsertId();
  108. } else {
  109. $info = $this->updateEntryPrepared == null ? array(2 => 'syntax error') : $this->updateEntryPrepared->errorInfo();
  110. if ($this->autoAddColumn($info)) {
  111. return $this->updateEntry($valuesTmp);
  112. }
  113. Minz_Log::error('SQL error updateEntry: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  114. . ' while updating entry with GUID ' . $valuesTmp['guid'] . ' in feed ' . $valuesTmp['id_feed']);
  115. return false;
  116. }
  117. }
  118. /**
  119. * Toggle favorite marker on one or more article
  120. *
  121. * @todo simplify the query by removing the str_repeat. I am pretty sure
  122. * there is an other way to do that.
  123. *
  124. * @param integer|array $ids
  125. * @param boolean $is_favorite
  126. * @return false|integer
  127. */
  128. public function markFavorite($ids, $is_favorite = true) {
  129. if (!is_array($ids)) {
  130. $ids = array($ids);
  131. }
  132. if (count($ids) < 1) {
  133. return 0;
  134. }
  135. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  136. . 'SET is_favorite=? '
  137. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?)';
  138. $values = array($is_favorite ? 1 : 0);
  139. $values = array_merge($values, $ids);
  140. $stm = $this->bd->prepare($sql);
  141. if ($stm && $stm->execute($values)) {
  142. return $stm->rowCount();
  143. } else {
  144. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  145. Minz_Log::error('SQL error markFavorite: ' . $info[2]);
  146. return false;
  147. }
  148. }
  149. /**
  150. * Update the unread article cache held on every feed details.
  151. * Depending on the parameters, it updates the cache on one feed, on all
  152. * feeds from one category or on all feeds.
  153. *
  154. * @todo It can use the query builder refactoring to build that query
  155. *
  156. * @param false|integer $catId category ID
  157. * @param false|integer $feedId feed ID
  158. * @return boolean
  159. */
  160. protected function updateCacheUnreads($catId = false, $feedId = false) {
  161. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  162. . 'LEFT OUTER JOIN ('
  163. . 'SELECT e.id_feed, '
  164. . 'COUNT(*) AS nbUnreads '
  165. . 'FROM `' . $this->prefix . 'entry` e '
  166. . 'WHERE e.is_read=0 '
  167. . 'GROUP BY e.id_feed'
  168. . ') x ON x.id_feed=f.id '
  169. . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0) '
  170. . 'WHERE 1';
  171. $values = array();
  172. if ($feedId !== false) {
  173. $sql .= ' AND f.id=?';
  174. $values[] = $id;
  175. }
  176. if ($catId !== false) {
  177. $sql .= ' AND f.category=?';
  178. $values[] = $catId;
  179. }
  180. $stm = $this->bd->prepare($sql);
  181. if ($stm && $stm->execute($values)) {
  182. return true;
  183. } else {
  184. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  185. Minz_Log::error('SQL error updateCacheUnreads: ' . $info[2]);
  186. return false;
  187. }
  188. }
  189. /**
  190. * Toggle the read marker on one or more article.
  191. * Then the cache is updated.
  192. *
  193. * @todo change the way the query is build because it seems there is
  194. * unnecessary code in here. For instance, the part with the str_repeat.
  195. * @todo remove code duplication. It seems the code is basically the
  196. * same if it is an array or not.
  197. *
  198. * @param integer|array $ids
  199. * @param boolean $is_read
  200. * @return integer affected rows
  201. */
  202. public function markRead($ids, $is_read = true) {
  203. if (is_array($ids)) { //Many IDs at once (used by API)
  204. if (count($ids) < 6) { //Speed heuristics
  205. $affected = 0;
  206. foreach ($ids as $id) {
  207. $affected += $this->markRead($id, $is_read);
  208. }
  209. return $affected;
  210. }
  211. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  212. . 'SET is_read=? '
  213. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?)';
  214. $values = array($is_read ? 1 : 0);
  215. $values = array_merge($values, $ids);
  216. $stm = $this->bd->prepare($sql);
  217. if (!($stm && $stm->execute($values))) {
  218. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  219. Minz_Log::error('SQL error markRead: ' . $info[2]);
  220. return false;
  221. }
  222. $affected = $stm->rowCount();
  223. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  224. return false;
  225. }
  226. return $affected;
  227. } else {
  228. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id '
  229. . 'SET e.is_read=?,'
  230. . 'f.cache_nbUnreads=f.cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
  231. . 'WHERE e.id=? AND e.is_read=?';
  232. $values = array($is_read ? 1 : 0, $ids, $is_read ? 0 : 1);
  233. $stm = $this->bd->prepare($sql);
  234. if ($stm && $stm->execute($values)) {
  235. return $stm->rowCount();
  236. } else {
  237. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  238. Minz_Log::error('SQL error markRead: ' . $info[2]);
  239. return false;
  240. }
  241. }
  242. }
  243. /**
  244. * Mark all entries as read depending on parameters.
  245. * If $onlyFavorites is true, it is used when the user mark as read in
  246. * the favorite pseudo-category.
  247. * If $priorityMin is greater than 0, it is used when the user mark as
  248. * read in the main feed pseudo-category.
  249. * Then the cache is updated.
  250. *
  251. * If $idMax equals 0, a deprecated debug message is logged
  252. *
  253. * @todo refactor this method along with markReadCat and markReadFeed
  254. * since they are all doing the same thing. I think we need to build a
  255. * tool to generate the query instead of having queries all over the
  256. * place. It will be reused also for the filtering making every thing
  257. * separated.
  258. *
  259. * @param integer $idMax fail safe article ID
  260. * @param boolean $onlyFavorites
  261. * @param integer $priorityMin
  262. * @return integer affected rows
  263. */
  264. public function markReadEntries($idMax = 0, $onlyFavorites = false, $priorityMin = 0) {
  265. if ($idMax == 0) {
  266. $idMax = time() . '000000';
  267. Minz_Log::debug('Calling markReadEntries(0) is deprecated!');
  268. }
  269. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id '
  270. . 'SET e.is_read=1 '
  271. . 'WHERE e.is_read=0 AND e.id <= ?';
  272. if ($onlyFavorites) {
  273. $sql .= ' AND e.is_favorite=1';
  274. } elseif ($priorityMin >= 0) {
  275. $sql .= ' AND f.priority > ' . intval($priorityMin);
  276. }
  277. $values = array($idMax);
  278. $stm = $this->bd->prepare($sql);
  279. if (!($stm && $stm->execute($values))) {
  280. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  281. Minz_Log::error('SQL error markReadEntries: ' . $info[2]);
  282. return false;
  283. }
  284. $affected = $stm->rowCount();
  285. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  286. return false;
  287. }
  288. return $affected;
  289. }
  290. /**
  291. * Mark all the articles in a category as read.
  292. * There is a fail safe to prevent to mark as read articles that are
  293. * loaded during the mark as read action. Then the cache is updated.
  294. *
  295. * If $idMax equals 0, a deprecated debug message is logged
  296. *
  297. * @param integer $id category ID
  298. * @param integer $idMax fail safe article ID
  299. * @return integer affected rows
  300. */
  301. public function markReadCat($id, $idMax = 0) {
  302. if ($idMax == 0) {
  303. $idMax = time() . '000000';
  304. Minz_Log::debug('Calling markReadCat(0) is deprecated!');
  305. }
  306. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id '
  307. . 'SET e.is_read=1 '
  308. . 'WHERE f.category=? AND e.is_read=0 AND e.id <= ?';
  309. $values = array($id, $idMax);
  310. $stm = $this->bd->prepare($sql);
  311. if (!($stm && $stm->execute($values))) {
  312. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  313. Minz_Log::error('SQL error markReadCat: ' . $info[2]);
  314. return false;
  315. }
  316. $affected = $stm->rowCount();
  317. if (($affected > 0) && (!$this->updateCacheUnreads($id, false))) {
  318. return false;
  319. }
  320. return $affected;
  321. }
  322. /**
  323. * Mark all the articles in a feed as read.
  324. * There is a fail safe to prevent to mark as read articles that are
  325. * loaded during the mark as read action. Then the cache is updated.
  326. *
  327. * If $idMax equals 0, a deprecated debug message is logged
  328. *
  329. * @param integer $id_feed feed ID
  330. * @param integer $idMax fail safe article ID
  331. * @return integer affected rows
  332. */
  333. public function markReadFeed($id_feed, $idMax = 0) {
  334. if ($idMax == 0) {
  335. $idMax = time() . '000000';
  336. Minz_Log::debug('Calling markReadFeed(0) is deprecated!');
  337. }
  338. $this->bd->beginTransaction();
  339. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  340. . 'SET is_read=1 '
  341. . 'WHERE id_feed=? AND is_read=0 AND id <= ?';
  342. $values = array($id_feed, $idMax);
  343. $stm = $this->bd->prepare($sql);
  344. if (!($stm && $stm->execute($values))) {
  345. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  346. Minz_Log::error('SQL error markReadFeed: ' . $info[2]);
  347. $this->bd->rollBack();
  348. return false;
  349. }
  350. $affected = $stm->rowCount();
  351. if ($affected > 0) {
  352. $sql = 'UPDATE `' . $this->prefix . 'feed` '
  353. . 'SET cache_nbUnreads=cache_nbUnreads-' . $affected
  354. . ' WHERE id=?';
  355. $values = array($id_feed);
  356. $stm = $this->bd->prepare($sql);
  357. if (!($stm && $stm->execute($values))) {
  358. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  359. Minz_Log::error('SQL error markReadFeed: ' . $info[2]);
  360. $this->bd->rollBack();
  361. return false;
  362. }
  363. }
  364. $this->bd->commit();
  365. return $affected;
  366. }
  367. public function searchByGuid($id_feed, $guid) {
  368. // un guid est unique pour un flux donné
  369. $sql = 'SELECT id, guid, title, author, '
  370. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  371. . ', link, date, is_read, is_favorite, id_feed, tags '
  372. . 'FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid=?';
  373. $stm = $this->bd->prepare($sql);
  374. $values = array(
  375. $id_feed,
  376. $guid,
  377. );
  378. $stm->execute($values);
  379. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  380. $entries = self::daoToEntry($res);
  381. return isset($entries[0]) ? $entries[0] : null;
  382. }
  383. public function searchById($id) {
  384. $sql = 'SELECT id, guid, title, author, '
  385. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  386. . ', link, date, is_read, is_favorite, id_feed, tags '
  387. . 'FROM `' . $this->prefix . 'entry` WHERE id=?';
  388. $stm = $this->bd->prepare($sql);
  389. $values = array($id);
  390. $stm->execute($values);
  391. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  392. $entries = self::daoToEntry($res);
  393. return isset($entries[0]) ? $entries[0] : null;
  394. }
  395. protected function sqlConcat($s1, $s2) {
  396. return 'CONCAT(' . $s1 . ',' . $s2 . ')'; //MySQL
  397. }
  398. private function sqlListWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0) {
  399. if (!$state) {
  400. $state = FreshRSS_Entry::STATE_ALL;
  401. }
  402. $where = '';
  403. $joinFeed = false;
  404. $values = array();
  405. switch ($type) {
  406. case 'a':
  407. $where .= 'f.priority > 0 ';
  408. $joinFeed = true;
  409. break;
  410. case 's': //Deprecated: use $state instead
  411. $where .= 'e1.is_favorite=1 ';
  412. break;
  413. case 'c':
  414. $where .= 'f.category=? ';
  415. $values[] = intval($id);
  416. $joinFeed = true;
  417. break;
  418. case 'f':
  419. $where .= 'e1.id_feed=? ';
  420. $values[] = intval($id);
  421. break;
  422. case 'A':
  423. $where .= '1 ';
  424. break;
  425. default:
  426. throw new FreshRSS_EntriesGetter_Exception('Bad type in Entry->listByType: [' . $type . ']!');
  427. }
  428. if ($state & FreshRSS_Entry::STATE_NOT_READ) {
  429. if (!($state & FreshRSS_Entry::STATE_READ)) {
  430. $where .= 'AND e1.is_read=0 ';
  431. }
  432. }
  433. elseif ($state & FreshRSS_Entry::STATE_READ) {
  434. $where .= 'AND e1.is_read=1 ';
  435. }
  436. if ($state & FreshRSS_Entry::STATE_FAVORITE) {
  437. if (!($state & FreshRSS_Entry::STATE_NOT_FAVORITE)) {
  438. $where .= 'AND e1.is_favorite=1 ';
  439. }
  440. }
  441. elseif ($state & FreshRSS_Entry::STATE_NOT_FAVORITE) {
  442. $where .= 'AND e1.is_favorite=0 ';
  443. }
  444. switch ($order) {
  445. case 'DESC':
  446. case 'ASC':
  447. break;
  448. default:
  449. throw new FreshRSS_EntriesGetter_Exception('Bad order in Entry->listByType: [' . $order . ']!');
  450. }
  451. /*if ($firstId === '' && parent::$sharedDbType === 'mysql') {
  452. $firstId = $order === 'DESC' ? '9000000000'. '000000' : '0'; //MySQL optimization. TODO: check if this is needed again, after the filtering for old articles has been removed in 0.9-dev
  453. }*/
  454. if ($firstId !== '') {
  455. $where .= 'AND e1.id ' . ($order === 'DESC' ? '<=' : '>=') . $firstId . ' ';
  456. }
  457. if ($date_min > 0) {
  458. $where .= 'AND e1.id >= ' . $date_min . '000000 ';
  459. }
  460. $search = '';
  461. if ($filter !== null) {
  462. if ($filter->getIntitle()) {
  463. $search .= 'AND e1.title LIKE ? ';
  464. $values[] = "%{$filter->getIntitle()}%";
  465. }
  466. if ($filter->getInurl()) {
  467. $search .= 'AND CONCAT(e1.link, e1.guid) LIKE ? ';
  468. $values[] = "%{$filter->getInurl()}%";
  469. }
  470. if ($filter->getAuthor()) {
  471. $search .= 'AND e1.author LIKE ? ';
  472. $values[] = "%{$filter->getAuthor()}%";
  473. }
  474. if ($filter->getMinDate()) {
  475. $search .= 'AND e1.id >= ? ';
  476. $values[] = "{$filter->getMinDate()}000000";
  477. }
  478. if ($filter->getMaxDate()) {
  479. $search .= 'AND e1.id <= ? ';
  480. $values[] = "{$filter->getMaxDate()}000000";
  481. }
  482. if ($filter->getMinPubdate()) {
  483. $search .= 'AND e1.date >= ? ';
  484. $values[] = $filter->getMinPubdate();
  485. }
  486. if ($filter->getMaxPubdate()) {
  487. $search .= 'AND e1.date <= ? ';
  488. $values[] = $filter->getMaxPubdate();
  489. }
  490. if ($filter->getTags()) {
  491. $tags = $filter->getTags();
  492. foreach ($tags as $tag) {
  493. $search .= 'AND e1.tags LIKE ? ';
  494. $values[] = "%{$tag}%";
  495. }
  496. }
  497. if ($filter->getSearch()) {
  498. $search .= 'AND ' . $this->sqlconcat('e1.title', $this->isCompressed() ? 'UNCOMPRESS(content_bin)' : 'content') . ' LIKE ? ';
  499. $values[] = "%{$filter->getSearch()}%";
  500. }
  501. }
  502. return array($values,
  503. 'SELECT e1.id FROM `' . $this->prefix . 'entry` e1 '
  504. . ($joinFeed ? 'INNER JOIN `' . $this->prefix . 'feed` f ON e1.id_feed=f.id ' : '')
  505. . 'WHERE ' . $where
  506. . $search
  507. . 'ORDER BY e1.id ' . $order
  508. . ($limit > 0 ? ' LIMIT ' . $limit : '')); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  509. }
  510. public function listWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0) {
  511. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min);
  512. $sql = 'SELECT e.id, e.guid, e.title, e.author, '
  513. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  514. . ', e.link, e.date, e.is_read, e.is_favorite, e.id_feed, e.tags '
  515. . 'FROM `' . $this->prefix . 'entry` e '
  516. . 'INNER JOIN ('
  517. . $sql
  518. . ') e2 ON e2.id=e.id '
  519. . 'ORDER BY e.id ' . $order;
  520. $stm = $this->bd->prepare($sql);
  521. $stm->execute($values);
  522. return self::daoToEntry($stm->fetchAll(PDO::FETCH_ASSOC));
  523. }
  524. public function listIdsWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0) { //For API
  525. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min);
  526. $stm = $this->bd->prepare($sql);
  527. $stm->execute($values);
  528. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  529. }
  530. public function listHashForFeedGuids($id_feed, $guids) {
  531. if (count($guids) < 1) {
  532. return array();
  533. }
  534. $sql = 'SELECT guid, hex(hash) AS hexHash FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
  535. $stm = $this->bd->prepare($sql);
  536. $values = array($id_feed);
  537. $values = array_merge($values, $guids);
  538. if ($stm && $stm->execute($values)) {
  539. $result = array();
  540. $rows = $stm->fetchAll(PDO::FETCH_ASSOC);
  541. foreach ($rows as $row) {
  542. $result[$row['guid']] = $row['hexHash'];
  543. }
  544. return $result;
  545. } else {
  546. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  547. if ($this->autoAddColumn($info)) {
  548. return $this->listHashForFeedGuids($id_feed, $guids);
  549. }
  550. Minz_Log::error('SQL error listHashForFeedGuids: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  551. . ' while querying feed ' . $id_feed);
  552. return false;
  553. }
  554. }
  555. public function updateLastSeen($id_feed, $guids) {
  556. if (count($guids) < 1) {
  557. return 0;
  558. }
  559. $sql = 'UPDATE `' . $this->prefix . 'entry` SET lastSeen=? WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
  560. $stm = $this->bd->prepare($sql);
  561. $values = array(time(), $id_feed);
  562. $values = array_merge($values, $guids);
  563. if ($stm && $stm->execute($values)) {
  564. return $stm->rowCount();
  565. } else {
  566. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  567. if ($this->autoAddColumn($info)) {
  568. return $this->updateLastSeen($id_feed, $guids);
  569. }
  570. Minz_Log::error('SQL error updateLastSeen: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  571. . ' while updating feed ' . $id_feed);
  572. return false;
  573. }
  574. }
  575. public function countUnreadRead() {
  576. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id WHERE priority > 0'
  577. . ' UNION SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id WHERE priority > 0 AND is_read=0';
  578. $stm = $this->bd->prepare($sql);
  579. $stm->execute();
  580. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  581. $all = empty($res[0]) ? 0 : $res[0];
  582. $unread = empty($res[1]) ? 0 : $res[1];
  583. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  584. }
  585. public function count($minPriority = null) {
  586. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id';
  587. if ($minPriority !== null) {
  588. $sql = ' WHERE priority > ' . intval($minPriority);
  589. }
  590. $stm = $this->bd->prepare($sql);
  591. $stm->execute();
  592. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  593. return $res[0];
  594. }
  595. public function countNotRead($minPriority = null) {
  596. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id WHERE is_read=0';
  597. if ($minPriority !== null) {
  598. $sql = ' AND priority > ' . intval($minPriority);
  599. }
  600. $stm = $this->bd->prepare($sql);
  601. $stm->execute();
  602. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  603. return $res[0];
  604. }
  605. public function countUnreadReadFavorites() {
  606. $sql = 'SELECT c FROM ('
  607. . 'SELECT COUNT(id) AS c, 1 as o FROM `' . $this->prefix . 'entry` WHERE is_favorite=1 '
  608. . 'UNION SELECT COUNT(id) AS c, 2 AS o FROM `' . $this->prefix . 'entry` WHERE is_favorite=1 AND is_read=0'
  609. . ') u ORDER BY o';
  610. $stm = $this->bd->prepare($sql);
  611. $stm->execute();
  612. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  613. $all = empty($res[0]) ? 0 : $res[0];
  614. $unread = empty($res[1]) ? 0 : $res[1];
  615. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  616. }
  617. public function optimizeTable() {
  618. $sql = 'OPTIMIZE TABLE `' . $this->prefix . 'entry`'; //MySQL
  619. $stm = $this->bd->prepare($sql);
  620. $stm->execute();
  621. }
  622. public function size($all = false) {
  623. $db = FreshRSS_Context::$system_conf->db;
  624. $sql = 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema=?'; //MySQL
  625. $values = array($db['base']);
  626. if (!$all) {
  627. $sql .= ' AND table_name LIKE ?';
  628. $values[] = $this->prefix . '%';
  629. }
  630. $stm = $this->bd->prepare($sql);
  631. $stm->execute($values);
  632. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  633. return $res[0];
  634. }
  635. public static function daoToEntry($listDAO) {
  636. $list = array();
  637. if (!is_array($listDAO)) {
  638. $listDAO = array($listDAO);
  639. }
  640. foreach ($listDAO as $key => $dao) {
  641. $entry = new FreshRSS_Entry(
  642. $dao['id_feed'],
  643. $dao['guid'],
  644. $dao['title'],
  645. $dao['author'],
  646. $dao['content'],
  647. $dao['link'],
  648. $dao['date'],
  649. $dao['is_read'],
  650. $dao['is_favorite'],
  651. $dao['tags']
  652. );
  653. if (isset($dao['id'])) {
  654. $entry->_id($dao['id']);
  655. }
  656. $list[] = $entry;
  657. }
  658. unset($listDAO);
  659. return $list;
  660. }
  661. }