4
0

EntryDAOSQLite.php 5.7 KB

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