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