EntryDAOSQLite.php 7.8 KB

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