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