EntryDAOSQLite.php 9.8 KB

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