FilterActionsTrait.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Logic to apply filter actions (for feeds, categories, user configuration...).
  5. */
  6. trait FreshRSS_FilterActionsTrait {
  7. /** @var array<FreshRSS_FilterAction>|null $filterActions */
  8. private ?array $filterActions = null;
  9. /**
  10. * @return array<FreshRSS_FilterAction>
  11. */
  12. private function filterActions(): array {
  13. if (empty($this->filterActions)) {
  14. $this->filterActions = [];
  15. $filters = $this->attributeArray('filters') ?? [];
  16. foreach ($filters as $filter) {
  17. $filterAction = FreshRSS_FilterAction::fromJSON($filter);
  18. if ($filterAction != null) {
  19. $this->filterActions[] = $filterAction;
  20. }
  21. }
  22. }
  23. return $this->filterActions;
  24. }
  25. /**
  26. * @param array<FreshRSS_FilterAction>|null $filterActions
  27. */
  28. private function _filterActions(?array $filterActions): void {
  29. $this->filterActions = $filterActions;
  30. if ($this->filterActions !== null && !empty($this->filterActions)) {
  31. $this->_attribute('filters', array_map(static function (?FreshRSS_FilterAction $af) {
  32. return $af == null ? null : $af->toJSON();
  33. }, $this->filterActions));
  34. } else {
  35. $this->_attribute('filters', null);
  36. }
  37. }
  38. /** @return array<FreshRSS_BooleanSearch> */
  39. public function filtersAction(string $action): array {
  40. $action = trim($action);
  41. if ($action == '') {
  42. return [];
  43. }
  44. $filters = [];
  45. $filterActions = $this->filterActions();
  46. for ($i = count($filterActions) - 1; $i >= 0; $i--) {
  47. $filterAction = $filterActions[$i];
  48. if ($filterAction != null && $filterAction->booleanSearch() != null &&
  49. $filterAction->actions() != null && in_array($action, $filterAction->actions(), true)) {
  50. $filters[] = $filterAction->booleanSearch();
  51. }
  52. }
  53. return $filters;
  54. }
  55. /**
  56. * @param array<string> $filters
  57. */
  58. public function _filtersAction(string $action, array $filters): void {
  59. $action = trim($action);
  60. if ($action === '') {
  61. return;
  62. }
  63. $filters = array_unique(array_map('trim', $filters), SORT_STRING);
  64. $filterActions = $this->filterActions();
  65. //Check existing filters
  66. for ($i = count($filterActions) - 1; $i >= 0; $i--) {
  67. $filterAction = $filterActions[$i];
  68. if ($filterAction == null || !is_array($filterAction->actions()) ||
  69. $filterAction->booleanSearch() == null || trim($filterAction->booleanSearch()->getRawInput()) == '') {
  70. array_splice($filterActions, $i, 1);
  71. continue;
  72. }
  73. $actions = $filterAction->actions();
  74. //Remove existing rules with same action
  75. for ($j = count($actions) - 1; $j >= 0; $j--) {
  76. if ($actions[$j] === $action) {
  77. array_splice($actions, $j, 1);
  78. }
  79. }
  80. //Update existing filter with new action
  81. for ($k = count($filters) - 1; $k >= 0; $k --) {
  82. $filter = $filters[$k];
  83. if ($filter === $filterAction->booleanSearch()->getRawInput()) {
  84. $actions[] = $action;
  85. array_splice($filters, $k, 1);
  86. }
  87. }
  88. //Save result
  89. if (empty($actions)) {
  90. array_splice($filterActions, $i, 1);
  91. } else {
  92. $filterAction->_actions($actions);
  93. }
  94. }
  95. //Add new filters
  96. for ($k = count($filters) - 1; $k >= 0; $k --) {
  97. $filter = $filters[$k];
  98. if ($filter != '') {
  99. $filterAction = FreshRSS_FilterAction::fromJSON([
  100. 'search' => $filter,
  101. 'actions' => [$action],
  102. ]);
  103. if ($filterAction != null) {
  104. $filterActions[] = $filterAction;
  105. }
  106. }
  107. }
  108. if (empty($filterActions)) {
  109. $filterActions = null;
  110. }
  111. $this->_filterActions($filterActions);
  112. }
  113. public function applyFilterActions(FreshRSS_Entry $entry): void {
  114. foreach ($this->filterActions() as $filterAction) {
  115. if ($entry->matches($filterAction->booleanSearch())) {
  116. foreach ($filterAction->actions() as $action) {
  117. switch ($action) {
  118. case 'read':
  119. if (!$entry->isRead()) {
  120. $entry->_isRead(true);
  121. Minz_ExtensionManager::callHook('entry_auto_read', $entry, 'filter');
  122. }
  123. break;
  124. case 'star':
  125. $entry->_isFavorite(true);
  126. break;
  127. case 'label':
  128. //TODO: Implement more actions
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }