]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwListing.pm
get rid of unnecessary bytes::length usage
[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::Linkify;
11 use PublicInbox::GzipFilter qw(gzf_maybe);
12 use PublicInbox::ConfigIter;
13 use PublicInbox::WwwStream;
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, 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         delete $ctx->{-list}; # reset if retried
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                 # FIXME: test this in t/
126                 $ALL->misc->reopen->retry_reopen(\&add_misc_ibx,
127                                                 $ctx, $re, $qs);
128         } else { # slow path, no [extindex "all"] configured
129                 my $iter = PublicInbox::ConfigIter->new($ctx->{www}->{pi_cfg},
130                                                 \&list_match_i, $re, $ctx);
131                 sub {
132                         $ctx->{-wcb} = $_[0]; # HTTP server callback
133                         $ctx->{env}->{'pi-httpd.async'} ?
134                                         $iter->event_step : $iter->each_section;
135                 }
136         }
137 }
138
139 sub mset_footer ($$) {
140         my ($ctx, $mset) = @_;
141         # no footer if too few matches
142         return '' if $mset->get_matches_estimated == $mset->size;
143         require PublicInbox::SearchView;
144         PublicInbox::SearchView::search_nav_bot($mset, $ctx->{-sq});
145 }
146
147 sub mset_nav_top {
148         my ($ctx, $mset) = @_;
149         my $q = $ctx->{-sq};
150         my $qh = $q->{'q'} // '';
151         utf8::decode($qh);
152         $qh = ascii_html($qh);
153         $qh = qq[\nvalue="$qh"] if $qh ne '';
154         my $rv = <<EOM;
155 <form
156 action="./"><pre><input
157 name=q
158 type=text$qh /><input
159 type=submit
160 value="locate inbox" /></pre></form><pre>
161 EOM
162         chomp $rv;
163         if (defined($q->{'q'})) {
164                 my $initial_q = $ctx->{-uxs_retried};
165                 if (defined $initial_q) {
166                         my $rewritten = $q->{'q'};
167                         utf8::decode($initial_q);
168                         utf8::decode($rewritten);
169                         $initial_q = ascii_html($initial_q);
170                         $rewritten = ascii_html($rewritten);
171                         $rv .= " Warning: Initial query:\n <b>$initial_q</b>\n";
172                         $rv .= " returned no results, used:\n";
173                         $rv .= " <b>$rewritten</b>\n instead\n\n";
174                 }
175                 $rv .= 'Search results ordered by [';
176                 if ($q->{r}) {
177                         my $d = $q->qs_html(r => 0);
178                         $rv .= qq{<a\nhref="?$d">updated</a>|<b>relevance</b>};
179                 } else {
180                         my $d = $q->qs_html(r => 1);
181                         $rv .= qq{<b>updated</b>|<a\nhref="?$d">relevance</a>};
182                 }
183                 $rv .= ']';
184         }
185         $rv .= qq{</pre>};
186 }
187
188 sub psgi_triple {
189         my ($ctx) = @_;
190         my $h = [ 'Content-Type', 'text/html; charset=UTF-8',
191                         'Content-Length', undef ];
192         my $gzf = gzf_maybe($h, $ctx->{env});
193         $gzf->zmore('<html><head><title>public-inbox listing</title>' .
194                         $ctx->{www}->style('+/') .
195                         '</head><body>');
196         my $code = 404;
197         if (my $list = delete $ctx->{-list}) {
198                 my $mset = delete $ctx->{-mset};
199                 $code = 200;
200                 if ($mset) { # already sorted, so search bar:
201                         $gzf->zmore(mset_nav_top($ctx, $mset));
202                 } else { # sort config dump by ->modified
203                         @$list = map { $_->[1] }
204                                 sort { $b->[0] <=> $a->[0] } @$list;
205                 }
206                 $list = join("\n", @$list);
207                 my $l = PublicInbox::Linkify->new;
208                 $gzf->zmore('<pre>'.$l->to_html($list));
209                 $gzf->zmore(mset_footer($ctx, $mset)) if $mset;
210         } else {
211                 $gzf->zmore('<pre>no inboxes, yet');
212         }
213         my $out = $gzf->zflush('</pre><hr><pre>'.
214                         PublicInbox::WwwStream::code_footer($ctx->{env}) .
215                         '</pre></body></html>');
216         $h->[3] = length($out);
217         [ $code, $h, [ $out ] ];
218 }
219
220 1;