EntryDAOSQLite.php 9.3 KB

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