EntryDAOSQLite.php 6.8 KB

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