fix(search): 🐛 fix lack of order_by selectability

This commit is contained in:
David Bailey 2025-02-24 22:41:39 +01:00
parent 77b3f858d1
commit 91500ccfe5

View file

@ -13,6 +13,14 @@ class MySQLHandler
'title', 'view_count', 'brief', 'search_score'];
CONST SQL_WRITE_COLUMNS = ['path', 'title', 'brief'];
CONST SQL_ORDER_BY_OPTIONS = [
'search_score' => 'post_search_score',
'search_score_desc' => 'post_search_score DESC',
'path' => 'post_path',
'path_desc' => 'post_path DESC',
'created_at' => 'post_created_at',
'created_at_desc' => 'post_created_at DESC'
];
private $sql_connection;
private $db_prefix;
@ -398,8 +406,6 @@ class MySQLHandler
$options['tags'] ??= [];
}
$options['limit'] = min($options['limit'] ?? 100, 100);
// This code will take a generic user-input string, and will process it
// to see if there are any special options to consider.
//
@ -467,6 +473,14 @@ class MySQLHandler
}
$options['offset'] ??= 0;
$options['limit'] = min($options['limit'] ?? 100, 100);
$options['order_by'] ??= 'search_score_desc';
if(!isset($this::SQL_ORDER_BY_OPTIONS[$options['order_by']])) {
throw new Exception("Incorrect order_by option chosen!");
}
$qry_order_by = $this::SQL_ORDER_BY_OPTIONS[$options['order_by']];
$qry =
"SELECT " . implode(', ', $qry_selects) . "
@ -474,12 +488,11 @@ class MySQLHandler
LEFT JOIN {$this->db_prefix}_post_markdown AS post_markdown
ON posts.post_id = post_markdown.post_id
WHERE " . implode(' and ', $qry_wheres) . "
ORDER BY post_search_score DESC
LIMIT " . $options['limit'] . "
OFFSET " . $options['offset'];
ORDER BY " . $qry_order_by . "
LIMIT ? OFFSET ?";
$search_results = $this->_exec($qry, $qry_select_types . $qry_where_types,
...array_merge($qry_select_data, $qry_where_data))->fetch_all(MYSQLI_ASSOC);
$search_results = $this->_exec($qry, $qry_select_types . $qry_where_types . "ii",
...array_merge($qry_select_data, $qry_where_data, [$options['limit'], $options['offset']]))->fetch_all(MYSQLI_ASSOC);
$outdata = [];
foreach($search_results AS $post_element) {