Category.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_Category extends Minz_Model {
  4. /**
  5. * Normal
  6. */
  7. public const KIND_NORMAL = 0;
  8. /**
  9. * Category tracking a third-party Dynamic OPML
  10. */
  11. public const KIND_DYNAMIC_OPML = 2;
  12. private int $id = 0;
  13. private int $kind = 0;
  14. private string $name;
  15. private int $nbFeeds = -1;
  16. private int $nbNotRead = -1;
  17. /** @var array<FreshRSS_Feed>|null */
  18. private ?array $feeds = null;
  19. /** @var bool|int */
  20. private $hasFeedsWithError = false;
  21. /** @var array<string,mixed> */
  22. private array $attributes = [];
  23. private int $lastUpdate = 0;
  24. private bool $error = false;
  25. /**
  26. * @param array<FreshRSS_Feed>|null $feeds
  27. */
  28. public function __construct(string $name = '', ?array $feeds = null) {
  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. $this->nbFeeds++;
  36. $this->nbNotRead += $feed->nbNotRead();
  37. $this->hasFeedsWithError |= $feed->inError();
  38. }
  39. }
  40. }
  41. public function id(): int {
  42. return $this->id;
  43. }
  44. public function kind(): int {
  45. return $this->kind;
  46. }
  47. /** @return string HTML-encoded name of the category */
  48. public function name(): string {
  49. return $this->name;
  50. }
  51. public function lastUpdate(): int {
  52. return $this->lastUpdate;
  53. }
  54. public function _lastUpdate(int $value): void {
  55. $this->lastUpdate = $value;
  56. }
  57. public function inError(): bool {
  58. return $this->error;
  59. }
  60. /** @param bool|int $value */
  61. public function _error($value): void {
  62. $this->error = (bool)$value;
  63. }
  64. public function isDefault(): bool {
  65. return $this->id == FreshRSS_CategoryDAO::DEFAULTCATEGORYID;
  66. }
  67. public function nbFeeds(): int {
  68. if ($this->nbFeeds < 0) {
  69. $catDAO = FreshRSS_Factory::createCategoryDao();
  70. $this->nbFeeds = $catDAO->countFeed($this->id());
  71. }
  72. return $this->nbFeeds;
  73. }
  74. /**
  75. * @throws Minz_ConfigurationNamespaceException
  76. * @throws Minz_PDOConnectionException
  77. */
  78. public function nbNotRead(): int {
  79. if ($this->nbNotRead < 0) {
  80. $catDAO = FreshRSS_Factory::createCategoryDao();
  81. $this->nbNotRead = $catDAO->countNotRead($this->id());
  82. }
  83. return $this->nbNotRead;
  84. }
  85. /**
  86. * @return array<FreshRSS_Feed>
  87. * @throws Minz_ConfigurationNamespaceException
  88. * @throws Minz_PDOConnectionException
  89. */
  90. public function feeds(): array {
  91. if ($this->feeds === null) {
  92. $feedDAO = FreshRSS_Factory::createFeedDao();
  93. $this->feeds = $feedDAO->listByCategory($this->id());
  94. $this->nbFeeds = 0;
  95. $this->nbNotRead = 0;
  96. foreach ($this->feeds as $feed) {
  97. $this->nbFeeds++;
  98. $this->nbNotRead += $feed->nbNotRead();
  99. $this->hasFeedsWithError |= $feed->inError();
  100. }
  101. $this->sortFeeds();
  102. }
  103. return $this->feeds ?? [];
  104. }
  105. public function hasFeedsWithError(): bool {
  106. return (bool)($this->hasFeedsWithError);
  107. }
  108. /**
  109. * @phpstan-return ($key is non-empty-string ? mixed : array<string,mixed>)
  110. * @return array<string,mixed>|mixed|null
  111. */
  112. public function attributes(string $key = '') {
  113. if ($key === '') {
  114. return $this->attributes;
  115. } else {
  116. return $this->attributes[$key] ?? null;
  117. }
  118. }
  119. public function _id(int $id): void {
  120. $this->id = $id;
  121. if ($id === FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
  122. $this->_name(_t('gen.short.default_category'));
  123. }
  124. }
  125. public function _kind(int $kind): void {
  126. $this->kind = $kind;
  127. }
  128. public function _name(string $value): void {
  129. $this->name = mb_strcut(trim($value), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8');
  130. }
  131. /** @param array<FreshRSS_Feed>|FreshRSS_Feed $values */
  132. public function _feeds($values): void {
  133. if (!is_array($values)) {
  134. $values = [$values];
  135. }
  136. $this->feeds = $values;
  137. $this->sortFeeds();
  138. }
  139. /**
  140. * To manually add feeds to this category (not committing to database).
  141. */
  142. public function addFeed(FreshRSS_Feed $feed): void {
  143. if ($this->feeds === null) {
  144. $this->feeds = [];
  145. }
  146. $this->feeds[] = $feed;
  147. $this->sortFeeds();
  148. }
  149. /** @param string|array<mixed>|bool|int|null $value Value, not HTML-encoded */
  150. public function _attributes(string $key, $value): void {
  151. if ('' === $key) {
  152. if (is_string($value)) {
  153. $value = json_decode($value, true);
  154. }
  155. if (is_array($value)) {
  156. $this->attributes = $value;
  157. }
  158. } elseif (null === $value) {
  159. unset($this->attributes[$key]);
  160. } else {
  161. $this->attributes[$key] = $value;
  162. }
  163. }
  164. /**
  165. * @param array<string> $attributes
  166. * @throws FreshRSS_Context_Exception
  167. */
  168. public static function cacheFilename(string $url, array $attributes): string {
  169. $simplePie = customSimplePie($attributes);
  170. $filename = $simplePie->get_cache_filename($url);
  171. return CACHE_PATH . '/' . $filename . '.opml.xml';
  172. }
  173. public function refreshDynamicOpml(): bool {
  174. $url = $this->attributes('opml_url');
  175. if ($url == '') {
  176. return false;
  177. }
  178. $ok = true;
  179. $attributes = []; //TODO
  180. $cachePath = self::cacheFilename($url, $attributes);
  181. $opml = httpGet($url, $cachePath, 'opml', $attributes);
  182. if ($opml == '') {
  183. Minz_Log::warning('Error getting dynamic OPML for category ' . $this->id() . '! ' .
  184. SimplePie_Misc::url_remove_credentials($url));
  185. $ok = false;
  186. } else {
  187. $dryRunCategory = new FreshRSS_Category();
  188. $importService = new FreshRSS_Import_Service();
  189. $importService->importOpml($opml, $dryRunCategory, true);
  190. if ($importService->lastStatus()) {
  191. $feedDAO = FreshRSS_Factory::createFeedDao();
  192. /** @var array<string,FreshRSS_Feed> */
  193. $dryRunFeeds = [];
  194. foreach ($dryRunCategory->feeds() as $dryRunFeed) {
  195. $dryRunFeeds[$dryRunFeed->url()] = $dryRunFeed;
  196. }
  197. /** @var array<string,FreshRSS_Feed> */
  198. $existingFeeds = [];
  199. foreach ($this->feeds() as $existingFeed) {
  200. $existingFeeds[$existingFeed->url()] = $existingFeed;
  201. if (empty($dryRunFeeds[$existingFeed->url()])) {
  202. // The feed does not exist in the new dynamic OPML, so mute (disable) that feed
  203. $existingFeed->_mute(true);
  204. $ok &= ($feedDAO->updateFeed($existingFeed->id(), [
  205. 'ttl' => $existingFeed->ttl(true),
  206. ]) !== false);
  207. }
  208. }
  209. foreach ($dryRunCategory->feeds() as $dryRunFeed) {
  210. if (empty($existingFeeds[$dryRunFeed->url()])) {
  211. // The feed does not exist in the current category, so add that feed
  212. $dryRunFeed->_categoryId($this->id());
  213. $ok &= ($feedDAO->addFeedObject($dryRunFeed) !== false);
  214. } else {
  215. $existingFeed = $existingFeeds[$dryRunFeed->url()];
  216. if ($existingFeed->mute()) {
  217. // The feed already exists in the current category but was muted (disabled), so unmute (enable) again
  218. $existingFeed->_mute(false);
  219. $ok &= ($feedDAO->updateFeed($existingFeed->id(), [
  220. 'ttl' => $existingFeed->ttl(true),
  221. ]) !== false);
  222. }
  223. }
  224. }
  225. } else {
  226. $ok = false;
  227. Minz_Log::warning('Error loading dynamic OPML for category ' . $this->id() . '! ' .
  228. SimplePie_Misc::url_remove_credentials($url));
  229. }
  230. }
  231. $catDAO = FreshRSS_Factory::createCategoryDao();
  232. $catDAO->updateLastUpdate($this->id(), !$ok);
  233. return (bool)$ok;
  234. }
  235. private function sortFeeds(): void {
  236. if ($this->feeds === null) {
  237. return;
  238. }
  239. usort($this->feeds, static function (FreshRSS_Feed $a, FreshRSS_Feed $b) {
  240. return strnatcasecmp($a->name(), $b->name());
  241. });
  242. }
  243. }