CategoryDAO.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 `' . $this->prefix . '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 `' . $this->prefix . 'tag` WHERE name = TRIM(?))'; //No tag of the same name
  8. $stm = $this->bd->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->bd->lastInsertId('"' . $this->prefix . 'category_id_seq"');
  16. } else {
  17. $info = $stm == null ? array(2 => 'syntax error') : $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 `' . $this->prefix . 'category` SET name=? WHERE id=? '
  35. . 'AND NOT EXISTS (SELECT 1 FROM `' . $this->prefix . 'tag` WHERE name = ?)'; //No tag of the same name
  36. $stm = $this->bd->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 ? array(2 => 'syntax error') : $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 `' . $this->prefix . 'category` WHERE id=?';
  56. $stm = $this->bd->prepare($sql);
  57. $values = array($id);
  58. if ($stm && $stm->execute($values)) {
  59. return $stm->rowCount();
  60. } else {
  61. $info = $stm == null ? array(2 => 'syntax error') : $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 `' . $this->prefix . 'category`';
  68. $stm = $this->bd->prepare($sql);
  69. $stm->execute();
  70. while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
  71. yield $row;
  72. }
  73. }
  74. public function searchById($id) {
  75. $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE id=?';
  76. $stm = $this->bd->prepare($sql);
  77. $values = array($id);
  78. $stm->execute($values);
  79. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  80. $cat = self::daoToCategory($res);
  81. if (isset($cat[0])) {
  82. return $cat[0];
  83. } else {
  84. return null;
  85. }
  86. }
  87. public function searchByName($name) {
  88. $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE name=?';
  89. $stm = $this->bd->prepare($sql);
  90. if ($stm == false) {
  91. return false;
  92. }
  93. $values = array($name);
  94. $stm->execute($values);
  95. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  96. $cat = self::daoToCategory($res);
  97. if (isset($cat[0])) {
  98. return $cat[0];
  99. } else {
  100. return null;
  101. }
  102. }
  103. public function listCategories($prePopulateFeeds = true, $details = false) {
  104. if ($prePopulateFeeds) {
  105. $sql = 'SELECT c.id AS c_id, c.name AS c_name, '
  106. . ($details ? 'f.* ' : 'f.id, f.name, f.url, f.website, f.priority, f.error, f.`cache_nbEntries`, f.`cache_nbUnreads`, f.ttl ')
  107. . 'FROM `' . $this->prefix . 'category` c '
  108. . 'LEFT OUTER JOIN `' . $this->prefix . 'feed` f ON f.category=c.id '
  109. . 'WHERE f.priority >= :priority_normal '
  110. . 'GROUP BY f.id, c_id '
  111. . 'ORDER BY c.name, f.name';
  112. $stm = $this->bd->prepare($sql);
  113. $stm->execute(array(':priority_normal' => FreshRSS_Feed::PRIORITY_NORMAL));
  114. return self::daoToCategoryPrepopulated($stm->fetchAll(PDO::FETCH_ASSOC));
  115. } else {
  116. $sql = 'SELECT * FROM `' . $this->prefix . 'category` ORDER BY name';
  117. $stm = $this->bd->prepare($sql);
  118. $stm->execute();
  119. return self::daoToCategory($stm->fetchAll(PDO::FETCH_ASSOC));
  120. }
  121. }
  122. public function getDefault() {
  123. $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE id=' . self::DEFAULTCATEGORYID;
  124. $stm = $this->bd->prepare($sql);
  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 `' . $this->prefix . 'category`(id, name) VALUES(?, ?)';
  144. if ($this->bd->dbType() === 'pgsql') {
  145. //Force call to nextval()
  146. $sql .= ' RETURNING nextval(\'"' . $this->prefix . 'category_id_seq"\');';
  147. }
  148. $stm = $this->bd->prepare($sql);
  149. $values = array(
  150. $cat->id(),
  151. $cat->name(),
  152. );
  153. if ($stm && $stm->execute($values)) {
  154. return $this->bd->lastInsertId('"' . $this->prefix . 'category_id_seq"');
  155. } else {
  156. $info = $stm == null ? array(2 => 'syntax error') : $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 `' . $this->prefix . 'category`';
  165. $stm = $this->bd->prepare($sql);
  166. $stm->execute();
  167. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  168. return $res[0]['count'];
  169. }
  170. public function countFeed($id) {
  171. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'feed` WHERE category=?';
  172. $stm = $this->bd->prepare($sql);
  173. $values = array($id);
  174. $stm->execute($values);
  175. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  176. return $res[0]['count'];
  177. }
  178. public function countNotRead($id) {
  179. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id WHERE category=? AND e.is_read=0';
  180. $stm = $this->bd->prepare($sql);
  181. $values = array($id);
  182. $stm->execute($values);
  183. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  184. return $res[0]['count'];
  185. }
  186. public static function findFeed($categories, $feed_id) {
  187. foreach ($categories as $category) {
  188. foreach ($category->feeds() as $feed) {
  189. if ($feed->id() === $feed_id) {
  190. return $feed;
  191. }
  192. }
  193. }
  194. return null;
  195. }
  196. public static function CountUnreads($categories, $minPriority = 0) {
  197. $n = 0;
  198. foreach ($categories as $category) {
  199. foreach ($category->feeds() as $feed) {
  200. if ($feed->priority() >= $minPriority) {
  201. $n += $feed->nbNotRead();
  202. }
  203. }
  204. }
  205. return $n;
  206. }
  207. public static function daoToCategoryPrepopulated($listDAO) {
  208. $list = array();
  209. if (!is_array($listDAO)) {
  210. $listDAO = array($listDAO);
  211. }
  212. $previousLine = null;
  213. $feedsDao = array();
  214. $feedDao = FreshRSS_Factory::createFeedDAO();
  215. foreach ($listDAO as $line) {
  216. if ($previousLine['c_id'] != null && $line['c_id'] !== $previousLine['c_id']) {
  217. // End of the current category, we add it to the $list
  218. $cat = new FreshRSS_Category(
  219. $previousLine['c_name'],
  220. $feedDao->daoToFeed($feedsDao, $previousLine['c_id'])
  221. );
  222. $cat->_id($previousLine['c_id']);
  223. $list[$previousLine['c_id']] = $cat;
  224. $feedsDao = array(); //Prepare for next category
  225. }
  226. $previousLine = $line;
  227. $feedsDao[] = $line;
  228. }
  229. // add the last category
  230. if ($previousLine != null) {
  231. $cat = new FreshRSS_Category(
  232. $previousLine['c_name'],
  233. $feedDao->daoToFeed($feedsDao, $previousLine['c_id'])
  234. );
  235. $cat->_id($previousLine['c_id']);
  236. $list[$previousLine['c_id']] = $cat;
  237. }
  238. return $list;
  239. }
  240. public static function daoToCategory($listDAO) {
  241. $list = array();
  242. if (!is_array($listDAO)) {
  243. $listDAO = array($listDAO);
  244. }
  245. foreach ($listDAO as $key => $dao) {
  246. $cat = new FreshRSS_Category(
  247. $dao['name']
  248. );
  249. $cat->_id($dao['id']);
  250. $cat->_isDefault(static::DEFAULTCATEGORYID === intval($dao['id']));
  251. $list[$key] = $cat;
  252. }
  253. return $list;
  254. }
  255. }