EntryDAOSQLite.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #[\Override]
  25. public static function sqlRandom(): string {
  26. return 'RANDOM()';
  27. }
  28. #[\Override]
  29. protected static function sqlRegex(string $expression, string $regex, array &$values): string {
  30. $values[] = $regex;
  31. return "{$expression} REGEXP ?";
  32. }
  33. #[\Override]
  34. protected function registerSqlFunctions(string $sql): void {
  35. if (!str_contains($sql, ' REGEXP ')) {
  36. return;
  37. }
  38. // https://php.net/pdo.sqlitecreatefunction
  39. // https://www.sqlite.org/lang_expr.html#the_like_glob_regexp_match_and_extract_operators
  40. $this->pdo->sqliteCreateFunction('regexp',
  41. function (string $pattern, string $text): bool {
  42. return preg_match($pattern, $text) === 1;
  43. },
  44. 2
  45. );
  46. }
  47. /** @param array{0:string,1:int,2:string} $errorInfo */
  48. #[\Override]
  49. protected function autoUpdateDb(array $errorInfo): bool {
  50. if (($tableInfo = $this->pdo->query("PRAGMA table_info('entry')")) !== false) {
  51. $columns = $tableInfo->fetchAll(PDO::FETCH_COLUMN, 1) ?: [];
  52. foreach (['attributes'] as $column) {
  53. if (!in_array($column, $columns, true)) {
  54. return $this->addColumn($column);
  55. }
  56. }
  57. }
  58. return false;
  59. }
  60. #[\Override]
  61. public function commitNewEntries(): bool {
  62. $sql = <<<'SQL'
  63. DROP TABLE IF EXISTS `tmp`;
  64. CREATE TEMP TABLE `tmp` AS
  65. SELECT id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes
  66. FROM `_entrytmp`
  67. ORDER BY date, id;
  68. INSERT OR IGNORE INTO `_entry`
  69. (id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes)
  70. SELECT rowid + (SELECT MAX(id) - COUNT(*) FROM `tmp`) AS id,
  71. guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes
  72. FROM `tmp`
  73. ORDER BY date, id;
  74. DELETE FROM `_entrytmp` WHERE id <= (SELECT MAX(id) FROM `tmp`);
  75. DROP TABLE IF EXISTS `tmp`;
  76. SQL;
  77. $hadTransaction = $this->pdo->inTransaction();
  78. if (!$hadTransaction) {
  79. $this->pdo->beginTransaction();
  80. }
  81. $result = $this->pdo->exec($sql) !== false;
  82. if (!$result) {
  83. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($this->pdo->errorInfo()));
  84. }
  85. if (!$hadTransaction) {
  86. $this->pdo->commit();
  87. }
  88. return $result;
  89. }
  90. /**
  91. * Toggle the read marker on one or more article.
  92. * Then the cache is updated.
  93. *
  94. * @param numeric-string|array<numeric-string> $ids
  95. * @return int|false affected rows
  96. */
  97. #[\Override]
  98. public function markRead(array|string $ids, bool $is_read = true): int|false {
  99. if (is_array($ids)) { //Many IDs at once (used by API)
  100. //if (true) { //Speed heuristics //TODO: Not implemented yet for SQLite (so always call IDs one by one)
  101. $affected = 0;
  102. foreach ($ids as $id) {
  103. $affected += ($this->markRead($id, $is_read) ?: 0);
  104. }
  105. return $affected;
  106. //}
  107. } else {
  108. FreshRSS_UserDAO::touch();
  109. $this->pdo->beginTransaction();
  110. $sql = 'UPDATE `_entry` SET is_read=? WHERE id=? AND is_read=?';
  111. $values = [$is_read ? 1 : 0, $ids, $is_read ? 0 : 1];
  112. $stm = $this->pdo->prepare($sql);
  113. if ($stm === false || !$stm->execute($values)) {
  114. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  115. Minz_Log::error('SQL error ' . __METHOD__ . ' A ' . json_encode($info));
  116. $this->pdo->rollBack();
  117. return false;
  118. }
  119. $affected = $stm->rowCount();
  120. if ($affected > 0) {
  121. $sql = 'UPDATE `_feed` SET `cache_nbUnreads`=`cache_nbUnreads`' . ($is_read ? '-' : '+') . '1 '
  122. . 'WHERE id=(SELECT e.id_feed FROM `_entry` e WHERE e.id=?)';
  123. $values = [$ids];
  124. $stm = $this->pdo->prepare($sql);
  125. if ($stm === false || !$stm->execute($values)) {
  126. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  127. Minz_Log::error('SQL error ' . __METHOD__ . ' B ' . json_encode($info));
  128. $this->pdo->rollBack();
  129. return false;
  130. }
  131. }
  132. $this->pdo->commit();
  133. return $affected;
  134. }
  135. }
  136. /**
  137. * Mark all the articles in a tag as read.
  138. * @param int $id tag ID, or empty for targeting any tag
  139. * @param string $idMax max article ID
  140. * @return int|false affected rows
  141. */
  142. #[\Override]
  143. public function markReadTag($id = 0, string $idMax = '0', ?FreshRSS_BooleanSearch $filters = null, int $state = 0, bool $is_read = true): int|false {
  144. FreshRSS_UserDAO::touch();
  145. if ($idMax == 0) {
  146. $idMax = time() . '000000';
  147. Minz_Log::debug('Calling markReadTag(0) is deprecated!');
  148. }
  149. $sql = 'UPDATE `_entry` SET is_read = ? WHERE is_read <> ? AND id <= ? AND '
  150. . 'id IN (SELECT et.id_entry FROM `_entrytag` et '
  151. . ($id == 0 ? '' : 'WHERE et.id_tag = ?')
  152. . ')';
  153. $values = [$is_read ? 1 : 0, $is_read ? 1 : 0, $idMax];
  154. if ($id != 0) {
  155. $values[] = $id;
  156. }
  157. [$searchValues, $search] = $this->sqlListEntriesWhere(alias: 'e.', state: $state, filters: $filters);
  158. $stm = $this->pdo->prepare($sql . $search);
  159. if ($stm === false || !$stm->execute(array_merge($values, $searchValues))) {
  160. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  161. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  162. return false;
  163. }
  164. $affected = $stm->rowCount();
  165. if (($affected > 0) && (!$this->updateCacheUnreads(null, null))) {
  166. return false;
  167. }
  168. return $affected;
  169. }
  170. }