EntryDAOSQLite.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. /**
  31. * Toggle the read marker on one or more article.
  32. * Then the cache is updated.
  33. *
  34. * @todo change the way the query is build because it seems there is
  35. * unnecessary code in here. For instance, the part with the str_repeat.
  36. * @todo remove code duplication. It seems the code is basically the
  37. * same if it is an array or not.
  38. *
  39. * @param integer|array $ids
  40. * @param boolean $is_read
  41. * @return integer affected rows
  42. */
  43. public function markRead($ids, $is_read = true) {
  44. if (is_array($ids)) { //Many IDs at once (used by API)
  45. if (true) { //Speed heuristics //TODO: Not implemented yet for SQLite (so always call IDs one by one)
  46. $affected = 0;
  47. foreach ($ids as $id) {
  48. $affected += $this->markRead($id, $is_read);
  49. }
  50. return $affected;
  51. }
  52. } else {
  53. $this->bd->beginTransaction();
  54. $sql = 'UPDATE `' . $this->prefix . 'entry` SET is_read=? WHERE id=? AND is_read=?';
  55. $values = array($is_read ? 1 : 0, $ids, $is_read ? 0 : 1);
  56. $stm = $this->bd->prepare($sql);
  57. if (!($stm && $stm->execute($values))) {
  58. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  59. Minz_Log::error('SQL error markRead 1: ' . $info[2]);
  60. $this->bd->rollBack();
  61. return false;
  62. }
  63. $affected = $stm->rowCount();
  64. if ($affected > 0) {
  65. $sql = 'UPDATE `' . $this->prefix . 'feed` SET cache_nbUnreads=cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
  66. . 'WHERE id=(SELECT e.id_feed FROM `' . $this->prefix . 'entry` e WHERE e.id=?)';
  67. $values = array($ids);
  68. $stm = $this->bd->prepare($sql);
  69. if (!($stm && $stm->execute($values))) {
  70. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  71. Minz_Log::error('SQL error markRead 2: ' . $info[2]);
  72. $this->bd->rollBack();
  73. return false;
  74. }
  75. }
  76. $this->bd->commit();
  77. return $affected;
  78. }
  79. }
  80. /**
  81. * Mark all entries as read depending on parameters.
  82. * If $onlyFavorites is true, it is used when the user mark as read in
  83. * the favorite pseudo-category.
  84. * If $priorityMin is greater than 0, it is used when the user mark as
  85. * read in the main feed pseudo-category.
  86. * Then the cache is updated.
  87. *
  88. * If $idMax equals 0, a deprecated debug message is logged
  89. *
  90. * @todo refactor this method along with markReadCat and markReadFeed
  91. * since they are all doing the same thing. I think we need to build a
  92. * tool to generate the query instead of having queries all over the
  93. * place. It will be reused also for the filtering making every thing
  94. * separated.
  95. *
  96. * @param integer $idMax fail safe article ID
  97. * @param boolean $onlyFavorites
  98. * @param integer $priorityMin
  99. * @return integer affected rows
  100. */
  101. public function markReadEntries($idMax = 0, $onlyFavorites = false, $priorityMin = 0) {
  102. if ($idMax == 0) {
  103. $idMax = time() . '000000';
  104. Minz_Log::debug('Calling markReadEntries(0) is deprecated!');
  105. }
  106. $sql = 'UPDATE `' . $this->prefix . 'entry` SET is_read=1 WHERE is_read=0 AND id <= ?';
  107. if ($onlyFavorites) {
  108. $sql .= ' AND is_favorite=1';
  109. } elseif ($priorityMin >= 0) {
  110. $sql .= ' AND id_feed IN (SELECT f.id FROM `' . $this->prefix . 'feed` f WHERE f.priority > ' . intval($priorityMin) . ')';
  111. }
  112. $values = array($idMax);
  113. $stm = $this->bd->prepare($sql);
  114. if (!($stm && $stm->execute($values))) {
  115. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  116. Minz_Log::error('SQL error markReadEntries: ' . $info[2]);
  117. return false;
  118. }
  119. $affected = $stm->rowCount();
  120. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  121. return false;
  122. }
  123. return $affected;
  124. }
  125. /**
  126. * Mark all the articles in a category as read.
  127. * There is a fail safe to prevent to mark as read articles that are
  128. * loaded during the mark as read action. Then the cache is updated.
  129. *
  130. * If $idMax equals 0, a deprecated debug message is logged
  131. *
  132. * @param integer $id category ID
  133. * @param integer $idMax fail safe article ID
  134. * @return integer affected rows
  135. */
  136. public function markReadCat($id, $idMax = 0) {
  137. if ($idMax == 0) {
  138. $idMax = time() . '000000';
  139. Minz_Log::debug('Calling markReadCat(0) is deprecated!');
  140. }
  141. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  142. . 'SET is_read=1 '
  143. . 'WHERE is_read=0 AND id <= ? AND '
  144. . 'id_feed IN (SELECT f.id FROM `' . $this->prefix . 'feed` f WHERE f.category=?)';
  145. $values = array($idMax, $id);
  146. $stm = $this->bd->prepare($sql);
  147. if (!($stm && $stm->execute($values))) {
  148. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  149. Minz_Log::error('SQL error markReadCat: ' . $info[2]);
  150. return false;
  151. }
  152. $affected = $stm->rowCount();
  153. if (($affected > 0) && (!$this->updateCacheUnreads($id, false))) {
  154. return false;
  155. }
  156. return $affected;
  157. }
  158. public function optimizeTable() {
  159. //TODO: Search for an equivalent in SQLite
  160. }
  161. public function size($all = false) {
  162. return @filesize(join_path(DATA_PATH, 'users', $this->current_user, 'db.sqlite'));
  163. }
  164. }