]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WWW.pm
implement per-thread Atom feeds
[public-inbox.git] / lib / PublicInbox / WWW.pm
index 527d2131a27f894c9e68678e53a34318508a674d..c99c25f827cc79db447044c6a5ca639e29add4d9 100644 (file)
@@ -17,12 +17,10 @@ use constant SSOMA_URL => 'http://ssoma.public-inbox.org/';
 use constant PI_URL => 'http://public-inbox.org/';
 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
 our $pi_config;
-BEGIN {
-       $pi_config = PublicInbox::Config->new;
-}
 
 sub run {
        my ($cgi, $method) = @_;
+       $pi_config ||= PublicInbox::Config->new;
        my %ctx = (cgi => $cgi, pi_config => $pi_config);
        if ($method !~ /\AGET|HEAD\z/) {
                return r(405, 'Method Not Allowed');
@@ -40,31 +38,46 @@ sub run {
                invalid_list(\%ctx, $1) || get_atom(\%ctx);
 
        # single-message pages
-       } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
-               invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx);
-       } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
+       } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
                invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx);
+       } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
+               invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx);
 
        # full-message page
-       } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
+       } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
                invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx);
 
        # thread display
-       } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
+       } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
                invalid_list_mid(\%ctx, $1, $2) || get_thread(\%ctx);
 
-       } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.mbox(\.gz)?\z!o) {
+       } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!x) {
                my $sfx = $3;
                invalid_list_mid(\%ctx, $1, $2) ||
                        get_thread_mbox(\%ctx, $sfx);
 
-       } elsif ($path_info =~ m!$LISTNAME_RE/f/\S+\.txt\z!o) {
-               invalid_list_mid(\%ctx, $1, $2) || redirect_mid_txt(\%ctx);
+       } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/atom\z!o) {
+               invalid_list_mid(\%ctx, $1, $2) || get_thread_atom(\%ctx);
+
+       # legacy redirects
+       } elsif ($path_info =~ m!$LISTNAME_RE/(t|m|f)/(\S+)\.html\z!o) {
+               my $pfx = $2;
+               invalid_list_mid(\%ctx, $1, $3) ||
+                       redirect_mid(\%ctx, $pfx, qr/\.html\z/, '/');
+       } elsif ($path_info =~ m!$LISTNAME_RE/(m|f)/(\S+)\.txt\z!o) {
+               my $pfx = $2;
+               invalid_list_mid(\%ctx, $1, $3) ||
+                       redirect_mid(\%ctx, $pfx, qr/\.txt\z/, '/raw');
+       } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
+               my $end = $3;
+               invalid_list_mid(\%ctx, $1, $2) ||
+                       redirect_mid(\%ctx, 't', $end, '/mbox.gz');
 
        # convenience redirects, order matters
        } elsif ($path_info =~ m!$LISTNAME_RE/(m|f|t|s)/(\S+)\z!o) {
                my $pfx = $2;
-               invalid_list_mid(\%ctx, $1, $3) || redirect_mid(\%ctx, $2);
+               invalid_list_mid(\%ctx, $1, $3) ||
+                       redirect_mid(\%ctx, $pfx, qr/\z/, '/');
 
        } else {
                r404();
@@ -207,13 +220,14 @@ sub redirect_list_index {
 }
 
 sub redirect_mid {
-       my ($ctx, $pfx) = @_;
+       my ($ctx, $pfx, $old, $sfx) = @_;
        my $url = self_url($ctx->{cgi});
        my $anchor = '';
-       if (lc($pfx) eq 't') {
+       if (lc($pfx) eq 't' && $sfx eq '/') {
                $anchor = '#u'; # <u id='#u'> is used to highlight in View.pm
        }
-       do_redirect($url . ".html$anchor");
+       $url =~ s/$old/$sfx/;
+       do_redirect($url . $anchor);
 }
 
 # only hit when somebody tries to guess URLs manually:
@@ -236,7 +250,7 @@ sub do_redirect {
 sub ctx_get {
        my ($ctx, $key) = @_;
        my $val = $ctx->{$key};
-       (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
+       (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
        $val;
 }
 
@@ -322,11 +336,11 @@ EOF
 sub msg_pfx {
        my ($ctx) = @_;
        my $href = PublicInbox::Hval::ascii_html(uri_escape_utf8($ctx->{mid}));
-       "../f/$href.html";
+       "../../f/$href/";
 }
 
-# /$LISTNAME/t/$MESSAGE_ID.mbox           -> thread as mbox
-# /$LISTNAME/t/$MESSAGE_ID.mbox.gz        -> thread as gzipped mbox
+# /$LISTNAME/t/$MESSAGE_ID/mbox           -> thread as mbox
+# /$LISTNAME/t/$MESSAGE_ID/mbox.gz        -> thread as gzipped mbox
 # note: I'm not a big fan of other compression formats since they're
 # significantly more expensive on CPU than gzip and less-widely available,
 # especially on older systems.  Stick to zlib since that's what git uses.
@@ -337,4 +351,14 @@ sub get_thread_mbox {
        PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
 }
 
+
+# /$LISTNAME/t/$MESSAGE_ID/atom                  -> thread as Atom feed
+sub get_thread_atom {
+       my ($ctx) = @_;
+       searcher($ctx) or return need_search($ctx);
+       $ctx->{self_url} = self_url($ctx->{cgi});
+       require PublicInbox::Feed;
+       PublicInbox::Feed::generate_thread_atom($ctx);
+}
+
 1;