EntryDAO.php 24 KB

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