about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2014-04-19 10:37:28 +0000
committerEric Wong <e@80x24.org>2014-04-19 10:37:28 +0000
commit93310d10f0d74554f36537639b2abd9903da3620 (patch)
treea0471ec4353d4fa0b64cfe7761042af6c3ae81b3
parent24edcaa76e03ddfba58bf0074d89e693de6eddb4 (diff)
downloadpublic-inbox-93310d10f0d74554f36537639b2abd9903da3620.tar.gz
This allows WWW readers to slowly page through the entire history
of the mailing list.
-rw-r--r--Documentation/design_www.txt2
-rw-r--r--lib/PublicInbox/Feed.pm103
-rwxr-xr-xpublic-inbox.cgi4
-rw-r--r--t/cgi.t3
4 files changed, 88 insertions, 24 deletions
diff --git a/Documentation/design_www.txt b/Documentation/design_www.txt
index 6e67d829..0f63fdf2 100644
--- a/Documentation/design_www.txt
+++ b/Documentation/design_www.txt
@@ -1,7 +1,7 @@
 URL naming
 ----------
 
-/$LISTNAME/?before=$GIT_COMMIT                  -> HTML only
+/$LISTNAME/?r=$GIT_COMMIT                       -> HTML only
 /$LISTNAME/index.atom.xml                       -> Atom feed
 /$LISTNAME/all.atom.xml                         -> Atom feed, includes replies
 /$LISTNAME/m/$MESSAGE_ID.html                   -> HTML content (short quotes)
diff --git a/lib/PublicInbox/Feed.pm b/lib/PublicInbox/Feed.pm
index 9db4e17d..75290c9e 100644
--- a/lib/PublicInbox/Feed.pm
+++ b/lib/PublicInbox/Feed.pm
@@ -12,7 +12,10 @@ use Encode::MIME::Header;
 use CGI qw(escapeHTML);
 use POSIX qw(strftime);
 use Date::Parse qw(strptime str2time);
-use constant DATEFMT => '%Y-%m-%dT%H:%M:%SZ';
+use constant {
+        DATEFMT => '%Y-%m-%dT%H:%M:%SZ',
+        MAX_PER_PAGE => 25,
+};
 use PublicInbox::View;
 use Mail::Thread;
 my $enc_utf8 = find_encoding('utf8');
@@ -24,7 +27,7 @@ my $enc_mime = find_encoding('MIME-Header');
 # main function
 sub generate {
         my ($class, $args) = @_;
-        my $max = $args->{max} || 25;
+        my $max = $args->{max} || MAX_PER_PAGE;
         my $top = $args->{top}; # bool
 
         local $ENV{GIT_DIR} = $args->{git_dir};
@@ -42,7 +45,7 @@ sub generate {
                 updated => strftime(DATEFMT, gmtime),
         );
 
-        each_recent_blob($max, sub {
+        each_recent_blob($args, sub {
                 my ($add) = @_;
                 add_to_feed($feed_opts, $feed, $add, $top);
         });
@@ -51,13 +54,13 @@ sub generate {
 
 sub generate_html_index {
         my ($class, $args) = @_;
-        my $max = $args->{max} || 50;
+        my $max = $args->{max} || MAX_PER_PAGE;
         my $top = $args->{top}; # bool
         local $ENV{GIT_DIR} = $args->{git_dir};
         my $feed_opts = get_feedopts($args);
         my $title = xs_html($feed_opts->{description} || "");
         my @messages;
-        each_recent_blob($max, sub {
+        my ($first, $last) = each_recent_blob($args, sub {
                 my $str = `git cat-file blob $_[0]`;
                 return 0 if $? != 0;
                 my $simple = Email::Simple->new($str);
@@ -76,12 +79,12 @@ sub generate_html_index {
 
         my $th = Mail::Thread->new(@messages);
         $th->thread;
-        my @args = (
+        my @out = (
                 "<html><head><title>$title</title>" .
                 '<link rel=alternate title=Atom.feed href="' .
                 $feed_opts->{atomurl} . '" type="application/atom+xml"/>' .
                 '</head><body><pre>');
-        push @args, $feed_opts->{midurl};
+        push @out, $feed_opts->{midurl};
 
         # sort by date, most recent at top
         $th->order(sub {
@@ -90,34 +93,97 @@ sub generate_html_index {
                         $a->topmost->message->header('X-PI-Date')
                 } @_;
         });
-        dump_html_line($_, 0, \@args) for $th->rootset;
-        $args[0] . '</pre></html>';
+        dump_html_line($_, 0, \@out) for $th->rootset;
+
+        my $footer = nav_footer($args->{cgi}, $first, $last);
+        $footer = "<hr /><pre>$footer</pre>" if $footer;
+        $out[0] . "</pre>$footer</html>";
 }
 
 # private subs
 
+sub nav_footer {
+        my ($cgi, $first, $last) = @_;
+        $cgi or return '';
+        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 = qq!<a href="$next">next</a>!;
+        }
+        if ($first && $old_r) {
+                $cgi->param('r', "$first..");
+                $prev = $cgi->url(%opts);
+                $prev = qq!<a href="$prev">prev</a>!;
+        }
+        "$prev $next";
+}
+
 sub each_recent_blob {
-        my ($max, $cb) = @_;
+        my ($args, $cb) = @_;
+        my $max = $args->{max} || MAX_PER_PAGE;
+        my $refhex = qr/[a-f0-9]{4,40}(?:~\d+)?/;
+        my $cgi = $args->{cgi};
+
+        # revision ranges may be specified
+        my $reverse;
+        my $range = 'HEAD';
+        my $r = $cgi->param('r') if $cgi;
+        if ($r) {
+                if ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o) {
+                        $range = $r;
+                } elsif ($r =~ /\A(?:$refhex\.\.)\z/o) {
+                        $reverse = 1;
+                        $range = $r;
+                }
+        }
 
         # 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 = "git log --no-notes --no-color --raw -r --no-abbrev HEAD |";
-        my $pid = open my $log, $cmd or die "open `$cmd' pipe failed: $!\n";
+        my @cmd = qw/git log --no-notes --no-color --raw -r --no-abbrev/;
+        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 $last;
         my $nr = 0;
-        foreach my $line (<$log>) {
+        my @commits = ();
+        while (my $line = <$log>) {
                 if ($line =~ /^:000000 100644 0{40} ([a-f0-9]{40})/) {
                         my $add = $1;
                         next if $deleted{$add};
                         $nr += $cb->($add);
-                        last if $nr >= $max;
+                        if ($nr >= $max) {
+                                $last = 1;
+                                last;
+                        }
                 } elsif ($line =~ /^:100644 000000 ([a-f0-9]{40}) 0{40}/) {
                         $deleted{$1} = 1;
+                } elsif ($line =~ /^commit ([a-f0-9]{40})/) {
+                        push @commits, $1;
+                }
+        }
+
+        if ($last) {
+                while (my $line = <$log>) {
+                        if ($line =~ /^commit ([a-f0-9]{40})/) {
+                                push @commits, $1;
+                                last;
+                        }
                 }
         }
 
-        close $log;
+        close $log; # we may EPIPE here
+        # for pagination
+        $reverse ? ($commits[-1],$commits[0]) : ($commits[0],$commits[-1]);
 }
 
 # private functions below
@@ -135,12 +201,11 @@ sub get_feedopts {
         }
         my $url_base;
         if ($cgi) {
-                my $cgi_url = $cgi->url(-path=>1, -query=>1, -relative=>1);
+                my $cgi_url = $cgi->url(-path=>1, -relative=>1);
                 my $base = $cgi->url(-base);
                 $url_base = $cgi_url;
-                if ($url_base =~ s!/(?:|(index|all)\.html)?\z!!) {
-                        my $ia = $1 || 'index';
-                        $rv{atomurl} = "$base$url_base/$ia.atom.xml";
+                if ($url_base =~ s!/(?:|index\.html)?\z!!) {
+                        $rv{atomurl} = "$base$url_base/index.atom.xml";
                 } else {
                         $url_base =~ s!/?(?:index|all)\.atom\.xml\z!!;
                         $rv{atomurl} = $base . $cgi_url;
diff --git a/public-inbox.cgi b/public-inbox.cgi
index 31d52133..5e281580 100755
--- a/public-inbox.cgi
+++ b/public-inbox.cgi
@@ -65,8 +65,6 @@ sub main {
         } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
                 invalid_list(\%ctx, $1) || redirect_list_index(\%ctx, $cgi);
         } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
-                invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 1);
-        } elsif ($path_info =~ m!$LISTNAME_RE/(?:all\.html)?\z!o) {
                 invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 0);
         } elsif ($path_info =~ m!$LISTNAME_RE/index\.atom\.xml\z!o) {
                 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 1);
@@ -133,7 +131,7 @@ sub get_atom {
         ];
 }
 
-# /$LISTNAME/?before=$GIT_COMMIT                 -> HTML only
+# /$LISTNAME/?r=$GIT_COMMIT                 -> HTML only
 sub get_index {
         my ($ctx, $cgi, $top) = @_;
         require PublicInbox::Feed;
diff --git a/t/cgi.t b/t/cgi.t
index 611fb604..3cf230c0 100644
--- a/t/cgi.t
+++ b/t/cgi.t
@@ -121,8 +121,9 @@ EOF
         like($res->{head}, qr/Status: 200 OK/, "index returns 200");
 
         my $idx = cgi_run("/test/index.html");
+        $idx->{body} =~ s!/index.html(\?r=)!/$1!; # dirty...
         is_deeply($res, $idx,
-                '/$LISTNAME/ and /$LISTNAME/index.html are identical');
+                '/$LISTNAME/ and /$LISTNAME/index.html are nearly identical');
         # more checks in t/feed.t
 }