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