EntryDAOSQLite.php 7.8 KB

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