EntryDAOSQLite.php 6.6 KB

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