EntryDAOSQLite.php 8.9 KB

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