Skip to content

Commit 25370b2

Browse files
committed
Merge branch 'ticket/17491' into ticket/17491-master
2 parents 9ae38d9 + 2dd4846 commit 25370b2

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

search/backend/base.php

+17-12
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,16 @@ protected function obtain_ids(string $search_key, int &$result_count, array &$id
112112
}
113113
}
114114

115-
// If the sort direction differs from the direction in the cache, then reverse the ids array
115+
// If the sort direction differs from the direction in the cache, then recalculate array keys
116116
if ($reverse_ids)
117117
{
118-
$stored_ids = array_reverse($stored_ids);
118+
$keys = array_keys($stored_ids);
119+
array_walk($keys, function (&$value, $key) use ($result_count)
120+
{
121+
$value = ($value >= 0) ? $result_count - $value - 1 : $value;
122+
}
123+
);
124+
$stored_ids = array_combine($keys, $stored_ids);
119125
}
120126

121127
for ($i = $start, $n = $start + $per_page; ($i < $n) && ($i < $result_count); $i++)
@@ -166,6 +172,8 @@ protected function save_ids(string $search_key, string $keywords, array $author_
166172
}
167173

168174
$store_ids = array_slice($id_ary, 0, $length);
175+
$id_range = range($start, $start + $length - 1);
176+
$store_ids = array_combine($id_range, $store_ids);
169177

170178
// create a new resultset if there is none for this search_key yet
171179
// or add the ids to the existing resultset
@@ -200,29 +208,26 @@ protected function save_ids(string $search_key, string $keywords, array $author_
200208
$this->db->sql_query($sql);
201209

202210
$store = array(-1 => $result_count, -2 => $sort_dir);
203-
$id_range = range($start, $start + $length - 1);
204211
}
205212
else
206213
{
207214
// we use one set of results for both sort directions so we have to calculate the indizes
208-
// for the reversed array and we also have to reverse the ids themselves
215+
// for the reversed array
209216
if ($store[-2] != $sort_dir)
210217
{
211-
$store_ids = array_reverse($store_ids);
212-
$id_range = range($store[-1] - $start - $length, $store[-1] - $start - 1);
213-
}
214-
else
215-
{
216-
$id_range = range($start, $start + $length - 1);
218+
$keys = array_keys($store_ids);
219+
array_walk($keys, function (&$value, $key) use ($store) {
220+
$value = $store[-1] - $value - 1;
221+
});
222+
$store_ids = array_combine($keys, $store_ids);
217223
}
218224
}
219225

220-
$store_ids = array_combine($id_range, $store_ids);
221-
222226
// append the ids
223227
if (is_array($store_ids))
224228
{
225229
$store += $store_ids;
230+
ksort($store);
226231

227232
// if the cache is too big
228233
if (count($store) - 2 > 20 * $this->config['search_block_size'])

search/backend/fulltext_mysql.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,6 @@ public function keyword_search(string $type, string $fields, string $terms, arra
550550
}
551551
$this->db->sql_freeresult($result);
552552

553-
$id_ary = array_unique($id_ary);
554553
// if the total result count is not cached yet, retrieve it from the db
555554
if (!$result_count && count($id_ary))
556555
{
@@ -576,10 +575,10 @@ public function keyword_search(string $type, string $fields, string $terms, arra
576575
$id_ary[] = (int) $row[$field];
577576
}
578577
$this->db->sql_freeresult($result);
579-
580-
$id_ary = array_unique($id_ary);
581578
}
582579

580+
$id_ary = array_unique($id_ary);
581+
583582
// store the ids, from start on then delete anything that isn't on the current page because we only need ids for one page
584583
$this->save_ids($search_key, implode(' ', $this->split_words), $author_ary, $result_count, $id_ary, $start, $sort_dir);
585584
$id_ary = array_slice($id_ary, 0, (int) $per_page);
@@ -758,6 +757,8 @@ public function author_search(string $type, bool $firstpost_only, array $sort_by
758757
// Build the query for really selecting the post_ids
759758
if ($type == 'posts')
760759
{
760+
// For sorting by non-unique columns, add unique sort key to avoid duplicated rows in results
761+
$sql_sort .= ', p.post_id' . (($sort_dir == 'a') ? ' ASC' : ' DESC');
761762
$sql = "SELECT $sql_select
762763
FROM " . $sql_sort_table . POSTS_TABLE . ' p' . (($firstpost_only) ? ', ' . TOPICS_TABLE . ' t ' : ' ') . "
763764
WHERE $sql_author
@@ -821,10 +822,10 @@ public function author_search(string $type, bool $firstpost_only, array $sort_by
821822
$id_ary[] = (int) $row[$field];
822823
}
823824
$this->db->sql_freeresult($result);
824-
825-
$id_ary = array_unique($id_ary);
826825
}
827826

827+
$id_ary = array_unique($id_ary);
828+
828829
if (count($id_ary))
829830
{
830831
$this->save_ids($search_key, '', $author_ary, $result_count, $id_ary, $start, $sort_dir);

search/backend/fulltext_native.php

+6
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,8 @@ public function keyword_search(string $type, string $fields, string $terms, arra
10041004
$this->db->sql_freeresult($result);
10051005
}
10061006

1007+
$id_ary = array_unique($id_ary);
1008+
10071009
// store the ids, from start on then delete anything that isn't on the current page because we only need ids for one page
10081010
$this->save_ids($search_key, $this->search_query, $author_ary, $total_results, $id_ary, $start, $sort_dir);
10091011
$id_ary = array_slice($id_ary, 0, (int) $per_page);
@@ -1225,6 +1227,8 @@ public function author_search(string $type, bool $firstpost_only, array $sort_by
12251227
// Build the query for really selecting the post_ids
12261228
if ($type == 'posts')
12271229
{
1230+
// For sorting by non-unique columns, add unique sort key to avoid duplicated rows in results
1231+
$sql_sort .= ', p.post_id' . (($sort_dir == 'a') ? ' ASC' : ' DESC');
12281232
$sql = "SELECT $select
12291233
FROM " . $sql_sort_table . POSTS_TABLE . ' p' . (($firstpost_only) ? ', ' . TOPICS_TABLE . ' t' : '') . "
12301234
WHERE $sql_author
@@ -1289,6 +1293,8 @@ public function author_search(string $type, bool $firstpost_only, array $sort_by
12891293
$this->db->sql_freeresult($result);
12901294
}
12911295

1296+
$id_ary = array_unique($id_ary);
1297+
12921298
if (count($id_ary))
12931299
{
12941300
$this->save_ids($search_key, '', $author_ary, $total_results, $id_ary, $start, $sort_dir);

search/backend/fulltext_postgres.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,6 @@ public function keyword_search(string $type, string $fields, string $terms, arra
478478
}
479479
$this->db->sql_freeresult($result);
480480

481-
$id_ary = array_unique($id_ary);
482-
483481
// if the total result count is not cached yet, retrieve it from the db
484482
if (!$result_count)
485483
{
@@ -509,10 +507,10 @@ public function keyword_search(string $type, string $fields, string $terms, arra
509507
$id_ary[] = $row[$field];
510508
}
511509
$this->db->sql_freeresult($result);
512-
513-
$id_ary = array_unique($id_ary);
514510
}
515511

512+
$id_ary = array_unique($id_ary);
513+
516514
// store the ids, from start on then delete anything that isn't on the current page because we only need ids for one page
517515
$this->save_ids($search_key, implode(' ', $this->split_words), $author_ary, $result_count, $id_ary, $start, $sort_dir);
518516
$id_ary = array_slice($id_ary, 0, (int) $per_page);
@@ -683,6 +681,8 @@ public function author_search(string $type, bool $firstpost_only, array $sort_by
683681
// Build the query for really selecting the post_ids
684682
if ($type == 'posts')
685683
{
684+
// For sorting by non-unique columns, add unique sort key to avoid duplicated rows in results
685+
$sql_sort .= ', p.post_id' . (($sort_dir == 'a') ? ' ASC' : ' DESC');
686686
$sql = "SELECT p.post_id
687687
FROM " . $sql_sort_table . POSTS_TABLE . ' p' . (($firstpost_only) ? ', ' . TOPICS_TABLE . ' t ' : ' ') . "
688688
WHERE $sql_author
@@ -775,10 +775,10 @@ public function author_search(string $type, bool $firstpost_only, array $sort_by
775775
$id_ary[] = (int) $row[$field];
776776
}
777777
$this->db->sql_freeresult($result);
778-
779-
$id_ary = array_unique($id_ary);
780778
}
781779

780+
$id_ary = array_unique($id_ary);
781+
782782
if (count($id_ary))
783783
{
784784
$this->save_ids($search_key, '', $author_ary, $result_count, $id_ary, $start, $sort_dir);

0 commit comments

Comments
 (0)