]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwListing.pm
eabda98abe484bebc0ff41276de648e91675f8bc
[public-inbox.git] / lib / PublicInbox / WwwListing.pm
1 # Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Provide an HTTP-accessible listing of inboxes.
5 # Used by PublicInbox::WWW
6 package PublicInbox::WwwListing;
7 use strict;
8 use v5.10.1;
9 use PublicInbox::Hval qw(prurl fmt_ts ascii_html);
10 use PublicInbox::GzipFilter qw(gzf_maybe);
11 use PublicInbox::ConfigIter;
12 use PublicInbox::WwwStream;
13 use URI::Escape qw(uri_escape_utf8);
14
15 sub ibx_entry {
16         my ($ctx, $ibx, $ce) = @_;
17         my $desc = ascii_html($ce->{description} //= $ibx->description);
18         my $ts = fmt_ts($ce->{-modified} //= $ibx->modified);
19         my ($url, $href);
20         if (defined($ibx->{url})) {
21                 $url = $href = ascii_html(prurl($ctx->{env}, $ibx->{url}));
22         } else {
23                 $href = ascii_html(uri_escape_utf8($ibx->{name})) . '/';
24                 $url = ascii_html($ibx->{name});
25         }
26         my $tmp = <<EOM;
27 * $ts - <a\nhref="$href">$url</a>
28   $desc
29 EOM
30         if (defined($url = $ibx->{infourl})) {
31                 $url = ascii_html(prurl($ctx->{env}, $url));
32                 $tmp .= qq(  <a\nhref="$url">$url</a>\n);
33         }
34         push(@{$ctx->{-list}}, (scalar(@_) == 3 ? # $misc in use, already sorted
35                                 $tmp : [ $ce->{-modified}, $tmp ] ));
36 }
37
38 sub list_match_i { # ConfigIter callback
39         my ($cfg, $section, $re, $ctx) = @_;
40         if (defined($section)) {
41                 return if $section !~ m!\Apublicinbox\.([^/]+)\z!;
42                 my $ibx = $cfg->lookup_name($1) or return;
43                 if (!$ibx->{-hide}->{$ctx->hide_key} &&
44                                         grep(/$re/, @{$ibx->{url}})) {
45                         $ctx->ibx_entry($ibx);
46                 }
47         } else { # undef == "EOF"
48                 $ctx->{-wcb}->($ctx->psgi_triple);
49         }
50 }
51
52 sub url_filter {
53         my ($ctx, $key, $default) = @_;
54         $key //= 'publicInbox.wwwListing';
55         $default //= '404';
56         my $v = $ctx->{www}->{pi_cfg}->{lc $key} // $default;
57 again:
58         if ($v eq 'match=domain') {
59                 my $h = $ctx->{env}->{HTTP_HOST} // $ctx->{env}->{SERVER_NAME};
60                 $h =~ s/:[0-9]+\z//;
61                 (qr!\A(?:https?:)?//\Q$h\E(?::[0-9]+)?/!i, "url:$h");
62         } elsif ($v eq 'all') {
63                 (qr/./, undef);
64         } elsif ($v eq '404') {
65                 (undef, undef);
66         } else {
67                 warn <<EOF;
68 `$v' is not a valid value for `$key'
69 $key be one of `all', `match=domain', or `404'
70 EOF
71                 $v = $default; # 'match=domain' or 'all'
72                 goto again;
73         }
74 }
75
76 sub hide_key { 'www' }
77
78 sub add_misc_ibx { # MiscSearch->retry_reopen callback
79         my ($misc, $ctx, $re, $qs) = @_;
80         require PublicInbox::SearchQuery;
81         my $q = $ctx->{-sq} = PublicInbox::SearchQuery->new($ctx->{qp});
82         my $o = $q->{o};
83         my ($asc, $min, $max);
84         if ($o < 0) {
85                 $asc = 1;
86                 $o = -($o + 1); # so [-1] is the last element, like Perl lists
87         }
88         my $r = $q->{r};
89         my $opt = {
90                 offset => $o,
91                 asc => $asc,
92                 relevance => $r,
93                 limit => $q->{l}
94         };
95         $qs .= ' type:inbox';
96
97         delete $ctx->{-list}; # reset if retried
98         my $pi_cfg = $ctx->{www}->{pi_cfg};
99         if (defined(my $user_query = $q->{'q'})) {
100                 $qs = "( $qs ) AND ( $user_query )";
101         } else { # special case for ALL
102                 $ctx->ibx_entry($pi_cfg->ALL // die('BUG: ->ALL expected'), {});
103         }
104         my $mset = $misc->mset($qs, $opt); # sorts by $MODIFIED (mtime)
105         my $hide_key = $ctx->hide_key;
106
107         for my $mi ($mset->items) {
108                 my $doc = $mi->get_document;
109                 my ($eidx_key) = PublicInbox::Search::xap_terms('Q', $doc);
110                 $eidx_key // next;
111                 my $ibx = $pi_cfg->lookup_eidx_key($eidx_key) // next;
112                 next if $ibx->{-hide}->{$hide_key};
113                 grep(/$re/, @{$ibx->{url} // []}) or next;
114                 $ctx->ibx_entry($ibx, $misc->doc2ibx_cache_ent($doc));
115                 if ($r) { # for descriptions in search_nav_bot
116                         my $pct = PublicInbox::Search::get_pct($mi);
117                         # only when sorting by relevance, ->items is always
118                         # ordered descending:
119                         $max //= $pct;
120                         $min = $pct;
121                 }
122         }
123         if ($r) { # for descriptions in search_nav_bot
124                 $q->{-min_pct} = $min;
125                 $q->{-max_pct} = $max;
126         }
127         $ctx->{-mset} = $mset;
128         psgi_triple($ctx);
129 }
130
131 sub response {
132         my ($class, $ctx) = @_;
133         bless $ctx, $class;
134         my ($re, $qs) = $ctx->url_filter;
135         $re // return $ctx->psgi_triple;
136         if (my $ALL = $ctx->{www}->{pi_cfg}->ALL) { # fast path
137                 # FIXME: test this in t/
138                 $ALL->misc->reopen->retry_reopen(\&add_misc_ibx,
139                                                 $ctx, $re, $qs);
140         } else { # slow path, no [extindex "all"] configured
141                 my $iter = PublicInbox::ConfigIter->new($ctx->{www}->{pi_cfg},
142                                                 \&list_match_i, $re, $ctx);
143                 sub {
144                         $ctx->{-wcb} = $_[0]; # HTTP server callback
145                         $ctx->{env}->{'pi-httpd.async'} ?
146                                         $iter->event_step : $iter->each_section;
147                 }
148         }
149 }
150
151 sub mset_footer ($$) {
152         my ($ctx, $mset) = @_;
153         # no footer if too few matches
154         return '' if $mset->get_matches_estimated == $mset->size;
155         require PublicInbox::SearchView;
156         PublicInbox::SearchView::search_nav_bot($mset, $ctx->{-sq});
157 }
158
159 sub mset_nav_top {
160         my ($ctx, $mset) = @_;
161         my $q = $ctx->{-sq};
162         my $qh = $q->{'q'} // '';
163         utf8::decode($qh);
164         $qh = ascii_html($qh);
165         $qh = qq[\nvalue="$qh"] if $qh ne '';
166         my $rv = <<EOM;
167 <form
168 action="./"><pre><input
169 name=q
170 type=text$qh /><input
171 type=submit
172 value="locate inbox" /></pre></form><pre>
173 EOM
174         chomp $rv;
175         if (defined($q->{'q'})) {
176                 my $initial_q = $ctx->{-uxs_retried};
177                 if (defined $initial_q) {
178                         my $rewritten = $q->{'q'};
179                         utf8::decode($initial_q);
180                         utf8::decode($rewritten);
181                         $initial_q = ascii_html($initial_q);
182                         $rewritten = ascii_html($rewritten);
183                         $rv .= " Warning: Initial query:\n <b>$initial_q</b>\n";
184                         $rv .= " returned no results, used:\n";
185                         $rv .= " <b>$rewritten</b>\n instead\n\n";
186                 }
187                 $rv .= 'Search results ordered by [';
188                 if ($q->{r}) {
189                         my $d = $q->qs_html(r => 0);
190                         $rv .= qq{<a\nhref="?$d">updated</a>|<b>relevance</b>};
191                 } else {
192                         my $d = $q->qs_html(r => 1);
193                         $rv .= qq{<b>updated</b>|<a\nhref="?$d">relevance</a>};
194                 }
195                 $rv .= ']';
196         }
197         $rv .= qq{</pre>};
198 }
199
200 sub psgi_triple {
201         my ($ctx) = @_;
202         my $h = [ 'Content-Type', 'text/html; charset=UTF-8',
203                         'Content-Length', undef ];
204         my $gzf = gzf_maybe($h, $ctx->{env});
205         $gzf->zmore('<html><head><title>public-inbox listing</title>' .
206                         $ctx->{www}->style('+/') .
207                         '</head><body>');
208         my $code = 404;
209         if (my $list = delete $ctx->{-list}) {
210                 my $mset = delete $ctx->{-mset};
211                 $code = 200;
212                 if ($mset) { # already sorted, so search bar:
213                         $gzf->zmore(mset_nav_top($ctx, $mset));
214                 } else { # sort config dump by ->modified
215                         @$list = map { $_->[1] }
216                                 sort { $b->[0] <=> $a->[0] } @$list;
217                 }
218                 $gzf->zmore('<pre>');
219                 $gzf->zmore(join("\n", @$list));
220                 $gzf->zmore(mset_footer($ctx, $mset)) if $mset;
221         } else {
222                 $gzf->zmore('<pre>no inboxes, yet');
223         }
224         my $out = $gzf->zflush('</pre><hr><pre>'.
225                         PublicInbox::WwwStream::code_footer($ctx->{env}) .
226                         '</pre></body></html>');
227         $h->[3] = length($out);
228         [ $code, $h, [ $out ] ];
229 }
230
231 1;