EntryDAOSQLite.php 4.8 KB

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