From 4bd503591469f47e710f1afbf0b5883f7770065d Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Wed, 17 Dec 2025 10:07:52 +0100 Subject: [PATCH] Rework encoding of search filters (#8324) Rework: * https://github.com/FreshRSS/FreshRSS/pull/8222 now that we have: * https://github.com/FreshRSS/FreshRSS/pull/8293 Follow-up of: * https://github.com/FreshRSS/FreshRSS/pull/8311 * More simplification * Deprecate getRawInput --- app/Controllers/categoryController.php | 2 +- app/Controllers/configureController.php | 4 ++-- app/Controllers/indexController.php | 2 +- app/Controllers/subscriptionController.php | 17 +++++++++++------ app/Models/BooleanSearch.php | 1 + app/Models/FilterAction.php | 4 ++-- app/Models/FilterActionsTrait.php | 5 ++--- app/Models/Search.php | 1 + app/Models/UserQuery.php | 4 ++-- app/layout/nav_menu.phtml | 6 +++--- app/views/configure/queries.phtml | 4 ++-- app/views/configure/reading.phtml | 4 ++-- app/views/helpers/category/update.phtml | 2 +- app/views/helpers/configure/query.phtml | 2 +- app/views/helpers/export/opml.phtml | 2 +- app/views/helpers/feed/update.phtml | 2 +- app/views/helpers/stream-footer.phtml | 2 +- app/views/tag/update.phtml | 2 +- p/api/query.php | 2 +- tests/app/Models/SearchTest.php | 5 +++-- 20 files changed, 40 insertions(+), 33 deletions(-) diff --git a/app/Controllers/categoryController.php b/app/Controllers/categoryController.php index fd148bf05..9bea88a1f 100644 --- a/app/Controllers/categoryController.php +++ b/app/Controllers/categoryController.php @@ -110,7 +110,7 @@ class FreshRSS_category_Controller extends FreshRSS_ActionController { $category->_attribute('read_when_same_title_in_category', null); } - $category->_filtersAction('read', Minz_Request::paramTextToArray('filteractions_read')); // Keep as HTML + $category->_filtersAction('read', Minz_Request::paramTextToArray('filteractions_read', plaintext: true)); if (Minz_Request::paramBoolean('use_default_purge_options')) { $category->_attribute('archiving', null); diff --git a/app/Controllers/configureController.php b/app/Controllers/configureController.php index 8bc0d08c4..8b42a1441 100644 --- a/app/Controllers/configureController.php +++ b/app/Controllers/configureController.php @@ -164,8 +164,8 @@ class FreshRSS_configure_Controller extends FreshRSS_ActionController { 'site' => Minz_Request::paramBoolean('mark_open_site'), 'focus' => Minz_Request::paramBoolean('mark_focus'), ]; - FreshRSS_Context::userConf()->_filtersAction('read', Minz_Request::paramTextToArray('filteractions_read')); // Keep as HTML - FreshRSS_Context::userConf()->_filtersAction('star', Minz_Request::paramTextToArray('filteractions_star')); // Keep as HTML + FreshRSS_Context::userConf()->_filtersAction('read', Minz_Request::paramTextToArray('filteractions_read', plaintext: true)); + FreshRSS_Context::userConf()->_filtersAction('star', Minz_Request::paramTextToArray('filteractions_star', plaintext: true)); FreshRSS_Context::userConf()->save(); invalidateHttpCache(); diff --git a/app/Controllers/indexController.php b/app/Controllers/indexController.php index c51dd6662..7def8e781 100644 --- a/app/Controllers/indexController.php +++ b/app/Controllers/indexController.php @@ -94,7 +94,7 @@ class FreshRSS_index_Controller extends FreshRSS_ActionController { }; $searchString = $operator . ':' . ($offset < 0 ? '/' : '') . date('Y-m-d', $timestamp + ($offset * 86400)) . ($offset > 0 ? '/' : ''); return Minz_Url::display(Minz_Request::modifiedCurrentRequest([ - 'search' => FreshRSS_Context::$search->getRawInput() === '' ? $searchString : + 'search' => FreshRSS_Context::$search->__toString() === '' ? $searchString : FreshRSS_Context::$search->enforce(new FreshRSS_Search($searchString))->__toString(), ])); } diff --git a/app/Controllers/subscriptionController.php b/app/Controllers/subscriptionController.php index ad9b05787..02942583f 100644 --- a/app/Controllers/subscriptionController.php +++ b/app/Controllers/subscriptionController.php @@ -247,7 +247,7 @@ class FreshRSS_subscription_Controller extends FreshRSS_ActionController { ]); } - $feed->_filtersAction('read', Minz_Request::paramTextToArray('filteractions_read')); // Keep as HTML + $feed->_filtersAction('read', Minz_Request::paramTextToArray('filteractions_read', plaintext: true)); $feed->_kind(Minz_Request::paramInt('feed_kind') ?: FreshRSS_Feed::KIND_RSS); if ($feed->kind() === FreshRSS_Feed::KIND_HTML_XPATH || $feed->kind() === FreshRSS_Feed::KIND_XML_XPATH) { @@ -418,17 +418,22 @@ class FreshRSS_subscription_Controller extends FreshRSS_ActionController { $filteractions = Minz_Request::paramTextToArray('filteractions_read', plaintext: true); $filteractions = array_map(fn(string $action): string => trim($action), $filteractions); $filteractions = array_filter($filteractions, fn(string $action): bool => $action !== ''); - $search = "f:$id ("; + $actionsSearch = new FreshRSS_BooleanSearch('', operator: 'AND'); foreach ($filteractions as $action) { - $search .= "($action) OR "; + $actionSearch = new FreshRSS_BooleanSearch($action, operator: 'OR'); + if ($actionSearch->__toString() === '') { + continue; + } + $actionsSearch->add($actionSearch); } - $search = preg_replace('/ OR $/', '', $search); - $search .= ')'; + $search = new FreshRSS_BooleanSearch(''); + $search->add(new FreshRSS_Search("f:$id")); + $search->add($actionsSearch); Minz_Request::forward([ 'c' => 'index', 'a' => 'index', 'params' => [ - 'search' => $search, + 'search' => $search->__toString(), ], ], redirect: true); } diff --git a/app/Models/BooleanSearch.php b/app/Models/BooleanSearch.php index e959f7af9..6be189cda 100644 --- a/app/Models/BooleanSearch.php +++ b/app/Models/BooleanSearch.php @@ -525,6 +525,7 @@ class FreshRSS_BooleanSearch implements \Stringable { } /** @return string Plain text search query. Must be XML-encoded or URL-encoded depending on the situation */ + #[Deprecated('Use __tostring() instead')] public function getRawInput(): string { return $this->raw_input; } diff --git a/app/Models/FilterAction.php b/app/Models/FilterAction.php index 56c182904..5d11d26db 100644 --- a/app/Models/FilterAction.php +++ b/app/Models/FilterAction.php @@ -29,11 +29,11 @@ class FreshRSS_FilterAction { } } - /** @return array{'search'?:string,'actions'?:array} */ + /** @return array{search?:string,actions?:array} */ public function toJSON(): array { if (is_array($this->actions) && $this->booleanSearch != null) { return [ - 'search' => $this->booleanSearch->getRawInput(), + 'search' => $this->booleanSearch->__toString(), 'actions' => $this->actions, ]; } diff --git a/app/Models/FilterActionsTrait.php b/app/Models/FilterActionsTrait.php index 3c36343d6..819f2d975 100644 --- a/app/Models/FilterActionsTrait.php +++ b/app/Models/FilterActionsTrait.php @@ -71,8 +71,7 @@ trait FreshRSS_FilterActionsTrait { //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()) == '') { + if ($filterAction === null || !is_array($filterAction->actions()) || $filterAction->booleanSearch()->__toString() === '') { array_splice($filterActions, $i, 1); continue; } @@ -86,7 +85,7 @@ trait FreshRSS_FilterActionsTrait { //Update existing filter with new action for ($k = count($filters) - 1; $k >= 0; $k--) { $filter = $filters[$k]; - if ($filter === $filterAction->booleanSearch()->getRawInput()) { + if ($filter === $filterAction->booleanSearch()->__toString()) { $actions[] = $action; array_splice($filters, $k, 1); } diff --git a/app/Models/Search.php b/app/Models/Search.php index 201fed9f9..046f61a2b 100644 --- a/app/Models/Search.php +++ b/app/Models/Search.php @@ -452,6 +452,7 @@ class FreshRSS_Search implements \Stringable { return trim($result); } + #[Deprecated('Use __tostring() instead')] public function getRawInput(): string { return $this->raw_input; } diff --git a/app/Models/UserQuery.php b/app/Models/UserQuery.php index 6050436f4..154f4d92c 100644 --- a/app/Models/UserQuery.php +++ b/app/Models/UserQuery.php @@ -123,7 +123,7 @@ class FreshRSS_UserQuery { 'get' => $this->get, 'name' => $this->name, 'order' => $this->order, - 'search' => $this->search->getRawInput(), + 'search' => $this->search->__toString(), 'state' => $this->state, 'url' => $this->url, 'token' => $this->token, @@ -221,7 +221,7 @@ class FreshRSS_UserQuery { * Check if there is a search in the search object */ public function hasSearch(): bool { - return $this->search->getRawInput() !== ''; + return $this->search->__toString() !== ''; } public function getGet(): string { diff --git a/app/layout/nav_menu.phtml b/app/layout/nav_menu.phtml index 04565c9b9..3c3a34243 100644 --- a/app/layout/nav_menu.phtml +++ b/app/layout/nav_menu.phtml @@ -39,7 +39,7 @@ -