4
0

Category.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. public function showUnreadCount(): bool {
  109. return $this->attributeBoolean('show_unread_count') ??
  110. (FreshRSS_Context::userConf()->show_unread_count === 'all');
  111. }
  112. /**
  113. * @return array<int,FreshRSS_Feed> where the key is the feed ID
  114. * @throws Minz_ConfigurationNamespaceException
  115. * @throws Minz_PDOConnectionException
  116. */
  117. public function feeds(): array {
  118. if ($this->feeds === null) {
  119. $feedDAO = FreshRSS_Factory::createFeedDao();
  120. $this->feeds = $feedDAO->listByCategory($this->id());
  121. $this->nbFeeds = 0;
  122. $this->nbNotRead = 0;
  123. foreach ($this->feeds as $feed) {
  124. $this->nbFeeds++;
  125. if ($feed->priority() > FreshRSS_Feed::PRIORITY_HIDDEN) {
  126. $this->nbNotRead += $feed->nbNotRead();
  127. $this->hasFeedsWithError |= ($feed->inError() && !$feed->mute());
  128. }
  129. }
  130. $this->sortFeeds();
  131. }
  132. return $this->feeds ?? [];
  133. }
  134. public function hasFeedsWithError(): bool {
  135. return (bool)($this->hasFeedsWithError);
  136. }
  137. public function _id(int $id): void {
  138. $this->id = $id;
  139. if ($id === FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
  140. $this->name = _t('gen.short.default_category');
  141. }
  142. }
  143. public function _kind(int $kind): void {
  144. $this->kind = $kind;
  145. }
  146. public function _name(string $value): void {
  147. if ($this->id !== FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
  148. $this->name = mb_strcut(trim($value), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8');
  149. }
  150. }
  151. /** @param array<FreshRSS_Feed>|FreshRSS_Feed $values */
  152. public function _feeds(array|FreshRSS_Feed $values): void {
  153. if (!is_array($values)) {
  154. $values = [$values];
  155. }
  156. $this->feeds = array_values($values);
  157. $this->sortFeeds();
  158. }
  159. public function defaultSort(): ?string {
  160. return $this->attributeString('defaultSort');
  161. }
  162. public function defaultOrder(): ?string {
  163. return $this->attributeString('defaultOrder');
  164. }
  165. /**
  166. * To manually add feeds to this category (not committing to database).
  167. */
  168. public function addFeed(FreshRSS_Feed $feed): void {
  169. if ($this->feeds === null) {
  170. $this->feeds = [];
  171. }
  172. $feed->_category($this);
  173. if ($feed->id() === 0) {
  174. // Feeds created on a dry run do not have an ID
  175. $this->feeds[] = $feed;
  176. } else {
  177. $this->feeds[$feed->id()] = $feed;
  178. }
  179. $this->sortFeeds();
  180. }
  181. /**
  182. * @throws FreshRSS_Context_Exception
  183. */
  184. public function cacheFilename(string $url): string {
  185. $simplePie = new FreshRSS_SimplePieCustom($this->attributes(), $this->curlOptions());
  186. $filename = $simplePie->get_cache_filename($url);
  187. return CACHE_PATH . '/' . $filename . '.opml.xml';
  188. }
  189. public function refreshDynamicOpml(): bool {
  190. $url = $this->attributeString('opml_url');
  191. if ($url == null) {
  192. return false;
  193. }
  194. $ok = true;
  195. $cachePath = $this->cacheFilename($url);
  196. $opml = FreshRSS_http_Util::httpGet($url, $cachePath, 'opml', $this->attributes(), $this->curlOptions())['body'];
  197. if ($opml == '') {
  198. Minz_Log::warning('Error getting dynamic OPML for category ' . $this->id() . '! ' .
  199. \SimplePie\Misc::url_remove_credentials($url));
  200. $ok = false;
  201. } else {
  202. $dryRunCategory = new FreshRSS_Category();
  203. $importService = new FreshRSS_Import_Service();
  204. $importService->importOpml($opml, $dryRunCategory, true);
  205. if ($importService->lastStatus()) {
  206. $feedDAO = FreshRSS_Factory::createFeedDao();
  207. $limits = FreshRSS_Context::systemConf()->limits;
  208. $maxFeeds = (int)($limits['max_feeds'] ?? 0);
  209. $nbFeeds = $maxFeeds > 0 ? $feedDAO->count() : 0;
  210. /** @var array<string,FreshRSS_Feed> */
  211. $dryRunFeeds = [];
  212. foreach ($dryRunCategory->feeds() as $dryRunFeed) {
  213. $dryRunFeeds[$dryRunFeed->url()] = $dryRunFeed;
  214. }
  215. /** @var array<string,FreshRSS_Feed> */
  216. $existingFeeds = [];
  217. foreach ($this->feeds() as $existingFeed) {
  218. $existingFeeds[$existingFeed->url()] = $existingFeed;
  219. if (empty($dryRunFeeds[$existingFeed->url()])) {
  220. // The feed does not exist in the new dynamic OPML, so mute (disable) that feed
  221. $existingFeed->_mute(true);
  222. $ok &= ($feedDAO->updateFeed($existingFeed->id(), [
  223. 'ttl' => $existingFeed->ttl(true),
  224. ]) !== false);
  225. }
  226. }
  227. foreach ($dryRunCategory->feeds() as $dryRunFeed) {
  228. if (empty($existingFeeds[$dryRunFeed->url()])) {
  229. // The feed does not exist in the current category, so add that feed
  230. if ($maxFeeds > 0 && $nbFeeds >= $maxFeeds) {
  231. // Respect the per-user maximum number of feeds
  232. Minz_Log::warning(_t('feedback.sub.feed.over_max', $maxFeeds) .
  233. ' (dynamic OPML category ' . $this->id() . ')');
  234. $ok = false;
  235. break;
  236. }
  237. $dryRunFeed->_category($this);
  238. if ($feedDAO->addFeedObject($dryRunFeed) === false) {
  239. $ok = false;
  240. } else {
  241. $nbFeeds++;
  242. }
  243. $existingFeeds[$dryRunFeed->url()] = $dryRunFeed;
  244. } else {
  245. $existingFeed = $existingFeeds[$dryRunFeed->url()];
  246. if ($existingFeed->mute()) {
  247. // The feed already exists in the current category but was muted (disabled), so unmute (enable) again
  248. $existingFeed->_mute(false);
  249. $ok &= ($feedDAO->updateFeed($existingFeed->id(), [
  250. 'ttl' => $existingFeed->ttl(true),
  251. ]) !== false);
  252. }
  253. }
  254. }
  255. } else {
  256. $ok = false;
  257. Minz_Log::warning('Error loading dynamic OPML for category ' . $this->id() . '! ' .
  258. \SimplePie\Misc::url_remove_credentials($url));
  259. }
  260. }
  261. $catDAO = FreshRSS_Factory::createCategoryDao();
  262. if ($ok) {
  263. $catDAO->updateLastUpdate($this->id());
  264. } else {
  265. $catDAO->updateLastError($this->id());
  266. }
  267. return (bool)$ok;
  268. }
  269. private function sortFeeds(): void {
  270. if ($this->feeds === null) {
  271. return;
  272. }
  273. uasort($this->feeds, static fn(FreshRSS_Feed $a, FreshRSS_Feed $b) => strnatcasecmp($a->name(), $b->name()));
  274. }
  275. /**
  276. * Access cached feed
  277. * @param array<FreshRSS_Category> $categories
  278. */
  279. public static function findFeed(array $categories, int $feed_id): ?FreshRSS_Feed {
  280. foreach ($categories as $category) {
  281. foreach ($category->feeds() as $feed) {
  282. if ($feed->id() === $feed_id) {
  283. $feed->_category($category); // Should already be done; just to be safe
  284. return $feed;
  285. }
  286. }
  287. }
  288. return null;
  289. }
  290. /**
  291. * Access cached feeds
  292. * @param array<FreshRSS_Category> $categories
  293. * @return array<int,FreshRSS_Feed> where the key is the feed ID
  294. */
  295. public static function findFeeds(array $categories): array {
  296. $result = [];
  297. foreach ($categories as $category) {
  298. foreach ($category->feeds() as $feed) {
  299. $result[$feed->id()] = $feed;
  300. }
  301. }
  302. return $result;
  303. }
  304. /**
  305. * @param array<FreshRSS_Category> $categories
  306. */
  307. public static function countUnread(array $categories, int $minPriority = FreshRSS_Feed::PRIORITY_FEED): int {
  308. $n = 0;
  309. foreach ($categories as $category) {
  310. $n += $category->nbNotRead($minPriority);
  311. }
  312. return $n;
  313. }
  314. }