EntryDAO.php 36 KB

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