Category.php 7.1 KB

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