]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Feed.pm
allow running as a Plack app without CGI emulation
[public-inbox.git] / lib / PublicInbox / Feed.pm
index 3bee3a517dafd0ef86da3668b81c527593a14e25..220703709264b988c3a503f99fa7cc789a56e04a 100644 (file)
@@ -36,7 +36,7 @@ sub generate {
                        href => $feed_opts->{atomurl} ||
                                "http://example.com/atom.xml",
                },
-               id => $addr || 'public-inbox@example.com',
+               id => 'mailto:' . ($addr || 'public-inbox@example.com'),
                updated => POSIX::strftime(DATEFMT, gmtime),
        );
 
@@ -109,16 +109,13 @@ sub nav_footer {
        my $old_r = $cgi->param('r');
        my $prev = '    ';
        my $next = '    ';
-       my %opts = (-path => 1, -query => 1, -relative => 1);
 
        if ($last) {
-               $cgi->param('r', $last);
-               $next = $cgi->url(%opts);
+               $next = $cgi->path_info . "?r=$last";
                $next = qq!<a href="$next">next</a>!;
        }
        if ($first && $old_r) {
-               $cgi->param('r', "$first..");
-               $prev = $cgi->url(%opts);
+               $prev = $cgi->path_info . "?r=$first..";
                $prev = qq!<a href="$prev">prev</a>!;
        }
        "$prev $next";
@@ -127,7 +124,10 @@ sub nav_footer {
 sub each_recent_blob {
        my ($args, $cb) = @_;
        my $max = $args->{max} || MAX_PER_PAGE;
-       my $refhex = qr/[a-f0-9]{4,40}(?:~\d+)?/;
+       my $hex = '[a-f0-9]';
+       my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
+       my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
+       my $refhex = qr/${hex}{4,40}(?:~\d+)?/;
        my $cgi = $args->{cgi};
 
        # revision ranges may be specified
@@ -146,19 +146,19 @@ sub each_recent_blob {
        # get recent messages
        # we could use git log -z, but, we already know ssoma will not
        # leave us with filenames with spaces in them..
-       my @cmd = qw/git log --no-notes --no-color --raw -r --no-abbrev/;
+       my @cmd = qw/git log --no-notes --no-color --raw -r/;
        push @cmd, '--reverse' if $reverse;
        push @cmd, $range;
        my $first;
 
        my $pid = open(my $log, '-|', @cmd) or
                die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
-       my %deleted;
+       my %deleted; # only an optimization at this point
        my $last;
        my $nr = 0;
        my @commits = ();
        while (my $line = <$log>) {
-               if ($line =~ /^:000000 100644 0{40} ([a-f0-9]{40})/) {
+               if ($line =~ /$addmsg/o) {
                        my $add = $1;
                        next if $deleted{$add};
                        $nr += $cb->($add);
@@ -166,16 +166,16 @@ sub each_recent_blob {
                                $last = 1;
                                last;
                        }
-               } elsif ($line =~ /^:100644 000000 ([a-f0-9]{40}) 0{40}/) {
+               } elsif ($line =~ /$delmsg/o) {
                        $deleted{$1} = 1;
-               } elsif ($line =~ /^commit ([a-f0-9]{40})/) {
+               } elsif ($line =~ /^commit (${hex}{40})/) {
                        push @commits, $1;
                }
        }
 
        if ($last) {
                while (my $line = <$log>) {
-                       if ($line =~ /^commit ([a-f0-9]{40})/) {
+                       if ($line =~ /^commit (${hex}{40})/) {
                                push @commits, $1;
                                last;
                        }
@@ -210,14 +210,20 @@ sub get_feedopts {
        }
        my $url_base;
        if ($cgi) {
-               my $cgi_url = $cgi->url(-path=>1, -relative=>1);
-               my $base = $cgi->url(-base);
-               $url_base = $cgi_url;
+               my $path_info = $cgi->path_info;
+               my $base;
+               if (ref($cgi) eq 'CGI') {
+                       $base = $cgi->url(-base);
+               } else {
+                       $base = "${$cgi->base}";
+                       $base =~ s!/\z!!;
+               }
+               $url_base = $path_info;
                if ($url_base =~ s!/(?:|index\.html)?\z!!) {
                        $rv{atomurl} = "$base$url_base/atom.xml";
                } else {
                        $url_base =~ s!/atom\.xml\z!!;
-                       $rv{atomurl} = $base . $cgi_url;
+                       $rv{atomurl} = $base . $path_info;
                        $url_base = $base . $url_base; # XXX is this needed?
                }
        } else {
@@ -275,13 +281,17 @@ sub add_to_feed {
        my $date = $mime->header('Date');
        $date = PublicInbox::Hval->new_oneline($date);
        $date = feed_date($date->raw) or return 0;
+       $add =~ tr!/!!d;
+       my $h = '[a-f0-9]';
+       my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
+
        $feed->add_entry(
                author => { name => $name, email => $email },
                title => $subject,
                updated => $date,
-               content => { type => "html", content => $content },
+               content => { type => 'xhtml', content => $content },
                link => $midurl . $href,
-               id => $add,
+               id => 'urn:uuid:' . join('-', @uuid5),
        );
        1;
 }
@@ -315,17 +325,27 @@ sub try_git_pm {
 };
 
 sub do_cat_mail {
-       my ($git, $sha1) = @_;
+       my ($git, $path) = @_;
        my $str;
        if ($git) {
                open my $fh, '>', \$str or
                                die "failed to setup string handle: $!\n";
                binmode $fh;
-               my $bytes = $git->cat_blob($sha1, $fh);
+               my $err = '';
+               my $bytes;
+               {
+                       local $SIG{__WARN__} = sub { $err .= $_[0] };
+                       $bytes = $git->cat_blob("HEAD:$path", $fh);
+               }
                close $fh or die "failed to close string handle: $!\n";
+
+               if ($bytes < 0 && $err &&
+                               $err !~ /doesn't exist in the repository/) {
+                       warn $err;
+               }
                return if $bytes <= 0;
        } else {
-               $str = `git cat-file blob $sha1`;
+               $str = `git cat-file blob HEAD:$path`;
                return if $? != 0 || length($str) == 0;
        }
        Email::MIME->new($str);