EntryDAOSQLite.php 9.4 KB

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