]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Feed.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / Feed.pm
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Used for generating Atom feeds for web-accessible mailing list archives.
5 package PublicInbox::Feed;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::View;
9 use PublicInbox::WwwAtomStream;
10 use PublicInbox::Smsg; # this loads w/o Search::Xapian
11
12 sub generate_i {
13         my ($ctx) = @_;
14         shift @{$ctx->{msgs}};
15 }
16
17 # main function
18 sub generate {
19         my ($ctx) = @_;
20         my $msgs = $ctx->{msgs} = recent_msgs($ctx);
21         return _no_thread() unless @$msgs;
22         PublicInbox::WwwAtomStream->response($ctx, \&generate_i);
23 }
24
25 sub generate_thread_atom {
26         my ($ctx) = @_;
27         my $msgs = $ctx->{msgs} = $ctx->{ibx}->over->get_thread($ctx->{mid});
28         return _no_thread() unless @$msgs;
29         PublicInbox::WwwAtomStream->response($ctx, \&generate_i);
30 }
31
32 sub generate_html_index {
33         my ($ctx) = @_;
34         # if the 'r' query parameter is given, it is a legacy permalink
35         # which we must continue supporting:
36         my $qp = $ctx->{qp};
37         my $ibx = $ctx->{ibx};
38         if ($qp && !$qp->{r} && $ibx->over) {
39                 return PublicInbox::View::index_topics($ctx);
40         }
41
42         my $env = $ctx->{env};
43         my $url = $ibx->base_url($env) . 'new.html';
44         my $qs = $env->{QUERY_STRING};
45         $url .= "?$qs" if $qs ne '';
46         [302, [ 'Location', $url, 'Content-Type', 'text/plain'],
47                 [ "Redirecting to $url\n" ] ];
48 }
49
50 sub new_html_i {
51         my ($ctx, $eml) = @_;
52         print { $ctx->zfh } $ctx->html_top if exists $ctx->{-html_tip};
53
54         if ($eml) {
55                 $ctx->{smsg}->populate($eml) if !$ctx->{ibx}->{over};
56                 return PublicInbox::View::eml_entry($ctx, $eml);
57         }
58         my $smsg = shift @{$ctx->{msgs}} or
59                 print { $ctx->zfh } PublicInbox::View::pagination_footer(
60                                                 $ctx, './new.html');
61         $smsg;
62 }
63
64 sub new_html {
65         my ($ctx) = @_;
66         my $msgs = $ctx->{msgs} = recent_msgs($ctx);
67         if (!@$msgs) {
68                 return [404, ['Content-Type', 'text/plain'],
69                         ["No messages, yet\n"] ];
70         }
71         $ctx->{-html_tip} = '<pre>';
72         $ctx->{-upfx} = '';
73         $ctx->{-spfx} = '' if $ctx->{ibx}->{coderepo};
74         $ctx->{-hr} = 1;
75         PublicInbox::WwwStream::aresponse($ctx, \&new_html_i);
76 }
77
78 # private subs
79
80 sub _no_thread () {
81         [404, ['Content-Type', 'text/plain'], ["No feed found for thread\n"]];
82 }
83
84 sub recent_msgs {
85         my ($ctx) = @_;
86         my $ibx = $ctx->{ibx};
87         my $max = $ibx->{feedmax} // 25;
88         return PublicInbox::View::paginate_recent($ctx, $max) if $ibx->over;
89
90         # only for rare v1 inboxes which aren't indexed at all
91         my $qp = $ctx->{qp};
92         my $hex = '[a-f0-9]';
93         my $addmsg = qr!^:000000 100644 \S+ (\S+) A\t${hex}{2}/${hex}{38}$!;
94         my $delmsg = qr!^:100644 000000 (\S+) \S+ D\t(${hex}{2}/${hex}{38})$!;
95         my $refhex = qr/(?:HEAD|${hex}{4,})(?:~[0-9]+)?/;
96
97         # revision ranges may be specified
98         my $range = 'HEAD';
99         my $r = $qp->{r} if $qp;
100         if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
101                 $range = $r;
102         }
103
104         # get recent messages
105         # we could use git log -z, but, we already know ssoma will not
106         # leave us with filenames with spaces in them..
107         my $log = $ibx->git->popen(qw/log
108                                 --no-notes --no-color --raw -r
109                                 --no-abbrev --abbrev-commit/,
110                                 "--format=%H", $range);
111         my %deleted; # only an optimization at this point
112         my $last;
113         my $last_commit;
114         local $/ = "\n";
115         my @ret;
116         while (defined(my $line = <$log>)) {
117                 if ($line =~ /$addmsg/o) {
118                         my $add = $1;
119                         next if $deleted{$add}; # optimization-only
120                         push(@ret, bless { blob => $add }, 'PublicInbox::Smsg');
121                         if (scalar(@ret) >= $max) {
122                                 $last = 1;
123                                 last;
124                         }
125                 } elsif ($line =~ /$delmsg/o) {
126                         $deleted{$1} = 1;
127                 }
128         }
129
130         if ($last) {
131                 local $/ = "\n";
132                 while (my $line = <$log>) {
133                         if ($line =~ /^(${hex}{7,})/) {
134                                 $last_commit = $1;
135                                 last;
136                         }
137                 }
138         }
139
140         $last_commit and
141                 $ctx->{next_page} = qq[<a\nhref="?r=$last_commit"\nrel=next>] .
142                                         'next (older)</a>';
143         \@ret;
144 }
145
146 1;