]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Feed.pm
feed: remove threading from index
[public-inbox.git] / lib / PublicInbox / Feed.pm
1 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::Feed;
4 use strict;
5 use warnings;
6 use Email::Address;
7 use Email::MIME;
8 use Date::Parse qw(strptime str2time);
9 use PublicInbox::Hval;
10 use PublicInbox::GitCatFile;
11 use PublicInbox::View;
12 use constant {
13         DATEFMT => '%Y-%m-%dT%H:%M:%SZ', # atom standard
14         MAX_PER_PAGE => 25, # this needs to be tunable
15         PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">",
16 };
17
18 # main function
19 sub generate {
20         my ($class, $ctx) = @_;
21         require XML::Atom::SimpleFeed;
22         require POSIX;
23         my $max = $ctx->{max} || MAX_PER_PAGE;
24
25         my $feed_opts = get_feedopts($ctx);
26         my $addr = $feed_opts->{address};
27         $addr = $addr->[0] if ref($addr);
28         my $feed = XML::Atom::SimpleFeed->new(
29                 title => $feed_opts->{description} || "unnamed feed",
30                 link => $feed_opts->{url} || "http://example.com/",
31                 link => {
32                         rel => 'self',
33                         href => $feed_opts->{atomurl} ||
34                                 "http://example.com/atom.xml",
35                 },
36                 id => 'mailto:' . ($addr || 'public-inbox@example.com'),
37                 updated => POSIX::strftime(DATEFMT, gmtime),
38         );
39         $feed->no_generator;
40
41         my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
42         each_recent_blob($ctx, sub {
43                 my ($add, undef) = @_;
44                 add_to_feed($feed_opts, $feed, $add, $git);
45         });
46         $git = undef; # destroy pipes
47         Email::Address->purge_cache;
48         $feed->as_string;
49 }
50
51 sub generate_html_index {
52         my ($class, $ctx) = @_;
53
54         my $max = $ctx->{max} || MAX_PER_PAGE;
55         my $feed_opts = get_feedopts($ctx);
56
57         my $title = $feed_opts->{description} || '';
58         $title = PublicInbox::Hval->new_oneline($title)->as_html;
59
60         my $html = "<html><head><title>$title</title>" .
61                 '<link rel="alternate" title="Atom feed"' . "\nhref=\"" .
62                 $feed_opts->{atomurl} . "\"\ntype=\"application/atom+xml\"/>" .
63                 '</head><body>' . PRE_WRAP;
64
65         my $state;
66         my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
67         my (undef, $last) = each_recent_blob($ctx, sub {
68                 my ($path, $commit) = @_;
69                 unless (defined $state) {
70                         $state = [ $ctx->{srch}, {}, $commit, 0 ];
71                 }
72                 my $mime = do_cat_mail($git, $_[0]) or return 0;
73                 my $t = eval { str2time($mime->header('Date')) };
74                 defined($t) or $t = 0;
75                 $mime->header_set('X-PI-TS', $t);
76                 $html .= PublicInbox::View->index_entry($mime, 0, $state);
77                 1;
78         });
79         Email::Address->purge_cache;
80         $git = undef; # destroy pipes.
81
82         my $footer = nav_footer($ctx->{cgi}, $last, $feed_opts, $state);
83         if ($footer) {
84                 my $list_footer = $ctx->{footer};
85                 $footer .= "\n" . $list_footer if $list_footer;
86                 $footer = "<hr />" . PRE_WRAP . "$footer</pre>";
87         }
88         $html . "</pre>$footer</body></html>";
89 }
90
91 # private subs
92
93 sub nav_footer {
94         my ($cgi, $last, $feed_opts, $state) = @_;
95         $cgi or return '';
96         my $old_r = $cgi->param('r');
97         my $head = '    ';
98         my $next = '    ';
99         my $first = $state->[2];
100         my $anchor = $state->[3];
101
102         if ($last) {
103                 $next = qq!<a\nhref="?r=$last">next</a>!;
104         }
105         if ($old_r) {
106                 $head = $cgi->path_info;
107                 $head = qq!<a\nhref="$head">head</a>!;
108         }
109         my $atom = "<a\nhref=\"$feed_opts->{atomurl}\">atom</a>";
110         my $permalink = "<a\nhref=\"?r=$first\">permalink</a>";
111         "<a\nname=\"s$anchor\">page:</a> $next $head $atom $permalink";
112 }
113
114 sub each_recent_blob {
115         my ($ctx, $cb) = @_;
116         my $max = $ctx->{max} || MAX_PER_PAGE;
117         my $hex = '[a-f0-9]';
118         my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
119         my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
120         my $refhex = qr/${hex}{4,40}(?:~\d+)?/;
121         my $cgi = $ctx->{cgi};
122
123         # revision ranges may be specified
124         my $range = 'HEAD';
125         my $r = $cgi->param('r') if $cgi;
126         if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
127                 $range = $r;
128         }
129
130         # get recent messages
131         # we could use git log -z, but, we already know ssoma will not
132         # leave us with filenames with spaces in them..
133         my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
134                         qw/log --no-notes --no-color --raw -r
135                            --abbrev=16 --abbrev-commit/);
136         push @cmd, $range;
137
138         my $pid = open(my $log, '-|', @cmd) or
139                 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
140         my %deleted; # only an optimization at this point
141         my $last;
142         my $nr = 0;
143         my ($cur_commit, $first_commit, $last_commit);
144         while (my $line = <$log>) {
145                 if ($line =~ /$addmsg/o) {
146                         my $add = $1;
147                         next if $deleted{$add}; # optimization-only
148                         $nr += $cb->($add, $cur_commit);
149                         if ($nr >= $max) {
150                                 $last = 1;
151                                 last;
152                         }
153                 } elsif ($line =~ /$delmsg/o) {
154                         $deleted{$1} = 1;
155                 } elsif ($line =~ /^commit (${hex}{7,40})/o) {
156                         $cur_commit = $1;
157                         $first_commit = $1 unless defined $first_commit;
158                 }
159         }
160
161         if ($last) {
162                 while (my $line = <$log>) {
163                         if ($line =~ /^commit (${hex}{7,40})/o) {
164                                 $last_commit = $1;
165                                 last;
166                         }
167                 }
168         }
169
170         close $log; # we may EPIPE here
171         # for pagination
172         ($first_commit, $last_commit);
173 }
174
175 # private functions below
176 sub get_feedopts {
177         my ($ctx) = @_;
178         my $pi_config = $ctx->{pi_config};
179         my $listname = $ctx->{listname};
180         my $cgi = $ctx->{cgi};
181         my %rv;
182         if (open my $fh, '<', "$ctx->{git_dir}/description") {
183                 chomp($rv{description} = <$fh>);
184                 close $fh;
185         }
186
187         if ($pi_config && defined $listname && length $listname) {
188                 foreach my $key (qw(address)) {
189                         $rv{$key} = $pi_config->get($listname, $key) || "";
190                 }
191         }
192         my $url_base;
193         if ($cgi) {
194                 my $path_info = $cgi->path_info;
195                 my $base;
196                 if (ref($cgi) eq 'CGI') {
197                         $base = $cgi->url(-base);
198                 } else {
199                         $base = $cgi->base->as_string;
200                         $base =~ s!/\z!!;
201                 }
202                 $url_base = $path_info;
203                 if ($url_base =~ s!/(?:|index\.html)?\z!!) {
204                         $rv{atomurl} = "$base$url_base/atom.xml";
205                 } else {
206                         $url_base =~ s!/atom\.xml\z!!;
207                         $rv{atomurl} = $base . $path_info;
208                         $url_base = $base . $url_base; # XXX is this needed?
209                 }
210         } else {
211                 $url_base = "http://example.com";
212                 $rv{atomurl} = "$url_base/atom.xml";
213         }
214         $rv{url} ||= "$url_base/";
215         $rv{midurl} = "$url_base/m/";
216         $rv{fullurl} = "$url_base/f/";
217
218         \%rv;
219 }
220
221 sub mime_header {
222         my ($mime, $name) = @_;
223         PublicInbox::Hval->new_oneline($mime->header($name))->raw;
224 }
225
226 sub feed_date {
227         my ($date) = @_;
228         my @t = eval { strptime($date) };
229
230         scalar(@t) ? POSIX::strftime(DATEFMT, @t) : 0;
231 }
232
233 # returns 0 (skipped) or 1 (added)
234 sub add_to_feed {
235         my ($feed_opts, $feed, $add, $git) = @_;
236
237         my $mime = do_cat_mail($git, $add) or return 0;
238         my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
239         my $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
240
241         my $header_obj = $mime->header_obj;
242         my $mid = $header_obj->header_raw('Message-ID');
243         defined $mid or return 0;
244         $mid = PublicInbox::Hval->new_msgid($mid);
245         my $href = $mid->as_href . '.html';
246         my $content = PublicInbox::View->feed_entry($mime, $fullurl . $href);
247         defined($content) or return 0;
248         $mime = undef;
249
250         my $subject = mime_header($header_obj, 'Subject') or return 0;
251
252         my $from = mime_header($header_obj, 'From') or return 0;
253         my @from = Email::Address->parse($from);
254         my $name = $from[0]->name;
255         defined $name or $name = "";
256         my $email = $from[0]->address;
257         defined $email or $email = "";
258
259         my $date = $header_obj->header('Date');
260         $date = PublicInbox::Hval->new_oneline($date);
261         $date = feed_date($date->raw) or return 0;
262         $add =~ tr!/!!d;
263         my $h = '[a-f0-9]';
264         my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
265
266         $feed->add_entry(
267                 author => { name => $name, email => $email },
268                 title => $subject,
269                 updated => $date,
270                 content => { type => 'xhtml', content => $content },
271                 link => $midurl . $href,
272                 id => 'urn:uuid:' . join('-', @uuid5),
273         );
274         1;
275 }
276
277 sub do_cat_mail {
278         my ($git, $path) = @_;
279         my $mime = eval {
280                 my $str = $git->cat_file("HEAD:$path");
281                 Email::MIME->new($str);
282         };
283         $@ ? undef : $mime;
284 }
285
286 1;