Skip to content

A free, self-hostable news aggregator…

Fix “mark as read older than…” widening the active search (#9173)

TowyTowyfa8e8a4400fa
diff --git a/app/Models/BooleanSearch.php b/app/Models/BooleanSearch.php
index a0b19fe19..e79d0401f 100644
--- a/app/Models/BooleanSearch.php
+++ b/app/Models/BooleanSearch.php
@@ -432,8 +432,28 @@ class FreshRSS_BooleanSearch implements \Stringable {
return $this->operator;
}
- /** @param FreshRSS_BooleanSearch|FreshRSS_Search $search */
+ /**
+ * Wrap the existing searches in a single BooleanSearch if needed,
+ * so that another search can be added as an additional restriction (AND).
+ */
+ private function wrapSearches(): void {
+ if (count($this->searches) > 1 || (count($this->searches) > 0 && $this->searches[0] instanceof FreshRSS_Search)) {
+ $wrap = new FreshRSS_BooleanSearch('');
+ foreach ($this->searches as $existingSearch) {
+ $wrap->add($existingSearch);
+ }
+ if (count($wrap->searches) > 0) {
+ $this->searches = [$wrap];
+ }
+ }
+ }
+
+ /**
+ * Add a search at the beginning of the Boolean expression, as an additional restriction (AND).
+ * @param FreshRSS_BooleanSearch|FreshRSS_Search $search
+ */
public function prepend(FreshRSS_BooleanSearch|FreshRSS_Search $search): void {
+ $this->wrapSearches();
array_unshift($this->searches, $search);
}
@@ -472,16 +492,7 @@ class FreshRSS_BooleanSearch implements \Stringable {
}
}
- if (count($result->searches) > 1 || (count($result->searches) > 0 && $result->searches[0] instanceof FreshRSS_Search)) {
- // Wrap the existing searches in a new BooleanSearch if needed
- $wrap = new FreshRSS_BooleanSearch('');
- foreach ($result->searches as $existingSearch) {
- $wrap->add($existingSearch);
- }
- if (count($wrap->searches) > 0) {
- $result->searches = [$wrap];
- }
- }
+ $result->wrapSearches();
array_unshift($result->searches, $search);
return $result;
}
diff --git a/app/Models/EntryDAO.php b/app/Models/EntryDAO.php
index d75e48f52..c9503e4f0 100644
--- a/app/Models/EntryDAO.php
+++ b/app/Models/EntryDAO.php
@@ -970,7 +970,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
if ($filterSearch !== '') {
if ($search !== '') {
- $search .= $filter->operator();
+ $search = rtrim($search) . ' ' . $filter->operator();
} elseif (in_array($filter->operator(), ['AND NOT', 'OR NOT'], true)) {
// Special case if we start with a negation (there is already the default AND before)
$search .= ' NOT';
diff --git a/tests/app/Models/BooleanSearchTest.php b/tests/app/Models/BooleanSearchTest.php
new file mode 100644
index 000000000..1d7ebeeb1
--- /dev/null
+++ b/tests/app/Models/BooleanSearchTest.php
@@ -0,0 +1,33 @@
+<?php
+declare(strict_types=1);
+
+use PHPUnit\Framework\Attributes\DataProvider;
+
+final class BooleanSearchTest extends \PHPUnit\Framework\TestCase {
+
+ /**
+ * `FreshRSS_BooleanSearch::prepend()` is used to restrict an existing search with an extra condition,
+ * such as the maximum publication date of the “mark as read → articles older than one day/week” action.
+ * @return list<array{string,string,list<string|int>}>
+ */
+ public static function providePrependMaxPubdate(): array {
+ return [
+ ['', '(e.date <= ?)', [1700000000]],
+ ['intitle:sale', '(e.date <= ?) AND ((e.title LIKE ?))', [1700000000, '%sale%']],
+ ['intitle:a OR intitle:b', '(e.date <= ?) AND ((e.title LIKE ?) OR (e.title LIKE ?))', [1700000000, '%a%', '%b%']],
+ ];
+ }
+
+ /** @param list<string|int> $expectedValues */
+ #[DataProvider('providePrependMaxPubdate')]
+ public function test_prepend_restrictsTheSearchInsteadOfWideningIt(string $input, string $expectedSql, array $expectedValues): void {
+ $booleanSearch = new FreshRSS_BooleanSearch($input);
+ $maxPubdate = new FreshRSS_Search('');
+ $maxPubdate->setMaxPubdate(1700000000);
+ $booleanSearch->prepend($maxPubdate);
+
+ [$values, $sql] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', $booleanSearch);
+ self::assertSame($expectedSql, trim($sql));
+ self::assertSame($expectedValues, $values);
+ }
+}