CategoryDAO.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 searchById($id) {
  67. $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE id=?';
  68. $stm = $this->bd->prepare($sql);
  69. $values = array($id);
  70. $stm->execute($values);
  71. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  72. $cat = self::daoToCategory($res);
  73. if (isset($cat[0])) {
  74. return $cat[0];
  75. } else {
  76. return null;
  77. }
  78. }
  79. public function searchByName($name) {
  80. $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE name=?';
  81. $stm = $this->bd->prepare($sql);
  82. $values = array($name);
  83. $stm->execute($values);
  84. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  85. $cat = self::daoToCategory($res);
  86. if (isset($cat[0])) {
  87. return $cat[0];
  88. } else {
  89. return null;
  90. }
  91. }
  92. public function listCategories($prePopulateFeeds = true, $details = false) {
  93. if ($prePopulateFeeds) {
  94. $sql = 'SELECT c.id AS c_id, c.name AS c_name, '
  95. . ($details ? 'f.* ' : 'f.id, f.name, f.url, f.website, f.priority, f.error, f.`cache_nbEntries`, f.`cache_nbUnreads`, f.ttl ')
  96. . 'FROM `' . $this->prefix . 'category` c '
  97. . 'LEFT OUTER JOIN `' . $this->prefix . 'feed` f ON f.category=c.id '
  98. . 'WHERE f.priority >= :priority_normal '
  99. . 'GROUP BY f.id, c_id '
  100. . 'ORDER BY c.name, f.name';
  101. $stm = $this->bd->prepare($sql);
  102. $stm->execute(array(':priority_normal' => FreshRSS_Feed::PRIORITY_NORMAL));
  103. return self::daoToCategoryPrepopulated($stm->fetchAll(PDO::FETCH_ASSOC));
  104. } else {
  105. $sql = 'SELECT * FROM `' . $this->prefix . 'category` ORDER BY name';
  106. $stm = $this->bd->prepare($sql);
  107. $stm->execute();
  108. return self::daoToCategory($stm->fetchAll(PDO::FETCH_ASSOC));
  109. }
  110. }
  111. public function getDefault() {
  112. $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE id=' . self::DEFAULTCATEGORYID;
  113. $stm = $this->bd->prepare($sql);
  114. $stm->execute();
  115. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  116. $cat = self::daoToCategory($res);
  117. if (isset($cat[0])) {
  118. return $cat[0];
  119. } else {
  120. if (FreshRSS_Context::$isCli) {
  121. fwrite(STDERR, 'FreshRSS database error: Default category not found!' . "\n");
  122. }
  123. Minz_Log::error('FreshRSS database error: Default category not found!');
  124. return null;
  125. }
  126. }
  127. public function checkDefault() {
  128. $def_cat = $this->searchById(self::DEFAULTCATEGORYID);
  129. if ($def_cat == null) {
  130. $cat = new FreshRSS_Category(_t('gen.short.default_category'));
  131. $cat->_id(self::DEFAULTCATEGORYID);
  132. $sql = 'INSERT INTO `' . $this->prefix . 'category`(id, name) VALUES(?, ?)';
  133. if (parent::$sharedDbType === 'pgsql') {
  134. //Force call to nextval()
  135. $sql .= ' RETURNING nextval(\'"' . $this->prefix . 'category_id_seq"\');';
  136. }
  137. $stm = $this->bd->prepare($sql);
  138. $values = array(
  139. $cat->id(),
  140. $cat->name(),
  141. );
  142. if ($stm && $stm->execute($values)) {
  143. return $this->bd->lastInsertId('"' . $this->prefix . 'category_id_seq"');
  144. } else {
  145. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  146. Minz_Log::error('SQL error check default category: ' . json_encode($info));
  147. return false;
  148. }
  149. }
  150. return true;
  151. }
  152. public function count() {
  153. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'category`';
  154. $stm = $this->bd->prepare($sql);
  155. $stm->execute();
  156. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  157. return $res[0]['count'];
  158. }
  159. public function countFeed($id) {
  160. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'feed` WHERE category=?';
  161. $stm = $this->bd->prepare($sql);
  162. $values = array($id);
  163. $stm->execute($values);
  164. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  165. return $res[0]['count'];
  166. }
  167. public function countNotRead($id) {
  168. $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';
  169. $stm = $this->bd->prepare($sql);
  170. $values = array($id);
  171. $stm->execute($values);
  172. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  173. return $res[0]['count'];
  174. }
  175. public static function findFeed($categories, $feed_id) {
  176. foreach ($categories as $category) {
  177. foreach ($category->feeds() as $feed) {
  178. if ($feed->id() === $feed_id) {
  179. return $feed;
  180. }
  181. }
  182. }
  183. return null;
  184. }
  185. public static function CountUnreads($categories, $minPriority = 0) {
  186. $n = 0;
  187. foreach ($categories as $category) {
  188. foreach ($category->feeds() as $feed) {
  189. if ($feed->priority() >= $minPriority) {
  190. $n += $feed->nbNotRead();
  191. }
  192. }
  193. }
  194. return $n;
  195. }
  196. public static function daoToCategoryPrepopulated($listDAO) {
  197. $list = array();
  198. if (!is_array($listDAO)) {
  199. $listDAO = array($listDAO);
  200. }
  201. $previousLine = null;
  202. $feedsDao = array();
  203. $feedDao = FreshRSS_Factory::createFeedDAO();
  204. foreach ($listDAO as $line) {
  205. if ($previousLine['c_id'] != null && $line['c_id'] !== $previousLine['c_id']) {
  206. // End of the current category, we add it to the $list
  207. $cat = new FreshRSS_Category(
  208. $previousLine['c_name'],
  209. $feedDao->daoToFeed($feedsDao, $previousLine['c_id'])
  210. );
  211. $cat->_id($previousLine['c_id']);
  212. $list[$previousLine['c_id']] = $cat;
  213. $feedsDao = array(); //Prepare for next category
  214. }
  215. $previousLine = $line;
  216. $feedsDao[] = $line;
  217. }
  218. // add the last category
  219. if ($previousLine != null) {
  220. $cat = new FreshRSS_Category(
  221. $previousLine['c_name'],
  222. $feedDao->daoToFeed($feedsDao, $previousLine['c_id'])
  223. );
  224. $cat->_id($previousLine['c_id']);
  225. $list[$previousLine['c_id']] = $cat;
  226. }
  227. return $list;
  228. }
  229. public static function daoToCategory($listDAO) {
  230. $list = array();
  231. if (!is_array($listDAO)) {
  232. $listDAO = array($listDAO);
  233. }
  234. foreach ($listDAO as $key => $dao) {
  235. $cat = new FreshRSS_Category(
  236. $dao['name']
  237. );
  238. $cat->_id($dao['id']);
  239. $list[$key] = $cat;
  240. }
  241. return $list;
  242. }
  243. }