Category.php 7.1 KB

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