A free, self-hostable news aggregator…
Fix search with double-quote and parenthesis (#6818)
Alexandre Alapetite7d0e1b1a8482
diff --git a/app/Models/BooleanSearch.php b/app/Models/BooleanSearch.phpindex 9e9dec2a2..b8e3af5e0 100644--- a/app/Models/BooleanSearch.php+++ b/app/Models/BooleanSearch.php@@ -27,7 +27,7 @@ class FreshRSS_BooleanSearch {if (!is_string($input)) {return;}- $input = preg_replace('/(?<=[\s!-]|^)"(.*?)"/', '"\1"', $input);+ $input = preg_replace('/(?<=[\s(!-]|^)"(.*?)"/', '"\1"', $input);if (!is_string($input)) {return;}diff --git a/app/Models/Search.php b/app/Models/Search.phpindex 755cf6b59..0e2f5da6f 100644--- a/app/Models/Search.php+++ b/app/Models/Search.php@@ -341,7 +341,7 @@ class FreshRSS_Search {}private function parseNotEntryIds(string $input): string {- if (preg_match_all('/(?<=\\s|^)[!-]e:(?P<search>[0-9,]*)/', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-]e:(?P<search>[0-9,]*)/', $input, $matches)) {$input = str_replace($matches[0], '', $input);$ids_lists = $matches['search'];$this->not_entry_ids = [];@@ -375,7 +375,7 @@ class FreshRSS_Search {}private function parseNotFeedIds(string $input): string {- if (preg_match_all('/(?<=\\s|^)[!-]f:(?P<search>[0-9,]*)/', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-]f:(?P<search>[0-9,]*)/', $input, $matches)) {$input = str_replace($matches[0], '', $input);$ids_lists = $matches['search'];$this->not_feed_ids = [];@@ -418,7 +418,7 @@ class FreshRSS_Search {}private function parseNotLabelIds(string $input): string {- if (preg_match_all('/(?<=\\s|^)[!-][lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-][lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {$input = str_replace($matches[0], '', $input);$ids_lists = $matches['search'];$this->not_label_ids = [];@@ -470,11 +470,11 @@ class FreshRSS_Search {*/private function parseNotLabelNames(string $input): string {$names_lists = [];- if (preg_match_all('/(?<=\\s|^)[!-]labels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-]labels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {$names_lists = $matches['search'];$input = str_replace($matches[0], '', $input);}- if (preg_match_all('/(?<=\\s|^)[!-]labels?:(?P<search>[^\\s"]*)/', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-]labels?:(?P<search>[^\\s"]*)/', $input, $matches)) {$names_lists = array_merge($names_lists, $matches['search']);$input = str_replace($matches[0], '', $input);}@@ -516,15 +516,15 @@ class FreshRSS_Search {}private function parseNotIntitleSearch(string $input): string {- if (preg_match_all('#(?<=\\s|^)[!-]intitle:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {+ if (preg_match_all('#(?<=[\\s(]|^)[!-]intitle:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {$this->not_intitle_regex = self::htmlspecialchars_decodes($matches['search']);$input = str_replace($matches[0], '', $input);}- if (preg_match_all('/(?<=\\s|^)[!-]intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-]intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {$this->not_intitle = $matches['search'];$input = str_replace($matches[0], '', $input);}- if (preg_match_all('/(?<=\\s|^)[!-]intitle:(?P<search>[^\s"]*)/', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-]intitle:(?P<search>[^\s"]*)/', $input, $matches)) {$this->not_intitle = array_merge($this->not_intitle ?: [], $matches['search']);$input = str_replace($matches[0], '', $input);}@@ -561,15 +561,15 @@ class FreshRSS_Search {}private function parseNotAuthorSearch(string $input): string {- if (preg_match_all('#(?<=\\s|^)[!-]author:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {+ if (preg_match_all('#(?<=[\\s(]|^)[!-]author:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {$this->not_author_regex = self::htmlspecialchars_decodes($matches['search']);$input = str_replace($matches[0], '', $input);}- if (preg_match_all('/(?<=\\s|^)[!-]author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-]author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {$this->not_author = $matches['search'];$input = str_replace($matches[0], '', $input);}- if (preg_match_all('/(?<=\\s|^)[!-]author:(?P<search>[^\s"]*)/', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-]author:(?P<search>[^\s"]*)/', $input, $matches)) {$this->not_author = array_merge($this->not_author ?: [], $matches['search']);$input = str_replace($matches[0], '', $input);}@@ -605,15 +605,15 @@ class FreshRSS_Search {}private function parseNotInurlSearch(string $input): string {- if (preg_match_all('#(?<=\\s|^)[!-]inurl:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {+ if (preg_match_all('#(?<=[\\s(]|^)[!-]inurl:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {$this->not_inurl_regex = self::htmlspecialchars_decodes($matches['search']);$input = str_replace($matches[0], '', $input);}- if (preg_match_all('/(?<=\\s|^)[!-]inurl:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-]inurl:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {$this->not_inurl = $matches['search'];$input = str_replace($matches[0], '', $input);}- if (preg_match_all('/(?<=\\s|^)[!-]inurl:(?P<search>[^\\s]*)/', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-]inurl:(?P<search>[^\\s]*)/', $input, $matches)) {$this->not_inurl = $matches['search'];$input = str_replace($matches[0], '', $input);}@@ -640,7 +640,7 @@ class FreshRSS_Search {}private function parseNotDateSearch(string $input): string {- if (preg_match_all('/(?<=\\s|^)[!-]date:(?P<search>[^\\s]*)/', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-]date:(?P<search>[^\\s]*)/', $input, $matches)) {$input = str_replace($matches[0], '', $input);$dates = self::removeEmptyValues($matches['search']);if (!empty($dates[0])) {@@ -667,7 +667,7 @@ class FreshRSS_Search {}private function parseNotPubdateSearch(string $input): string {- if (preg_match_all('/(?<=\\s|^)[!-]pubdate:(?P<search>[^\\s]*)/', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-]pubdate:(?P<search>[^\\s]*)/', $input, $matches)) {$input = str_replace($matches[0], '', $input);$dates = self::removeEmptyValues($matches['search']);if (!empty($dates[0])) {@@ -705,15 +705,15 @@ class FreshRSS_Search {}private function parseNotTagsSearch(string $input): string {- if (preg_match_all('%(?<=\\s|^)[!-]#(?P<search>/.*?(?<!\\\\)/[im]*)%', $input, $matches)) {+ if (preg_match_all('%(?<=[\\s(]|^)[!-]#(?P<search>/.*?(?<!\\\\)/[im]*)%', $input, $matches)) {$this->not_tags_regex = self::htmlspecialchars_decodes($matches['search']);$input = str_replace($matches[0], '', $input);}- if (preg_match_all('/(?<=\\s|^)[!-]#(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-]#(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {$this->not_tags = $matches['search'];$input = str_replace($matches[0], '', $input);}- if (preg_match_all('/(?<=\\s|^)[!-]#(?P<search>[^\\s]+)/', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-]#(?P<search>[^\\s]+)/', $input, $matches)) {$this->not_tags = $matches['search'];$input = str_replace($matches[0], '', $input);}@@ -736,12 +736,12 @@ class FreshRSS_Search {if ($input === '') {return '';}- if (preg_match_all('#(?<=\\s|^)(?<![!-\\\\])(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {+ if (preg_match_all('#(?<=[\\s(]|^)(?<![!-\\\\])(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {$this->search_regex = self::htmlspecialchars_decodes($matches['search']);//TODO: Replace all those str_replace with PREG_OFFSET_CAPTURE$input = str_replace($matches[0], '', $input);}- if (preg_match_all('/(?<![!-])(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)(?<![!-\\\\])(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {$this->search = $matches['search'];//TODO: Replace all those str_replace with PREG_OFFSET_CAPTURE$input = str_replace($matches[0], '', $input);@@ -771,11 +771,11 @@ class FreshRSS_Search {if ($input === '') {return '';}- if (preg_match_all('#(?<=\\s|^)[!-](?P<search>(?<!\\\\)/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {+ if (preg_match_all('#(?<=[\\s(]|^)[!-](?P<search>(?<!\\\\)/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {$this->not_search_regex = self::htmlspecialchars_decodes($matches['search']);$input = str_replace($matches[0], '', $input);}- if (preg_match_all('/(?<=\\s|^)[!-](?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-](?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {$this->not_search = $matches['search'];$input = str_replace($matches[0], '', $input);}@@ -783,7 +783,7 @@ class FreshRSS_Search {if ($input === '') {return '';}- if (preg_match_all('/(?<=\\s|^)[!-](?P<search>[^\\s]+)/', $input, $matches)) {+ if (preg_match_all('/(?<=[\\s(]|^)[!-](?P<search>[^\\s]+)/', $input, $matches)) {$this->not_search = array_merge(is_array($this->not_search) ? $this->not_search : [], $matches['search']);$input = str_replace($matches[0], '', $input);}diff --git a/tests/app/Models/SearchTest.php b/tests/app/Models/SearchTest.phpindex 58eb875e0..3092b43ca 100644--- a/tests/app/Models/SearchTest.php+++ b/tests/app/Models/SearchTest.php@@ -296,6 +296,7 @@ class SearchTest extends PHPUnit\Framework\TestCase {['!ab OR -cd', '(!ab) OR (-cd)'],['ab cd OR ef OR "gh ij"', '(ab cd) OR (ef) OR ("gh ij")'],['ab (!cd)', 'ab (!cd)'],+ ['"ab" (!"cd")', '"ab" (!"cd")'],];}@@ -321,6 +322,7 @@ class SearchTest extends PHPUnit\Framework\TestCase {['(ab (cd OR ef OR (gh))) OR ij', '(ab ((cd) OR (ef) OR (gh))) OR (ij)'],['(ab (!cd OR ef OR (gh))) OR ij', '(ab ((!cd) OR (ef) OR (gh))) OR (ij)'],['(ab !(cd OR ef OR !(gh))) OR ij', '(ab !((cd) OR (ef) OR !(gh))) OR (ij)'],+ ['"ab" OR (!"cd")', '("ab") OR (!"cd")'],];}@@ -440,6 +442,18 @@ class SearchTest extends PHPUnit\Framework\TestCase {'OR (((e.title LIKE ? OR e.content LIKE ?) )))) OR NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%'],],+ [+ '"ab" "cd" ("ef") intitle:"gh" !"ij" -"kl"',+ '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .+ 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? ))',+ ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']+ ],+ [+ '"ab" "cd" ("ef") intitle:"gh" !"ij" -"kl"',+ '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .+ 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? ))',+ ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']+ ],];}