Commit 0f4c4546 authored by Simon Welsh's avatar Simon Welsh

Sorting of results

git-svn-id: svn+ssh://lsgn/var/logbot-www@22 5f99df0c-6b0a-442a-b40a-040688e84a9d
parent 926218be
......@@ -20,9 +20,12 @@ final class DB {
return pg_query($this->con, $sql);
}
public function search($value) {
public function search($value, $order = 'Relevance') {
$value = pg_escape_literal($this->con, $value);
$sql = 'SELECT "Time", "Text", "Command", "Target", "Nick", "ID", "Old", ts_rank_cd("Search", query, 16 | 32) AS "Rank" FROM "Log", plainto_tsquery(\'english\', ' . $value . ') query WHERE "Search" @@ query ORDER BY "Rank" DESC LIMIT 5000';
if(!in_array($order, ['Relevance', 'Time'])) {
$order = 'Relevance';
}
$sql = 'SELECT "Time", "Text", "Command", "Target", "Nick", "ID", "Old", ts_rank_cd("Search", query, 16 | 32) AS "Relevance" FROM "Log", plainto_tsquery(\'english\', ' . $value . ') query WHERE "Search" @@ query ORDER BY "' . $order . '" DESC LIMIT 5000';
$res = $this->query($sql);
$results = array();
while($row = pg_fetch_assoc($res)) {
......
......@@ -41,6 +41,10 @@ table {
margin: 0;
}
table th {
padding: 0 5px;
}
table td {
vertical-align: text-top;
font-size: 12px;
......
......@@ -12,7 +12,11 @@ if(empty($_GET['q']) || !trim($_GET['q'])) {
$robots = 'noindex,nofollow';
$term = $_GET['q'];
$results = DB::inst()->search($term);
$order = isset($_GET['s']) ? $_GET['s'] : null;
if(!in_array($order, ['Relevance', 'Time', null])) {
$order = 'Relevance';
}
$results = DB::inst()->search($term, $order);
$term = htmlentities($term);
......@@ -22,12 +26,20 @@ $body = '<p><a href="index.php">Index</a></p>';
$body .= search_form($_GET['q']);
$body .= '<h2>Search results for "';
$body .= $term;
$body .= '"</h2><p>Timestamps are in NZT (<a href="http://timeanddate.com/worldclock/city.html?n=264" title="Click here to see what time it is right now in Wellington, New Zealand">New Zealand Time</a>).</p>';
$body .= '"';
if($order) {
$body .= ' ordered by ' . strtolower($order);
}
$body .= '</h2><p>Timestamps are in NZT (<a href="http://timeanddate.com/worldclock/city.html?n=264" title="Click here to see what time it is right now in Wellington, New Zealand">New Zealand Time</a>).</p>';
if(empty($results)) {
$body .= '<strong>No results found</strong>';
$body .= '<p><strong>No results found</strong></p>';
} else {
$body .= '<p>Order by <a href="search.php?q=' . urlencode($_GET['q']) . '&s=Relevance">relevance</a> or <a href="search.php?q=' . urlencode($_GET['q']) . '&s=Time">time</a>.</p>';
$body .= '<table class="logs">';
$body .= '<tbody>';
foreach($results as $row) {
$log = handle_log(++$id, $row, ['d/m/Y H:i:s', 'd/m/Y H:i']);
if($log) {
......@@ -36,7 +48,7 @@ if(empty($results)) {
--$id;
}
}
$body .= '</table>';
$body .= '</tbody></table>';
}
load_defaults();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment