4
0

EntryDAOSQLite.php 5.6 KB

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