diff options
| author | 2022-08-16 10:56:07 +0200 | |
|---|---|---|
| committer | 2022-08-16 10:56:07 +0200 | |
| commit | e27eb1ca9198119ea1b0bd79be5f1aead45d615a (patch) | |
| tree | fd4e2767ab4d65a68b437d77f57b9e6274a65b9d /app/Models | |
| parent | 8587efa62189a30e3e47075739382d52ecc34cb6 (diff) | |
Basic support for negative searches with parentheses (#4503)
* Basic support for negative searches with parentheses
* `!((author:Alice intitle:hello) OR (author:Bob intitle:world))`
* `(author:Alice intitle:hello) !(author:Bob intitle:world)`
* `!(S:1 OR S:2)`
* Minor documentation / comment
* Remove syslog debug line
Diffstat (limited to 'app/Models')
| -rw-r--r-- | app/Models/BooleanSearch.php | 17 | ||||
| -rw-r--r-- | app/Models/Entry.php | 6 | ||||
| -rw-r--r-- | app/Models/EntryDAO.php | 3 |
3 files changed, 22 insertions, 4 deletions
diff --git a/app/Models/BooleanSearch.php b/app/Models/BooleanSearch.php index 332757556..b1c7bbd3b 100644 --- a/app/Models/BooleanSearch.php +++ b/app/Models/BooleanSearch.php @@ -10,7 +10,7 @@ class FreshRSS_BooleanSearch { /** @var array<FreshRSS_BooleanSearch|FreshRSS_Search> */ private $searches = array(); - /** @var string 'AND' or 'OR' */ + /** @var string 'AND' or 'OR' or 'AND NOT' */ private $operator; public function __construct(string $input, int $level = 0, $operator = 'AND') { @@ -123,7 +123,20 @@ class FreshRSS_BooleanSearch { $hasParenthesis = true; $before = trim($before); - if (preg_match('/\bOR$/i', $before)) { + if (preg_match('/[!-]$/i', $before)) { + // Trim trailing negation + $before = substr($before, 0, -1); + + // The text prior to the negation is a BooleanSearch + $searchBefore = new FreshRSS_BooleanSearch($before, $level + 1, $nextOperator); + if (count($searchBefore->searches()) > 0) { + $this->searches[] = $searchBefore; + } + $before = ''; + + // The next BooleanSearch will have to be combined with AND NOT instead of default AND + $nextOperator = 'AND NOT'; + } elseif (preg_match('/\bOR$/i', $before)) { // Trim trailing OR $before = substr($before, 0, -2); diff --git a/app/Models/Entry.php b/app/Models/Entry.php index d20f5f2a7..8d20e5412 100644 --- a/app/Models/Entry.php +++ b/app/Models/Entry.php @@ -364,10 +364,12 @@ class FreshRSS_Entry extends Minz_Model { $ok = true; foreach ($booleanSearch->searches() as $filter) { if ($filter instanceof FreshRSS_BooleanSearch) { - // BooleanSearches are combined by AND (default) or OR (special case) operator and are recursive + // BooleanSearches are combined by AND (default) or OR or AND NOT (special cases) operators and are recursive if ($filter->operator() === 'OR') { $ok |= $this->matches($filter); - } else { + } elseif ($filter->operator() === 'AND NOT') { + $ok &= !$this->matches($filter); + } else { // AND $ok &= $this->matches($filter); } } elseif ($filter instanceof FreshRSS_Search) { diff --git a/app/Models/EntryDAO.php b/app/Models/EntryDAO.php index d69702c60..02552affe 100644 --- a/app/Models/EntryDAO.php +++ b/app/Models/EntryDAO.php @@ -770,6 +770,9 @@ SQL; if ($filterSearch !== '') { if ($search !== '') { $search .= $filter->operator(); + } elseif ($filter->operator() === 'AND NOT') { + // Special case if we start with a negation (there is already the default AND before) + $search .= ' NOT'; } $search .= ' (' . $filterSearch . ') '; $values = array_merge($values, $filterValues); |
