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)
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 $ALL->misc->reopen->retry_reopen(\&add_misc_ibx,
127 } else { # slow path, no [extindex "all"] configured
128 my $iter = PublicInbox::ConfigIter->new($ctx->{www}->{pi_cfg},
129 \&list_match_i, $re, $ctx);
131 $ctx->{-wcb} = $_[0]; # HTTP server callback
132 $ctx->{env}->{'pi-httpd.async'} ?
133 $iter->event_step : $iter->each_section;
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});
147 my ($ctx, $mset) = @_;
149 my $qh = $q->{'q'} // '';
151 $qh = ascii_html($qh);
152 $qh = qq[\nvalue="$qh"] if $qh ne '';
155 action="./"><pre><input
157 type=text$qh /><input
159 value="locate inbox" /></pre></form><pre>
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";
174 $rv .= 'Search results ordered by [';
176 my $d = $q->qs_html(r => 0);
177 $rv .= qq{<a\nhref="?$d">updated</a>|<b>relevance</b>};
179 my $d = $q->qs_html(r => 1);
180 $rv .= qq{<b>updated</b>|<a\nhref="?$d">relevance</a>};
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>' .
196 if (my $list = delete $ctx->{-list}) {
197 my $mset = delete $ctx->{-mset};
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;
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;
210 $gzf->zmore('<pre>no inboxes, yet');
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 ] ];