EntryDAOSQLite.php 6.6 KB

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