From aa07582419998cfa82465c28559f2fa1bba15ee3 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Wed, 23 Nov 2022 22:41:00 +0100 Subject: Better restrict tag search (#4882) * Better restrict tag search #fix https://github.com/FreshRSS/FreshRSS/issues/4877 Search only on full tag names and not on parts of tag names * Better whitespace handling --- tests/app/Models/SearchTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/app/Models/SearchTest.php') diff --git a/tests/app/Models/SearchTest.php b/tests/app/Models/SearchTest.php index a2832c710..fe686e7ba 100644 --- a/tests/app/Models/SearchTest.php +++ b/tests/app/Models/SearchTest.php @@ -318,17 +318,17 @@ class SearchTest extends PHPUnit\Framework\TestCase { ], [ '#tag Hello OR (author:Alice inurl:example) OR (f:3 intitle:World) OR L:12', - ' ((e.tags LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) OR ((e.author LIKE ? AND e.link LIKE ? )) OR' . + " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) OR ((e.author LIKE ? AND e.link LIKE ? )) OR" . ' ((e.id_feed IN (?) AND e.title LIKE ? )) OR ((e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) )) ', - ['%tag%','%Hello%', '%Hello%', '%Alice%', '%example%', '3', '%World%', '12'] + ['%tag #%','%Hello%', '%Hello%', '%Alice%', '%example%', '3', '%World%', '12'] ], [ '#tag Hello (author:Alice inurl:example) (f:3 intitle:World) label:Bleu', - ' ((e.tags LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) AND' . + " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) AND" . ' ((e.author LIKE ? AND e.link LIKE ? )) AND' . ' ((e.id_feed IN (?) AND e.title LIKE ? )) AND' . ' ((e.id IN (SELECT et.id_entry FROM `_entrytag` et, `_tag` t WHERE et.id_tag = t.id AND t.name IN (?)) )) ', - ['%tag%', '%Hello%', '%Hello%', '%Alice%', '%example%', '3', '%World%', 'Bleu'] + ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', '3', '%World%', 'Bleu'] ], [ '!((author:Alice intitle:hello) OR (author:Bob intitle:world))', -- cgit v1.2.3 From 3fb8ab8eb5c88042320bbe006825b21f5a8f21de Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Mon, 9 Jan 2023 12:59:09 +0100 Subject: Handling of parentheses as special characters in searches (#4989) #fix https://github.com/FreshRSS/FreshRSS/issues/4987 --- app/Models/BooleanSearch.php | 8 +++++--- docs/en/users/03_Main_view.md | 2 ++ docs/fr/users/03_Main_view.md | 2 ++ tests/app/Models/SearchTest.php | 5 +++++ 4 files changed, 14 insertions(+), 3 deletions(-) (limited to 'tests/app/Models/SearchTest.php') diff --git a/app/Models/BooleanSearch.php b/app/Models/BooleanSearch.php index b1c7bbd3b..279040a5a 100644 --- a/app/Models/BooleanSearch.php +++ b/app/Models/BooleanSearch.php @@ -118,8 +118,9 @@ class FreshRSS_BooleanSearch { $nextOperator = 'AND'; while ($i < $length) { $c = $input[$i]; + $backslashed = $i >= 1 ? $input[$i - 1] === '\\' : false; - if ($c === '(') { + if ($c === '(' && !$backslashed) { $hasParenthesis = true; $before = trim($before); @@ -164,11 +165,12 @@ class FreshRSS_BooleanSearch { $i++; while ($i < $length) { $c = $input[$i]; - if ($c === '(') { + $backslashed = $input[$i - 1] === '\\'; + if ($c === '(' && !$backslashed) { // One nested level deeper $parentheses++; $sub .= $c; - } elseif ($c === ')') { + } elseif ($c === ')' && !$backslashed) { $parentheses--; if ($parentheses === 0) { // Found the matching closing parenthesis diff --git a/docs/en/users/03_Main_view.md b/docs/en/users/03_Main_view.md index eb8fe0f01..d940774f5 100644 --- a/docs/en/users/03_Main_view.md +++ b/docs/en/users/03_Main_view.md @@ -247,6 +247,8 @@ Finally, parentheses may be used to express more complex queries, with basic neg * `(author:Alice intitle:hello) !(author:Bob intitle:world)` * `!(S:1 OR S:2)` +> ℹ️ If you need to search for a parenthesis, it needs to be escaped like `\(` or `\)` + ### By sorting by date You can change the sort order by clicking the toggle button available in the header. diff --git a/docs/fr/users/03_Main_view.md b/docs/fr/users/03_Main_view.md index 3a65c1f7f..3ca3b907c 100644 --- a/docs/fr/users/03_Main_view.md +++ b/docs/fr/users/03_Main_view.md @@ -275,3 +275,5 @@ Enfin, les parenthèses peuvent être utilisées pour des expressions plus compl * `!((author:Alice intitle:bonjour) OR (author:Bob intitle:monde))` * `(author:Alice intitle:bonjour) !(author:Bob intitle:monde)` * `!(S:1 OR S:2)` + +> ℹ️ Si vous devez chercher une parenthèse, elle doit être *échappée* comme suit : `\(` ou `\)` diff --git a/tests/app/Models/SearchTest.php b/tests/app/Models/SearchTest.php index fe686e7ba..52c10244d 100644 --- a/tests/app/Models/SearchTest.php +++ b/tests/app/Models/SearchTest.php @@ -339,6 +339,11 @@ class SearchTest extends PHPUnit\Framework\TestCase { '(author:Alice intitle:hello) !(author:Bob intitle:world)', ' ((e.author LIKE ? AND e.title LIKE ? )) AND NOT ((e.author LIKE ? AND e.title LIKE ? )) ', ['%Alice%', '%hello%', '%Bob%', '%world%'], + ], + [ + 'intitle:"\\(test\\)"', + '(e.title LIKE ? )', + ['%\\(test\\)%'], ] ]; } -- cgit v1.2.3