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;
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);
16 my ($ctx, $ibx, $ce) = @_;
17 my $desc = ascii_html($ce->{description} //= $ibx->description);
18 my $ts = fmt_ts($ce->{-modified} //= $ibx->modified);
20 if (defined($ibx->{url})) {
21 $url = $href = ascii_html(prurl($ctx->{env}, $ibx->{url}));
23 $href = ascii_html(uri_escape_utf8($ibx->{name})) . '/';
24 $url = ascii_html($ibx->{name});
27 * $ts - <a\nhref="$href">$url</a>
30 if (defined($url = $ibx->{infourl})) {
31 $url = ascii_html(prurl($ctx->{env}, $url));
32 $tmp .= qq( <a\nhref="$url">$url</a>\n);
34 push(@{$ctx->{-list}}, (scalar(@_) == 3 ? # $misc in use, already sorted
35 $tmp : [ $ce->{-modified}, $tmp ] ));
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);
47 } else { # undef == "EOF"
48 $ctx->{-wcb}->($ctx->psgi_triple);
53 my ($ctx, $key, $default) = @_;
54 $key //= 'publicInbox.wwwListing';
56 my $v = $ctx->{www}->{pi_cfg}->{lc $key} // $default;
58 if ($v eq 'match=domain') {
59 my $h = $ctx->{env}->{HTTP_HOST} // $ctx->{env}->{SERVER_NAME};
61 (qr!\A(?:https?:)?//\Q$h\E(?::[0-9]+)?/!i, "url:$h");
62 } elsif ($v eq 'all') {
64 } elsif ($v eq '404') {
68 `$v' is not a valid value for `$key'
69 $key be one of `all', `match=domain', or `404'
71 $v = $default; # 'match=domain' or 'all'
76 sub hide_key { 'www' }
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});
83 my ($asc, $min, $max);
86 $o = -($o + 1); # so [-1] is the last element, like Perl lists
97 delete $ctx->{-list}; # reset if retried
98 my $pi_cfg = $ctx->{www}->{pi_cfg};
99 if (defined(my $user_query = $q->{'q'})) {
100 $qs = "( $qs ) AND ( $user_query )";
101 } else { # special case for ALL
102 $ctx->ibx_entry($pi_cfg->ALL // die('BUG: ->ALL expected'), {});
104 my $mset = $misc->mset($qs, $opt); # sorts by $MODIFIED (mtime)
105 my $hide_key = $ctx->hide_key;
107 for my $mi ($mset->items) {
108 my $doc = $mi->get_document;
109 my ($eidx_key) = PublicInbox::Search::xap_terms('Q', $doc);
111 my $ibx = $pi_cfg->lookup_eidx_key($eidx_key) // next;
112 next if $ibx->{-hide}->{$hide_key};
113 grep(/$re/, @{$ibx->{url} // []}) or next;
114 $ctx->ibx_entry($ibx, $misc->doc2ibx_cache_ent($doc));
115 if ($r) { # for descriptions in search_nav_bot
116 my $pct = PublicInbox::Search::get_pct($mi);
117 # only when sorting by relevance, ->items is always
118 # ordered descending:
123 if ($r) { # for descriptions in search_nav_bot
124 $q->{-min_pct} = $min;
125 $q->{-max_pct} = $max;
127 $ctx->{-mset} = $mset;
132 my ($class, $ctx) = @_;
134 my ($re, $qs) = $ctx->url_filter;
135 $re // return $ctx->psgi_triple;
136 if (my $ALL = $ctx->{www}->{pi_cfg}->ALL) { # fast path
137 # FIXME: test this in t/
138 $ALL->misc->reopen->retry_reopen(\&add_misc_ibx,
140 } else { # slow path, no [extindex "all"] configured
141 my $iter = PublicInbox::ConfigIter->new($ctx->{www}->{pi_cfg},
142 \&list_match_i, $re, $ctx);
144 $ctx->{-wcb} = $_[0]; # HTTP server callback
145 $ctx->{env}->{'pi-httpd.async'} ?
146 $iter->event_step : $iter->each_section;
151 sub mset_footer ($$) {
152 my ($ctx, $mset) = @_;
153 # no footer if too few matches
154 return '' if $mset->get_matches_estimated == $mset->size;
155 require PublicInbox::SearchView;
156 PublicInbox::SearchView::search_nav_bot($mset, $ctx->{-sq});
160 my ($ctx, $mset) = @_;
162 my $qh = $q->{'q'} // '';
164 $qh = ascii_html($qh);
165 $qh = qq[\nvalue="$qh"] if $qh ne '';
168 action="./"><pre><input
170 type=text$qh /><input
172 value="locate inbox" /></pre></form><pre>
175 if (defined($q->{'q'})) {
176 my $initial_q = $ctx->{-uxs_retried};
177 if (defined $initial_q) {
178 my $rewritten = $q->{'q'};
179 utf8::decode($initial_q);
180 utf8::decode($rewritten);
181 $initial_q = ascii_html($initial_q);
182 $rewritten = ascii_html($rewritten);
183 $rv .= " Warning: Initial query:\n <b>$initial_q</b>\n";
184 $rv .= " returned no results, used:\n";
185 $rv .= " <b>$rewritten</b>\n instead\n\n";
187 $rv .= 'Search results ordered by [';
189 my $d = $q->qs_html(r => 0);
190 $rv .= qq{<a\nhref="?$d">updated</a>|<b>relevance</b>};
192 my $d = $q->qs_html(r => 1);
193 $rv .= qq{<b>updated</b>|<a\nhref="?$d">relevance</a>};
202 my $h = [ 'Content-Type', 'text/html; charset=UTF-8',
203 'Content-Length', undef ];
204 my $gzf = gzf_maybe($h, $ctx->{env});
205 $gzf->zmore('<html><head><title>public-inbox listing</title>' .
206 $ctx->{www}->style('+/') .
209 if (my $list = delete $ctx->{-list}) {
210 my $mset = delete $ctx->{-mset};
212 if ($mset) { # already sorted, so search bar:
213 $gzf->zmore(mset_nav_top($ctx, $mset));
214 } else { # sort config dump by ->modified
215 @$list = map { $_->[1] }
216 sort { $b->[0] <=> $a->[0] } @$list;
218 $gzf->zmore('<pre>');
219 $gzf->zmore(join("\n", @$list));
220 $gzf->zmore(mset_footer($ctx, $mset)) if $mset;
222 $gzf->zmore('<pre>no inboxes, yet');
224 my $out = $gzf->zflush('</pre><hr><pre>'.
225 PublicInbox::WwwStream::code_footer($ctx->{env}) .
226 '</pre></body></html>');
227 $h->[3] = length($out);
228 [ $code, $h, [ $out ] ];