EntryDAOSQLite.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_EntryDAOSQLite extends FreshRSS_EntryDAO {
  4. #[\Override]
  5. public static function isCompressed(): bool {
  6. return false;
  7. }
  8. #[\Override]
  9. public static function hasNativeHex(): bool {
  10. return false;
  11. }
  12. #[\Override]
  13. protected static function sqlConcat(string $s1, string $s2): string {
  14. return $s1 . '||' . $s2;
  15. }
  16. #[\Override]
  17. public static function sqlHexDecode(string $x): string {
  18. return $x;
  19. }
  20. #[\Override]
  21. public static function sqlIgnoreConflict(string $sql): string {
  22. return str_replace('INSERT INTO ', 'INSERT OR IGNORE INTO ', $sql);
  23. }
  24. /** @param array<string|int> $errorInfo */
  25. #[\Override]
  26. protected function autoUpdateDb(array $errorInfo): bool {
  27. if ($tableInfo = $this->pdo->query("PRAGMA table_info('entry')")) {
  28. $columns = $tableInfo->fetchAll(PDO::FETCH_COLUMN, 1) ?: [];
  29. foreach (['attributes'] as $column) {
  30. if (!in_array($column, $columns, true)) {
  31. return $this->addColumn($column);
  32. }
  33. }
  34. }
  35. return false;
  36. }
  37. #[\Override]
  38. public function commitNewEntries(): bool {
  39. $sql = <<<'SQL'
  40. DROP TABLE IF EXISTS `tmp`;
  41. CREATE TEMP TABLE `tmp` AS
  42. SELECT id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes
  43. FROM `_entrytmp`
  44. ORDER BY date, id;
  45. INSERT OR IGNORE INTO `_entry`
  46. (id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes)
  47. SELECT rowid + (SELECT MAX(id) - COUNT(*) FROM `tmp`) AS id,
  48. guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes
  49. FROM `tmp`
  50. ORDER BY date, id;
  51. DELETE FROM `_entrytmp` WHERE id <= (SELECT MAX(id) FROM `tmp`);
  52. DROP TABLE IF EXISTS `tmp`;
  53. SQL;
  54. $hadTransaction = $this->pdo->inTransaction();
  55. if (!$hadTransaction) {
  56. $this->pdo->beginTransaction();
  57. }
  58. $result = $this->pdo->exec($sql) !== false;
  59. if (!$result) {
  60. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($this->pdo->errorInfo()));
  61. }
  62. if (!$hadTransaction) {
  63. $this->pdo->commit();
  64. }
  65. return $result;
  66. }
  67. /**
  68. * Toggle the read marker on one or more article.
  69. * Then the cache is updated.
  70. *
  71. * @param numeric-string|array<numeric-string> $ids
  72. * @return int|false affected rows
  73. */
  74. #[\Override]
  75. public function markRead(array|string $ids, bool $is_read = true): int|false {
  76. if (is_array($ids)) { //Many IDs at once (used by API)
  77. //if (true) { //Speed heuristics //TODO: Not implemented yet for SQLite (so always call IDs one by one)
  78. $affected = 0;
  79. foreach ($ids as $id) {
  80. $affected += ($this->markRead($id, $is_read) ?: 0);
  81. }
  82. return $affected;
  83. //}
  84. } else {
  85. FreshRSS_UserDAO::touch();
  86. $this->pdo->beginTransaction();
  87. $sql = 'UPDATE `_entry` SET is_read=? WHERE id=? AND is_read=?';
  88. $values = [$is_read ? 1 : 0, $ids, $is_read ? 0 : 1];
  89. $stm = $this->pdo->prepare($sql);
  90. if (!($stm && $stm->execute($values))) {
  91. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  92. Minz_Log::error('SQL error ' . __METHOD__ . ' A ' . json_encode($info));
  93. $this->pdo->rollBack();
  94. return false;
  95. }
  96. $affected = $stm->rowCount();
  97. if ($affected > 0) {
  98. $sql = 'UPDATE `_feed` SET `cache_nbUnreads`=`cache_nbUnreads`' . ($is_read ? '-' : '+') . '1 '
  99. . 'WHERE id=(SELECT e.id_feed FROM `_entry` e WHERE e.id=?)';
  100. $values = [$ids];
  101. $stm = $this->pdo->prepare($sql);
  102. if (!($stm && $stm->execute($values))) {
  103. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  104. Minz_Log::error('SQL error ' . __METHOD__ . ' B ' . json_encode($info));
  105. $this->pdo->rollBack();
  106. return false;
  107. }
  108. }
  109. $this->pdo->commit();
  110. return $affected;
  111. }
  112. }
  113. /**
  114. * Mark all the articles in a tag as read.
  115. * @param int $id tag ID, or empty for targeting any tag
  116. * @param string $idMax max article ID
  117. * @return int|false affected rows
  118. */
  119. #[\Override]
  120. public function markReadTag($id = 0, string $idMax = '0', ?FreshRSS_BooleanSearch $filters = null, int $state = 0, bool $is_read = true): int|false {
  121. FreshRSS_UserDAO::touch();
  122. if ($idMax == 0) {
  123. $idMax = time() . '000000';
  124. Minz_Log::debug('Calling markReadTag(0) is deprecated!');
  125. }
  126. $sql = 'UPDATE `_entry` SET is_read = ? WHERE is_read <> ? AND id <= ? AND '
  127. . 'id IN (SELECT et.id_entry FROM `_entrytag` et '
  128. . ($id == 0 ? '' : 'WHERE et.id_tag = ?')
  129. . ')';
  130. $values = [$is_read ? 1 : 0, $is_read ? 1 : 0, $idMax];
  131. if ($id != 0) {
  132. $values[] = $id;
  133. }
  134. [$searchValues, $search] = $this->sqlListEntriesWhere('e.', $filters, $state);
  135. $stm = $this->pdo->prepare($sql . $search);
  136. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  137. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  138. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  139. return false;
  140. }
  141. $affected = $stm->rowCount();
  142. if (($affected > 0) && (!$this->updateCacheUnreads(null, null))) {
  143. return false;
  144. }
  145. return $affected;
  146. }
  147. }