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 (scalar(@{$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 my $user_query = $q->{'q'} // '';
100 if ($user_query =~ /\S/) {
101 $qs = "( $qs ) AND ( $user_query )";
102 } else { # special case for ALL
103 $ctx->ibx_entry($pi_cfg->ALL // die('BUG: ->ALL expected'), {});
105 my $mset = $misc->mset($qs, $opt); # sorts by $MODIFIED (mtime)
106 my $hide_key = $ctx->hide_key;
108 for my $mi ($mset->items) {
109 my $doc = $mi->get_document;
110 my ($eidx_key) = PublicInbox::Search::xap_terms('Q', $doc);
112 my $ibx = $pi_cfg->lookup_eidx_key($eidx_key) // next;
113 next if $ibx->{-hide}->{$hide_key};
114 grep(/$re/, @{$ibx->{url} // []}) or next;
115 $ctx->ibx_entry($ibx, $misc->doc2ibx_cache_ent($doc));
116 if ($r) { # for descriptions in search_nav_bot
117 my $pct = PublicInbox::Search::get_pct($mi);
118 # only when sorting by relevance, ->items is always
119 # ordered descending:
124 if ($r) { # for descriptions in search_nav_bot
125 $q->{-min_pct} = $min;
126 $q->{-max_pct} = $max;
128 $ctx->{-mset} = $mset;
133 my ($class, $ctx) = @_;
135 my ($re, $qs) = $ctx->url_filter;
136 $re // return $ctx->psgi_triple;
137 if (my $ALL = $ctx->{www}->{pi_cfg}->ALL) { # fast path
138 # FIXME: test this in t/
139 $ALL->misc->reopen->retry_reopen(\&add_misc_ibx,
141 } else { # slow path, no [extindex "all"] configured
142 my $iter = PublicInbox::ConfigIter->new($ctx->{www}->{pi_cfg},
143 \&list_match_i, $re, $ctx);
145 $ctx->{-wcb} = $_[0]; # HTTP server callback
146 $ctx->{env}->{'pi-httpd.async'} ?
147 $iter->event_step : $iter->each_section;
152 sub mset_footer ($$) {
153 my ($ctx, $mset) = @_;
154 # no footer if too few matches
155 return '' if $mset->get_matches_estimated == $mset->size;
156 require PublicInbox::SearchView;
157 PublicInbox::SearchView::search_nav_bot($mset, $ctx->{-sq});
161 my ($ctx, $mset) = @_;
163 my $qh = $q->{'q'} // '';
165 $qh = ascii_html($qh);
166 $qh = qq[\nvalue="$qh"] if $qh ne '';
169 action="./"><pre><input
171 type=text$qh /><input
173 value="locate inbox" /></pre></form><pre>
176 if (defined($q->{'q'})) {
177 my $initial_q = $ctx->{-uxs_retried};
178 if (defined $initial_q) {
179 my $rewritten = $q->{'q'};
180 utf8::decode($initial_q);
181 utf8::decode($rewritten);
182 $initial_q = ascii_html($initial_q);
183 $rewritten = ascii_html($rewritten);
184 $rv .= " Warning: Initial query:\n <b>$initial_q</b>\n";
185 $rv .= " returned no results, used:\n";
186 $rv .= " <b>$rewritten</b>\n instead\n\n";
188 $rv .= 'Search results ordered by [';
190 my $d = $q->qs_html(r => 0);
191 $rv .= qq{<a\nhref="?$d">updated</a>|<b>relevance</b>};
193 my $d = $q->qs_html(r => 1);
194 $rv .= qq{<b>updated</b>|<a\nhref="?$d">relevance</a>};
203 my $h = [ 'Content-Type', 'text/html; charset=UTF-8',
204 'Content-Length', undef ];
205 my $gzf = gzf_maybe($h, $ctx->{env});
206 $gzf->zmore('<html><head><title>public-inbox listing</title>' .
207 $ctx->{www}->style('+/') .
210 if (my $list = delete $ctx->{-list}) {
211 my $mset = delete $ctx->{-mset};
213 if ($mset) { # already sorted, so search bar:
214 $gzf->zmore(mset_nav_top($ctx, $mset));
215 } else { # sort config dump by ->modified
216 @$list = map { $_->[1] }
217 sort { $b->[0] <=> $a->[0] } @$list;
219 $gzf->zmore('<pre>');
220 $gzf->zmore(join("\n", @$list));
221 $gzf->zmore(mset_footer($ctx, $mset)) if $mset;
222 } elsif (my $mset = delete $ctx->{-mset}) {
223 $gzf->zmore(mset_nav_top($ctx, $mset));
224 $gzf->zmore('<pre>no matching inboxes');
225 $gzf->zmore(mset_footer($ctx, $mset));
227 $gzf->zmore('<pre>no inboxes, yet');
229 my $out = $gzf->zflush('</pre><hr><pre>'.
230 qq(This is a listing of public inboxes, see the `mirror' link of each inbox
231 for instructions on how to mirror all the data and code on this site.) .
232 '</pre></body></html>');
233 $h->[3] = length($out);
234 [ $code, $h, [ $out ] ];