Category.php 7.7 KB

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