EntryDAOSQLite.php 9.6 KB

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