Category.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. class FreshRSS_Category extends Minz_Model {
  3. /**
  4. * Normal
  5. * @var int
  6. */
  7. const KIND_NORMAL = 0;
  8. /**
  9. * Category tracking a third-party Dynamic OPML
  10. * @var int
  11. */
  12. const KIND_DYNAMIC_OPML = 2;
  13. const TTL_DEFAULT = 0;
  14. /**
  15. * @var int
  16. */
  17. private $id = 0;
  18. /** @var int */
  19. private $kind = 0;
  20. private $name;
  21. private $nbFeeds = -1;
  22. private $nbNotRead = -1;
  23. /** @var array<FreshRSS_Feed>|null */
  24. private $feeds = null;
  25. private $hasFeedsWithError = false;
  26. private $attributes = [];
  27. /** @var int */
  28. private $lastUpdate = 0;
  29. /** @var bool */
  30. private $error = false;
  31. public function __construct(string $name = '', $feeds = null) {
  32. $this->_name($name);
  33. if (isset($feeds)) {
  34. $this->_feeds($feeds);
  35. $this->nbFeeds = 0;
  36. $this->nbNotRead = 0;
  37. foreach ($feeds as $feed) {
  38. $this->nbFeeds++;
  39. $this->nbNotRead += $feed->nbNotRead();
  40. $this->hasFeedsWithError |= $feed->inError();
  41. }
  42. }
  43. }
  44. public function id(): int {
  45. return $this->id;
  46. }
  47. public function kind(): int {
  48. return $this->kind;
  49. }
  50. public function name(): string {
  51. return $this->name;
  52. }
  53. public function lastUpdate(): int {
  54. return $this->lastUpdate;
  55. }
  56. public function _lastUpdate(int $value) {
  57. $this->lastUpdate = $value;
  58. }
  59. public function inError(): bool {
  60. return $this->error;
  61. }
  62. public function _error($value) {
  63. $this->error = (bool)$value;
  64. }
  65. public function isDefault(): bool {
  66. return $this->id == FreshRSS_CategoryDAO::DEFAULTCATEGORYID;
  67. }
  68. public function nbFeeds(): int {
  69. if ($this->nbFeeds < 0) {
  70. $catDAO = FreshRSS_Factory::createCategoryDao();
  71. $this->nbFeeds = $catDAO->countFeed($this->id());
  72. }
  73. return $this->nbFeeds;
  74. }
  75. public function nbNotRead(): int {
  76. if ($this->nbNotRead < 0) {
  77. $catDAO = FreshRSS_Factory::createCategoryDao();
  78. $this->nbNotRead = $catDAO->countNotRead($this->id());
  79. }
  80. return $this->nbNotRead;
  81. }
  82. /** @return array<FreshRSS_Feed> */
  83. public function feeds(): array {
  84. if ($this->feeds === null) {
  85. $feedDAO = FreshRSS_Factory::createFeedDao();
  86. $this->feeds = $feedDAO->listByCategory($this->id());
  87. $this->nbFeeds = 0;
  88. $this->nbNotRead = 0;
  89. foreach ($this->feeds as $feed) {
  90. $this->nbFeeds++;
  91. $this->nbNotRead += $feed->nbNotRead();
  92. $this->hasFeedsWithError |= $feed->inError();
  93. }
  94. usort($this->feeds, function ($a, $b) {
  95. return strnatcasecmp($a->name(), $b->name());
  96. });
  97. }
  98. return $this->feeds;
  99. }
  100. public function hasFeedsWithError() {
  101. return $this->hasFeedsWithError;
  102. }
  103. public function attributes($key = '') {
  104. if ($key == '') {
  105. return $this->attributes;
  106. } else {
  107. return isset($this->attributes[$key]) ? $this->attributes[$key] : null;
  108. }
  109. }
  110. public function _id($id) {
  111. $this->id = intval($id);
  112. if ($id == FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
  113. $this->_name(_t('gen.short.default_category'));
  114. }
  115. }
  116. public function _kind(int $kind) {
  117. $this->kind = $kind;
  118. }
  119. public function _name($value) {
  120. $this->name = mb_strcut(trim($value), 0, 255, 'UTF-8');
  121. }
  122. /** @param array<FreshRSS_Feed>|FreshRSS_Feed $values */
  123. public function _feeds($values) {
  124. if (!is_array($values)) {
  125. $values = array($values);
  126. }
  127. $this->feeds = $values;
  128. }
  129. /**
  130. * To manually add feeds to this category (not committing to database).
  131. * @param FreshRSS_Feed $feed
  132. */
  133. public function addFeed($feed) {
  134. if ($this->feeds === null) {
  135. $this->feeds = [];
  136. }
  137. $this->feeds[] = $feed;
  138. }
  139. public function _attributes($key, $value) {
  140. if ('' == $key) {
  141. if (is_string($value)) {
  142. $value = json_decode($value, true);
  143. }
  144. if (is_array($value)) {
  145. $this->attributes = $value;
  146. }
  147. } elseif (null === $value) {
  148. unset($this->attributes[$key]);
  149. } else {
  150. $this->attributes[$key] = $value;
  151. }
  152. }
  153. public static function cacheFilename(string $url, array $attributes): string {
  154. $simplePie = customSimplePie($attributes);
  155. $filename = $simplePie->get_cache_filename($url);
  156. return CACHE_PATH . '/' . $filename . '.opml.xml';
  157. }
  158. public function refreshDynamicOpml(): bool {
  159. $url = $this->attributes('opml_url');
  160. if ($url == '') {
  161. return false;
  162. }
  163. $ok = true;
  164. $attributes = []; //TODO
  165. $cachePath = self::cacheFilename($url, $attributes);
  166. $opml = httpGet($url, $cachePath, 'opml', $attributes);
  167. if ($opml == '') {
  168. Minz_Log::warning('Error getting dynamic OPML for category ' . $this->id() . '! ' .
  169. SimplePie_Misc::url_remove_credentials($url));
  170. $ok = false;
  171. } else {
  172. $dryRunCategory = new FreshRSS_Category();
  173. $importService = new FreshRSS_Import_Service();
  174. $importService->importOpml($opml, $dryRunCategory, true, true);
  175. if ($importService->lastStatus()) {
  176. $feedDAO = FreshRSS_Factory::createFeedDao();
  177. /** @var array<string,FreshRSS_Feed> */
  178. $dryRunFeeds = [];
  179. foreach ($dryRunCategory->feeds() as $dryRunFeed) {
  180. $dryRunFeeds[$dryRunFeed->url()] = $dryRunFeed;
  181. }
  182. /** @var array<string,FreshRSS_Feed> */
  183. $existingFeeds = [];
  184. foreach ($this->feeds() as $existingFeed) {
  185. $existingFeeds[$existingFeed->url()] = $existingFeed;
  186. if (empty($dryRunFeeds[$existingFeed->url()])) {
  187. // The feed does not exist in the new dynamic OPML, so mute (disable) that feed
  188. $existingFeed->_mute(true);
  189. $ok &= ($feedDAO->updateFeed($existingFeed->id(), [
  190. 'ttl' => $existingFeed->ttl(true),
  191. ]) !== false);
  192. }
  193. }
  194. foreach ($dryRunCategory->feeds() as $dryRunFeed) {
  195. if (empty($existingFeeds[$dryRunFeed->url()])) {
  196. // The feed does not exist in the current category, so add that feed
  197. $dryRunFeed->_categoryId($this->id());
  198. $ok &= ($feedDAO->addFeedObject($dryRunFeed) !== false);
  199. } else {
  200. $existingFeed = $existingFeeds[$dryRunFeed->url()];
  201. if ($existingFeed->mute()) {
  202. // The feed already exists in the current category but was muted (disabled), so unmute (enable) again
  203. $existingFeed->_mute(false);
  204. $ok &= ($feedDAO->updateFeed($existingFeed->id(), [
  205. 'ttl' => $existingFeed->ttl(true),
  206. ]) !== false);
  207. }
  208. }
  209. }
  210. } else {
  211. $ok = false;
  212. Minz_Log::warning('Error loading dynamic OPML for category ' . $this->id() . '! ' .
  213. SimplePie_Misc::url_remove_credentials($url));
  214. }
  215. }
  216. $catDAO = FreshRSS_Factory::createCategoryDao();
  217. $catDAO->updateLastUpdate($this->id(), !$ok);
  218. return $ok;
  219. }
  220. }