4
0

EntryDAOSQLite.php 7.7 KB

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