EntryDAOSQLite.php 9.1 KB

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