]> Sergey Matveev's repositories - public-inbox.git/commitdiff
extmsg: fall back to partial Message-ID matching
authorEric Wong <e@80x24.org>
Sat, 5 Sep 2015 08:56:54 +0000 (08:56 +0000)
committerEric Wong <e@80x24.org>
Sat, 5 Sep 2015 08:58:51 +0000 (08:58 +0000)
In case a URL gets truncated (as is common with long URLs),
we can rely on Xapian for partial matches and bring the user
to their destination.

lib/PublicInbox/ExtMsg.pm
lib/PublicInbox/Search.pm
lib/PublicInbox/WWW.pm

index 7cf696d4318d3a682825eaf2bc93406d9bdb6fe9..243b6bad4ccd9d1e990972d828c5bf05ef3539bb 100644 (file)
@@ -23,7 +23,7 @@ sub ext_msg {
 
        eval { require PublicInbox::Search };
        my $have_xap = $@ ? 0 : 1;
-       my @nox;
+       my (@nox, @pfx);
 
        foreach my $k (keys %$pi_config) {
                $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.url\z/ or next;
@@ -40,8 +40,9 @@ sub ext_msg {
 
                # try to find the URL with Xapian to avoid forking
                if ($have_xap) {
+                       my $s;
                        my $doc_id = eval {
-                               my $s = PublicInbox::Search->new($git_dir);
+                               $s = PublicInbox::Search->new($git_dir);
                                $s->find_unique_doc_id('mid', $mid);
                        };
                        if ($@) {
@@ -53,6 +54,7 @@ sub ext_msg {
                                # no point in trying the fork fallback if we
                                # know Xapian is up-to-date but missing the
                                # message in the current repo
+                               push @pfx, { srch => $s, url => $url };
                                next;
                        }
                }
@@ -82,19 +84,50 @@ sub ext_msg {
                }
        }
 
+       # fall back to partial MID matching
+       my $n_partial = 0;
+       my @partial;
+       if ($have_xap) {
+               my $cgi = $ctx->{cgi};
+               my $url = ref($cgi) eq 'CGI' ? $cgi->url(-base) . '/'
+                                       : $cgi->base->as_string;
+               $url .= $listname;
+               unshift @pfx, { srch => $ctx->{srch}, url => $url };
+               foreach my $pfx (@pfx) {
+                       my $srch = delete $pfx->{srch} or next;
+                       if (my $res = $srch->mid_prefix($mid)) {
+                               $n_partial += scalar(@$res);
+                               $pfx->{res} = $res;
+                               push @partial, $pfx;
+                       }
+               }
+       }
        my $code = 404;
        my $h = PublicInbox::Hval->new_msgid($mid, 1);
        my $href = $h->as_href;
        my $html = $h->as_html;
        my $title = "Message-ID &lt;$html&gt; not found";
-
-       # Fall back to external repos if configured
        my $s = "<html><head><title>$title</title>" .
-               "</head><body><pre><b>$title</b>";
+               "</head><body><pre><b>$title</b>\n";
 
+       if ($n_partial) {
+               $code = 300;
+               $s.= "\nPartial matches found:\n\n";
+               foreach my $pfx (@partial) {
+                       my $u = $pfx->{url};
+                       foreach my $m (@{$pfx->{res}}) {
+                               $h = PublicInbox::Hval->new($m);
+                               $href = $h->as_href;
+                               $html = $h->as_html;
+                               $s .= qq{<a\nhref="$u/$href/">$u/$html/</a>\n};
+                       }
+               }
+       }
+
+       # Fall back to external repos if configured
        if (@EXT_URL) {
                $code = 300;
-               $s .= "\n\nPerhaps try an external site:\n\n";
+               $s .= "\nPerhaps try an external site:\n\n";
                foreach my $u (@EXT_URL) {
                        my $r = sprintf($u, $href);
                        my $t = sprintf($u, $html);
index 20650554c3337e45cec5c4030aad83de524cfacb..e7ea96c7c751a6690bdcddabebfb0d261256405a 100644 (file)
@@ -269,4 +269,13 @@ sub enquire {
        $self->{enquire} ||= Search::Xapian::Enquire->new($self->{xdb});
 }
 
+sub mid_prefix {
+       my ($self, $mpfx) = @_;
+       my $query = eval { $self->qp->parse_query("m:$mpfx", FLAG_PARTIAL) };
+       return if $@;
+       my $res = $self->do_enquire($query, { relevance => 1 });
+       return unless $res->{total};
+       [ map { $_->mid } @{$res->{msgs}} ];
+}
+
 1;
index 2718854c6b6f28523344aa84a33ecd6190943ef9..8f155063324bb715b786d719235451ba3f01ade6 100644 (file)
@@ -79,6 +79,7 @@ sub r404 {
        my ($ctx) = @_;
        if ($ctx && $ctx->{mid}) {
                require PublicInbox::ExtMsg;
+               searcher($ctx);
                return PublicInbox::ExtMsg::ext_msg($ctx);
        }
        r(404, 'Not Found');