EntryDAOSQLite.php 6.3 KB

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