EntryDAO.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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_once(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) !== false;
  26. } catch (Exception $e) {
  27. Minz_Log::error('FreshRSS_EntryDAO::createEntryTempTable error: ' . $e->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 ? array(0 => '', 1 => '', 2 => 'syntax error') : $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 ? array(0 => '', 1 => '', 2 => 'syntax error') : $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 ? array(2 => 'syntax error') : $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 ? array(2 => 'syntax error') : $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 ? array(2 => 'syntax error') : $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 ? array(2 => 'syntax error') : $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 ? array(2 => 'syntax error') : $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 ? array(2 => 'syntax error') : $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 ? array(2 => 'syntax error') : $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 ? array(2 => 'syntax error') : $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 ? array(2 => 'syntax error') : $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, $date_min, $keep = 15) { //Remember to call updateCachedValue($id_feed) or updateCachedValues() just after
  504. $sql = 'DELETE FROM `_entry` '
  505. . 'WHERE id_feed=:id_feed1 AND id<=:id_max '
  506. . 'AND is_favorite=0 ' //Do not remove favourites
  507. . 'AND `lastSeen` < (SELECT maxLastSeen FROM (SELECT (MAX(e3.`lastSeen`)-99) AS maxLastSeen FROM `_entry` e3 WHERE e3.id_feed=:id_feed2) recent) ' //Do not remove the most newly seen articles, plus a few seconds of tolerance
  508. . 'AND id NOT IN (SELECT id_entry FROM `_entrytag`) ' //Do not purge tagged entries
  509. . 'AND id NOT IN (SELECT id FROM (SELECT e2.id FROM `_entry` e2 WHERE e2.id_feed=:id_feed3 ORDER BY id DESC LIMIT :keep) keep)'; //Double select: MySQL doesn't support 'LIMIT & IN/ALL/ANY/SOME subquery'
  510. $stm = $this->pdo->prepare($sql);
  511. if ($stm) {
  512. $id_max = intval($date_min) . '000000';
  513. $stm->bindParam(':id_max', $id_max, PDO::PARAM_STR);
  514. $stm->bindParam(':id_feed1', $id_feed, PDO::PARAM_INT);
  515. $stm->bindParam(':id_feed2', $id_feed, PDO::PARAM_INT);
  516. $stm->bindParam(':id_feed3', $id_feed, PDO::PARAM_INT);
  517. $stm->bindParam(':keep', $keep, PDO::PARAM_INT);
  518. }
  519. if ($stm && $stm->execute()) {
  520. return $stm->rowCount();
  521. } else {
  522. $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
  523. if ($this->autoUpdateDb($info)) {
  524. return $this->cleanOldEntries($id_feed, $date_min, $keep);
  525. }
  526. Minz_Log::error('SQL error cleanOldEntries: ' . $info[2]);
  527. return false;
  528. }
  529. }
  530. public function selectAll() {
  531. $sql = 'SELECT id, guid, title, author, '
  532. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  533. . ', link, date, `lastSeen`, ' . $this->sqlHexEncode('hash') . ' AS hash, is_read, is_favorite, id_feed, tags '
  534. . 'FROM `_entry`';
  535. $stm = $this->pdo->query($sql);
  536. while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
  537. yield $row;
  538. }
  539. }
  540. public function searchByGuid($id_feed, $guid) {
  541. // un guid est unique pour un flux donné
  542. $sql = 'SELECT id, guid, title, author, '
  543. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  544. . ', link, date, is_read, is_favorite, id_feed, tags '
  545. . 'FROM `_entry` WHERE id_feed=:id_feed AND guid=:guid';
  546. $stm = $this->pdo->prepare($sql);
  547. $stm->bindParam(':id_feed', $id_feed, PDO::PARAM_INT);
  548. $stm->bindParam(':guid', $guid);
  549. $stm->execute();
  550. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  551. $entries = self::daoToEntries($res);
  552. return isset($entries[0]) ? $entries[0] : null;
  553. }
  554. public function searchById($id) {
  555. $sql = 'SELECT id, guid, title, author, '
  556. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  557. . ', link, date, is_read, is_favorite, id_feed, tags '
  558. . 'FROM `_entry` WHERE id=:id';
  559. $stm = $this->pdo->prepare($sql);
  560. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  561. $stm->execute();
  562. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  563. $entries = self::daoToEntries($res);
  564. return isset($entries[0]) ? $entries[0] : null;
  565. }
  566. public function searchIdByGuid($id_feed, $guid) {
  567. $sql = 'SELECT id FROM `_entry` WHERE id_feed=:id_feed AND guid=:guid';
  568. $stm = $this->pdo->prepare($sql);
  569. $stm->bindParam(':id_feed', $id_feed, PDO::PARAM_INT);
  570. $stm->bindParam(':guid', $guid);
  571. $stm->execute();
  572. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  573. return isset($res[0]) ? $res[0] : null;
  574. }
  575. protected function sqlConcat($s1, $s2) {
  576. return 'CONCAT(' . $s1 . ',' . $s2 . ')'; //MySQL
  577. }
  578. protected function sqlListEntriesWhere($alias = '', $filters = null, $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $firstId = '', $date_min = 0) {
  579. $search = ' ';
  580. $values = array();
  581. if ($state & FreshRSS_Entry::STATE_NOT_READ) {
  582. if (!($state & FreshRSS_Entry::STATE_READ)) {
  583. $search .= 'AND ' . $alias . 'is_read=0 ';
  584. }
  585. } elseif ($state & FreshRSS_Entry::STATE_READ) {
  586. $search .= 'AND ' . $alias . 'is_read=1 ';
  587. }
  588. if ($state & FreshRSS_Entry::STATE_FAVORITE) {
  589. if (!($state & FreshRSS_Entry::STATE_NOT_FAVORITE)) {
  590. $search .= 'AND ' . $alias . 'is_favorite=1 ';
  591. }
  592. } elseif ($state & FreshRSS_Entry::STATE_NOT_FAVORITE) {
  593. $search .= 'AND ' . $alias . 'is_favorite=0 ';
  594. }
  595. switch ($order) {
  596. case 'DESC':
  597. case 'ASC':
  598. break;
  599. default:
  600. throw new FreshRSS_EntriesGetter_Exception('Bad order in Entry->listByType: [' . $order . ']!');
  601. }
  602. if ($firstId !== '') {
  603. $search .= 'AND ' . $alias . 'id ' . ($order === 'DESC' ? '<=' : '>=') . ' ? ';
  604. $values[] = $firstId;
  605. }
  606. if ($date_min > 0) {
  607. $search .= 'AND ' . $alias . 'id >= ? ';
  608. $values[] = $date_min . '000000';
  609. }
  610. if ($filters && count($filters->searches()) > 0) {
  611. $isOpen = false;
  612. foreach ($filters->searches() as $filter) {
  613. if ($filter == null) {
  614. continue;
  615. }
  616. $sub_search = '';
  617. if ($filter->getMinDate()) {
  618. $sub_search .= 'AND ' . $alias . 'id >= ? ';
  619. $values[] = "{$filter->getMinDate()}000000";
  620. }
  621. if ($filter->getMaxDate()) {
  622. $sub_search .= 'AND ' . $alias . 'id <= ? ';
  623. $values[] = "{$filter->getMaxDate()}000000";
  624. }
  625. if ($filter->getMinPubdate()) {
  626. $sub_search .= 'AND ' . $alias . 'date >= ? ';
  627. $values[] = $filter->getMinPubdate();
  628. }
  629. if ($filter->getMaxPubdate()) {
  630. $sub_search .= 'AND ' . $alias . 'date <= ? ';
  631. $values[] = $filter->getMaxPubdate();
  632. }
  633. if ($filter->getAuthor()) {
  634. foreach ($filter->getAuthor() as $author) {
  635. $sub_search .= 'AND ' . $alias . 'author LIKE ? ';
  636. $values[] = "%{$author}%";
  637. }
  638. }
  639. if ($filter->getIntitle()) {
  640. foreach ($filter->getIntitle() as $title) {
  641. $sub_search .= 'AND ' . $alias . 'title LIKE ? ';
  642. $values[] = "%{$title}%";
  643. }
  644. }
  645. if ($filter->getTags()) {
  646. foreach ($filter->getTags() as $tag) {
  647. $sub_search .= 'AND ' . $alias . 'tags LIKE ? ';
  648. $values[] = "%{$tag}%";
  649. }
  650. }
  651. if ($filter->getInurl()) {
  652. foreach ($filter->getInurl() as $url) {
  653. $sub_search .= 'AND CONCAT(' . $alias . 'link, ' . $alias . 'guid) LIKE ? ';
  654. $values[] = "%{$url}%";
  655. }
  656. }
  657. if ($filter->getNotAuthor()) {
  658. foreach ($filter->getNotAuthor() as $author) {
  659. $sub_search .= 'AND (NOT ' . $alias . 'author LIKE ?) ';
  660. $values[] = "%{$author}%";
  661. }
  662. }
  663. if ($filter->getNotIntitle()) {
  664. foreach ($filter->getNotIntitle() as $title) {
  665. $sub_search .= 'AND (NOT ' . $alias . 'title LIKE ?) ';
  666. $values[] = "%{$title}%";
  667. }
  668. }
  669. if ($filter->getNotTags()) {
  670. foreach ($filter->getNotTags() as $tag) {
  671. $sub_search .= 'AND (NOT ' . $alias . 'tags LIKE ?) ';
  672. $values[] = "%{$tag}%";
  673. }
  674. }
  675. if ($filter->getNotInurl()) {
  676. foreach ($filter->getNotInurl() as $url) {
  677. $sub_search .= 'AND (NOT CONCAT(' . $alias . 'link, ' . $alias . 'guid) LIKE ?) ';
  678. $values[] = "%{$url}%";
  679. }
  680. }
  681. if ($filter->getSearch()) {
  682. foreach ($filter->getSearch() as $search_value) {
  683. $sub_search .= 'AND ' . $this->sqlconcat($alias . 'title', $this->isCompressed() ? 'UNCOMPRESS(' . $alias . 'content_bin)' : '' . $alias . 'content') . ' LIKE ? ';
  684. $values[] = "%{$search_value}%";
  685. }
  686. }
  687. if ($filter->getNotSearch()) {
  688. foreach ($filter->getNotSearch() as $search_value) {
  689. $sub_search .= 'AND (NOT ' . $this->sqlconcat($alias . 'title', $this->isCompressed() ? 'UNCOMPRESS(' . $alias . 'content_bin)' : '' . $alias . 'content') . ' LIKE ?) ';
  690. $values[] = "%{$search_value}%";
  691. }
  692. }
  693. if ($sub_search != '') {
  694. if ($isOpen) {
  695. $search .= 'OR ';
  696. } else {
  697. $search .= 'AND (';
  698. $isOpen = true;
  699. }
  700. $search .= '(' . substr($sub_search, 4) . ') ';
  701. }
  702. }
  703. if ($isOpen) {
  704. $search .= ') ';
  705. }
  706. }
  707. return array($values, $search);
  708. }
  709. private function sqlListWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filters = null, $date_min = 0) {
  710. if (!$state) {
  711. $state = FreshRSS_Entry::STATE_ALL;
  712. }
  713. $where = '';
  714. $joinFeed = false;
  715. $values = array();
  716. switch ($type) {
  717. case 'a': //All PRIORITY_MAIN_STREAM
  718. $where .= 'f.priority > ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  719. break;
  720. case 'A': //All except PRIORITY_ARCHIVED
  721. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  722. break;
  723. case 's': //Starred. Deprecated: use $state instead
  724. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  725. $where .= 'AND e.is_favorite=1 ';
  726. break;
  727. case 'S': //Starred
  728. $where .= 'e.is_favorite=1 ';
  729. break;
  730. case 'c': //Category
  731. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  732. $where .= 'AND f.category=? ';
  733. $values[] = intval($id);
  734. break;
  735. case 'f': //Feed
  736. $where .= 'e.id_feed=? ';
  737. $values[] = intval($id);
  738. break;
  739. case 't': //Tag
  740. $where .= 'et.id_tag=? ';
  741. $values[] = intval($id);
  742. break;
  743. case 'T': //Any tag
  744. $where .= '1=1 ';
  745. break;
  746. case 'ST': //Starred or tagged
  747. $where .= 'e.is_favorite=1 OR EXISTS (SELECT et2.id_tag FROM `_entrytag` et2 WHERE et2.id_entry = e.id) ';
  748. break;
  749. default:
  750. throw new FreshRSS_EntriesGetter_Exception('Bad type in Entry->listByType: [' . $type . ']!');
  751. }
  752. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state, $order, $firstId, $date_min);
  753. return array(array_merge($values, $searchValues),
  754. 'SELECT '
  755. . ($type === 'T' ? 'DISTINCT ' : '')
  756. . 'e.id FROM `_entry` e '
  757. . 'INNER JOIN `_feed` f ON e.id_feed = f.id '
  758. . ($type === 't' || $type === 'T' ? 'INNER JOIN `_entrytag` et ON et.id_entry = e.id ' : '')
  759. . 'WHERE ' . $where
  760. . $search
  761. . 'ORDER BY e.id ' . $order
  762. . ($limit > 0 ? ' LIMIT ' . intval($limit) : '')); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  763. }
  764. public function listWhereRaw($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filters = null, $date_min = 0) {
  765. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filters, $date_min);
  766. $sql = 'SELECT e0.id, e0.guid, e0.title, e0.author, '
  767. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  768. . ', e0.link, e0.date, e0.is_read, e0.is_favorite, e0.id_feed, e0.tags '
  769. . 'FROM `_entry` e0 '
  770. . 'INNER JOIN ('
  771. . $sql
  772. . ') e2 ON e2.id=e0.id '
  773. . 'ORDER BY e0.id ' . $order;
  774. $stm = $this->pdo->prepare($sql);
  775. if ($stm && $stm->execute($values)) {
  776. return $stm;
  777. } else {
  778. $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
  779. Minz_Log::error('SQL error listWhereRaw: ' . $info[2]);
  780. return false;
  781. }
  782. }
  783. public function listWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filters = null, $date_min = 0) {
  784. $stm = $this->listWhereRaw($type, $id, $state, $order, $limit, $firstId, $filters, $date_min);
  785. if ($stm) {
  786. return self::daoToEntries($stm->fetchAll(PDO::FETCH_ASSOC));
  787. } else {
  788. return false;
  789. }
  790. }
  791. public function listByIds($ids, $order = 'DESC') {
  792. if (count($ids) < 1) {
  793. return array();
  794. }
  795. $sql = 'SELECT id, guid, title, author, '
  796. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  797. . ', link, date, is_read, is_favorite, id_feed, tags '
  798. . 'FROM `_entry` '
  799. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?) '
  800. . 'ORDER BY id ' . $order;
  801. $stm = $this->pdo->prepare($sql);
  802. $stm->execute($ids);
  803. return self::daoToEntries($stm->fetchAll(PDO::FETCH_ASSOC));
  804. }
  805. public function listIdsWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filters = null) { //For API
  806. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filters);
  807. $stm = $this->pdo->prepare($sql);
  808. $stm->execute($values);
  809. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  810. }
  811. public function listHashForFeedGuids($id_feed, $guids) {
  812. if (count($guids) < 1) {
  813. return array();
  814. }
  815. $guids = array_unique($guids);
  816. $sql = 'SELECT guid, ' . $this->sqlHexEncode('hash') . ' AS hex_hash FROM `_entry` WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
  817. $stm = $this->pdo->prepare($sql);
  818. $values = array($id_feed);
  819. $values = array_merge($values, $guids);
  820. if ($stm && $stm->execute($values)) {
  821. $result = array();
  822. $rows = $stm->fetchAll(PDO::FETCH_ASSOC);
  823. foreach ($rows as $row) {
  824. $result[$row['guid']] = $row['hex_hash'];
  825. }
  826. return $result;
  827. } else {
  828. $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
  829. if ($this->autoUpdateDb($info)) {
  830. return $this->listHashForFeedGuids($id_feed, $guids);
  831. }
  832. Minz_Log::error('SQL error listHashForFeedGuids: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  833. . ' while querying feed ' . $id_feed);
  834. return false;
  835. }
  836. }
  837. public function updateLastSeen($id_feed, $guids, $mtime = 0) {
  838. if (count($guids) < 1) {
  839. return 0;
  840. }
  841. $sql = 'UPDATE `_entry` SET `lastSeen`=? WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
  842. $stm = $this->pdo->prepare($sql);
  843. if ($mtime <= 0) {
  844. $mtime = time();
  845. }
  846. $values = array($mtime, $id_feed);
  847. $values = array_merge($values, $guids);
  848. if ($stm && $stm->execute($values)) {
  849. return $stm->rowCount();
  850. } else {
  851. $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
  852. if ($this->autoUpdateDb($info)) {
  853. return $this->updateLastSeen($id_feed, $guids);
  854. }
  855. Minz_Log::error('SQL error updateLastSeen: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  856. . ' while updating feed ' . $id_feed);
  857. return false;
  858. }
  859. }
  860. public function countUnreadRead() {
  861. $sql = 'SELECT COUNT(e.id) AS count FROM `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id WHERE f.priority > 0'
  862. . ' 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';
  863. $stm = $this->pdo->query($sql);
  864. if ($stm === false) {
  865. return false;
  866. }
  867. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  868. rsort($res);
  869. $all = empty($res[0]) ? 0 : $res[0];
  870. $unread = empty($res[1]) ? 0 : $res[1];
  871. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  872. }
  873. public function count($minPriority = null) {
  874. $sql = 'SELECT COUNT(e.id) AS count FROM `_entry` e';
  875. if ($minPriority !== null) {
  876. $sql .= ' INNER JOIN `_feed` f ON e.id_feed=f.id';
  877. $sql .= ' WHERE f.priority > ' . intval($minPriority);
  878. }
  879. $stm = $this->pdo->query($sql);
  880. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  881. return isset($res[0]) ? $res[0] : 0;
  882. }
  883. public function countNotRead($minPriority = null) {
  884. $sql = 'SELECT COUNT(e.id) AS count FROM `_entry` e';
  885. if ($minPriority !== null) {
  886. $sql .= ' INNER JOIN `_feed` f ON e.id_feed=f.id';
  887. }
  888. $sql .= ' WHERE e.is_read=0';
  889. if ($minPriority !== null) {
  890. $sql .= ' AND f.priority > ' . intval($minPriority);
  891. }
  892. $stm = $this->pdo->query($sql);
  893. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  894. return $res[0];
  895. }
  896. public function countUnreadReadFavorites() {
  897. $sql = <<<'SQL'
  898. SELECT c FROM (
  899. SELECT COUNT(e1.id) AS c, 1 AS o
  900. FROM `_entry` AS e1
  901. JOIN `_feed` AS f1 ON e1.id_feed = f1.id
  902. WHERE e1.is_favorite = 1
  903. AND f1.priority >= :priority_normal1
  904. UNION
  905. SELECT COUNT(e2.id) AS c, 2 AS o
  906. FROM `_entry` AS e2
  907. JOIN `_feed` AS f2 ON e2.id_feed = f2.id
  908. WHERE e2.is_favorite = 1
  909. AND e2.is_read = 0
  910. AND f2.priority >= :priority_normal2
  911. ) u
  912. ORDER BY o
  913. SQL;
  914. $stm = $this->pdo->prepare($sql);
  915. //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
  916. $stm->bindValue(':priority_normal1', FreshRSS_Feed::PRIORITY_NORMAL, PDO::PARAM_INT);
  917. $stm->bindValue(':priority_normal2', FreshRSS_Feed::PRIORITY_NORMAL, PDO::PARAM_INT);
  918. $stm->execute();
  919. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  920. rsort($res);
  921. $all = empty($res[0]) ? 0 : $res[0];
  922. $unread = empty($res[1]) ? 0 : $res[1];
  923. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  924. }
  925. public static function daoToEntry($dao) {
  926. $entry = new FreshRSS_Entry(
  927. $dao['id_feed'],
  928. $dao['guid'],
  929. $dao['title'],
  930. $dao['author'],
  931. $dao['content'],
  932. $dao['link'],
  933. $dao['date'],
  934. $dao['is_read'],
  935. $dao['is_favorite'],
  936. isset($dao['tags']) ? $dao['tags'] : ''
  937. );
  938. if (isset($dao['id'])) {
  939. $entry->_id($dao['id']);
  940. }
  941. return $entry;
  942. }
  943. private static function daoToEntries($listDAO) {
  944. $list = array();
  945. if (!is_array($listDAO)) {
  946. $listDAO = array($listDAO);
  947. }
  948. foreach ($listDAO as $key => $dao) {
  949. $list[] = self::daoToEntry($dao);
  950. }
  951. unset($listDAO);
  952. return $list;
  953. }
  954. }