unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
* [PATCH] view: preliminary HTML search interface
@ 2015-09-05  2:52 Eric Wong
  2015-09-05  3:02 ` Eric Wong
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Wong @ 2015-09-05  2:52 UTC (permalink / raw)
  To: meta

This hopefully makes it easier to find things without resorting
to proprietary external services.
---
 lib/PublicInbox/Feed.pm       | 20 ++++++++--
 lib/PublicInbox/Search.pm     |  3 +-
 lib/PublicInbox/SearchView.pm | 92 +++++++++++++++++++++++++++++++++++++++++++
 lib/PublicInbox/WWW.pm        |  9 ++++-
 4 files changed, 118 insertions(+), 6 deletions(-)
 create mode 100644 lib/PublicInbox/SearchView.pm

diff --git a/lib/PublicInbox/Feed.pm b/lib/PublicInbox/Feed.pm
index 8fcb8d8..973a3c5 100644
--- a/lib/PublicInbox/Feed.pm
+++ b/lib/PublicInbox/Feed.pm
@@ -123,19 +123,31 @@ sub emit_html_index {
 	my $title = $feed_opts->{description} || '';
 	$title = PublicInbox::Hval->new_oneline($title)->as_html;
 	my $atom_url = $feed_opts->{atomurl};
-	my ($footer, $param, $last, $srch);
+	my ($footer, $param, $last);
 	my $state = { ctx => $ctx, seen => {}, anchor_idx => 0 };
+	my $srch = $ctx->{srch};
+
+	my $top = "<b>$title</b> (<a\nhref=\"$atom_url\">Atom feed</a>)";
+
+	if ($srch) {
+		$top = qq{<form\naction="">$top} .
+			  qq{ <input\nname=q\ntype=text />} .
+			  qq{<input\ntype=submit\nvalue=search />} .
+			  qq{</form>} .
+			  PublicInbox::View::PRE_WRAP;
+	} else {
+		$top = PublicInbox::View::PRE_WRAP . $top . "\n";
+	}
 
 	$fh->write("<html><head><title>$title</title>" .
 		   "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
 		   "href=\"$atom_url\"\ntype=\"application/atom+xml\"/>" .
-		   '</head><body>' . PublicInbox::View::PRE_WRAP .
-		   "<b>$title</b> (<a\nhref=\"$atom_url\">Atom feed</a>)\n");
+		   "</head><body>$top");
 
 	# if the 'r' query parameter is given, it is a legacy permalink
 	# which we must continue supporting:
 	my $cgi = $ctx->{cgi};
-	if ($cgi && !$cgi->param('r') && ($srch = $ctx->{srch})) {
+	if ($cgi && !$cgi->param('r') && $srch) {
 		$state->{srch} = $srch;
 		$last = PublicInbox::View::emit_index_topics($state, $fh);
 		$param = 'o';
diff --git a/lib/PublicInbox/Search.pm b/lib/PublicInbox/Search.pm
index eb49f72..fd79b63 100644
--- a/lib/PublicInbox/Search.pm
+++ b/lib/PublicInbox/Search.pm
@@ -80,7 +80,7 @@ sub query {
 	$opts ||= {};
 	unless ($query_string eq '') {
 		$query = $self->qp->parse_query($query_string, QP_FLAGS);
-		$opts->{relevance} = 1;
+		$opts->{relevance} = 1 unless exists $opts->{relevance};
 	}
 
 	$self->do_enquire($query, $opts);
@@ -124,6 +124,7 @@ sub do_enquire {
 	my $offset = $opts->{offset} || 0;
 	my $limit = $opts->{limit} || 50;
 	my $mset = $enquire->get_mset($offset, $limit);
+	return $mset if $opts->{mset};
 	my @msgs = map {
 		PublicInbox::SearchMsg->load_doc($_->get_document);
 	} $mset->items;
diff --git a/lib/PublicInbox/SearchView.pm b/lib/PublicInbox/SearchView.pm
new file mode 100644
index 0000000..6113bbf
--- /dev/null
+++ b/lib/PublicInbox/SearchView.pm
@@ -0,0 +1,92 @@
+# Copyright (C) 2015 all contributors <meta@public-inbox.org>
+# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
+package PublicInbox::SearchView;
+use strict;
+use warnings;
+use PublicInbox::SearchMsg;
+use PublicInbox::Hval;
+use PublicInbox::View;
+use POSIX qw/strftime/;
+
+sub sres_top_html {
+	my ($ctx, $q) = @_;
+	my $cgi = $ctx->{cgi};
+	# $q ||= $cgi->param('q');
+	my $o = int($cgi->param('o') || 0);
+	my $r = $cgi->param('r');
+	$r = (defined $r && $r ne '0');
+	my $opts = { offset => $o, mset => 1, relevance => $r };
+	my $mset = $ctx->{srch}->query($q, $opts);
+	my $total = $mset->get_matches_estimated;
+	my $query = PublicInbox::Hval->new_oneline($q);
+	my $qh = $query->as_html;
+	my $res = "<html><head><title>$qh - search results</title></head>" .
+		  qq{<body><form\naction="">} .
+		  qq{<input\nname=q\nvalue="$qh"\ntype=text />};
+
+	$res .= qq{<input\ntype=hidden\nname=r />} if $r;
+
+	$res .= qq{<input\ntype=submit\nvalue=search /></form>} .
+		  PublicInbox::View::PRE_WRAP;
+
+	my $foot = $ctx->{footer};
+	if ($total == 0) {
+		$foot ||= '';
+		$res .= "\n\n[No results found]</pre><hr /><pre>$foot";
+	} else {
+		$q = $query->as_href;
+		$q =~ s/%20/+/g; # improve URL readability
+		my $qp = "?q=$q";
+		$qp .= "&amp;o=$o" if $o;
+
+		$res .= "Search results ordered by [";
+		if ($r) {
+			$res .= qq{<a\nhref="$qp">date</a>|<b>relevance</b>};
+		} else {
+			$qp .= '&amp;r';
+			$res .= qq{<b>date</b>|<a\nhref="$qp">relevance</a>};
+		}
+		$res .= "]\n\n";
+
+		my $pad = length("$total");
+		my $pfx = ' ' x $pad;
+		foreach my $m ($mset->items) {
+			my $rank = sprintf("%${pad}d", $m->get_rank + 1);
+			my $pct = $m->get_percent;
+			my $smsg = $m->get_document;
+			$smsg = PublicInbox::SearchMsg->load_doc($smsg);
+			my $s = PublicInbox::Hval->new_oneline($smsg->subject);
+			my $f = $smsg->from_name;
+			$f = PublicInbox::Hval->new_oneline($f)->as_html;
+			my $d = strftime('%Y-%m-%d %H:%M', gmtime($smsg->ts));
+			my $mid = $smsg->mid;
+			$mid = PublicInbox::Hval->new_msgid($mid)->as_href;
+			$res .= qq{$rank. <b><a\nhref="$mid/t/#u">}.
+				$s->as_html . "</a></b>\n";
+			$res .= "$pfx  - by $f @ $d UTC [$pct%]\n\n";
+		}
+		my $nr = scalar $mset->items;
+		my $end = $o + $nr;
+		my $beg = $o + 1;
+		$res .= "<hr /><pre>";
+		$res .= "Results $beg-$end of $total.";
+		if ($nr < $total) {
+			$o = $o + $nr;
+			$qp = "q=$q&amp;o=$o";
+			$qp .= "&amp;r" if $r;
+			$res .= qq{ <a\nhref="?$qp">more</a>}
+		}
+		$res .= "\n\n".$foot if $foot;
+	}
+
+	$res .= "</pre></body></html>";
+	[200, ['Content-Type'=>'text/html; charset=UTF-8'], [$res]];
+}
+
+sub sres_top_atom {
+}
+
+sub sres_top_thread {
+}
+
+1;
diff --git a/lib/PublicInbox/WWW.pm b/lib/PublicInbox/WWW.pm
index 5584e49..2718854 100644
--- a/lib/PublicInbox/WWW.pm
+++ b/lib/PublicInbox/WWW.pm
@@ -67,6 +67,7 @@ sub preload {
 
 	eval {
 		require PublicInbox::Search;
+		require PublicInbox::SearchView;
 		require PublicInbox::Mbox;
 		require IO::Compress::Gzip;
 	};
@@ -128,8 +129,14 @@ sub get_index {
 	my ($ctx) = @_;
 	require PublicInbox::Feed;
 	my $srch = searcher($ctx);
+	my $q = $ctx->{cgi}->param('q');
 	footer($ctx);
-	PublicInbox::Feed::generate_html_index($ctx);
+	if (defined $q) {
+		require PublicInbox::SearchView;
+		PublicInbox::SearchView::sres_top_html($ctx, $q);
+	} else {
+		PublicInbox::Feed::generate_html_index($ctx);
+	}
 }
 
 # just returns a string ref for the blob in the current ctx
-- 
EW


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] view: preliminary HTML search interface
  2015-09-05  2:52 [PATCH] view: preliminary HTML search interface Eric Wong
@ 2015-09-05  3:02 ` Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2015-09-05  3:02 UTC (permalink / raw)
  To: meta

Oops, forgot to use a fixed width font for the title.  Unfortunately,
that probably does not affect the search form on a mainstream GUI
browser.  Squashing the following:

diff --git a/lib/PublicInbox/Feed.pm b/lib/PublicInbox/Feed.pm
index 973a3c5..dbb80ca 100644
--- a/lib/PublicInbox/Feed.pm
+++ b/lib/PublicInbox/Feed.pm
@@ -130,10 +130,10 @@ sub emit_html_index {
 	my $top = "<b>$title</b> (<a\nhref=\"$atom_url\">Atom feed</a>)";
 
 	if ($srch) {
-		$top = qq{<form\naction="">$top} .
+		$top = qq{<form\naction=""><tt>$top} .
 			  qq{ <input\nname=q\ntype=text />} .
 			  qq{<input\ntype=submit\nvalue=search />} .
-			  qq{</form>} .
+			  qq{</tt></form>} .
 			  PublicInbox::View::PRE_WRAP;
 	} else {
 		$top = PublicInbox::View::PRE_WRAP . $top . "\n";

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-09-05  3:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-05  2:52 [PATCH] view: preliminary HTML search interface Eric Wong
2015-09-05  3:02 ` Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).