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);
14 use PublicInbox::MID qw(mid_escape);
17 my ($ctx, $ibx, $ce) = @_;
18 my $desc = ascii_html($ce->{description} //= $ibx->description);
19 my $ts = fmt_ts($ce->{-modified} //= $ibx->modified);
21 if (scalar(@{$ibx->{url} // []})) {
22 $url = $href = ascii_html(prurl($ctx->{env}, $ibx->{url}));
24 $href = ascii_html(uri_escape_utf8($ibx->{name})) . '/';
25 $url = ascii_html($ibx->{name});
28 * $ts - <a\nhref="$href">$url</a>
31 if (defined($url = $ibx->{infourl})) {
32 $url = ascii_html(prurl($ctx->{env}, $url));
33 $tmp .= qq( <a\nhref="$url">$url</a>\n);
35 push(@{$ctx->{-list}}, (scalar(@_) == 3 ? # $misc in use, already sorted
36 $tmp : [ $ce->{-modified}, $tmp ] ));
39 sub list_match_i { # ConfigIter callback
40 my ($cfg, $section, $re, $ctx) = @_;
41 if (defined($section)) {
42 return if $section !~ m!\Apublicinbox\.([^/]+)\z!;
43 my $ibx = $cfg->lookup_name($1) or return;
44 if (!$ibx->{-hide}->{$ctx->hide_key} &&
45 grep(/$re/, @{$ibx->{url} // []})) {
46 $ctx->ibx_entry($ibx);
48 } else { # undef == "EOF"
49 $ctx->{-wcb}->($ctx->psgi_triple);
54 my ($ctx, $key, $default) = @_;
55 $key //= 'publicInbox.wwwListing';
57 my $v = $ctx->{www}->{pi_cfg}->{lc $key} // $default;
59 if ($v eq 'match=domain') {
60 my $h = $ctx->{env}->{HTTP_HOST} // $ctx->{env}->{SERVER_NAME};
62 (qr!\A(?:https?:)?//\Q$h\E(?::[0-9]+)?/!i, "url:$h");
63 } elsif ($v eq 'all') {
65 } elsif ($v eq '404') {
69 `$v' is not a valid value for `$key'
70 $key be one of `all', `match=domain', or `404'
72 $v = $default; # 'match=domain' or 'all'
77 sub hide_key { 'www' }
79 sub add_misc_ibx { # MiscSearch->retry_reopen callback
80 my ($misc, $ctx, $re, $qs) = @_;
81 require PublicInbox::SearchQuery;
82 my $q = $ctx->{-sq} = PublicInbox::SearchQuery->new($ctx->{qp});
84 my ($asc, $min, $max);
87 $o = -($o + 1); # so [-1] is the last element, like Perl lists
98 delete $ctx->{-list}; # reset if retried
99 my $pi_cfg = $ctx->{www}->{pi_cfg};
100 my $user_query = $q->{'q'} // '';
101 if ($user_query =~ /\S/) {
102 $qs = "( $qs ) AND ( $user_query )";
103 } else { # special case for ALL
104 $ctx->ibx_entry($pi_cfg->ALL // die('BUG: ->ALL expected'), {});
106 my $mset = $misc->mset($qs, $opt); # sorts by $MODIFIED (mtime)
107 my $hide_key = $ctx->hide_key;
109 for my $mi ($mset->items) {
110 my $doc = $mi->get_document;
111 my ($eidx_key) = PublicInbox::Search::xap_terms('Q', $doc);
113 my $ibx = $pi_cfg->lookup_eidx_key($eidx_key) // next;
114 next if $ibx->{-hide}->{$hide_key};
115 grep(/$re/, @{$ibx->{url} // []}) or next;
116 $ctx->ibx_entry($ibx, $misc->doc2ibx_cache_ent($doc));
117 if ($r) { # for descriptions in search_nav_bot
118 my $pct = PublicInbox::Search::get_pct($mi);
119 # only when sorting by relevance, ->items is always
120 # ordered descending:
125 if ($r) { # for descriptions in search_nav_bot
126 $q->{-min_pct} = $min;
127 $q->{-max_pct} = $max;
129 $ctx->{-mset} = $mset;
134 my ($class, $ctx) = @_;
136 my ($re, $qs) = $ctx->url_filter;
137 $re // return $ctx->psgi_triple;
138 if (my $ALL = $ctx->{www}->{pi_cfg}->ALL) { # fast path
139 if ($ctx->{qp}->{a} && # "search all inboxes"
141 my $u = 'all/?q='.mid_escape($ctx->{qp}->{'q'});
142 return [ 302, [ 'Location' => $u,
143 qw(Content-Type text/plain) ],
144 [ "Redirecting to $u\n" ] ];
146 # FIXME: test this in t/
147 $ALL->misc->reopen->retry_reopen(\&add_misc_ibx,
149 } else { # slow path, no [extindex "all"] configured
150 my $iter = PublicInbox::ConfigIter->new($ctx->{www}->{pi_cfg},
151 \&list_match_i, $re, $ctx);
153 $ctx->{-wcb} = $_[0]; # HTTP server callback
154 $ctx->{env}->{'pi-httpd.async'} ?
155 $iter->event_step : $iter->each_section;
160 sub mset_footer ($$) {
161 my ($ctx, $mset) = @_;
162 # no footer if too few matches
163 return '' if $mset->get_matches_estimated == $mset->size;
164 require PublicInbox::SearchView;
165 PublicInbox::SearchView::search_nav_bot($mset, $ctx->{-sq});
169 my ($ctx, $mset) = @_;
171 my $qh = $q->{'q'} // '';
173 $qh = ascii_html($qh);
174 $qh = qq[\nvalue="$qh"] if $qh ne '';
177 action="./"><pre><input name=q type=text$qh
178 /><input type=submit value="locate inbox"
179 /><input type=submit name=a value="search all inboxes"
183 if (defined($q->{'q'})) {
184 my $initial_q = $ctx->{-uxs_retried};
185 if (defined $initial_q) {
186 my $rewritten = $q->{'q'};
187 utf8::decode($initial_q);
188 utf8::decode($rewritten);
189 $initial_q = ascii_html($initial_q);
190 $rewritten = ascii_html($rewritten);
191 $rv .= " Warning: Initial query:\n <b>$initial_q</b>\n";
192 $rv .= " returned no results, used:\n";
193 $rv .= " <b>$rewritten</b>\n instead\n\n";
195 $rv .= 'Search results ordered by [';
197 my $d = $q->qs_html(r => 0);
198 $rv .= qq{<a\nhref="?$d">updated</a>|<b>relevance</b>};
200 my $d = $q->qs_html(r => 1);
201 $rv .= qq{<b>updated</b>|<a\nhref="?$d">relevance</a>};
210 my $h = [ 'Content-Type', 'text/html; charset=UTF-8',
211 'Content-Length', undef ];
212 my $gzf = gzf_maybe($h, $ctx->{env});
213 $gzf->zmore('<html><head><title>public-inbox listing</title>' .
214 $ctx->{www}->style('+/') .
217 if (my $list = delete $ctx->{-list}) {
218 my $mset = delete $ctx->{-mset};
220 if ($mset) { # already sorted, so search bar:
221 $gzf->zmore(mset_nav_top($ctx, $mset));
222 } else { # sort config dump by ->modified
223 @$list = map { $_->[1] }
224 sort { $b->[0] <=> $a->[0] } @$list;
226 $gzf->zmore('<pre>');
227 $gzf->zmore(join("\n", @$list));
228 $gzf->zmore(mset_footer($ctx, $mset)) if $mset;
229 } elsif (my $mset = delete $ctx->{-mset}) {
230 $gzf->zmore(mset_nav_top($ctx, $mset));
231 $gzf->zmore('<pre>no matching inboxes');
232 $gzf->zmore(mset_footer($ctx, $mset));
234 $gzf->zmore('<pre>no inboxes, yet');
236 my $out = $gzf->zflush('</pre><hr><pre>'.
237 qq(This is a listing of public inboxes, see the `mirror' link of each inbox
238 for instructions on how to mirror all the data and code on this site.) .
239 '</pre></body></html>');
240 $h->[3] = length($out);
241 [ $code, $h, [ $out ] ];