EntryDAO.php 24 KB

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