EntryDAOSQLite.php 9.1 KB

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