EntryDAO.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. <?php
  2. class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
  3. public function isCompressed() {
  4. return true;
  5. }
  6. public function hasNativeHex() {
  7. return true;
  8. }
  9. public function sqlHexDecode($x) {
  10. return 'unhex(' . $x . ')';
  11. }
  12. public function sqlHexEncode($x) {
  13. return 'hex(' . $x . ')';
  14. }
  15. //TODO: Move the database auto-updates to DatabaseDAO
  16. protected function createEntryTempTable() {
  17. $ok = false;
  18. $hadTransaction = $this->pdo->inTransaction();
  19. if ($hadTransaction) {
  20. $this->pdo->commit();
  21. }
  22. try {
  23. require(APP_PATH . '/SQL/install.sql.' . $this->pdo->dbType() . '.php');
  24. Minz_Log::warning('SQL CREATE TABLE entrytmp...');
  25. $ok = $this->pdo->exec($SQL_CREATE_TABLE_ENTRYTMP . $SQL_CREATE_INDEX_ENTRY_1) !== false;
  26. } catch (Exception $ex) {
  27. Minz_Log::error(__method__ . ' error: ' . $ex->getMessage());
  28. }
  29. if ($hadTransaction) {
  30. $this->pdo->beginTransaction();
  31. }
  32. return $ok;
  33. }
  34. private function updateToMediumBlob() {
  35. if ($this->pdo->dbType() !== 'mysql') {
  36. return false;
  37. }
  38. Minz_Log::warning('Update MySQL table to use MEDIUMBLOB...');
  39. $sql = <<<'SQL'
  40. ALTER TABLE `_entry` MODIFY `content_bin` MEDIUMBLOB;
  41. ALTER TABLE `_entrytmp` MODIFY `content_bin` MEDIUMBLOB;
  42. SQL;
  43. try {
  44. $ok = $this->pdo->exec($sql) !== false;
  45. } catch (Exception $e) {
  46. $ok = false;
  47. Minz_Log::error(__method__ . ' error: ' . $e->getMessage());
  48. }
  49. return $ok;
  50. }
  51. //TODO: Move the database auto-updates to DatabaseDAO
  52. protected function autoUpdateDb($errorInfo) {
  53. if (isset($errorInfo[0])) {
  54. if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_TABLE_ERROR) {
  55. if (stripos($errorInfo[2], 'tag') !== false) {
  56. $tagDAO = FreshRSS_Factory::createTagDao();
  57. return $tagDAO->createTagTable(); //v1.12.0
  58. } elseif (stripos($errorInfo[2], 'entrytmp') !== false) {
  59. return $this->createEntryTempTable(); //v1.7.0
  60. }
  61. }
  62. }
  63. if (isset($errorInfo[1])) {
  64. if ($errorInfo[1] == FreshRSS_DatabaseDAO::ER_DATA_TOO_LONG) {
  65. if (stripos($errorInfo[2], 'content_bin') !== false) {
  66. return $this->updateToMediumBlob(); //v1.15.0
  67. }
  68. }
  69. }
  70. return false;
  71. }
  72. private $addEntryPrepared = null;
  73. public function addEntry($valuesTmp, $useTmpTable = true) {
  74. if ($this->addEntryPrepared == null) {
  75. $sql = 'INSERT INTO `_' . ($useTmpTable ? 'entrytmp' : 'entry') . '` (id, guid, title, author, '
  76. . ($this->isCompressed() ? 'content_bin' : 'content')
  77. . ', link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags) '
  78. . 'VALUES(:id, :guid, :title, :author, '
  79. . ($this->isCompressed() ? 'COMPRESS(:content)' : ':content')
  80. . ', :link, :date, :last_seen, '
  81. . $this->sqlHexDecode(':hash')
  82. . ', :is_read, :is_favorite, :id_feed, :tags)';
  83. $this->addEntryPrepared = $this->pdo->prepare($sql);
  84. }
  85. if ($this->addEntryPrepared) {
  86. $this->addEntryPrepared->bindParam(':id', $valuesTmp['id']);
  87. $valuesTmp['guid'] = substr($valuesTmp['guid'], 0, 760);
  88. $valuesTmp['guid'] = safe_ascii($valuesTmp['guid']);
  89. $this->addEntryPrepared->bindParam(':guid', $valuesTmp['guid']);
  90. $valuesTmp['title'] = mb_strcut($valuesTmp['title'], 0, 255, 'UTF-8');
  91. $this->addEntryPrepared->bindParam(':title', $valuesTmp['title']);
  92. $valuesTmp['author'] = mb_strcut($valuesTmp['author'], 0, 255, 'UTF-8');
  93. $this->addEntryPrepared->bindParam(':author', $valuesTmp['author']);
  94. $this->addEntryPrepared->bindParam(':content', $valuesTmp['content']);
  95. $valuesTmp['link'] = substr($valuesTmp['link'], 0, 1023);
  96. $valuesTmp['link'] = safe_ascii($valuesTmp['link']);
  97. $this->addEntryPrepared->bindParam(':link', $valuesTmp['link']);
  98. $this->addEntryPrepared->bindParam(':date', $valuesTmp['date'], PDO::PARAM_INT);
  99. if (empty($valuesTmp['lastSeen'])) {
  100. $valuesTmp['lastSeen'] = time();
  101. }
  102. $this->addEntryPrepared->bindParam(':last_seen', $valuesTmp['lastSeen'], PDO::PARAM_INT);
  103. $valuesTmp['is_read'] = $valuesTmp['is_read'] ? 1 : 0;
  104. $this->addEntryPrepared->bindParam(':is_read', $valuesTmp['is_read'], PDO::PARAM_INT);
  105. $valuesTmp['is_favorite'] = $valuesTmp['is_favorite'] ? 1 : 0;
  106. $this->addEntryPrepared->bindParam(':is_favorite', $valuesTmp['is_favorite'], PDO::PARAM_INT);
  107. $this->addEntryPrepared->bindParam(':id_feed', $valuesTmp['id_feed'], PDO::PARAM_INT);
  108. $valuesTmp['tags'] = mb_strcut($valuesTmp['tags'], 0, 1023, 'UTF-8');
  109. $this->addEntryPrepared->bindParam(':tags', $valuesTmp['tags']);
  110. if ($this->hasNativeHex()) {
  111. $this->addEntryPrepared->bindParam(':hash', $valuesTmp['hash']);
  112. } else {
  113. $valuesTmp['hashBin'] = hex2bin($valuesTmp['hash']);
  114. $this->addEntryPrepared->bindParam(':hash', $valuesTmp['hashBin']);
  115. }
  116. }
  117. if ($this->addEntryPrepared && $this->addEntryPrepared->execute()) {
  118. return true;
  119. } else {
  120. $info = $this->addEntryPrepared == null ? $this->pdo->errorInfo() : $this->addEntryPrepared->errorInfo();
  121. if ($this->autoUpdateDb($info)) {
  122. $this->addEntryPrepared = null;
  123. return $this->addEntry($valuesTmp);
  124. } elseif ((int)((int)$info[0] / 1000) !== 23) { //Filter out "SQLSTATE Class code 23: Constraint Violation" because of expected duplicate entries
  125. Minz_Log::error('SQL error addEntry: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  126. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title']);
  127. }
  128. return false;
  129. }
  130. }
  131. public function commitNewEntries() {
  132. $sql = <<<'SQL'
  133. SET @rank=(SELECT MAX(id) - COUNT(*) FROM `_entrytmp`);
  134. INSERT IGNORE INTO `_entry` (
  135. id, guid, title, author, content_bin, link, date, `lastSeen`,
  136. hash, is_read, is_favorite, id_feed, tags
  137. )
  138. SELECT @rank:=@rank+1 AS id, guid, title, author, content_bin, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags
  139. FROM `_entrytmp`
  140. ORDER BY date;
  141. DELETE FROM `_entrytmp` WHERE id <= @rank;';
  142. SQL;
  143. $hadTransaction = $this->pdo->inTransaction();
  144. if (!$hadTransaction) {
  145. $this->pdo->beginTransaction();
  146. }
  147. $result = $this->pdo->exec($sql) !== false;
  148. if (!$hadTransaction) {
  149. $this->pdo->commit();
  150. }
  151. return $result;
  152. }
  153. private $updateEntryPrepared = null;
  154. public function updateEntry($valuesTmp) {
  155. if (!isset($valuesTmp['is_read'])) {
  156. $valuesTmp['is_read'] = null;
  157. }
  158. if ($this->updateEntryPrepared === null) {
  159. $sql = 'UPDATE `_entry` '
  160. . 'SET title=:title, author=:author, '
  161. . ($this->isCompressed() ? 'content_bin=COMPRESS(:content)' : 'content=:content')
  162. . ', link=:link, date=:date, `lastSeen`=:last_seen, '
  163. . 'hash=' . $this->sqlHexDecode(':hash')
  164. . ', ' . ($valuesTmp['is_read'] === null ? '' : 'is_read=:is_read, ')
  165. . 'tags=:tags '
  166. . 'WHERE id_feed=:id_feed AND guid=:guid';
  167. $this->updateEntryPrepared = $this->pdo->prepare($sql);
  168. }
  169. $valuesTmp['guid'] = substr($valuesTmp['guid'], 0, 760);
  170. $this->updateEntryPrepared->bindParam(':guid', $valuesTmp['guid']);
  171. $valuesTmp['title'] = mb_strcut($valuesTmp['title'], 0, 255, 'UTF-8');
  172. $this->updateEntryPrepared->bindParam(':title', $valuesTmp['title']);
  173. $valuesTmp['author'] = mb_strcut($valuesTmp['author'], 0, 255, 'UTF-8');
  174. $this->updateEntryPrepared->bindParam(':author', $valuesTmp['author']);
  175. $this->updateEntryPrepared->bindParam(':content', $valuesTmp['content']);
  176. $valuesTmp['link'] = substr($valuesTmp['link'], 0, 1023);
  177. $valuesTmp['link'] = safe_ascii($valuesTmp['link']);
  178. $this->updateEntryPrepared->bindParam(':link', $valuesTmp['link']);
  179. $this->updateEntryPrepared->bindParam(':date', $valuesTmp['date'], PDO::PARAM_INT);
  180. $valuesTmp['lastSeen'] = time();
  181. $this->updateEntryPrepared->bindParam(':last_seen', $valuesTmp['lastSeen'], PDO::PARAM_INT);
  182. if ($valuesTmp['is_read'] !== null) {
  183. $this->updateEntryPrepared->bindValue(':is_read', $valuesTmp['is_read'] ? 1 : 0, PDO::PARAM_INT);
  184. }
  185. $this->updateEntryPrepared->bindParam(':id_feed', $valuesTmp['id_feed'], PDO::PARAM_INT);
  186. $valuesTmp['tags'] = mb_strcut($valuesTmp['tags'], 0, 1023, 'UTF-8');
  187. $this->updateEntryPrepared->bindParam(':tags', $valuesTmp['tags']);
  188. if ($this->hasNativeHex()) {
  189. $this->updateEntryPrepared->bindParam(':hash', $valuesTmp['hash']);
  190. } else {
  191. $valuesTmp['hashBin'] = hex2bin($valuesTmp['hash']);
  192. $this->updateEntryPrepared->bindParam(':hash', $valuesTmp['hashBin']);
  193. }
  194. if ($this->updateEntryPrepared && $this->updateEntryPrepared->execute()) {
  195. return true;
  196. } else {
  197. $info = $this->updateEntryPrepared == null ? $this->pdo->errorInfo() : $this->updateEntryPrepared->errorInfo();
  198. if ($this->autoUpdateDb($info)) {
  199. return $this->updateEntry($valuesTmp);
  200. }
  201. Minz_Log::error('SQL error updateEntry: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  202. . ' while updating entry with GUID ' . $valuesTmp['guid'] . ' in feed ' . $valuesTmp['id_feed']);
  203. return false;
  204. }
  205. }
  206. /**
  207. * Toggle favorite marker on one or more article
  208. *
  209. * @todo simplify the query by removing the str_repeat. I am pretty sure
  210. * there is an other way to do that.
  211. *
  212. * @param integer|array $ids
  213. * @param boolean $is_favorite
  214. * @return false|integer
  215. */
  216. public function markFavorite($ids, $is_favorite = true) {
  217. if (!is_array($ids)) {
  218. $ids = array($ids);
  219. }
  220. if (count($ids) < 1) {
  221. return 0;
  222. }
  223. FreshRSS_UserDAO::touch();
  224. $sql = 'UPDATE `_entry` '
  225. . 'SET is_favorite=? '
  226. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?)';
  227. $values = array($is_favorite ? 1 : 0);
  228. $values = array_merge($values, $ids);
  229. $stm = $this->pdo->prepare($sql);
  230. if ($stm && $stm->execute($values)) {
  231. return $stm->rowCount();
  232. } else {
  233. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  234. Minz_Log::error('SQL error markFavorite: ' . $info[2]);
  235. return false;
  236. }
  237. }
  238. /**
  239. * Update the unread article cache held on every feed details.
  240. * Depending on the parameters, it updates the cache on one feed, on all
  241. * feeds from one category or on all feeds.
  242. *
  243. * @todo It can use the query builder refactoring to build that query
  244. *
  245. * @param false|integer $catId category ID
  246. * @param false|integer $feedId feed ID
  247. * @return boolean
  248. */
  249. protected function updateCacheUnreads($catId = false, $feedId = false) {
  250. $sql = 'UPDATE `_feed` f '
  251. . 'LEFT OUTER JOIN ('
  252. . 'SELECT e.id_feed, '
  253. . 'COUNT(*) AS nbUnreads '
  254. . 'FROM `_entry` e '
  255. . 'WHERE e.is_read=0 '
  256. . 'GROUP BY e.id_feed'
  257. . ') x ON x.id_feed=f.id '
  258. . 'SET f.`cache_nbUnreads`=COALESCE(x.nbUnreads, 0)';
  259. $hasWhere = false;
  260. $values = array();
  261. if ($feedId !== false) {
  262. $sql .= $hasWhere ? ' AND' : ' WHERE';
  263. $hasWhere = true;
  264. $sql .= ' f.id=?';
  265. $values[] = $feedId;
  266. }
  267. if ($catId !== false) {
  268. $sql .= $hasWhere ? ' AND' : ' WHERE';
  269. $hasWhere = true;
  270. $sql .= ' f.category=?';
  271. $values[] = $catId;
  272. }
  273. $stm = $this->pdo->prepare($sql);
  274. if ($stm && $stm->execute($values)) {
  275. return true;
  276. } else {
  277. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  278. Minz_Log::error('SQL error updateCacheUnreads: ' . $info[2]);
  279. return false;
  280. }
  281. }
  282. /**
  283. * Toggle the read marker on one or more article.
  284. * Then the cache is updated.
  285. *
  286. * @todo change the way the query is build because it seems there is
  287. * unnecessary code in here. For instance, the part with the str_repeat.
  288. * @todo remove code duplication. It seems the code is basically the
  289. * same if it is an array or not.
  290. *
  291. * @param integer|array $ids
  292. * @param boolean $is_read
  293. * @return integer affected rows
  294. */
  295. public function markRead($ids, $is_read = true) {
  296. FreshRSS_UserDAO::touch();
  297. if (is_array($ids)) { //Many IDs at once
  298. if (count($ids) < 6) { //Speed heuristics
  299. $affected = 0;
  300. foreach ($ids as $id) {
  301. $affected += $this->markRead($id, $is_read);
  302. }
  303. return $affected;
  304. }
  305. $sql = 'UPDATE `_entry` '
  306. . 'SET is_read=? '
  307. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?)';
  308. $values = array($is_read ? 1 : 0);
  309. $values = array_merge($values, $ids);
  310. $stm = $this->pdo->prepare($sql);
  311. if (!($stm && $stm->execute($values))) {
  312. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  313. Minz_Log::error('SQL error markRead: ' . $info[2]);
  314. return false;
  315. }
  316. $affected = $stm->rowCount();
  317. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  318. return false;
  319. }
  320. return $affected;
  321. } else {
  322. $sql = 'UPDATE `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id '
  323. . 'SET e.is_read=?,'
  324. . 'f.`cache_nbUnreads`=f.`cache_nbUnreads`' . ($is_read ? '-' : '+') . '1 '
  325. . 'WHERE e.id=? AND e.is_read=?';
  326. $values = array($is_read ? 1 : 0, $ids, $is_read ? 0 : 1);
  327. $stm = $this->pdo->prepare($sql);
  328. if ($stm && $stm->execute($values)) {
  329. return $stm->rowCount();
  330. } else {
  331. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  332. Minz_Log::error('SQL error markRead: ' . $info[2]);
  333. return false;
  334. }
  335. }
  336. }
  337. /**
  338. * Mark all entries as read depending on parameters.
  339. * If $onlyFavorites is true, it is used when the user mark as read in
  340. * the favorite pseudo-category.
  341. * If $priorityMin is greater than 0, it is used when the user mark as
  342. * read in the main feed pseudo-category.
  343. * Then the cache is updated.
  344. *
  345. * If $idMax equals 0, a deprecated debug message is logged
  346. *
  347. * @todo refactor this method along with markReadCat and markReadFeed
  348. * since they are all doing the same thing. I think we need to build a
  349. * tool to generate the query instead of having queries all over the
  350. * place. It will be reused also for the filtering making every thing
  351. * separated.
  352. *
  353. * @param integer $idMax fail safe article ID
  354. * @param boolean $onlyFavorites
  355. * @param integer $priorityMin
  356. * @return integer affected rows
  357. */
  358. public function markReadEntries($idMax = 0, $onlyFavorites = false, $priorityMin = 0, $filters = null, $state = 0, $is_read = true) {
  359. FreshRSS_UserDAO::touch();
  360. if ($idMax == 0) {
  361. $idMax = time() . '000000';
  362. Minz_Log::debug('Calling markReadEntries(0) is deprecated!');
  363. }
  364. $sql = 'UPDATE `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id '
  365. . 'SET e.is_read=? '
  366. . 'WHERE e.is_read <> ? AND e.id <= ?';
  367. if ($onlyFavorites) {
  368. $sql .= ' AND e.is_favorite=1';
  369. } elseif ($priorityMin >= 0) {
  370. $sql .= ' AND f.priority > ' . intval($priorityMin);
  371. }
  372. $values = array($is_read ? 1 : 0, $is_read ? 1 : 0, $idMax);
  373. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state);
  374. $stm = $this->pdo->prepare($sql . $search);
  375. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  376. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  377. Minz_Log::error('SQL error markReadEntries: ' . $info[2]);
  378. return false;
  379. }
  380. $affected = $stm->rowCount();
  381. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  382. return false;
  383. }
  384. return $affected;
  385. }
  386. /**
  387. * Mark all the articles in a category as read.
  388. * There is a fail safe to prevent to mark as read articles that are
  389. * loaded during the mark as read action. Then the cache is updated.
  390. *
  391. * If $idMax equals 0, a deprecated debug message is logged
  392. *
  393. * @param integer $id category ID
  394. * @param integer $idMax fail safe article ID
  395. * @return integer affected rows
  396. */
  397. public function markReadCat($id, $idMax = 0, $filters = null, $state = 0, $is_read = true) {
  398. FreshRSS_UserDAO::touch();
  399. if ($idMax == 0) {
  400. $idMax = time() . '000000';
  401. Minz_Log::debug('Calling markReadCat(0) is deprecated!');
  402. }
  403. $sql = 'UPDATE `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id '
  404. . 'SET e.is_read=? '
  405. . 'WHERE f.category=? AND e.is_read <> ? AND e.id <= ?';
  406. $values = array($is_read ? 1 : 0, $id, $is_read ? 1 : 0, $idMax);
  407. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state);
  408. $stm = $this->pdo->prepare($sql . $search);
  409. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  410. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  411. Minz_Log::error('SQL error markReadCat: ' . $info[2]);
  412. return false;
  413. }
  414. $affected = $stm->rowCount();
  415. if (($affected > 0) && (!$this->updateCacheUnreads($id, false))) {
  416. return false;
  417. }
  418. return $affected;
  419. }
  420. /**
  421. * Mark all the articles in a feed as read.
  422. * There is a fail safe to prevent to mark as read articles that are
  423. * loaded during the mark as read action. Then the cache is updated.
  424. *
  425. * If $idMax equals 0, a deprecated debug message is logged
  426. *
  427. * @param integer $id_feed feed ID
  428. * @param integer $idMax fail safe article ID
  429. * @return integer affected rows
  430. */
  431. public function markReadFeed($id_feed, $idMax = 0, $filters = null, $state = 0, $is_read = true) {
  432. FreshRSS_UserDAO::touch();
  433. if ($idMax == 0) {
  434. $idMax = time() . '000000';
  435. Minz_Log::debug('Calling markReadFeed(0) is deprecated!');
  436. }
  437. $this->pdo->beginTransaction();
  438. $sql = 'UPDATE `_entry` '
  439. . 'SET is_read=? '
  440. . 'WHERE id_feed=? AND is_read <> ? AND id <= ?';
  441. $values = array($is_read ? 1 : 0, $id_feed, $is_read ? 1 : 0, $idMax);
  442. list($searchValues, $search) = $this->sqlListEntriesWhere('', $filters, $state);
  443. $stm = $this->pdo->prepare($sql . $search);
  444. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  445. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  446. Minz_Log::error('SQL error markReadFeed: ' . $info[2] . ' with SQL: ' . $sql . $search);
  447. $this->pdo->rollBack();
  448. return false;
  449. }
  450. $affected = $stm->rowCount();
  451. if ($affected > 0) {
  452. $sql = 'UPDATE `_feed` '
  453. . 'SET `cache_nbUnreads`=`cache_nbUnreads`-' . $affected
  454. . ' WHERE id=:id';
  455. $stm = $this->pdo->prepare($sql);
  456. $stm->bindParam(':id', $id_feed, PDO::PARAM_INT);
  457. if (!($stm && $stm->execute())) {
  458. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  459. Minz_Log::error('SQL error markReadFeed cache: ' . $info[2]);
  460. $this->pdo->rollBack();
  461. return false;
  462. }
  463. }
  464. $this->pdo->commit();
  465. return $affected;
  466. }
  467. /**
  468. * Mark all the articles in a tag as read.
  469. * @param integer $id tag ID, or empty for targetting any tag
  470. * @param integer $idMax max article ID
  471. * @return integer affected rows
  472. */
  473. public function markReadTag($id = '', $idMax = 0, $filters = null, $state = 0, $is_read = true) {
  474. FreshRSS_UserDAO::touch();
  475. if ($idMax == 0) {
  476. $idMax = time() . '000000';
  477. Minz_Log::debug('Calling markReadTag(0) is deprecated!');
  478. }
  479. $sql = 'UPDATE `_entry` e INNER JOIN `_entrytag` et ON et.id_entry = e.id '
  480. . 'SET e.is_read = ? '
  481. . 'WHERE '
  482. . ($id == '' ? '' : 'et.id_tag = ? AND ')
  483. . 'e.is_read <> ? AND e.id <= ?';
  484. $values = array($is_read ? 1 : 0);
  485. if ($id != '') {
  486. $values[] = $id;
  487. }
  488. $values[] = $is_read ? 1 : 0;
  489. $values[] = $idMax;
  490. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state);
  491. $stm = $this->pdo->prepare($sql . $search);
  492. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  493. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  494. Minz_Log::error('SQL error markReadTag: ' . $info[2]);
  495. return false;
  496. }
  497. $affected = $stm->rowCount();
  498. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  499. return false;
  500. }
  501. return $affected;
  502. }
  503. public function cleanOldEntries($id_feed, $options = []) { //Remember to call updateCachedValue($id_feed) or updateCachedValues() just after
  504. $sql = 'DELETE FROM `_entry` WHERE id_feed = :id_feed1'; //No alias for MySQL / MariaDB
  505. $params = [];
  506. $params[':id_feed1'] = $id_feed;
  507. //==Exclusions==
  508. if (!empty($options['keep_favourites'])) {
  509. $sql .= ' AND is_favorite = 0';
  510. }
  511. if (!empty($options['keep_unreads'])) {
  512. $sql .= ' AND is_read = 1';
  513. }
  514. if (!empty($options['keep_labels'])) {
  515. $sql .= ' AND NOT EXISTS (SELECT 1 FROM `_entrytag` WHERE id_entry = id)';
  516. }
  517. if (!empty($options['keep_min']) && $options['keep_min'] > 0) {
  518. //Double SELECT for MySQL workaround ERROR 1093 (HY000)
  519. $sql .= ' AND `lastSeen` < (SELECT `lastSeen`'
  520. . ' FROM (SELECT e2.`lastSeen` FROM `_entry` e2 WHERE e2.id_feed = :id_feed2'
  521. . ' ORDER BY e2.`lastSeen` DESC LIMIT 1 OFFSET :keep_min) last_seen2)';
  522. $params[':id_feed2'] = $id_feed;
  523. $params[':keep_min'] = (int)$options['keep_min'];
  524. }
  525. //Keep at least the articles seen at the last refresh
  526. $sql .= ' AND `lastSeen` < (SELECT maxlastseen'
  527. . ' FROM (SELECT MAX(e3.`lastSeen`) AS maxlastseen FROM `_entry` e3 WHERE e3.id_feed = :id_feed3) last_seen3)';
  528. $params[':id_feed3'] = $id_feed;
  529. //==Inclusions==
  530. $sql .= ' AND (1=0';
  531. if (!empty($options['keep_period'])) {
  532. $sql .= ' OR `lastSeen` < :max_last_seen';
  533. $now = new DateTime('now');
  534. $now->sub(new DateInterval($options['keep_period']));
  535. $params[':max_last_seen'] = $now->format('U');
  536. }
  537. if (!empty($options['keep_max']) && $options['keep_max'] > 0) {
  538. $sql .= ' OR `lastSeen` <= (SELECT `lastSeen`'
  539. . ' FROM (SELECT e4.`lastSeen` FROM `_entry` e4 WHERE e4.id_feed = :id_feed4'
  540. . ' ORDER BY e4.`lastSeen` DESC LIMIT 1 OFFSET :keep_max) last_seen4)';
  541. $params[':id_feed4'] = $id_feed;
  542. $params[':keep_max'] = (int)$options['keep_max'];
  543. }
  544. $sql .= ')';
  545. $stm = $this->pdo->prepare($sql);
  546. if ($stm && $stm->execute($params)) {
  547. return $stm->rowCount();
  548. } else {
  549. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  550. if ($this->autoUpdateDb($info)) {
  551. return $this->cleanOldEntries($id_feed, $options);
  552. }
  553. Minz_Log::error(__method__ . ' error:' . json_encode($info));
  554. return false;
  555. }
  556. }
  557. public function selectAll() {
  558. $sql = 'SELECT id, guid, title, author, '
  559. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  560. . ', link, date, `lastSeen`, ' . $this->sqlHexEncode('hash') . ' AS hash, is_read, is_favorite, id_feed, tags '
  561. . 'FROM `_entry`';
  562. $stm = $this->pdo->query($sql);
  563. while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
  564. yield $row;
  565. }
  566. }
  567. public function searchByGuid($id_feed, $guid) {
  568. // un guid est unique pour un flux donné
  569. $sql = 'SELECT id, guid, title, author, '
  570. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  571. . ', link, date, is_read, is_favorite, id_feed, tags '
  572. . 'FROM `_entry` WHERE id_feed=:id_feed AND guid=:guid';
  573. $stm = $this->pdo->prepare($sql);
  574. $stm->bindParam(':id_feed', $id_feed, PDO::PARAM_INT);
  575. $stm->bindParam(':guid', $guid);
  576. $stm->execute();
  577. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  578. $entries = self::daoToEntries($res);
  579. return isset($entries[0]) ? $entries[0] : null;
  580. }
  581. public function searchById($id) {
  582. $sql = 'SELECT id, guid, title, author, '
  583. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  584. . ', link, date, is_read, is_favorite, id_feed, tags '
  585. . 'FROM `_entry` WHERE id=:id';
  586. $stm = $this->pdo->prepare($sql);
  587. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  588. $stm->execute();
  589. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  590. $entries = self::daoToEntries($res);
  591. return isset($entries[0]) ? $entries[0] : null;
  592. }
  593. public function searchIdByGuid($id_feed, $guid) {
  594. $sql = 'SELECT id FROM `_entry` WHERE id_feed=:id_feed AND guid=:guid';
  595. $stm = $this->pdo->prepare($sql);
  596. $stm->bindParam(':id_feed', $id_feed, PDO::PARAM_INT);
  597. $stm->bindParam(':guid', $guid);
  598. $stm->execute();
  599. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  600. return isset($res[0]) ? $res[0] : null;
  601. }
  602. protected function sqlConcat($s1, $s2) {
  603. return 'CONCAT(' . $s1 . ',' . $s2 . ')'; //MySQL
  604. }
  605. protected function sqlListEntriesWhere($alias = '', $filters = null, $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $firstId = '', $date_min = 0) {
  606. $search = ' ';
  607. $values = array();
  608. if ($state & FreshRSS_Entry::STATE_NOT_READ) {
  609. if (!($state & FreshRSS_Entry::STATE_READ)) {
  610. $search .= 'AND ' . $alias . 'is_read=0 ';
  611. }
  612. } elseif ($state & FreshRSS_Entry::STATE_READ) {
  613. $search .= 'AND ' . $alias . 'is_read=1 ';
  614. }
  615. if ($state & FreshRSS_Entry::STATE_FAVORITE) {
  616. if (!($state & FreshRSS_Entry::STATE_NOT_FAVORITE)) {
  617. $search .= 'AND ' . $alias . 'is_favorite=1 ';
  618. }
  619. } elseif ($state & FreshRSS_Entry::STATE_NOT_FAVORITE) {
  620. $search .= 'AND ' . $alias . 'is_favorite=0 ';
  621. }
  622. switch ($order) {
  623. case 'DESC':
  624. case 'ASC':
  625. break;
  626. default:
  627. throw new FreshRSS_EntriesGetter_Exception('Bad order in Entry->listByType: [' . $order . ']!');
  628. }
  629. if ($firstId !== '') {
  630. $search .= 'AND ' . $alias . 'id ' . ($order === 'DESC' ? '<=' : '>=') . ' ? ';
  631. $values[] = $firstId;
  632. }
  633. if ($date_min > 0) {
  634. $search .= 'AND ' . $alias . 'id >= ? ';
  635. $values[] = $date_min . '000000';
  636. }
  637. if ($filters && count($filters->searches()) > 0) {
  638. $isOpen = false;
  639. foreach ($filters->searches() as $filter) {
  640. if ($filter == null) {
  641. continue;
  642. }
  643. $sub_search = '';
  644. if ($filter->getMinDate()) {
  645. $sub_search .= 'AND ' . $alias . 'id >= ? ';
  646. $values[] = "{$filter->getMinDate()}000000";
  647. }
  648. if ($filter->getMaxDate()) {
  649. $sub_search .= 'AND ' . $alias . 'id <= ? ';
  650. $values[] = "{$filter->getMaxDate()}000000";
  651. }
  652. if ($filter->getMinPubdate()) {
  653. $sub_search .= 'AND ' . $alias . 'date >= ? ';
  654. $values[] = $filter->getMinPubdate();
  655. }
  656. if ($filter->getMaxPubdate()) {
  657. $sub_search .= 'AND ' . $alias . 'date <= ? ';
  658. $values[] = $filter->getMaxPubdate();
  659. }
  660. if ($filter->getAuthor()) {
  661. foreach ($filter->getAuthor() as $author) {
  662. $sub_search .= 'AND ' . $alias . 'author LIKE ? ';
  663. $values[] = "%{$author}%";
  664. }
  665. }
  666. if ($filter->getIntitle()) {
  667. foreach ($filter->getIntitle() as $title) {
  668. $sub_search .= 'AND ' . $alias . 'title LIKE ? ';
  669. $values[] = "%{$title}%";
  670. }
  671. }
  672. if ($filter->getTags()) {
  673. foreach ($filter->getTags() as $tag) {
  674. $sub_search .= 'AND ' . $alias . 'tags LIKE ? ';
  675. $values[] = "%{$tag}%";
  676. }
  677. }
  678. if ($filter->getInurl()) {
  679. foreach ($filter->getInurl() as $url) {
  680. $sub_search .= 'AND CONCAT(' . $alias . 'link, ' . $alias . 'guid) LIKE ? ';
  681. $values[] = "%{$url}%";
  682. }
  683. }
  684. if ($filter->getNotAuthor()) {
  685. foreach ($filter->getNotAuthor() as $author) {
  686. $sub_search .= 'AND (NOT ' . $alias . 'author LIKE ?) ';
  687. $values[] = "%{$author}%";
  688. }
  689. }
  690. if ($filter->getNotIntitle()) {
  691. foreach ($filter->getNotIntitle() as $title) {
  692. $sub_search .= 'AND (NOT ' . $alias . 'title LIKE ?) ';
  693. $values[] = "%{$title}%";
  694. }
  695. }
  696. if ($filter->getNotTags()) {
  697. foreach ($filter->getNotTags() as $tag) {
  698. $sub_search .= 'AND (NOT ' . $alias . 'tags LIKE ?) ';
  699. $values[] = "%{$tag}%";
  700. }
  701. }
  702. if ($filter->getNotInurl()) {
  703. foreach ($filter->getNotInurl() as $url) {
  704. $sub_search .= 'AND (NOT CONCAT(' . $alias . 'link, ' . $alias . 'guid) LIKE ?) ';
  705. $values[] = "%{$url}%";
  706. }
  707. }
  708. if ($filter->getSearch()) {
  709. foreach ($filter->getSearch() as $search_value) {
  710. $sub_search .= 'AND ' . $this->sqlconcat($alias . 'title', $this->isCompressed() ? 'UNCOMPRESS(' . $alias . 'content_bin)' : '' . $alias . 'content') . ' LIKE ? ';
  711. $values[] = "%{$search_value}%";
  712. }
  713. }
  714. if ($filter->getNotSearch()) {
  715. foreach ($filter->getNotSearch() as $search_value) {
  716. $sub_search .= 'AND (NOT ' . $this->sqlconcat($alias . 'title', $this->isCompressed() ? 'UNCOMPRESS(' . $alias . 'content_bin)' : '' . $alias . 'content') . ' LIKE ?) ';
  717. $values[] = "%{$search_value}%";
  718. }
  719. }
  720. if ($sub_search != '') {
  721. if ($isOpen) {
  722. $search .= 'OR ';
  723. } else {
  724. $search .= 'AND (';
  725. $isOpen = true;
  726. }
  727. $search .= '(' . substr($sub_search, 4) . ') ';
  728. }
  729. }
  730. if ($isOpen) {
  731. $search .= ') ';
  732. }
  733. }
  734. return array($values, $search);
  735. }
  736. private function sqlListWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filters = null, $date_min = 0) {
  737. if (!$state) {
  738. $state = FreshRSS_Entry::STATE_ALL;
  739. }
  740. $where = '';
  741. $joinFeed = false;
  742. $values = array();
  743. switch ($type) {
  744. case 'a': //All PRIORITY_MAIN_STREAM
  745. $where .= 'f.priority > ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  746. break;
  747. case 'A': //All except PRIORITY_ARCHIVED
  748. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  749. break;
  750. case 's': //Starred. Deprecated: use $state instead
  751. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  752. $where .= 'AND e.is_favorite=1 ';
  753. break;
  754. case 'S': //Starred
  755. $where .= 'e.is_favorite=1 ';
  756. break;
  757. case 'c': //Category
  758. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  759. $where .= 'AND f.category=? ';
  760. $values[] = intval($id);
  761. break;
  762. case 'f': //Feed
  763. $where .= 'e.id_feed=? ';
  764. $values[] = intval($id);
  765. break;
  766. case 't': //Tag
  767. $where .= 'et.id_tag=? ';
  768. $values[] = intval($id);
  769. break;
  770. case 'T': //Any tag
  771. $where .= '1=1 ';
  772. break;
  773. case 'ST': //Starred or tagged
  774. $where .= 'e.is_favorite=1 OR EXISTS (SELECT et2.id_tag FROM `_entrytag` et2 WHERE et2.id_entry = e.id) ';
  775. break;
  776. default:
  777. throw new FreshRSS_EntriesGetter_Exception('Bad type in Entry->listByType: [' . $type . ']!');
  778. }
  779. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state, $order, $firstId, $date_min);
  780. return array(array_merge($values, $searchValues),
  781. 'SELECT '
  782. . ($type === 'T' ? 'DISTINCT ' : '')
  783. . 'e.id FROM `_entry` e '
  784. . 'INNER JOIN `_feed` f ON e.id_feed = f.id '
  785. . ($type === 't' || $type === 'T' ? 'INNER JOIN `_entrytag` et ON et.id_entry = e.id ' : '')
  786. . 'WHERE ' . $where
  787. . $search
  788. . 'ORDER BY e.id ' . $order
  789. . ($limit > 0 ? ' LIMIT ' . intval($limit) : '')); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  790. }
  791. public function listWhereRaw($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filters = null, $date_min = 0) {
  792. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filters, $date_min);
  793. $sql = 'SELECT e0.id, e0.guid, e0.title, e0.author, '
  794. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  795. . ', e0.link, e0.date, e0.is_read, e0.is_favorite, e0.id_feed, e0.tags '
  796. . 'FROM `_entry` e0 '
  797. . 'INNER JOIN ('
  798. . $sql
  799. . ') e2 ON e2.id=e0.id '
  800. . 'ORDER BY e0.id ' . $order;
  801. $stm = $this->pdo->prepare($sql);
  802. if ($stm && $stm->execute($values)) {
  803. return $stm;
  804. } else {
  805. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  806. Minz_Log::error('SQL error listWhereRaw: ' . $info[2]);
  807. return false;
  808. }
  809. }
  810. public function listWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filters = null, $date_min = 0) {
  811. $stm = $this->listWhereRaw($type, $id, $state, $order, $limit, $firstId, $filters, $date_min);
  812. if ($stm) {
  813. return self::daoToEntries($stm->fetchAll(PDO::FETCH_ASSOC));
  814. } else {
  815. return false;
  816. }
  817. }
  818. public function listByIds($ids, $order = 'DESC') {
  819. if (count($ids) < 1) {
  820. return array();
  821. }
  822. $sql = 'SELECT id, guid, title, author, '
  823. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  824. . ', link, date, is_read, is_favorite, id_feed, tags '
  825. . 'FROM `_entry` '
  826. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?) '
  827. . 'ORDER BY id ' . $order;
  828. $stm = $this->pdo->prepare($sql);
  829. $stm->execute($ids);
  830. return self::daoToEntries($stm->fetchAll(PDO::FETCH_ASSOC));
  831. }
  832. public function listIdsWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filters = null) { //For API
  833. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filters);
  834. $stm = $this->pdo->prepare($sql);
  835. $stm->execute($values);
  836. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  837. }
  838. public function listHashForFeedGuids($id_feed, $guids) {
  839. if (count($guids) < 1) {
  840. return array();
  841. }
  842. $guids = array_unique($guids);
  843. $sql = 'SELECT guid, ' . $this->sqlHexEncode('hash') . ' AS hex_hash FROM `_entry` WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
  844. $stm = $this->pdo->prepare($sql);
  845. $values = array($id_feed);
  846. $values = array_merge($values, $guids);
  847. if ($stm && $stm->execute($values)) {
  848. $result = array();
  849. $rows = $stm->fetchAll(PDO::FETCH_ASSOC);
  850. foreach ($rows as $row) {
  851. $result[$row['guid']] = $row['hex_hash'];
  852. }
  853. return $result;
  854. } else {
  855. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  856. if ($this->autoUpdateDb($info)) {
  857. return $this->listHashForFeedGuids($id_feed, $guids);
  858. }
  859. Minz_Log::error('SQL error listHashForFeedGuids: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  860. . ' while querying feed ' . $id_feed);
  861. return false;
  862. }
  863. }
  864. public function updateLastSeen($id_feed, $guids, $mtime = 0) {
  865. if (count($guids) < 1) {
  866. return 0;
  867. }
  868. $sql = 'UPDATE `_entry` SET `lastSeen`=? WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
  869. $stm = $this->pdo->prepare($sql);
  870. if ($mtime <= 0) {
  871. $mtime = time();
  872. }
  873. $values = array($mtime, $id_feed);
  874. $values = array_merge($values, $guids);
  875. if ($stm && $stm->execute($values)) {
  876. return $stm->rowCount();
  877. } else {
  878. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  879. if ($this->autoUpdateDb($info)) {
  880. return $this->updateLastSeen($id_feed, $guids);
  881. }
  882. Minz_Log::error('SQL error updateLastSeen: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  883. . ' while updating feed ' . $id_feed);
  884. return false;
  885. }
  886. }
  887. public function countUnreadRead() {
  888. $sql = 'SELECT COUNT(e.id) AS count FROM `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id WHERE f.priority > 0'
  889. . ' UNION SELECT COUNT(e.id) AS count FROM `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id WHERE f.priority > 0 AND e.is_read=0';
  890. $stm = $this->pdo->query($sql);
  891. if ($stm === false) {
  892. return false;
  893. }
  894. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  895. rsort($res);
  896. $all = empty($res[0]) ? 0 : $res[0];
  897. $unread = empty($res[1]) ? 0 : $res[1];
  898. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  899. }
  900. public function count($minPriority = null) {
  901. $sql = 'SELECT COUNT(e.id) AS count FROM `_entry` e';
  902. if ($minPriority !== null) {
  903. $sql .= ' INNER JOIN `_feed` f ON e.id_feed=f.id';
  904. $sql .= ' WHERE f.priority > ' . intval($minPriority);
  905. }
  906. $stm = $this->pdo->query($sql);
  907. if ($stm == false) {
  908. return false;
  909. }
  910. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  911. return isset($res[0]) ? $res[0] : 0;
  912. }
  913. public function countNotRead($minPriority = null) {
  914. $sql = 'SELECT COUNT(e.id) AS count FROM `_entry` e';
  915. if ($minPriority !== null) {
  916. $sql .= ' INNER JOIN `_feed` f ON e.id_feed=f.id';
  917. }
  918. $sql .= ' WHERE e.is_read=0';
  919. if ($minPriority !== null) {
  920. $sql .= ' AND f.priority > ' . intval($minPriority);
  921. }
  922. $stm = $this->pdo->query($sql);
  923. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  924. return $res[0];
  925. }
  926. public function countUnreadReadFavorites() {
  927. $sql = <<<'SQL'
  928. SELECT c FROM (
  929. SELECT COUNT(e1.id) AS c, 1 AS o
  930. FROM `_entry` AS e1
  931. JOIN `_feed` AS f1 ON e1.id_feed = f1.id
  932. WHERE e1.is_favorite = 1
  933. AND f1.priority >= :priority_normal1
  934. UNION
  935. SELECT COUNT(e2.id) AS c, 2 AS o
  936. FROM `_entry` AS e2
  937. JOIN `_feed` AS f2 ON e2.id_feed = f2.id
  938. WHERE e2.is_favorite = 1
  939. AND e2.is_read = 0
  940. AND f2.priority >= :priority_normal2
  941. ) u
  942. ORDER BY o
  943. SQL;
  944. $stm = $this->pdo->prepare($sql);
  945. //Binding a value more than once is not standard and does not work with native prepared statements (e.g. MySQL) https://bugs.php.net/bug.php?id=40417
  946. $stm->bindValue(':priority_normal1', FreshRSS_Feed::PRIORITY_NORMAL, PDO::PARAM_INT);
  947. $stm->bindValue(':priority_normal2', FreshRSS_Feed::PRIORITY_NORMAL, PDO::PARAM_INT);
  948. $stm->execute();
  949. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  950. rsort($res);
  951. $all = empty($res[0]) ? 0 : $res[0];
  952. $unread = empty($res[1]) ? 0 : $res[1];
  953. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  954. }
  955. public static function daoToEntry($dao) {
  956. $entry = new FreshRSS_Entry(
  957. $dao['id_feed'],
  958. $dao['guid'],
  959. $dao['title'],
  960. $dao['author'],
  961. $dao['content'],
  962. $dao['link'],
  963. $dao['date'],
  964. $dao['is_read'],
  965. $dao['is_favorite'],
  966. isset($dao['tags']) ? $dao['tags'] : ''
  967. );
  968. if (isset($dao['id'])) {
  969. $entry->_id($dao['id']);
  970. }
  971. return $entry;
  972. }
  973. private static function daoToEntries($listDAO) {
  974. $list = array();
  975. if (!is_array($listDAO)) {
  976. $listDAO = array($listDAO);
  977. }
  978. foreach ($listDAO as $key => $dao) {
  979. $list[] = self::daoToEntry($dao);
  980. }
  981. unset($listDAO);
  982. return $list;
  983. }
  984. }