Category.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. private int $nbNotRead = -1;
  18. /** @var array<FreshRSS_Feed>|null */
  19. private ?array $feeds = null;
  20. /** @var bool|int */
  21. private $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. $this->nbNotRead += $feed->nbNotRead();
  38. $this->hasFeedsWithError |= ($feed->inError() && !$feed->mute());
  39. }
  40. }
  41. }
  42. public function id(): int {
  43. return $this->id;
  44. }
  45. public function kind(): int {
  46. return $this->kind;
  47. }
  48. /** @return string HTML-encoded name of the category */
  49. public function name(): string {
  50. return $this->name;
  51. }
  52. public function lastUpdate(): int {
  53. return $this->lastUpdate;
  54. }
  55. public function _lastUpdate(int $value): void {
  56. $this->lastUpdate = $value;
  57. }
  58. public function inError(): bool {
  59. return $this->error;
  60. }
  61. /** @param bool|int $value */
  62. public function _error($value): void {
  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. /**
  76. * @throws Minz_ConfigurationNamespaceException
  77. * @throws Minz_PDOConnectionException
  78. */
  79. public function nbNotRead(): int {
  80. if ($this->nbNotRead < 0) {
  81. $catDAO = FreshRSS_Factory::createCategoryDao();
  82. $this->nbNotRead = $catDAO->countNotRead($this->id());
  83. }
  84. return $this->nbNotRead;
  85. }
  86. /** @return array<int,mixed> */
  87. public function curlOptions(): array {
  88. return []; // TODO (e.g., credentials for Dynamic OPML)
  89. }
  90. /**
  91. * @return array<int,FreshRSS_Feed>
  92. * @throws Minz_ConfigurationNamespaceException
  93. * @throws Minz_PDOConnectionException
  94. */
  95. public function feeds(): array {
  96. if ($this->feeds === null) {
  97. $feedDAO = FreshRSS_Factory::createFeedDao();
  98. $this->feeds = $feedDAO->listByCategory($this->id());
  99. $this->nbFeeds = 0;
  100. $this->nbNotRead = 0;
  101. foreach ($this->feeds as $feed) {
  102. $this->nbFeeds++;
  103. $this->nbNotRead += $feed->nbNotRead();
  104. $this->hasFeedsWithError |= ($feed->inError() && !$feed->mute());
  105. }
  106. $this->sortFeeds();
  107. }
  108. return $this->feeds ?? [];
  109. }
  110. public function hasFeedsWithError(): bool {
  111. return (bool)($this->hasFeedsWithError);
  112. }
  113. public function _id(int $id): void {
  114. $this->id = $id;
  115. if ($id === FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
  116. $this->name = _t('gen.short.default_category');
  117. }
  118. }
  119. public function _kind(int $kind): void {
  120. $this->kind = $kind;
  121. }
  122. public function _name(string $value): void {
  123. if ($this->id !== FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
  124. $this->name = mb_strcut(trim($value), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8');
  125. }
  126. }
  127. /** @param array<FreshRSS_Feed>|FreshRSS_Feed $values */
  128. public function _feeds($values): void {
  129. if (!is_array($values)) {
  130. $values = [$values];
  131. }
  132. $this->feeds = $values;
  133. $this->sortFeeds();
  134. }
  135. /**
  136. * To manually add feeds to this category (not committing to database).
  137. */
  138. public function addFeed(FreshRSS_Feed $feed): void {
  139. if ($this->feeds === null) {
  140. $this->feeds = [];
  141. }
  142. $feed->_category($this);
  143. $this->feeds[] = $feed;
  144. $this->sortFeeds();
  145. }
  146. /**
  147. * @throws FreshRSS_Context_Exception
  148. */
  149. public function cacheFilename(string $url): string {
  150. $simplePie = customSimplePie($this->attributes(), $this->curlOptions());
  151. $filename = $simplePie->get_cache_filename($url);
  152. return CACHE_PATH . '/' . $filename . '.opml.xml';
  153. }
  154. public function refreshDynamicOpml(): bool {
  155. $url = $this->attributeString('opml_url');
  156. if ($url == null) {
  157. return false;
  158. }
  159. $ok = true;
  160. $cachePath = $this->cacheFilename($url);
  161. $opml = httpGet($url, $cachePath, 'opml', $this->attributes(), $this->curlOptions());
  162. if ($opml == '') {
  163. Minz_Log::warning('Error getting dynamic OPML for category ' . $this->id() . '! ' .
  164. \SimplePie\Misc::url_remove_credentials($url));
  165. $ok = false;
  166. } else {
  167. $dryRunCategory = new FreshRSS_Category();
  168. $importService = new FreshRSS_Import_Service();
  169. $importService->importOpml($opml, $dryRunCategory, true);
  170. if ($importService->lastStatus()) {
  171. $feedDAO = FreshRSS_Factory::createFeedDao();
  172. /** @var array<string,FreshRSS_Feed> */
  173. $dryRunFeeds = [];
  174. foreach ($dryRunCategory->feeds() as $dryRunFeed) {
  175. $dryRunFeeds[$dryRunFeed->url()] = $dryRunFeed;
  176. }
  177. /** @var array<string,FreshRSS_Feed> */
  178. $existingFeeds = [];
  179. foreach ($this->feeds() as $existingFeed) {
  180. $existingFeeds[$existingFeed->url()] = $existingFeed;
  181. if (empty($dryRunFeeds[$existingFeed->url()])) {
  182. // The feed does not exist in the new dynamic OPML, so mute (disable) that feed
  183. $existingFeed->_mute(true);
  184. $ok &= ($feedDAO->updateFeed($existingFeed->id(), [
  185. 'ttl' => $existingFeed->ttl(true),
  186. ]) !== false);
  187. }
  188. }
  189. foreach ($dryRunCategory->feeds() as $dryRunFeed) {
  190. if (empty($existingFeeds[$dryRunFeed->url()])) {
  191. // The feed does not exist in the current category, so add that feed
  192. $dryRunFeed->_category($this);
  193. $ok &= ($feedDAO->addFeedObject($dryRunFeed) !== false);
  194. $existingFeeds[$dryRunFeed->url()] = $dryRunFeed;
  195. } else {
  196. $existingFeed = $existingFeeds[$dryRunFeed->url()];
  197. if ($existingFeed->mute()) {
  198. // The feed already exists in the current category but was muted (disabled), so unmute (enable) again
  199. $existingFeed->_mute(false);
  200. $ok &= ($feedDAO->updateFeed($existingFeed->id(), [
  201. 'ttl' => $existingFeed->ttl(true),
  202. ]) !== false);
  203. }
  204. }
  205. }
  206. } else {
  207. $ok = false;
  208. Minz_Log::warning('Error loading dynamic OPML for category ' . $this->id() . '! ' .
  209. \SimplePie\Misc::url_remove_credentials($url));
  210. }
  211. }
  212. $catDAO = FreshRSS_Factory::createCategoryDao();
  213. $catDAO->updateLastUpdate($this->id(), !$ok);
  214. return (bool)$ok;
  215. }
  216. private function sortFeeds(): void {
  217. if ($this->feeds === null) {
  218. return;
  219. }
  220. uasort($this->feeds, static fn(FreshRSS_Feed $a, FreshRSS_Feed $b) => strnatcasecmp($a->name(), $b->name()));
  221. }
  222. /**
  223. * Access cached feed
  224. * @param array<FreshRSS_Category> $categories
  225. */
  226. public static function findFeed(array $categories, int $feed_id): ?FreshRSS_Feed {
  227. foreach ($categories as $category) {
  228. foreach ($category->feeds() as $feed) {
  229. if ($feed->id() === $feed_id) {
  230. $feed->_category($category); // Should already be done; just to be safe
  231. return $feed;
  232. }
  233. }
  234. }
  235. return null;
  236. }
  237. /**
  238. * Access cached feeds
  239. * @param array<FreshRSS_Category> $categories
  240. * @return array<int,FreshRSS_Feed>
  241. */
  242. public static function findFeeds(array $categories): array {
  243. $result = [];
  244. foreach ($categories as $category) {
  245. foreach ($category->feeds() as $feed) {
  246. $result[$feed->id()] = $feed;
  247. }
  248. }
  249. return $result;
  250. }
  251. /**
  252. * @param array<FreshRSS_Category> $categories
  253. */
  254. public static function countUnread(array $categories, int $minPriority = 0): int {
  255. $n = 0;
  256. foreach ($categories as $category) {
  257. foreach ($category->feeds() as $feed) {
  258. if ($feed->priority() >= $minPriority) {
  259. $n += $feed->nbNotRead();
  260. }
  261. }
  262. }
  263. return $n;
  264. }
  265. }