From 3d76a8bd0943c9f451c6b127918fdadd278599de Mon Sep 17 00:00:00 2001 From: rxu Date: Wed, 7 May 2025 00:01:05 +0700 Subject: [PATCH] [ticket/17491] Fix rows duplication in search results PHPBB-17491 --- phpBB/phpbb/search/fulltext_mysql.php | 2 ++ phpBB/phpbb/search/fulltext_native.php | 2 ++ phpBB/phpbb/search/fulltext_postgres.php | 2 ++ tests/functional/search/base.php | 3 ++- 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/search/fulltext_mysql.php b/phpBB/phpbb/search/fulltext_mysql.php index e839ef86eb..30fe6a87b3 100644 --- a/phpBB/phpbb/search/fulltext_mysql.php +++ b/phpBB/phpbb/search/fulltext_mysql.php @@ -832,6 +832,8 @@ class fulltext_mysql extends \phpbb\search\base // Build the query for really selecting the post_ids if ($type == 'posts') { + // For sorting by non-unique columns, add unique sort key to avoid duplicated rows in results + $sql_sort .= ', p.post_id' . (($sort_dir == 'a') ? ' ASC' : ' DESC'); $sql = "SELECT $sql_select FROM " . $sql_sort_table . POSTS_TABLE . ' p' . (($firstpost_only) ? ', ' . TOPICS_TABLE . ' t ' : ' ') . " WHERE $sql_author diff --git a/phpBB/phpbb/search/fulltext_native.php b/phpBB/phpbb/search/fulltext_native.php index 8df80d2772..f15f58ab97 100644 --- a/phpBB/phpbb/search/fulltext_native.php +++ b/phpBB/phpbb/search/fulltext_native.php @@ -1251,6 +1251,8 @@ class fulltext_native extends \phpbb\search\base // Build the query for really selecting the post_ids if ($type == 'posts') { + // For sorting by non-unique columns, add unique sort key to avoid duplicated rows in results + $sql_sort .= ', p.post_id' . (($sort_dir == 'a') ? ' ASC' : ' DESC'); $sql = "SELECT $select FROM " . $sql_sort_table . POSTS_TABLE . ' p' . (($firstpost_only) ? ', ' . TOPICS_TABLE . ' t' : '') . " WHERE $sql_author diff --git a/phpBB/phpbb/search/fulltext_postgres.php b/phpBB/phpbb/search/fulltext_postgres.php index be2b7f704c..18a6321e8e 100644 --- a/phpBB/phpbb/search/fulltext_postgres.php +++ b/phpBB/phpbb/search/fulltext_postgres.php @@ -764,6 +764,8 @@ class fulltext_postgres extends \phpbb\search\base // Build the query for really selecting the post_ids if ($type == 'posts') { + // For sorting by non-unique columns, add unique sort key to avoid duplicated rows in results + $sql_sort .= ', p.post_id' . (($sort_dir == 'a') ? ' ASC' : ' DESC'); $sql = "SELECT p.post_id FROM " . $sql_sort_table . POSTS_TABLE . ' p' . (($firstpost_only) ? ', ' . TOPICS_TABLE . ' t ' : ' ') . " WHERE $sql_author diff --git a/tests/functional/search/base.php b/tests/functional/search/base.php index a937fc62da..e485d046a1 100644 --- a/tests/functional/search/base.php +++ b/tests/functional/search/base.php @@ -300,7 +300,8 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case ); // Browse the rest of search results pages with new sort direction - foreach (range(2, $last_page) as $page_number) + $pages = range(2, $last_page); + foreach ($pages as $page_number) { $crawler = self::$client->click($pagination->selectLink($page_number)->link()); $pagination = $crawler->filter('.pagination')->eq(0);