unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 3/6] extmsg: rework to use Inbox objects
Date: Sat,  2 Jul 2016 07:56:35 +0000	[thread overview]
Message-ID: <20160702075638.31017-4-e@80x24.org> (raw)
In-Reply-To: <20160702075638.31017-1-e@80x24.org>

This is less code and hopefully easier-to-understand.
---
 lib/PublicInbox/ExtMsg.pm | 102 ++++++++++++++++++++--------------------------
 lib/PublicInbox/Inbox.pm  |   5 +++
 2 files changed, 49 insertions(+), 58 deletions(-)

diff --git a/lib/PublicInbox/ExtMsg.pm b/lib/PublicInbox/ExtMsg.pm
index e15abab..4b9e025 100644
--- a/lib/PublicInbox/ExtMsg.pm
+++ b/lib/PublicInbox/ExtMsg.pm
@@ -24,62 +24,53 @@ our @EXT_URL = (
 sub ext_msg {
 	my ($ctx) = @_;
 	my $pi_config = $ctx->{pi_config};
-	my $inbox = $ctx->{inbox};
+	my $cur = $ctx->{-inbox};
 	my $mid = $ctx->{mid};
-	my $cgi = $ctx->{cgi};
-	my $env = $cgi->{env};
+	my $env = $ctx->{env};
 
 	eval { require PublicInbox::Search };
 	my $have_xap = $@ ? 0 : 1;
-	my (@nox, @pfx);
+	my (@nox, @ibx);
 
 	foreach my $k (keys %$pi_config) {
 		$k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.url\z/ or next;
 		my $name = $1;
-		next if $name eq $inbox;
-
-		my $git_dir = $pi_config->{"publicinbox.$name.mainrepo"};
-		defined $git_dir or next;
-
-		my $url = $pi_config->{"publicinbox.$name.url"};
-		defined $url or next;
-
-		$url =~ s!/+\z!!;
-		$url = PublicInbox::Hval::prurl($env, $url);
+		next if $name eq $cur->{name};
+		my $other = $pi_config->lookup_name($name) or next;
+		next unless $other->base_url;
+
+		my $s = $other->search;
+		if (!$s) {
+			push @nox, $other;
+			next;
+		}
 
 		# try to find the URL with Xapian to avoid forking
-		if ($have_xap) {
-			my $s;
-			my $doc_id = eval {
-				$s = PublicInbox::Search->new($git_dir);
-				$s->find_unique_doc_id('mid', $mid);
-			};
-			if ($@) {
-				# xapian not configured for this repo
-			} else {
-				# maybe we found it!
-				return r302($url, $mid) if (defined $doc_id);
-
-				# 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, { git_dir => $git_dir, url => $url };
-				next;
-			}
+		my $doc_id = eval { $s->find_unique_doc_id('mid', $mid) };
+		if ($@) {
+			# xapian not configured properly for this repo
+			push @nox, $other;
+			next;
 		}
 
-		# queue up for forking after we've tried Xapian on all of them
-		push @nox, { git_dir => $git_dir, url => $url };
+		# maybe we found it!
+		return r302($other, $mid) if defined $doc_id;
+
+		# no point in trying the fork fallback if we
+		# know Xapian is up-to-date but missing the
+		# message in the current repo
+		push @ibx, $other;
 	}
 
-	# Xapian not installed or configured for some repos
-	my $path = "HEAD:" . mid2path($mid);
+	# Xapian not installed or configured for some repos,
+	# do a full MID check:
+	if (@nox) {
+		my $path = mid2path($mid);
+		foreach my $other (@nox) {
+			my (undef, $type, undef) = $other->path_check($path);
 
-	foreach my $n (@nox) {
-		# TODO: reuse existing PublicInbox::Git objects to save forks
-		my $git = PublicInbox::Git->new($n->{git_dir});
-		my (undef, $type, undef) = $git->check($path);
-		return r302($n->{url}, $mid) if ($type && $type eq 'blob');
+			return r302($other, $mid) if $type && $type eq 'blob';
+		}
 	}
 
 	# fall back to partial MID matching
@@ -88,22 +79,15 @@ sub ext_msg {
 
 	eval { require PublicInbox::Msgmap };
 	my $have_mm = $@ ? 0 : 1;
-	my $base_url = $cgi->base->as_string;
 	if ($have_mm) {
 		my $tmp_mid = $mid;
-		my $url;
 again:
-		$url = $base_url . $inbox;
-		unshift @pfx, { git_dir => $ctx->{git_dir}, url => $url };
-		foreach my $pfx (@pfx) {
-			my $git_dir = delete $pfx->{git_dir} or next;
-			my $mm = eval { PublicInbox::Msgmap->new($git_dir) };
-
-			$mm or next;
+		unshift @ibx, $cur;
+		foreach my $ibx (@ibx) {
+			my $mm = $ibx->mm or next;
 			if (my $res = $mm->mid_prefixes($tmp_mid)) {
 				$n_partial += scalar(@$res);
-				$pfx->{res} = $res;
-				push @partial, $pfx;
+				push @partial, [ $ibx, $res ];
 			}
 		}
 		# fixup common errors:
@@ -124,13 +108,14 @@ again:
 		$code = 300;
 		my $es = $n_partial == 1 ? '' : 'es';
 		$s.= "\n$n_partial partial match$es found:\n\n";
-		foreach my $pfx (@partial) {
-			my $u = $pfx->{url};
-			foreach my $m (@{$pfx->{res}}) {
+		foreach my $pair (@partial) {
+			my ($ibx, $res) = @$pair;
+			my $u = $ibx->base_url or next;
+			foreach my $m (@$res) {
 				my $p = PublicInbox::Hval->new_msgid($m);
 				my $r = $p->as_href;
 				my $t = $p->as_html;
-				$s .= qq{<a\nhref="$u/$r/">$u/$t/</a>\n};
+				$s .= qq{<a\nhref="$u$r/">$u$t/</a>\n};
 			}
 		}
 	}
@@ -152,9 +137,10 @@ again:
 }
 
 # Redirect to another public-inbox which is mapped by $pi_config
+# TODO: prompt for inbox-switching
 sub r302 {
-	my ($url, $mid) = @_;
-	$url .= '/' . uri_escape_utf8($mid) . '/';
+	my ($inbox, $mid) = @_;
+	my $url = $inbox->base_url . uri_escape_utf8($mid) . '/';
 	[ 302,
 	  [ 'Location' => $url, 'Content-Type' => 'text/plain' ],
 	  [ "Redirecting to\n$url\n" ] ]
diff --git a/lib/PublicInbox/Inbox.pm b/lib/PublicInbox/Inbox.pm
index 96c9265..728caa0 100644
--- a/lib/PublicInbox/Inbox.pm
+++ b/lib/PublicInbox/Inbox.pm
@@ -108,6 +108,11 @@ sub msg_by_path ($$;$) {
 	$str;
 }
 
+sub path_check {
+	my ($self, $path) = @_;
+	git($self)->check('HEAD:'.$path);
+}
+
 sub msg_by_mid ($$;$) {
 	my ($self, $mid, $ref) = @_;
 	msg_by_path($self, mid2path($mid), $ref);
-- 
EW


  parent reply	other threads:[~2016-07-02  7:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-02  7:56 [PATCH 0/6] misc cleanups Eric Wong
2016-07-02  7:56 ` [PATCH 1/6] TODO: clarify streaming Email::MIME replacement Eric Wong
2016-07-02  7:56 ` [PATCH 2/6] inbox: base_url method takes PSGI env hashref instead Eric Wong
2016-07-02  7:56 ` Eric Wong [this message]
2016-07-02  7:56 ` [PATCH 4/6] www: use PSGI env directly Eric Wong
2016-07-02  7:56 ` [PATCH 5/6] view: rely on internal query parser for 'o' param Eric Wong
2016-07-02  7:56 ` [PATCH 6/6] www: remove Plack::Request dependency entirely Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160702075638.31017-4-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).