Category.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. /** @return string HTML-encoded name of the category */
  51. public function name(): string {
  52. return $this->name;
  53. }
  54. public function lastUpdate(): int {
  55. return $this->lastUpdate;
  56. }
  57. public function _lastUpdate(int $value) {
  58. $this->lastUpdate = $value;
  59. }
  60. public function inError(): bool {
  61. return $this->error;
  62. }
  63. public function _error($value) {
  64. $this->error = (bool)$value;
  65. }
  66. public function isDefault(): bool {
  67. return $this->id == FreshRSS_CategoryDAO::DEFAULTCATEGORYID;
  68. }
  69. public function nbFeeds(): int {
  70. if ($this->nbFeeds < 0) {
  71. $catDAO = FreshRSS_Factory::createCategoryDao();
  72. $this->nbFeeds = $catDAO->countFeed($this->id());
  73. }
  74. return $this->nbFeeds;
  75. }
  76. public function nbNotRead(): int {
  77. if ($this->nbNotRead < 0) {
  78. $catDAO = FreshRSS_Factory::createCategoryDao();
  79. $this->nbNotRead = $catDAO->countNotRead($this->id());
  80. }
  81. return $this->nbNotRead;
  82. }
  83. /** @return array<FreshRSS_Feed> */
  84. public function feeds(): array {
  85. if ($this->feeds === null) {
  86. $feedDAO = FreshRSS_Factory::createFeedDao();
  87. $this->feeds = $feedDAO->listByCategory($this->id());
  88. $this->nbFeeds = 0;
  89. $this->nbNotRead = 0;
  90. foreach ($this->feeds as $feed) {
  91. $this->nbFeeds++;
  92. $this->nbNotRead += $feed->nbNotRead();
  93. $this->hasFeedsWithError |= $feed->inError();
  94. }
  95. $this->sortFeeds();
  96. }
  97. return $this->feeds;
  98. }
  99. public function hasFeedsWithError() {
  100. return $this->hasFeedsWithError;
  101. }
  102. public function attributes($key = '') {
  103. if ($key == '') {
  104. return $this->attributes;
  105. } else {
  106. return isset($this->attributes[$key]) ? $this->attributes[$key] : null;
  107. }
  108. }
  109. public function _id($id) {
  110. $this->id = intval($id);
  111. if ($id == FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
  112. $this->_name(_t('gen.short.default_category'));
  113. }
  114. }
  115. public function _kind(int $kind) {
  116. $this->kind = $kind;
  117. }
  118. public function _name($value) {
  119. $this->name = mb_strcut(trim($value), 0, 255, 'UTF-8');
  120. }
  121. /** @param array<FreshRSS_Feed>|FreshRSS_Feed $values */
  122. public function _feeds($values) {
  123. if (!is_array($values)) {
  124. $values = array($values);
  125. }
  126. $this->feeds = $values;
  127. $this->sortFeeds();
  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. $this->sortFeeds();
  139. }
  140. public function _attributes($key, $value) {
  141. if ('' == $key) {
  142. if (is_string($value)) {
  143. $value = json_decode($value, true);
  144. }
  145. if (is_array($value)) {
  146. $this->attributes = $value;
  147. }
  148. } elseif (null === $value) {
  149. unset($this->attributes[$key]);
  150. } else {
  151. $this->attributes[$key] = $value;
  152. }
  153. }
  154. public static function cacheFilename(string $url, array $attributes): string {
  155. $simplePie = customSimplePie($attributes);
  156. $filename = $simplePie->get_cache_filename($url);
  157. return CACHE_PATH . '/' . $filename . '.opml.xml';
  158. }
  159. public function refreshDynamicOpml(): bool {
  160. $url = $this->attributes('opml_url');
  161. if ($url == '') {
  162. return false;
  163. }
  164. $ok = true;
  165. $attributes = []; //TODO
  166. $cachePath = self::cacheFilename($url, $attributes);
  167. $opml = httpGet($url, $cachePath, 'opml', $attributes);
  168. if ($opml == '') {
  169. Minz_Log::warning('Error getting dynamic OPML for category ' . $this->id() . '! ' .
  170. SimplePie_Misc::url_remove_credentials($url));
  171. $ok = false;
  172. } else {
  173. $dryRunCategory = new FreshRSS_Category();
  174. $importService = new FreshRSS_Import_Service();
  175. $importService->importOpml($opml, $dryRunCategory, true);
  176. if ($importService->lastStatus()) {
  177. $feedDAO = FreshRSS_Factory::createFeedDao();
  178. /** @var array<string,FreshRSS_Feed> */
  179. $dryRunFeeds = [];
  180. foreach ($dryRunCategory->feeds() as $dryRunFeed) {
  181. $dryRunFeeds[$dryRunFeed->url()] = $dryRunFeed;
  182. }
  183. /** @var array<string,FreshRSS_Feed> */
  184. $existingFeeds = [];
  185. foreach ($this->feeds() as $existingFeed) {
  186. $existingFeeds[$existingFeed->url()] = $existingFeed;
  187. if (empty($dryRunFeeds[$existingFeed->url()])) {
  188. // The feed does not exist in the new dynamic OPML, so mute (disable) that feed
  189. $existingFeed->_mute(true);
  190. $ok &= ($feedDAO->updateFeed($existingFeed->id(), [
  191. 'ttl' => $existingFeed->ttl(true),
  192. ]) !== false);
  193. }
  194. }
  195. foreach ($dryRunCategory->feeds() as $dryRunFeed) {
  196. if (empty($existingFeeds[$dryRunFeed->url()])) {
  197. // The feed does not exist in the current category, so add that feed
  198. $dryRunFeed->_categoryId($this->id());
  199. $ok &= ($feedDAO->addFeedObject($dryRunFeed) !== false);
  200. } else {
  201. $existingFeed = $existingFeeds[$dryRunFeed->url()];
  202. if ($existingFeed->mute()) {
  203. // The feed already exists in the current category but was muted (disabled), so unmute (enable) again
  204. $existingFeed->_mute(false);
  205. $ok &= ($feedDAO->updateFeed($existingFeed->id(), [
  206. 'ttl' => $existingFeed->ttl(true),
  207. ]) !== false);
  208. }
  209. }
  210. }
  211. } else {
  212. $ok = false;
  213. Minz_Log::warning('Error loading dynamic OPML for category ' . $this->id() . '! ' .
  214. SimplePie_Misc::url_remove_credentials($url));
  215. }
  216. }
  217. $catDAO = FreshRSS_Factory::createCategoryDao();
  218. $catDAO->updateLastUpdate($this->id(), !$ok);
  219. return $ok;
  220. }
  221. private function sortFeeds() {
  222. usort($this->feeds, static function ($a, $b) {
  223. return strnatcasecmp($a->name(), $b->name());
  224. });
  225. }
  226. }