EntryDAOSQLite.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. class FreshRSS_EntryDAOSQLite extends FreshRSS_EntryDAO {
  3. protected function sqlConcat($s1, $s2) {
  4. return $s1 . '||' . $s2;
  5. }
  6. protected function updateCacheUnreads($catId = false, $feedId = false) {
  7. $sql = 'UPDATE `' . $this->prefix . 'feed` '
  8. . 'SET cache_nbUnreads=('
  9. . 'SELECT COUNT(*) AS nbUnreads FROM `' . $this->prefix . 'entry` e '
  10. . 'WHERE e.id_feed=`' . $this->prefix . 'feed`.id AND e.is_read=0) '
  11. . 'WHERE 1';
  12. $values = array();
  13. if ($feedId !== false) {
  14. $sql .= ' AND id=?';
  15. $values[] = $feedId;
  16. }
  17. if ($catId !== false) {
  18. $sql .= ' AND category=?';
  19. $values[] = $catId;
  20. }
  21. $stm = $this->bd->prepare($sql);
  22. if ($stm && $stm->execute($values)) {
  23. return true;
  24. } else {
  25. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  26. Minz_Log::error('SQL error updateCacheUnreads: ' . $info[2]);
  27. return false;
  28. }
  29. }
  30. public function markRead($ids, $is_read = true) {
  31. if (is_array($ids)) { //Many IDs at once (used by API)
  32. if (true) { //Speed heuristics //TODO: Not implemented yet for SQLite (so always call IDs one by one)
  33. $affected = 0;
  34. foreach ($ids as $id) {
  35. $affected += $this->markRead($id, $is_read);
  36. }
  37. return $affected;
  38. }
  39. } else {
  40. $this->bd->beginTransaction();
  41. $sql = 'UPDATE `' . $this->prefix . 'entry` SET is_read=? WHERE id=? AND is_read=?';
  42. $values = array($is_read ? 1 : 0, $ids, $is_read ? 0 : 1);
  43. $stm = $this->bd->prepare($sql);
  44. if (!($stm && $stm->execute($values))) {
  45. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  46. Minz_Log::error('SQL error markRead 1: ' . $info[2]);
  47. $this->bd->rollBack();
  48. return false;
  49. }
  50. $affected = $stm->rowCount();
  51. if ($affected > 0) {
  52. $sql = 'UPDATE `' . $this->prefix . 'feed` SET cache_nbUnreads=cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
  53. . 'WHERE id=(SELECT e.id_feed FROM `' . $this->prefix . 'entry` e WHERE e.id=?)';
  54. $values = array($ids);
  55. $stm = $this->bd->prepare($sql);
  56. if (!($stm && $stm->execute($values))) {
  57. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  58. Minz_Log::error('SQL error markRead 2: ' . $info[2]);
  59. $this->bd->rollBack();
  60. return false;
  61. }
  62. }
  63. $this->bd->commit();
  64. return $affected;
  65. }
  66. }
  67. public function markReadEntries($idMax = 0, $onlyFavorites = false, $priorityMin = 0) {
  68. if ($idMax == 0) {
  69. $idMax = time() . '000000';
  70. Minz_Log::debug('Calling markReadEntries(0) is deprecated!');
  71. }
  72. $sql = 'UPDATE `' . $this->prefix . 'entry` SET is_read=1 WHERE is_read=0 AND id <= ?';
  73. if ($onlyFavorites) {
  74. $sql .= ' AND is_favorite=1';
  75. } elseif ($priorityMin >= 0) {
  76. $sql .= ' AND id_feed IN (SELECT f.id FROM `' . $this->prefix . 'feed` f WHERE f.priority > ' . intval($priorityMin) . ')';
  77. }
  78. $values = array($idMax);
  79. $stm = $this->bd->prepare($sql);
  80. if (!($stm && $stm->execute($values))) {
  81. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  82. Minz_Log::error('SQL error markReadEntries: ' . $info[2]);
  83. return false;
  84. }
  85. $affected = $stm->rowCount();
  86. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  87. return false;
  88. }
  89. return $affected;
  90. }
  91. public function markReadCat($id, $idMax = 0) {
  92. if ($idMax == 0) {
  93. $idMax = time() . '000000';
  94. Minz_Log::debug('Calling markReadCat(0) is deprecated!');
  95. }
  96. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  97. . 'SET is_read=1 '
  98. . 'WHERE is_read=0 AND id <= ? AND '
  99. . 'id_feed IN (SELECT f.id FROM `' . $this->prefix . 'feed` f WHERE f.category=?)';
  100. $values = array($idMax, $id);
  101. $stm = $this->bd->prepare($sql);
  102. if (!($stm && $stm->execute($values))) {
  103. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  104. Minz_Log::error('SQL error markReadCat: ' . $info[2]);
  105. return false;
  106. }
  107. $affected = $stm->rowCount();
  108. if (($affected > 0) && (!$this->updateCacheUnreads($id, false))) {
  109. return false;
  110. }
  111. return $affected;
  112. }
  113. public function optimizeTable() {
  114. //TODO: Search for an equivalent in SQLite
  115. }
  116. public function size($all = false) {
  117. return @filesize(DATA_PATH . '/' . $this->current_user . '.sqlite');
  118. }
  119. }