# Copyright (C) 2015 all contributors <meta@public-inbox.org>
# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
+#
+# Displays search results for the web interface
package PublicInbox::SearchView;
use strict;
use warnings;
$total = $mset->get_matches_estimated;
};
my $err = $@;
- my $res = html_start($q) . PublicInbox::View::PRE_WRAP;
+ my $res = html_start($q, $ctx) . PublicInbox::View::PRE_WRAP;
if ($err) {
$code = 400;
$res .= err_txt($err) . "</pre><hr /><pre>" . foot($ctx);
$res .= "\n\n[No results found]</pre><hr /><pre>".foot($ctx);
} else {
my $x = $q->{x};
- # TODO
- #return sub { adump($_[0], $mset, $q, $ctx) } if ($x eq 'A');
+ return sub { adump($_[0], $mset, $q, $ctx) } if ($x eq 'A');
$res .= search_nav_top($mset, $q);
if ($x eq 't') {
$rv .= qq{<a\nhref="?$s">summary</a>|};
$rv .= qq{<b>threaded</b>};
}
- # my $A = $q->qs_html(x => 'a');
- # $rv .= qq{|<a\nhref="?$A">Atom</a>}; # TODO
+ my $A = $q->qs_html(x => 'A', r => undef);
+ $rv .= qq{|<a\nhref="?$A">Atom</a>};
$rv .= ']';
}
}
sub html_start {
- my ($q) = @_;
+ my ($q, $ctx) = @_;
my $query = PublicInbox::Hval->new_oneline($q->{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 />};
+ my $A = $q->qs_html(x => 'A', r => undef);
+ my $res = "<html><head><title>$qh - search results</title>" .
+ qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
+ qq!href="?$A"\ntype="application/atom+xml"/></head>! .
+ qq{<body><form\naction="">} .
+ qq{<input\nname=q\nvalue="$qh"\ntype=text />};
$res .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
if (my $x = $q->{x}) {
$res .= qq{<input\ntype=submit\nvalue=search /></form>};
}
+sub adump {
+ my ($cb, $mset, $q, $ctx) = @_;
+ my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
+ require PublicInbox::GitCatFile;
+ my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
+ my $feed_opts = PublicInbox::Feed::get_feedopts($ctx);
+ my $x = PublicInbox::Hval->new_oneline($q->{q})->as_html;
+ $x = qq{$x - search results};
+ $feed_opts->{atomurl} = $feed_opts->{url} . '?'. $q->qs_html;
+ $feed_opts->{url} .= '?'. $q->qs_html(x => undef);
+ $x = PublicInbox::Feed::atom_header($feed_opts, $x);
+ $fh->write($x. PublicInbox::Feed::feed_updated());
+
+ for ($mset->items) {
+ $x = PublicInbox::SearchMsg->load_doc($_->get_document)->mid;
+ $x = mid2path($x);
+ PublicInbox::Feed::add_to_feed($feed_opts, $fh, $x, $git);
+ }
+ $git = undef;
+ PublicInbox::Feed::end_feed($fh);
+}
+
package PublicInbox::SearchQuery;
use strict;
use warnings;
-use fields qw(q o t x r);
use PublicInbox::Hval;
sub new {
my ($class, $cgi) = @_;
- my $self = fields::new($class);
- $self->{q} = $cgi->param('q');
- $self->{x} = $cgi->param('x') || '';
- $self->{o} = int($cgi->param('o') || 0) || 0;
my $r = $cgi->param('r');
- $self->{r} = (defined $r && $r ne '0');
-
- $self;
+ bless {
+ q => $cgi->param('q'),
+ x => $cgi->param('x') || '',
+ o => int($cgi->param('o') || 0) || 0,
+ r => (defined $r && $r ne '0'),
+ }, $class;
}
sub qs_html {
my ($self, %over) = @_;
if (keys %over) {
- my $tmp = fields::new(ref($self));
- %$tmp = %$self;
+ my $tmp = bless { %$self }, ref($self);
foreach my $k (keys %over) {
$tmp->{$k} = $over{$k};
}