1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 # Used for displaying the HTML web interface.
5 # See Documentation/design_www.txt for this.
6 package PublicInbox::View;
9 use URI::Escape qw/uri_escape_utf8/;
10 use Date::Parse qw/str2time/;
11 use Encode qw/find_encoding/;
12 use Encode::MIME::Header;
13 use Email::MIME::ContentType qw/parse_content_type/;
14 use PublicInbox::Hval;
15 use PublicInbox::MID qw/mid_clean id_compress mid2path/;
16 use Digest::SHA qw/sha1_hex/;
18 my $MBOX_TITLE = 'title="download thread as gzipped mbox"';
21 # TODO: make these constants tunable
22 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
23 use constant MAX_TRUNC_LEN => 72;
24 use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
25 use constant T_ANCHOR => '#u';
26 use constant INDENT => ' ';
28 *ascii_html = *PublicInbox::Hval::ascii_html;
30 my $enc_utf8 = find_encoding('UTF-8');
34 my ($ctx, $mime, $full_pfx, $footer) = @_;
35 if (defined $footer) {
36 $footer = "\n" . $footer;
40 headers_to_html_header($mime, $full_pfx, $ctx) .
41 multipart_text_as_html($mime, $full_pfx) .
42 '</pre><hr />' . PRE_WRAP .
43 html_footer($mime, 1, $full_pfx, $ctx) .
45 '</pre></body></html>';
49 my ($class, $mime, $full_pfx) = @_;
51 PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
55 my ($header_obj) = @_;
56 my $irt = $header_obj->header('In-Reply-To');
58 return mid_clean($irt) if (defined $irt);
60 my $refs = $header_obj->header('References');
61 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
67 # this is already inside a <pre>
69 my ($fh, $mime, $level, $state) = @_;
70 my $midx = $state->{anchor_idx}++;
71 my $ctx = $state->{ctx};
72 my $srch = $ctx->{srch};
73 my ($prev, $next) = ($midx - 1, $midx + 1);
75 my $enc = enc_for($mime->header("Content-Type"));
76 my $subj = $mime->header('Subject');
77 my $header_obj = $mime->header_obj;
79 my $mid_raw = mid_clean($header_obj->header('Message-ID'));
80 my $id = anchor_for($mid_raw);
81 my $seen = $state->{seen};
82 $seen->{$id} = "#$id"; # save the anchor for children, later
84 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
85 my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
86 my @from = Email::Address->parse($from);
87 $from = $from[0]->name;
89 $from = PublicInbox::Hval->new_oneline($from)->as_html;
90 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
91 my $more = 'permalink';
92 my $root_anchor = $state->{root_anchor} || '';
93 my $path = $root_anchor ? '../../' : '';
94 my $href = $mid->as_href;
95 my $irt = in_reply_to($header_obj);
96 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
99 my $t = $ctx->{flat} ? 'T' : 't';
100 $subj = "<a\nhref=\"${path}$href/$t/#u\">$subj</a>";
102 if ($root_anchor eq $id) {
103 $subj = "<u\nid=\"u\">$subj</u>";
106 my $ts = _msg_date($mime);
107 my $rv = "<table\nsummary=l$level><tr>";
109 $rv .= '<td><pre>' . (INDENT x $level) . '</pre></td>';
111 $rv .= "<td\nid=s$midx>" . PRE_WRAP;
112 $rv .= "<b\nid=\"$id\">$subj</b>\n";
113 $rv .= "- $from @ $ts UTC - ";
114 $rv .= "<a\nhref=\"#s$next\">next</a>";
116 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
118 $fh->write($rv .= "\n\n");
120 my ($fhref, $more_ref);
121 my $mhref = "${path}$href/";
123 # show full message if it's our root message
124 if ($root_anchor ne $id || ($level != 0 && !$ctx->{flat})) {
125 $fhref = "${path}$href/f/";
128 # scan through all parts, looking for displayable text
129 $mime->walk_parts(sub {
130 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
134 my $txt = "${path}$href/raw";
135 $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
136 $rv .= html_footer($mime, 0, undef, $ctx);
139 unless (defined $parent_anchor) {
140 my $v = PublicInbox::Hval->new_msgid($irt, 1);
142 $parent_anchor = "${path}$v/";
144 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
146 if (my $pct = $state->{pct}) {
147 $rv .= " [$pct->{$mid_raw}%]";
150 $rv .= " [<a\nhref=\"${path}$href/t/#u\">threaded</a>" .
153 $rv .= " [<b>threaded</b>|" .
154 "<a\nhref=\"${path}$href/T/#u\">flat</a>]";
158 $fh->write($rv .= '</pre></td></tr></table>');
162 my ($ctx, $foot, $srch) = @_;
163 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
166 # only private functions below.
168 sub emit_thread_html {
169 my ($cb, $ctx, $foot, $srch) = @_;
170 my $mid = $ctx->{mid};
171 my $res = $srch->get_thread($mid);
172 my $msgs = load_results($res);
173 my $nr = scalar @$msgs;
174 return missing_thread($cb, $ctx) if $nr == 0;
175 my $flat = $ctx->{flat};
181 root_anchor => anchor_for($mid),
185 require PublicInbox::GitCatFile;
186 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
188 pre_anchor_entry($seen, $_) for (@$msgs);
189 __thread_entry(\$cb, $git, $state, $_, 0) for (@$msgs);
191 my $th = thread_results($msgs);
192 thread_entry(\$cb, $git, $state, $_, 0) for $th->rootset;
195 Email::Address->purge_cache;
197 # there could be a race due to a message being deleted in git
198 # but still being in the Xapian index:
199 return missing_thread($cb, $ctx) if ($orig_cb eq $cb);
201 my $final_anchor = $state->{anchor_idx};
202 my $next = "<a\nid=\"s$final_anchor\">";
203 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
204 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>";
205 $next .= "\ndownload thread: ";
206 $next .= "<a\n$MBOX_TITLE\nhref=\"../t.mbox.gz\">mbox.gz</a>";
207 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
208 $cb->write("<hr />" . PRE_WRAP . $next . "\n\n". $foot .
209 "</pre></body></html>");
214 my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
215 my $s = add_text_body($enc, $part, $part_nr, $fhref);
218 # drop the remainder of git patches, they're usually better
219 # to review when the full message is viewed
220 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
223 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
226 # kill any leading or trailing whitespace lines
231 # kill per-line trailing whitespace
232 $s =~ s/[ \t]+$//sgm;
233 $s .= "\n" unless $s =~ /\n\z/s;
239 my ($ct, $default) = @_;
240 $default ||= $enc_utf8;
241 defined $ct or return $default;
242 my $ct_parsed = parse_content_type($ct);
244 if (my $charset = $ct_parsed->{attributes}->{charset}) {
245 my $enc = find_encoding($charset);
252 sub multipart_text_as_html {
253 my ($mime, $full_pfx, $srch) = @_;
256 my $enc = enc_for($mime->header("Content-Type"));
258 # scan through all parts, looking for displayable text
259 $mime->walk_parts(sub {
261 $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx, 1);
267 sub add_filename_line {
271 $fn = $enc->decode($fn);
273 $pad x= ($len/2) if ($len > 0);
274 "$pad " . ascii_html($fn) . " $pad\n";
277 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://
279 ?[\@\w\+\&\?\.\%\;/#=-]*)!x;
282 my ($link_map, $s) = @_;
285 # salt this, as this could be exploited to show
286 # links in the HTML which don't show up in the raw mail.
287 my $key = sha1_hex($url . $SALT);
288 $link_map->{$key} = $url;
295 my ($link_map, $s) = @_;
297 # Added "PI-LINK-" prefix to avoid false-positives on git commits
298 $s =~ s!\bPI-LINK-([a-f0-9]{40})\b!
300 my $url = $link_map->{$key};
302 $url = ascii_html($url);
303 "<a\nhref=\"$url\">$url</a>";
305 # false positive or somebody tried to mess with us
313 my ($quot, $n, $part_nr, $full_pfx, $final, $do_anchor) = @_;
316 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
319 my $rv = join('', map { linkify_1(\%l, $_) } @$quot);
321 $rv = ascii_html($rv);
322 return linkify_2(\%l, $rv);
325 # show a short snippet of quoted text and link to full version:
326 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
327 my $cur = join(' ', @$quot);
328 @$quot = split(/\s+/, $cur);
331 my $tmp = shift(@$quot);
332 my $len = length($tmp) + length($cur);
333 if ($len > MAX_TRUNC_LEN) {
338 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
340 $cur =~ s/ \z/ .../s;
341 $cur = ascii_html($cur);
343 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
345 # show everything in the full version with anchor from
346 # short version (see above)
348 my $rv .= join('', map { linkify_1(\%l, $_) } @$quot);
350 $rv = ascii_html($rv);
351 return linkify_2(\%l, $rv) unless $do_anchor;
353 "<a\nid=q${part_nr}_$nr></a>" . linkify_2(\%l, $rv);
358 my ($enc_msg, $part, $part_nr, $full_pfx, $do_anchor) = @_;
359 return '' if $part->subparts;
361 my $ct = $part->content_type;
362 # account for filter bugs...
363 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
367 my $enc = enc_for($ct, $enc_msg);
372 $s = $enc->decode($s);
373 my @lines = split(/^/m, $s);
377 my $fn = $part->filename;
378 defined($fn) or $fn = "part #" . ($$part_nr + 1);
379 $s .= add_filename_line($enc, $fn);
383 while (defined(my $cur = shift @lines)) {
385 # show the previously buffered quote inline
387 $s .= flush_quote(\@quot, \$n, $$part_nr,
388 $full_pfx, 0, $do_anchor);
393 $cur = linkify_1(\%l, $cur);
394 $cur = ascii_html($cur);
395 $s .= linkify_2(\%l, $cur);
401 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1,
404 $s .= "\n" unless $s =~ /\n\z/s;
409 sub headers_to_html_header {
410 my ($mime, $full_pfx, $ctx) = @_;
411 my $srch = $ctx->{srch} if $ctx;
414 my $header_obj = $mime->header_obj;
415 my $mid = $header_obj->header('Message-ID');
416 $mid = PublicInbox::Hval->new_msgid($mid);
417 foreach my $h (qw(From To Cc Subject Date)) {
418 my $v = $header_obj->header($h);
419 defined($v) && ($v ne '') or next;
420 $v = PublicInbox::Hval->new_oneline($v);
423 my @from = Email::Address->parse($v->raw);
424 $title[1] = ascii_html($from[0]->name);
425 } elsif ($h eq 'Subject') {
426 $title[0] = $v->as_html;
428 my $p = $full_pfx ? '' : '../';
429 $rv .= "$h: <a\nid=\"t\"\nhref=\"${p}t/#u\">";
430 $rv .= $v->as_html . "</a>\n";
434 $rv .= "$h: " . $v->as_html . "\n";
437 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
438 my $upfx = $full_pfx ? '' : '../';
439 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
442 if ($header_obj->header('In-Reply-To') ||
443 $header_obj->header('References')) {
444 $rv .= "<a\nhref=\"${upfx}t/#u\">" .
445 "References: [expand]</a>\n";
448 $atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
449 qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
451 $rv .= _parent_headers_nosrch($header_obj);
456 ("<html><head><title>". join(' - ', @title) .
457 "</title>$atom</head><body>" . PRE_WRAP . $rv);
461 my ($dst, $ctx, $cur, $full_pfx) = @_;
462 my $srch = $ctx->{srch};
463 my $mid = mid_clean($cur->header('Message-ID'));
464 my $res = $srch->get_thread($mid);
465 my $nr = $res->{total};
466 my $upfx = $full_pfx ? '' : '../';
467 my $expand = "(<a\nhref=\"${upfx}t/#u\">expand</a> " .
468 "/ <a\nhref=\"${upfx}t.mbox.gz\">mbox.gz</a>)";
471 $$dst .= "\n[no followups, yet] $expand\n";
472 return (undef, in_reply_to($cur));
475 $$dst .= "\n\n~$nr messages in thread: $expand\n";
477 my $subj = $srch->subject_path($cur->header('Subject'));
478 my $parent = in_reply_to($cur);
480 seen => { $subj => 1 },
483 parent_cmp => defined $parent ? $parent : '',
488 for (thread_results(load_results($res))->rootset) {
489 inline_dump($dst, $state, $upfx, $_, 0);
491 ($state->{next_msg}, $state->{parent});
494 sub _parent_headers_nosrch {
495 my ($header_obj) = @_;
498 my $irt = in_reply_to($header_obj);
500 my $v = PublicInbox::Hval->new_msgid($irt, 1);
501 my $html = $v->as_html;
502 my $href = $v->as_href;
503 $rv .= "In-Reply-To: <";
504 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
507 my $refs = $header_obj->header('References');
509 # avoid redundant URLs wasting bandwidth
511 $seen{$irt} = 1 if defined $irt;
513 my @raw_refs = ($refs =~ /<([^>]+)>/g);
514 foreach my $ref (@raw_refs) {
517 push @refs, linkify_ref_nosrch($ref);
521 $rv .= 'References: '. join(' ', @refs) . "\n";
528 my ($mime, $standalone, $full_pfx, $ctx) = @_;
529 my %cc; # everyone else
530 my $to; # this is the From address
532 foreach my $h (qw(From To Cc)) {
533 my $v = $mime->header($h);
534 defined($v) && ($v ne '') or next;
535 my @addrs = Email::Address->parse($v);
536 foreach my $recip (@addrs) {
537 my $address = $recip->address;
538 my $dst = lc($address);
539 $cc{$dst} ||= $address;
543 Email::Address->purge_cache if $standalone;
545 my $subj = $mime->header('Subject') || '';
546 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
547 my $mid = $mime->header('Message-ID');
548 my $irt = uri_escape_utf8($mid);
550 $to = uri_escape_utf8($to);
551 $subj = uri_escape_utf8($subj);
553 my $cc = uri_escape_utf8(join(',', sort values %cc));
554 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
557 my $srch = $ctx->{srch} if $ctx;
558 my $upfx = $full_pfx ? '../' : '../../';
559 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
561 if ($srch && $standalone) {
562 $idx .= qq{ / follow: <a\nhref="t.atom">Atom feed</a>};
565 my ($next, $p) = thread_inline(\$idx, $ctx, $mime, $full_pfx);
567 $p = PublicInbox::Hval->new_oneline($p);
569 $irt = "<a\nhref=\"$upfx$p/\">parent</a> ";
571 $irt = ' ' x length('parent ');
574 $irt .= "<a\nhref=\"$upfx$next/\">next</a> ";
582 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
585 sub linkify_ref_nosrch {
586 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
587 my $html = $v->as_html;
588 my $href = $v->as_href;
589 "<<a\nhref=\"../$href/\">$html</a>>";
595 if ($id !~ /\A[a-f0-9]{40}\z/) {
596 $id = id_compress(mid_clean($id), 1);
601 sub thread_html_head {
602 my ($cb, $header, $state) = @_;
603 $$cb = $$cb->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
605 my $s = PublicInbox::Hval->new_oneline($header->header('Subject'));
607 $$cb->write("<html><head><title>$s</title>".
608 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
609 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
613 sub pre_anchor_entry {
614 my ($seen, $mime) = @_;
615 my $id = anchor_for($mime->header('Message-ID'));
616 $seen->{$id} = "#$id"; # save the anchor for children, later
620 my ($upfx, $mid) = @_;
621 # 'subject dummy' is used internally by Mail::Thread
622 return '[no common parent]' if ($mid eq 'subject dummy');
624 $mid = PublicInbox::Hval->new_msgid($mid);
625 my $href = $mid->as_href;
626 my $html = $mid->as_html;
627 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
631 my ($upfx, $mid, $level) = @_;
632 "<table\nsummary=ghost><tr><td>" .
633 (INDENT x $level) . "</td><td>" .
634 PRE_WRAP . ghost_parent($upfx, $mid) .
635 '</pre></td></table>';
639 my ($cb, $git, $state, $mime, $level) = @_;
641 # lazy load the full message from mini_mime:
643 my $path = mid2path(mid_clean($mime->header('Message-ID')));
644 Email::MIME->new($git->cat_file('HEAD:'.$path));
647 if ($state->{anchor_idx} == 0) {
648 thread_html_head($cb, $mime, $state);
651 if (my $ghost = delete $state->{ghost}) {
652 # n.b. ghost messages may only be parents, not children
653 foreach my $g (@$ghost) {
654 $$cb->write(ghost_table('../../', @$g));
657 index_entry($$cb, $mime, $level, $state);
662 my ($state, $node, $level) = @_;
663 my $ghost = $state->{ghost} ||= [];
664 push @$ghost, [ $node->messageid, $level ];
668 my ($cb, $git, $state, $node, $level) = @_;
670 if (my $mime = $node->message) {
671 unless (__thread_entry($cb, $git, $state, $mime, $level)) {
672 __ghost_entry($state, $node, $level);
675 __ghost_entry($state, $node, $level);
678 thread_entry($cb, $git, $state, $node->child, $level + 1);
679 thread_entry($cb, $git, $state, $node->next, $level);
685 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
690 my $ts = eval { str2time($mime->header('Date')) };
691 defined($ts) ? $ts : 0;
695 my ($msgs, $nosubject) = @_;
696 require PublicInbox::Thread;
697 my $th = PublicInbox::Thread->new(@$msgs);
699 $Mail::Thread::nosubject = $nosubject;
701 $th->order(*sort_ts);
707 require PublicInbox::ExtMsg;
709 $cb->(PublicInbox::ExtMsg::ext_msg($ctx))
714 my $ts = $mime->header('X-PI-TS') || msg_timestamp($mime);
718 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
721 my ($dst, $state, $upfx, $mime, $level) = @_;
722 my $pfx = INDENT x ($level - 1);
723 my $dot = $level == 0 ? '' : '` ';
725 my $cur = $state->{cur};
726 my $mid = mid_clean($mime->header('Message-ID'));
727 my $f = $mime->header('X-PI-From');
728 my $d = _msg_date($mime);
729 $f = PublicInbox::Hval->new($f)->as_html;
730 $d = PublicInbox::Hval->new($d)->as_html;
731 my $attr = "$f @ $d";
732 $state->{first_level} ||= $level;
733 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
734 $state->{prev_attr} = $attr;
735 $attr = ' - ' . $attr;
736 $attr .= ' UTC' if $level >= $state->{first_level};
740 $state->{prev_level} = $level;
744 delete $state->{cur};
745 $$dst .= "$pfx$dot<b><a\nid=\"r\"\nhref=\"#t\">".
746 "[this message]</a></b>$attr\n";
751 $state->{next_msg} ||= $mid;
754 # Subject is never undef, this mail was loaded from
755 # our Xapian which would've resulted in '' if it were
756 # really missing (and Filter rejects empty subjects)
757 my $s = $mime->header('Subject');
758 my $h = $state->{srch}->subject_path($s);
759 if ($state->{seen}->{$h}) {
762 $state->{seen}->{$h} = 1;
763 $s = PublicInbox::Hval->new($s);
766 my $m = PublicInbox::Hval->new_msgid($mid);
767 $m = $upfx . '../' . $m->as_href . '/';
769 $$dst .= "$pfx$dot<a\nhref=\"$m\">$s</a>$attr\n";
771 $$dst .= "$pfx$dot<a\nhref=\"$m\">$f @ $d</a>\n";
776 my ($dst, $state, $upfx, $node, $level) = @_;
778 if (my $mime = $node->message) {
779 my $mid = mid_clean($mime->header('Message-ID'));
780 if ($mid eq $state->{parent_cmp}) {
781 $state->{parent} = $mid;
783 _inline_header($dst, $state, $upfx, $mime, $level);
785 my $dot = $level == 0 ? '' : '` ';
786 my $pfx = (INDENT x $level) . $dot;
788 $$dst .= ghost_parent("$upfx../", $node->messageid) . "\n";
790 inline_dump($dst, $state, $upfx, $node->child, $level+1);
791 inline_dump($dst, $state, $upfx, $node->next, $level);
796 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
797 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
803 (eval { $b->topmost->message->header('X-PI-TS') } || 0) <=>
804 (eval { $a->topmost->message->header('X-PI-TS') } || 0)
808 # accumulate recent topics if search is supported
809 # returns 1 if done, undef if not
811 my ($state, $node, $level) = @_;
813 my $child_adjust = 1;
815 if (my $x = $node->message) {
819 $subj = $x->header('Subject');
820 $subj = $state->{srch}->subject_normalized($subj);
823 # kill "[PATCH v2]" etc. for summarization
824 unless ($level == 0) {
825 $topic =~ s/\A\s*\[[^\]]+\]\s*//g;
828 if (++$state->{subjs}->{$topic} == 1) {
829 push @{$state->{order}}, [ $level, $subj, $topic ];
832 my $mid = mid_clean($x->header('Message-ID'));
834 my $ts = $x->header('X-PI-TS');
835 my $exist = $state->{latest}->{$topic};
836 if (!$exist || $exist->[1] < $ts) {
837 $state->{latest}->{$topic} = [ $mid, $ts ];
840 # ghost message, do not bump level
844 add_topic($state, $node->child, $level + $child_adjust);
845 add_topic($state, $node->next, $level);
850 my $order = $state->{order};
851 my $subjs = $state->{subjs};
852 my $latest = $state->{latest};
853 return "\n[No recent topics]</pre>" unless (scalar @$order);
858 while (defined(my $info = shift @$order)) {
859 my ($level, $subj, $topic) = @$info;
860 my $n = delete $subjs->{$topic};
861 my ($mid, $ts) = @{delete $latest->{$topic}};
862 $mid = PublicInbox::Hval->new($mid)->as_href;
863 $subj = PublicInbox::Hval->new($subj)->as_html;
864 $pfx = INDENT x ($level - 1);
865 my $nl = $level == $prev ? "\n" : '';
866 my $dot = $level == 0 ? '' : '` ';
867 $dst .= "$nl$pfx$dot<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";
875 # $n isn't the total number of posts on the topic,
876 # just the number of posts in the current results
877 # window, so leave it unlabeled
881 if ($level == 0 || $attr ne $prev_attr) {
882 my $mbox = qq(<a\n$MBOX_TITLE\n) .
883 qq(href="$mid/t.mbox.gz">mbox.gz</a>);
884 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
885 $pfx .= INDENT if $level > 0;
886 $dst .= $pfx . $attr . $n . " - $mbox / $atom\n";
893 sub emit_index_topics {
894 my ($state, $fh) = @_;
895 my $off = $state->{ctx}->{cgi}->param('o');
896 $off = 0 unless defined $off;
897 $state->{order} = [];
898 $state->{subjs} = {};
899 $state->{latest} = {};
901 my %opts = ( offset => int $off, limit => $max * 4 );
902 while (scalar @{$state->{order}} < $max) {
903 my $res = $state->{srch}->query('', \%opts);
904 my $nr = scalar @{$res->{msgs}} or last;
906 for (rsort_ts(thread_results(load_results($res), 1)->rootset)) {
907 add_topic($state, $_, 0);
909 $opts{offset} += $nr;
912 $fh->write(dump_topics($state));