CategoryDAO.php 7.5 KB

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