Category.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_Category extends Minz_Model {
  4. use FreshRSS_AttributesTrait, FreshRSS_FilterActionsTrait;
  5. /**
  6. * Normal
  7. */
  8. public const KIND_NORMAL = 0;
  9. /**
  10. * Category tracking a third-party Dynamic OPML
  11. */
  12. public const KIND_DYNAMIC_OPML = 2;
  13. private int $id = 0;
  14. private int $kind = 0;
  15. private string $name;
  16. private int $nbFeeds = -1;
  17. /** Number of unread articles in feeds with visibility FreshRSS_Feed::PRIORITY_FEED */
  18. private int $nbNotRead = -1;
  19. /** @var array<int,FreshRSS_Feed>|null where the key is the feed ID */
  20. private ?array $feeds = null;
  21. private bool|int $hasFeedsWithError = false;
  22. private int $lastUpdate = 0;
  23. private bool $error = false;
  24. /**
  25. * @param array<FreshRSS_Feed>|null $feeds
  26. */
  27. public function __construct(string $name = '', int $id = 0, ?array $feeds = null) {
  28. $this->_id($id);
  29. $this->_name($name);
  30. if ($feeds !== null) {
  31. $this->_feeds($feeds);
  32. $this->nbFeeds = 0;
  33. $this->nbNotRead = 0;
  34. foreach ($feeds as $feed) {
  35. $feed->_category($this);
  36. $this->nbFeeds++;
  37. if ($feed->priority() > FreshRSS_Feed::PRIORITY_HIDDEN) {
  38. $this->nbNotRead += $feed->nbNotRead();
  39. $this->hasFeedsWithError |= ($feed->inError() && !$feed->mute());
  40. }
  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. /**
  58. * @param int|numeric-string $value
  59. * 32-bit systems provide a string and will fail in year 2038
  60. */
  61. public function _lastUpdate(int|string $value): void {
  62. $this->lastUpdate = (int)$value;
  63. }
  64. public function inError(): bool {
  65. return $this->error;
  66. }
  67. public function _error(bool|int $value): void {
  68. $this->error = (bool)$value;
  69. }
  70. public function isDefault(): bool {
  71. return $this->id == FreshRSS_CategoryDAO::DEFAULTCATEGORYID;
  72. }
  73. public function nbFeeds(): int {
  74. if ($this->nbFeeds < 0) {
  75. $catDAO = FreshRSS_Factory::createCategoryDao();
  76. $this->nbFeeds = $catDAO->countFeed($this->id());
  77. }
  78. return $this->nbFeeds;
  79. }
  80. /**
  81. * @throws Minz_ConfigurationNamespaceException
  82. * @throws Minz_PDOConnectionException
  83. */
  84. public function nbNotRead(int $minPriority = FreshRSS_Feed::PRIORITY_FEED): int {
  85. if ($this->nbNotRead > 0 && $minPriority === FreshRSS_Feed::PRIORITY_FEED) {
  86. return $this->nbNotRead;
  87. }
  88. if ($this->feeds === null) {
  89. $catDAO = FreshRSS_Factory::createCategoryDao();
  90. $nb = $catDAO->countNotRead($this->id(), $minPriority);
  91. if ($minPriority === FreshRSS_Feed::PRIORITY_FEED) {
  92. $this->nbNotRead = $nb;
  93. }
  94. return $nb;
  95. }
  96. $nb = 0;
  97. foreach ($this->feeds as $feed) {
  98. if ($feed->priority() >= $minPriority) {
  99. $nb += $feed->nbNotRead();
  100. }
  101. }
  102. return $nb;
  103. }
  104. /** @return array<int,mixed> */
  105. public function curlOptions(): array {
  106. return []; // TODO (e.g., credentials for Dynamic OPML)
  107. }
  108. /**
  109. * @return array<int,FreshRSS_Feed> where the key is the feed ID
  110. * @throws Minz_ConfigurationNamespaceException
  111. * @throws Minz_PDOConnectionException
  112. */
  113. public function feeds(): array {
  114. if ($this->feeds === null) {
  115. $feedDAO = FreshRSS_Factory::createFeedDao();
  116. $this->feeds = $feedDAO->listByCategory($this->id());
  117. $this->nbFeeds = 0;
  118. $this->nbNotRead = 0;
  119. foreach ($this->feeds as $feed) {
  120. $this->nbFeeds++;
  121. if ($feed->priority() > FreshRSS_Feed::PRIORITY_HIDDEN) {
  122. $this->nbNotRead += $feed->nbNotRead();
  123. $this->hasFeedsWithError |= ($feed->inError() && !$feed->mute());
  124. }
  125. }
  126. $this->sortFeeds();
  127. }
  128. return $this->feeds ?? [];
  129. }
  130. public function hasFeedsWithError(): bool {
  131. return (bool)($this->hasFeedsWithError);
  132. }
  133. public function _id(int $id): void {
  134. $this->id = $id;
  135. if ($id === FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
  136. $this->name = _t('gen.short.default_category');
  137. }
  138. }
  139. public function _kind(int $kind): void {
  140. $this->kind = $kind;
  141. }
  142. public function _name(string $value): void {
  143. if ($this->id !== FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
  144. $this->name = mb_strcut(trim($value), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8');
  145. }
  146. }
  147. /** @param array<FreshRSS_Feed>|FreshRSS_Feed $values */
  148. public function _feeds(array|FreshRSS_Feed $values): void {
  149. if (!is_array($values)) {
  150. $values = [$values];
  151. }
  152. $this->feeds = array_values($values);
  153. $this->sortFeeds();
  154. }
  155. public function defaultSort(): ?string {
  156. return $this->attributeString('defaultSort');
  157. }
  158. public function defaultOrder(): ?string {
  159. return $this->attributeString('defaultOrder');
  160. }
  161. /**
  162. * To manually add feeds to this category (not committing to database).
  163. */
  164. public function addFeed(FreshRSS_Feed $feed): void {
  165. if ($this->feeds === null) {
  166. $this->feeds = [];
  167. }
  168. $feed->_category($this);
  169. if ($feed->id() === 0) {
  170. // Feeds created on a dry run do not have an ID
  171. $this->feeds[] = $feed;
  172. } else {
  173. $this->feeds[$feed->id()] = $feed;
  174. }
  175. $this->sortFeeds();
  176. }
  177. /**
  178. * @throws FreshRSS_Context_Exception
  179. */
  180. public function cacheFilename(string $url): string {
  181. $simplePie = new FreshRSS_SimplePieCustom($this->attributes(), $this->curlOptions());
  182. $filename = $simplePie->get_cache_filename($url);
  183. return CACHE_PATH . '/' . $filename . '.opml.xml';
  184. }
  185. public function refreshDynamicOpml(): bool {
  186. $url = $this->attributeString('opml_url');
  187. if ($url == null) {
  188. return false;
  189. }
  190. $ok = true;
  191. $cachePath = $this->cacheFilename($url);
  192. $opml = FreshRSS_http_Util::httpGet($url, $cachePath, 'opml', $this->attributes(), $this->curlOptions())['body'];
  193. if ($opml == '') {
  194. Minz_Log::warning('Error getting dynamic OPML for category ' . $this->id() . '! ' .
  195. \SimplePie\Misc::url_remove_credentials($url));
  196. $ok = false;
  197. } else {
  198. $dryRunCategory = new FreshRSS_Category();
  199. $importService = new FreshRSS_Import_Service();
  200. $importService->importOpml($opml, $dryRunCategory, true);
  201. if ($importService->lastStatus()) {
  202. $feedDAO = FreshRSS_Factory::createFeedDao();
  203. /** @var array<string,FreshRSS_Feed> */
  204. $dryRunFeeds = [];
  205. foreach ($dryRunCategory->feeds() as $dryRunFeed) {
  206. $dryRunFeeds[$dryRunFeed->url()] = $dryRunFeed;
  207. }
  208. /** @var array<string,FreshRSS_Feed> */
  209. $existingFeeds = [];
  210. foreach ($this->feeds() as $existingFeed) {
  211. $existingFeeds[$existingFeed->url()] = $existingFeed;
  212. if (empty($dryRunFeeds[$existingFeed->url()])) {
  213. // The feed does not exist in the new dynamic OPML, so mute (disable) that feed
  214. $existingFeed->_mute(true);
  215. $ok &= ($feedDAO->updateFeed($existingFeed->id(), [
  216. 'ttl' => $existingFeed->ttl(true),
  217. ]) !== false);
  218. }
  219. }
  220. foreach ($dryRunCategory->feeds() as $dryRunFeed) {
  221. if (empty($existingFeeds[$dryRunFeed->url()])) {
  222. // The feed does not exist in the current category, so add that feed
  223. $dryRunFeed->_category($this);
  224. $ok &= ($feedDAO->addFeedObject($dryRunFeed) !== false);
  225. $existingFeeds[$dryRunFeed->url()] = $dryRunFeed;
  226. } else {
  227. $existingFeed = $existingFeeds[$dryRunFeed->url()];
  228. if ($existingFeed->mute()) {
  229. // The feed already exists in the current category but was muted (disabled), so unmute (enable) again
  230. $existingFeed->_mute(false);
  231. $ok &= ($feedDAO->updateFeed($existingFeed->id(), [
  232. 'ttl' => $existingFeed->ttl(true),
  233. ]) !== false);
  234. }
  235. }
  236. }
  237. } else {
  238. $ok = false;
  239. Minz_Log::warning('Error loading dynamic OPML for category ' . $this->id() . '! ' .
  240. \SimplePie\Misc::url_remove_credentials($url));
  241. }
  242. }
  243. $catDAO = FreshRSS_Factory::createCategoryDao();
  244. $catDAO->updateLastUpdate($this->id(), !$ok);
  245. return (bool)$ok;
  246. }
  247. private function sortFeeds(): void {
  248. if ($this->feeds === null) {
  249. return;
  250. }
  251. uasort($this->feeds, static fn(FreshRSS_Feed $a, FreshRSS_Feed $b) => strnatcasecmp($a->name(), $b->name()));
  252. }
  253. /**
  254. * Access cached feed
  255. * @param array<FreshRSS_Category> $categories
  256. */
  257. public static function findFeed(array $categories, int $feed_id): ?FreshRSS_Feed {
  258. foreach ($categories as $category) {
  259. foreach ($category->feeds() as $feed) {
  260. if ($feed->id() === $feed_id) {
  261. $feed->_category($category); // Should already be done; just to be safe
  262. return $feed;
  263. }
  264. }
  265. }
  266. return null;
  267. }
  268. /**
  269. * Access cached feeds
  270. * @param array<FreshRSS_Category> $categories
  271. * @return array<int,FreshRSS_Feed> where the key is the feed ID
  272. */
  273. public static function findFeeds(array $categories): array {
  274. $result = [];
  275. foreach ($categories as $category) {
  276. foreach ($category->feeds() as $feed) {
  277. $result[$feed->id()] = $feed;
  278. }
  279. }
  280. return $result;
  281. }
  282. /**
  283. * @param array<FreshRSS_Category> $categories
  284. */
  285. public static function countUnread(array $categories, int $minPriority = FreshRSS_Feed::PRIORITY_FEED): int {
  286. $n = 0;
  287. foreach ($categories as $category) {
  288. $n += $category->nbNotRead($minPriority);
  289. }
  290. return $n;
  291. }
  292. }