EntryDAOSQLite.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. public static function sqlHexDecode(string $x): string {
  10. return $x;
  11. }
  12. public static function sqlIgnoreConflict(string $sql): string {
  13. return str_replace('INSERT INTO ', 'INSERT OR IGNORE INTO ', $sql);
  14. }
  15. protected function autoUpdateDb(array $errorInfo) {
  16. if ($tableInfo = $this->pdo->query("PRAGMA table_info('entry')")) {
  17. $columns = $tableInfo->fetchAll(PDO::FETCH_COLUMN, 1);
  18. foreach (['attributes'] as $column) {
  19. if (!in_array($column, $columns)) {
  20. return $this->addColumn($column);
  21. }
  22. }
  23. }
  24. if ($tableInfo = $this->pdo->query("SELECT sql FROM sqlite_master where name='tag'")) {
  25. $showCreate = $tableInfo->fetchColumn();
  26. if (stripos($showCreate, 'tag') === false) {
  27. $tagDAO = FreshRSS_Factory::createTagDao();
  28. return $tagDAO->createTagTable(); //v1.12.0
  29. }
  30. }
  31. if ($tableInfo = $this->pdo->query("SELECT sql FROM sqlite_master where name='entrytmp'")) {
  32. $showCreate = $tableInfo->fetchColumn();
  33. if (stripos($showCreate, 'entrytmp') === false) {
  34. return $this->createEntryTempTable(); //v1.7.0
  35. }
  36. }
  37. return false;
  38. }
  39. public function commitNewEntries() {
  40. $sql = '
  41. DROP TABLE IF EXISTS `tmp`;
  42. CREATE TEMP TABLE `tmp` AS
  43. SELECT id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes
  44. FROM `_entrytmp`
  45. ORDER BY date, id;
  46. INSERT OR IGNORE INTO `_entry`
  47. (id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes)
  48. SELECT rowid + (SELECT MAX(id) - COUNT(*) FROM `tmp`) AS id,
  49. guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes
  50. FROM `tmp`
  51. ORDER BY date, id;
  52. DELETE FROM `_entrytmp` WHERE id <= (SELECT MAX(id) FROM `tmp`);
  53. DROP TABLE IF EXISTS `tmp`;
  54. ';
  55. $hadTransaction = $this->pdo->inTransaction();
  56. if (!$hadTransaction) {
  57. $this->pdo->beginTransaction();
  58. }
  59. $result = $this->pdo->exec($sql) !== false;
  60. if (!$result) {
  61. Minz_Log::error('SQL error commitNewEntries: ' . json_encode($this->pdo->errorInfo()));
  62. }
  63. if (!$hadTransaction) {
  64. $this->pdo->commit();
  65. }
  66. return $result;
  67. }
  68. protected function updateCacheUnreads($catId = false, $feedId = false) {
  69. $sql = 'UPDATE `_feed` '
  70. . 'SET `cache_nbUnreads`=('
  71. . 'SELECT COUNT(*) AS nbUnreads FROM `_entry` e '
  72. . 'WHERE e.id_feed=`_feed`.id AND e.is_read=0)';
  73. $hasWhere = false;
  74. $values = array();
  75. if ($feedId !== false) {
  76. $sql .= ' WHERE';
  77. $hasWhere = true;
  78. $sql .= ' id=?';
  79. $values[] = $feedId;
  80. }
  81. if ($catId !== false) {
  82. $sql .= $hasWhere ? ' AND' : ' WHERE';
  83. $hasWhere = true;
  84. $sql .= ' category=?';
  85. $values[] = $catId;
  86. }
  87. $stm = $this->pdo->prepare($sql);
  88. if ($stm && $stm->execute($values)) {
  89. return true;
  90. } else {
  91. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  92. Minz_Log::error('SQL error updateCacheUnreads: ' . $info[2]);
  93. return false;
  94. }
  95. }
  96. /**
  97. * Toggle the read marker on one or more article.
  98. * Then the cache is updated.
  99. *
  100. * @todo change the way the query is build because it seems there is
  101. * unnecessary code in here. For instance, the part with the str_repeat.
  102. * @todo remove code duplication. It seems the code is basically the
  103. * same if it is an array or not.
  104. *
  105. * @param integer|array $ids
  106. * @param boolean $is_read
  107. * @return integer|false affected rows
  108. */
  109. public function markRead($ids, $is_read = true) {
  110. FreshRSS_UserDAO::touch();
  111. if (is_array($ids)) { //Many IDs at once (used by API)
  112. //if (true) { //Speed heuristics //TODO: Not implemented yet for SQLite (so always call IDs one by one)
  113. $affected = 0;
  114. foreach ($ids as $id) {
  115. $affected += $this->markRead($id, $is_read);
  116. }
  117. return $affected;
  118. //}
  119. } else {
  120. $this->pdo->beginTransaction();
  121. $sql = 'UPDATE `_entry` SET is_read=? WHERE id=? AND is_read=?';
  122. $values = array($is_read ? 1 : 0, $ids, $is_read ? 0 : 1);
  123. $stm = $this->pdo->prepare($sql);
  124. if (!($stm && $stm->execute($values))) {
  125. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  126. Minz_Log::error('SQL error markRead 1: ' . $info[2]);
  127. $this->pdo->rollBack();
  128. return false;
  129. }
  130. $affected = $stm->rowCount();
  131. if ($affected > 0) {
  132. $sql = 'UPDATE `_feed` SET `cache_nbUnreads`=`cache_nbUnreads`' . ($is_read ? '-' : '+') . '1 '
  133. . 'WHERE id=(SELECT e.id_feed FROM `_entry` e WHERE e.id=?)';
  134. $values = array($ids);
  135. $stm = $this->pdo->prepare($sql);
  136. if (!($stm && $stm->execute($values))) {
  137. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  138. Minz_Log::error('SQL error markRead 2: ' . $info[2]);
  139. $this->pdo->rollBack();
  140. return false;
  141. }
  142. }
  143. $this->pdo->commit();
  144. return $affected;
  145. }
  146. }
  147. /**
  148. * Mark all entries as read depending on parameters.
  149. * If $onlyFavorites is true, it is used when the user mark as read in
  150. * the favorite pseudo-category.
  151. * If $priorityMin is greater than 0, it is used when the user mark as
  152. * read in the main feed pseudo-category.
  153. * Then the cache is updated.
  154. *
  155. * If $idMax equals 0, a deprecated debug message is logged
  156. *
  157. * @todo refactor this method along with markReadCat and markReadFeed
  158. * since they are all doing the same thing. I think we need to build a
  159. * tool to generate the query instead of having queries all over the
  160. * place. It will be reused also for the filtering making every thing
  161. * separated.
  162. *
  163. * @param string $idMax fail safe article ID
  164. * @param boolean $onlyFavorites
  165. * @param integer $priorityMin
  166. * @param FreshRSS_BooleanSearch|null $filters
  167. * @return integer|false affected rows
  168. */
  169. public function markReadEntries(string $idMax = '0', bool $onlyFavorites = false, int $priorityMin = 0, $filters = null, int $state = 0, bool $is_read = true) {
  170. FreshRSS_UserDAO::touch();
  171. if ($idMax == '0') {
  172. $idMax = time() . '000000';
  173. Minz_Log::debug('Calling markReadEntries(0) is deprecated!');
  174. }
  175. $sql = 'UPDATE `_entry` SET is_read = ? WHERE is_read <> ? AND id <= ?';
  176. if ($onlyFavorites) {
  177. $sql .= ' AND is_favorite=1';
  178. } elseif ($priorityMin >= 0) {
  179. $sql .= ' AND id_feed IN (SELECT f.id FROM `_feed` f WHERE f.priority > ' . intval($priorityMin) . ')';
  180. }
  181. $values = array($is_read ? 1 : 0, $is_read ? 1 : 0, $idMax);
  182. list($searchValues, $search) = $this->sqlListEntriesWhere('', $filters, $state);
  183. $stm = $this->pdo->prepare($sql . $search);
  184. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  185. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  186. Minz_Log::error('SQL error markReadEntries: ' . $info[2]);
  187. return false;
  188. }
  189. $affected = $stm->rowCount();
  190. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  191. return false;
  192. }
  193. return $affected;
  194. }
  195. /**
  196. * Mark all the articles in a category as read.
  197. * There is a fail safe to prevent to mark as read articles that are
  198. * loaded during the mark as read action. Then the cache is updated.
  199. *
  200. * If $idMax equals 0, a deprecated debug message is logged
  201. *
  202. * @param integer $id category ID
  203. * @param string $idMax fail safe article ID
  204. * @param FreshRSS_BooleanSearch|null $filters
  205. * @return integer|false affected rows
  206. */
  207. public function markReadCat(int $id, string $idMax = '0', $filters = null, int $state = 0, bool $is_read = true) {
  208. FreshRSS_UserDAO::touch();
  209. if ($idMax == '0') {
  210. $idMax = time() . '000000';
  211. Minz_Log::debug('Calling markReadCat(0) is deprecated!');
  212. }
  213. $sql = 'UPDATE `_entry` '
  214. . 'SET is_read = ? '
  215. . 'WHERE is_read <> ? AND id <= ? AND '
  216. . 'id_feed IN (SELECT f.id FROM `_feed` f WHERE f.category=?)';
  217. $values = array($is_read ? 1 : 0, $is_read ? 1 : 0, $idMax, $id);
  218. list($searchValues, $search) = $this->sqlListEntriesWhere('', $filters, $state);
  219. $stm = $this->pdo->prepare($sql . $search);
  220. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  221. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  222. Minz_Log::error('SQL error markReadCat: ' . $info[2]);
  223. return false;
  224. }
  225. $affected = $stm->rowCount();
  226. if (($affected > 0) && (!$this->updateCacheUnreads($id, false))) {
  227. return false;
  228. }
  229. return $affected;
  230. }
  231. /**
  232. * Mark all the articles in a tag as read.
  233. * @param integer $id tag ID, or empty for targeting any tag
  234. * @param string $idMax max article ID
  235. * @return integer|false affected rows
  236. */
  237. public function markReadTag($id = 0, string $idMax = '0', $filters = null, int $state = 0, bool $is_read = true) {
  238. FreshRSS_UserDAO::touch();
  239. if ($idMax == 0) {
  240. $idMax = time() . '000000';
  241. Minz_Log::debug('Calling markReadTag(0) is deprecated!');
  242. }
  243. $sql = 'UPDATE `_entry` '
  244. . 'SET is_read = ? '
  245. . 'WHERE is_read <> ? AND id <= ? AND '
  246. . 'id IN (SELECT et.id_entry FROM `_entrytag` et '
  247. . ($id == 0 ? '' : 'WHERE et.id_tag = ?')
  248. . ')';
  249. $values = array($is_read ? 1 : 0, $is_read ? 1 : 0, $idMax);
  250. if ($id != 0) {
  251. $values[] = $id;
  252. }
  253. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state);
  254. $stm = $this->pdo->prepare($sql . $search);
  255. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  256. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  257. Minz_Log::error('SQL error markReadTag: ' . $info[2]);
  258. return false;
  259. }
  260. $affected = $stm->rowCount();
  261. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  262. return false;
  263. }
  264. return $affected;
  265. }
  266. }