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>
4 # Provide an HTTP-accessible listing of inboxes.
5 # Used by PublicInbox::WWW
6 package PublicInbox::WwwListing;
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
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});
24 if (defined(my $info_url = $ibx->{infourl})) {
25 $tmp .= ' ' . prurl($ctx->{env}, $info_url) . "\n";
27 push(@{$ctx->{-list}}, (scalar(@_) == 3 ? # $misc in use, already sorted
28 $tmp : [ $ce->{-modified}, $tmp ] ));
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);
40 } else { # undef == "EOF"
41 $ctx->{-wcb}->($ctx->psgi_triple);
46 my ($ctx, $key, $default) = @_;
47 $key //= 'publicInbox.wwwListing';
49 my $v = $ctx->{www}->{pi_cfg}->{lc $key} // $default;
51 if ($v eq 'match=domain') {
52 my $h = $ctx->{env}->{HTTP_HOST} // $ctx->{env}->{SERVER_NAME};
54 (qr!\A(?:https?:)?//\Q$h\E(?::[0-9]+)?/!i, "url:$h");
55 } elsif ($v eq 'all') {
57 } elsif ($v eq '404') {
61 `$v' is not a valid value for `$key'
62 $key be one of `all', `match=domain', or `404'
64 $v = $default; # 'match=domain' or 'all'
69 sub hide_key { 'www' }
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});
76 my ($asc, $min, $max);
79 $o = -($o + 1); # so [-1] is the last element, like Perl lists
89 if (my $user_query = $q->{'q'}) {
90 $qs = "( $qs ) AND ( $user_query )";
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);
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:
111 if ($r) { # for descriptions in search_nav_bot
112 $q->{-min_pct} = $min;
113 $q->{-max_pct} = $max;
115 $ctx->{-mset} = $mset;
120 my ($class, $ctx) = @_;
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,
128 } else { # slow path, no [extindex "all"] configured
129 my $iter = PublicInbox::ConfigIter->new($ctx->{www}->{pi_cfg},
130 \&list_match_i, $re, $ctx);
132 $ctx->{-wcb} = $_[0]; # HTTP server callback
133 $ctx->{env}->{'pi-httpd.async'} ?
134 $iter->event_step : $iter->each_section;
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});
148 my ($ctx, $mset) = @_;
150 my $qh = $q->{'q'} // '';
152 $qh = ascii_html($qh);
153 $qh = qq[\nvalue="$qh"] if $qh ne '';
156 action="./"><pre><input
158 type=text$qh /><input
160 value="locate inbox" /></pre></form><pre>
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";
175 $rv .= 'Search results ordered by [';
177 my $d = $q->qs_html(r => 0);
178 $rv .= qq{<a\nhref="?$d">updated</a>|<b>relevance</b>};
180 my $d = $q->qs_html(r => 1);
181 $rv .= qq{<b>updated</b>|<a\nhref="?$d">relevance</a>};
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>' .
194 'public-inbox listing</title>' .
197 if (my $list = delete $ctx->{-list}) {
198 my $mset = delete $ctx->{-mset};
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;
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;
211 $gzf->zmore('<pre>no inboxes, yet');
213 my $out = $gzf->zflush('</pre><hr><pre>'.
214 PublicInbox::WwwStream::code_footer($ctx->{env}) .
215 '</pre></body></html>');
216 $h->[3] = bytes::length($out);
217 [ $code, $h, [ $out ] ];