CategoryDAO.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
  3. const DEFAULTCATEGORYID = 1;
  4. public function addCategory($valuesTmp) {
  5. $sql = 'INSERT INTO `_category`(name) '
  6. . 'SELECT * FROM (SELECT TRIM(?)) c2 ' //TRIM() to provide a type hint as text for PostgreSQL
  7. . 'WHERE NOT EXISTS (SELECT 1 FROM `_tag` WHERE name = TRIM(?))'; //No tag of the same name
  8. $stm = $this->pdo->prepare($sql);
  9. $valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8');
  10. $values = array(
  11. $valuesTmp['name'],
  12. $valuesTmp['name'],
  13. );
  14. if ($stm && $stm->execute($values)) {
  15. return $this->pdo->lastInsertId('`_category_id_seq`');
  16. } else {
  17. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  18. Minz_Log::error('SQL error addCategory: ' . $info[2]);
  19. return false;
  20. }
  21. }
  22. public function addCategoryObject($category) {
  23. $cat = $this->searchByName($category->name());
  24. if (!$cat) {
  25. // Category does not exist yet in DB so we add it before continue
  26. $values = array(
  27. 'name' => $category->name(),
  28. );
  29. return $this->addCategory($values);
  30. }
  31. return $cat->id();
  32. }
  33. public function updateCategory($id, $valuesTmp) {
  34. $sql = 'UPDATE `_category` SET name=? WHERE id=? '
  35. . 'AND NOT EXISTS (SELECT 1 FROM `_tag` WHERE name = ?)'; //No tag of the same name
  36. $stm = $this->pdo->prepare($sql);
  37. $valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8');
  38. $values = array(
  39. $valuesTmp['name'],
  40. $id,
  41. $valuesTmp['name'],
  42. );
  43. if ($stm && $stm->execute($values)) {
  44. return $stm->rowCount();
  45. } else {
  46. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  47. Minz_Log::error('SQL error updateCategory: ' . $info[2]);
  48. return false;
  49. }
  50. }
  51. public function deleteCategory($id) {
  52. if ($id <= self::DEFAULTCATEGORYID) {
  53. return false;
  54. }
  55. $sql = 'DELETE FROM `_category` WHERE id=:id';
  56. $stm = $this->pdo->prepare($sql);
  57. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  58. if ($stm && $stm->execute()) {
  59. return $stm->rowCount();
  60. } else {
  61. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  62. Minz_Log::error('SQL error deleteCategory: ' . $info[2]);
  63. return false;
  64. }
  65. }
  66. public function selectAll() {
  67. $sql = 'SELECT id, name FROM `_category`';
  68. $stm = $this->pdo->query($sql);
  69. while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
  70. yield $row;
  71. }
  72. }
  73. public function searchById($id) {
  74. $sql = 'SELECT * FROM `_category` WHERE id=:id';
  75. $stm = $this->pdo->prepare($sql);
  76. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  77. $stm->execute();
  78. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  79. $cat = self::daoToCategory($res);
  80. if (isset($cat[0])) {
  81. return $cat[0];
  82. } else {
  83. return null;
  84. }
  85. }
  86. public function searchByName($name) {
  87. $sql = 'SELECT * FROM `_category` WHERE name=:name';
  88. $stm = $this->pdo->prepare($sql);
  89. if ($stm == false) {
  90. return false;
  91. }
  92. $stm->bindParam(':name', $name);
  93. $stm->execute();
  94. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  95. $cat = self::daoToCategory($res);
  96. if (isset($cat[0])) {
  97. return $cat[0];
  98. } else {
  99. return null;
  100. }
  101. }
  102. public function listCategories($prePopulateFeeds = true, $details = false) {
  103. if ($prePopulateFeeds) {
  104. $sql = 'SELECT c.id AS c_id, c.name AS c_name, '
  105. . ($details ? 'f.* ' : 'f.id, f.name, f.url, f.website, f.priority, f.error, f.`cache_nbEntries`, f.`cache_nbUnreads`, f.ttl ')
  106. . 'FROM `_category` c '
  107. . 'LEFT OUTER JOIN `_feed` f ON f.category=c.id '
  108. . 'WHERE f.priority >= :priority_normal '
  109. . 'GROUP BY f.id, c_id '
  110. . 'ORDER BY c.name, f.name';
  111. $stm = $this->pdo->prepare($sql);
  112. $stm->bindValue(':priority_normal', FreshRSS_Feed::PRIORITY_NORMAL, PDO::PARAM_INT);
  113. $stm->execute();
  114. return self::daoToCategoryPrepopulated($stm->fetchAll(PDO::FETCH_ASSOC));
  115. } else {
  116. $sql = 'SELECT * FROM `_category` ORDER BY name';
  117. $stm = $this->pdo->query($sql);
  118. return self::daoToCategory($stm->fetchAll(PDO::FETCH_ASSOC));
  119. }
  120. }
  121. public function getDefault() {
  122. $sql = 'SELECT * FROM `_category` WHERE id=:id';
  123. $stm = $this->pdo->prepare($sql);
  124. $stm->bindValue(':id', self::DEFAULTCATEGORYID, PDO::PARAM_INT);
  125. $stm->execute();
  126. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  127. $cat = self::daoToCategory($res);
  128. if (isset($cat[0])) {
  129. return $cat[0];
  130. } else {
  131. if (FreshRSS_Context::$isCli) {
  132. fwrite(STDERR, 'FreshRSS database error: Default category not found!' . "\n");
  133. }
  134. Minz_Log::error('FreshRSS database error: Default category not found!');
  135. return null;
  136. }
  137. }
  138. public function checkDefault() {
  139. $def_cat = $this->searchById(self::DEFAULTCATEGORYID);
  140. if ($def_cat == null) {
  141. $cat = new FreshRSS_Category(_t('gen.short.default_category'));
  142. $cat->_id(self::DEFAULTCATEGORYID);
  143. $sql = 'INSERT INTO `_category`(id, name) VALUES(?, ?)';
  144. if ($this->pdo->dbType() === 'pgsql') {
  145. //Force call to nextval()
  146. $sql .= " RETURNING nextval('`_category_id_seq`');";
  147. }
  148. $stm = $this->pdo->prepare($sql);
  149. $values = array(
  150. $cat->id(),
  151. $cat->name(),
  152. );
  153. if ($stm && $stm->execute($values)) {
  154. return $this->pdo->lastInsertId('`_category_id_seq`');
  155. } else {
  156. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  157. Minz_Log::error('SQL error check default category: ' . json_encode($info));
  158. return false;
  159. }
  160. }
  161. return true;
  162. }
  163. public function count() {
  164. $sql = 'SELECT COUNT(*) AS count FROM `_category`';
  165. $stm = $this->pdo->query($sql);
  166. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  167. return $res[0]['count'];
  168. }
  169. public function countFeed($id) {
  170. $sql = 'SELECT COUNT(*) AS count FROM `_feed` WHERE category=:id';
  171. $stm = $this->pdo->prepare($sql);
  172. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  173. $stm->execute();
  174. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  175. return $res[0]['count'];
  176. }
  177. public function countNotRead($id) {
  178. $sql = 'SELECT COUNT(*) AS count FROM `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id WHERE category=:id AND e.is_read=0';
  179. $stm = $this->pdo->prepare($sql);
  180. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  181. $stm->execute();
  182. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  183. return $res[0]['count'];
  184. }
  185. public static function findFeed($categories, $feed_id) {
  186. foreach ($categories as $category) {
  187. foreach ($category->feeds() as $feed) {
  188. if ($feed->id() === $feed_id) {
  189. return $feed;
  190. }
  191. }
  192. }
  193. return null;
  194. }
  195. public static function CountUnreads($categories, $minPriority = 0) {
  196. $n = 0;
  197. foreach ($categories as $category) {
  198. foreach ($category->feeds() as $feed) {
  199. if ($feed->priority() >= $minPriority) {
  200. $n += $feed->nbNotRead();
  201. }
  202. }
  203. }
  204. return $n;
  205. }
  206. public static function daoToCategoryPrepopulated($listDAO) {
  207. $list = array();
  208. if (!is_array($listDAO)) {
  209. $listDAO = array($listDAO);
  210. }
  211. $previousLine = null;
  212. $feedsDao = array();
  213. $feedDao = FreshRSS_Factory::createFeedDAO();
  214. foreach ($listDAO as $line) {
  215. if ($previousLine['c_id'] != null && $line['c_id'] !== $previousLine['c_id']) {
  216. // End of the current category, we add it to the $list
  217. $cat = new FreshRSS_Category(
  218. $previousLine['c_name'],
  219. $feedDao->daoToFeed($feedsDao, $previousLine['c_id'])
  220. );
  221. $cat->_id($previousLine['c_id']);
  222. $list[$previousLine['c_id']] = $cat;
  223. $feedsDao = array(); //Prepare for next category
  224. }
  225. $previousLine = $line;
  226. $feedsDao[] = $line;
  227. }
  228. // add the last category
  229. if ($previousLine != null) {
  230. $cat = new FreshRSS_Category(
  231. $previousLine['c_name'],
  232. $feedDao->daoToFeed($feedsDao, $previousLine['c_id'])
  233. );
  234. $cat->_id($previousLine['c_id']);
  235. $list[$previousLine['c_id']] = $cat;
  236. }
  237. return $list;
  238. }
  239. public static function daoToCategory($listDAO) {
  240. $list = array();
  241. if (!is_array($listDAO)) {
  242. $listDAO = array($listDAO);
  243. }
  244. foreach ($listDAO as $key => $dao) {
  245. $cat = new FreshRSS_Category(
  246. $dao['name']
  247. );
  248. $cat->_id($dao['id']);
  249. $cat->_isDefault(static::DEFAULTCATEGORYID === intval($dao['id']));
  250. $list[$key] = $cat;
  251. }
  252. return $list;
  253. }
  254. }