EntryDAOSQLite.php 4.8 KB

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