Category.php 8.2 KB

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