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