]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwListing.pm
www: move mirror instructions to /text/
[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 (scalar(@{$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         my $user_query = $q->{'q'} // '';
100         if ($user_query =~ /\S/) {
101                 $qs = "( $qs ) AND ( $user_query )";
102         } else { # special case for ALL
103                 $ctx->ibx_entry($pi_cfg->ALL // die('BUG: ->ALL expected'), {});
104         }
105         my $mset = $misc->mset($qs, $opt); # sorts by $MODIFIED (mtime)
106         my $hide_key = $ctx->hide_key;
107
108         for my $mi ($mset->items) {
109                 my $doc = $mi->get_document;
110                 my ($eidx_key) = PublicInbox::Search::xap_terms('Q', $doc);
111                 $eidx_key // next;
112                 my $ibx = $pi_cfg->lookup_eidx_key($eidx_key) // next;
113                 next if $ibx->{-hide}->{$hide_key};
114                 grep(/$re/, @{$ibx->{url} // []}) or next;
115                 $ctx->ibx_entry($ibx, $misc->doc2ibx_cache_ent($doc));
116                 if ($r) { # for descriptions in search_nav_bot
117                         my $pct = PublicInbox::Search::get_pct($mi);
118                         # only when sorting by relevance, ->items is always
119                         # ordered descending:
120                         $max //= $pct;
121                         $min = $pct;
122                 }
123         }
124         if ($r) { # for descriptions in search_nav_bot
125                 $q->{-min_pct} = $min;
126                 $q->{-max_pct} = $max;
127         }
128         $ctx->{-mset} = $mset;
129         psgi_triple($ctx);
130 }
131
132 sub response {
133         my ($class, $ctx) = @_;
134         bless $ctx, $class;
135         my ($re, $qs) = $ctx->url_filter;
136         $re // return $ctx->psgi_triple;
137         if (my $ALL = $ctx->{www}->{pi_cfg}->ALL) { # fast path
138                 # FIXME: test this in t/
139                 $ALL->misc->reopen->retry_reopen(\&add_misc_ibx,
140                                                 $ctx, $re, $qs);
141         } else { # slow path, no [extindex "all"] configured
142                 my $iter = PublicInbox::ConfigIter->new($ctx->{www}->{pi_cfg},
143                                                 \&list_match_i, $re, $ctx);
144                 sub {
145                         $ctx->{-wcb} = $_[0]; # HTTP server callback
146                         $ctx->{env}->{'pi-httpd.async'} ?
147                                         $iter->event_step : $iter->each_section;
148                 }
149         }
150 }
151
152 sub mset_footer ($$) {
153         my ($ctx, $mset) = @_;
154         # no footer if too few matches
155         return '' if $mset->get_matches_estimated == $mset->size;
156         require PublicInbox::SearchView;
157         PublicInbox::SearchView::search_nav_bot($mset, $ctx->{-sq});
158 }
159
160 sub mset_nav_top {
161         my ($ctx, $mset) = @_;
162         my $q = $ctx->{-sq};
163         my $qh = $q->{'q'} // '';
164         utf8::decode($qh);
165         $qh = ascii_html($qh);
166         $qh = qq[\nvalue="$qh"] if $qh ne '';
167         my $rv = <<EOM;
168 <form
169 action="./"><pre><input
170 name=q
171 type=text$qh /><input
172 type=submit
173 value="locate inbox" /></pre></form><pre>
174 EOM
175         chomp $rv;
176         if (defined($q->{'q'})) {
177                 my $initial_q = $ctx->{-uxs_retried};
178                 if (defined $initial_q) {
179                         my $rewritten = $q->{'q'};
180                         utf8::decode($initial_q);
181                         utf8::decode($rewritten);
182                         $initial_q = ascii_html($initial_q);
183                         $rewritten = ascii_html($rewritten);
184                         $rv .= " Warning: Initial query:\n <b>$initial_q</b>\n";
185                         $rv .= " returned no results, used:\n";
186                         $rv .= " <b>$rewritten</b>\n instead\n\n";
187                 }
188                 $rv .= 'Search results ordered by [';
189                 if ($q->{r}) {
190                         my $d = $q->qs_html(r => 0);
191                         $rv .= qq{<a\nhref="?$d">updated</a>|<b>relevance</b>};
192                 } else {
193                         my $d = $q->qs_html(r => 1);
194                         $rv .= qq{<b>updated</b>|<a\nhref="?$d">relevance</a>};
195                 }
196                 $rv .= ']';
197         }
198         $rv .= qq{</pre>};
199 }
200
201 sub psgi_triple {
202         my ($ctx) = @_;
203         my $h = [ 'Content-Type', 'text/html; charset=UTF-8',
204                         'Content-Length', undef ];
205         my $gzf = gzf_maybe($h, $ctx->{env});
206         $gzf->zmore('<html><head><title>public-inbox listing</title>' .
207                         $ctx->{www}->style('+/') .
208                         '</head><body>');
209         my $code = 404;
210         if (my $list = delete $ctx->{-list}) {
211                 my $mset = delete $ctx->{-mset};
212                 $code = 200;
213                 if ($mset) { # already sorted, so search bar:
214                         $gzf->zmore(mset_nav_top($ctx, $mset));
215                 } else { # sort config dump by ->modified
216                         @$list = map { $_->[1] }
217                                 sort { $b->[0] <=> $a->[0] } @$list;
218                 }
219                 $gzf->zmore('<pre>');
220                 $gzf->zmore(join("\n", @$list));
221                 $gzf->zmore(mset_footer($ctx, $mset)) if $mset;
222         } elsif (my $mset = delete $ctx->{-mset}) {
223                 $gzf->zmore(mset_nav_top($ctx, $mset));
224                 $gzf->zmore('<pre>no matching inboxes');
225                 $gzf->zmore(mset_footer($ctx, $mset));
226         } else {
227                 $gzf->zmore('<pre>no inboxes, yet');
228         }
229         my $out = $gzf->zflush('</pre></body></html>');
230         $h->[3] = length($out);
231         [ $code, $h, [ $out ] ];
232 }
233
234 1;