EntryDAOSQLite.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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->bd->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->bd->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. if ($tableInfo = $this->bd->query("SELECT sql FROM sqlite_master where name='entry'")) {
  27. $showCreate = $tableInfo->fetchColumn();
  28. foreach (array('lastSeen', 'hash') as $column) {
  29. if (stripos($showCreate, $column) === false) {
  30. return $this->addColumn($column);
  31. }
  32. }
  33. }
  34. return false;
  35. }
  36. public function commitNewEntries() {
  37. $sql = '
  38. DROP TABLE IF EXISTS `tmp`;
  39. CREATE TEMP TABLE `tmp` AS
  40. SELECT id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags
  41. FROM `' . $this->prefix . 'entrytmp`
  42. ORDER BY date;
  43. INSERT OR IGNORE INTO `' . $this->prefix . 'entry`
  44. (id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags)
  45. SELECT rowid + (SELECT MAX(id) - COUNT(*) FROM `tmp`) AS id,
  46. guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags
  47. FROM `tmp`
  48. ORDER BY date;
  49. DELETE FROM `' . $this->prefix . 'entrytmp` WHERE id <= (SELECT MAX(id) FROM `tmp`);
  50. DROP TABLE IF EXISTS `tmp`;
  51. ';
  52. $hadTransaction = $this->bd->inTransaction();
  53. if (!$hadTransaction) {
  54. $this->bd->beginTransaction();
  55. }
  56. $result = $this->bd->exec($sql) !== false;
  57. if (!$result) {
  58. Minz_Log::error('SQL error commitNewEntries: ' . json_encode($this->bd->errorInfo()));
  59. }
  60. if (!$hadTransaction) {
  61. $this->bd->commit();
  62. }
  63. return $result;
  64. }
  65. protected function sqlConcat($s1, $s2) {
  66. return $s1 . '||' . $s2;
  67. }
  68. protected function updateCacheUnreads($catId = false, $feedId = false) {
  69. $sql = 'UPDATE `' . $this->prefix . 'feed` '
  70. . 'SET `cache_nbUnreads`=('
  71. . 'SELECT COUNT(*) AS nbUnreads FROM `' . $this->prefix . 'entry` e '
  72. . 'WHERE e.id_feed=`' . $this->prefix . 'feed`.id AND e.is_read=0)';
  73. $hasWhere = false;
  74. $values = array();
  75. if ($feedId !== false) {
  76. $sql .= $hasWhere ? ' AND' : ' 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->bd->prepare($sql);
  88. if ($stm && $stm->execute($values)) {
  89. return true;
  90. } else {
  91. $info = $stm == null ? array(2 => 'syntax error') : $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 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->bd->beginTransaction();
  121. $sql = 'UPDATE `' . $this->prefix . 'entry` SET is_read=? WHERE id=? AND is_read=?';
  122. $values = array($is_read ? 1 : 0, $ids, $is_read ? 0 : 1);
  123. $stm = $this->bd->prepare($sql);
  124. if (!($stm && $stm->execute($values))) {
  125. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  126. Minz_Log::error('SQL error markRead 1: ' . $info[2]);
  127. $this->bd->rollBack();
  128. return false;
  129. }
  130. $affected = $stm->rowCount();
  131. if ($affected > 0) {
  132. $sql = 'UPDATE `' . $this->prefix . 'feed` SET `cache_nbUnreads`=`cache_nbUnreads`' . ($is_read ? '-' : '+') . '1 '
  133. . 'WHERE id=(SELECT e.id_feed FROM `' . $this->prefix . 'entry` e WHERE e.id=?)';
  134. $values = array($ids);
  135. $stm = $this->bd->prepare($sql);
  136. if (!($stm && $stm->execute($values))) {
  137. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  138. Minz_Log::error('SQL error markRead 2: ' . $info[2]);
  139. $this->bd->rollBack();
  140. return false;
  141. }
  142. }
  143. $this->bd->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 integer $idMax fail safe article ID
  164. * @param boolean $onlyFavorites
  165. * @param integer $priorityMin
  166. * @return integer affected rows
  167. */
  168. public function markReadEntries($idMax = 0, $onlyFavorites = false, $priorityMin = 0, $filters = null, $state = 0, $is_read = true) {
  169. FreshRSS_UserDAO::touch();
  170. if ($idMax == 0) {
  171. $idMax = time() . '000000';
  172. Minz_Log::debug('Calling markReadEntries(0) is deprecated!');
  173. }
  174. $sql = 'UPDATE `' . $this->prefix . 'entry` SET is_read = ? WHERE is_read <> ? AND id <= ?';
  175. if ($onlyFavorites) {
  176. $sql .= ' AND is_favorite=1';
  177. } elseif ($priorityMin >= 0) {
  178. $sql .= ' AND id_feed IN (SELECT f.id FROM `' . $this->prefix . 'feed` f WHERE f.priority > ' . intval($priorityMin) . ')';
  179. }
  180. $values = array($is_read ? 1 : 0, $is_read ? 1 : 0, $idMax);
  181. list($searchValues, $search) = $this->sqlListEntriesWhere('', $filters, $state);
  182. $stm = $this->bd->prepare($sql . $search);
  183. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  184. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  185. Minz_Log::error('SQL error markReadEntries: ' . $info[2]);
  186. return false;
  187. }
  188. $affected = $stm->rowCount();
  189. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  190. return false;
  191. }
  192. return $affected;
  193. }
  194. /**
  195. * Mark all the articles in a category as read.
  196. * There is a fail safe to prevent to mark as read articles that are
  197. * loaded during the mark as read action. Then the cache is updated.
  198. *
  199. * If $idMax equals 0, a deprecated debug message is logged
  200. *
  201. * @param integer $id category ID
  202. * @param integer $idMax fail safe article ID
  203. * @return integer affected rows
  204. */
  205. public function markReadCat($id, $idMax = 0, $filters = null, $state = 0, $is_read = true) {
  206. FreshRSS_UserDAO::touch();
  207. if ($idMax == 0) {
  208. $idMax = time() . '000000';
  209. Minz_Log::debug('Calling markReadCat(0) is deprecated!');
  210. }
  211. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  212. . 'SET is_read = ? '
  213. . 'WHERE is_read <> ? AND id <= ? AND '
  214. . 'id_feed IN (SELECT f.id FROM `' . $this->prefix . 'feed` f WHERE f.category=?)';
  215. $values = array($is_read ? 1 : 0, $is_read ? 1 : 0, $idMax, $id);
  216. list($searchValues, $search) = $this->sqlListEntriesWhere('', $filters, $state);
  217. $stm = $this->bd->prepare($sql . $search);
  218. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  219. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  220. Minz_Log::error('SQL error markReadCat: ' . $info[2]);
  221. return false;
  222. }
  223. $affected = $stm->rowCount();
  224. if (($affected > 0) && (!$this->updateCacheUnreads($id, false))) {
  225. return false;
  226. }
  227. return $affected;
  228. }
  229. /**
  230. * Mark all the articles in a tag as read.
  231. * @param integer $id tag ID, or empty for targetting any tag
  232. * @param integer $idMax max article ID
  233. * @return integer affected rows
  234. */
  235. public function markReadTag($id = '', $idMax = 0, $filters = null, $state = 0, $is_read = true) {
  236. FreshRSS_UserDAO::touch();
  237. if ($idMax == 0) {
  238. $idMax = time() . '000000';
  239. Minz_Log::debug('Calling markReadTag(0) is deprecated!');
  240. }
  241. $sql = 'UPDATE `' . $this->prefix . 'entry` e '
  242. . 'SET e.is_read = ? '
  243. . 'WHERE e.is_read <> ? AND e.id <= ? AND '
  244. . 'e.id IN (SELECT et.id_entry FROM `' . $this->prefix . 'entrytag` et '
  245. . ($id == '' ? '' : 'WHERE et.id = ?')
  246. . ')';
  247. $values = array($is_read ? 1 : 0, $is_read ? 1 : 0, $idMax);
  248. if ($id != '') {
  249. $values[] = $id;
  250. }
  251. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state);
  252. $stm = $this->bd->prepare($sql . $search);
  253. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  254. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  255. Minz_Log::error('SQL error markReadTag: ' . $info[2]);
  256. return false;
  257. }
  258. $affected = $stm->rowCount();
  259. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  260. return false;
  261. }
  262. return $affected;
  263. }
  264. }