EntryDAOSQLite.php 7.9 KB

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