about summary refs log tree commit homepage
path: root/lib/PublicInbox/LeiExternal.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-01-21 19:46:23 +0000
committerEric Wong <e@80x24.org>2021-01-22 16:18:01 -0400
commitec1f473957a529cc51a381e64274e42c03c6487b (patch)
tree7b61a5211640d42f8fb757e92bef566fd5179e83 /lib/PublicInbox/LeiExternal.pm
parentc39a2d8bb1ee6e062030e21f6b0f638990234b2e (diff)
downloadpublic-inbox-ec1f473957a529cc51a381e64274e42c03c6487b.tar.gz
For proper matching, we'll do a better job canonicalizing
URLs and path names for matching.  Of course, users may edit
the file outside of lei, so ensure we try both the canonicalized
and as-is form provided by the user.

I also don't think we'll need to store externals info in
MiscIdx; just the config file is fine.
Diffstat (limited to 'lib/PublicInbox/LeiExternal.pm')
-rw-r--r--lib/PublicInbox/LeiExternal.pm54
1 files changed, 44 insertions, 10 deletions
diff --git a/lib/PublicInbox/LeiExternal.pm b/lib/PublicInbox/LeiExternal.pm
index 64faf5a0..21071058 100644
--- a/lib/PublicInbox/LeiExternal.pm
+++ b/lib/PublicInbox/LeiExternal.pm
@@ -7,6 +7,7 @@ use strict;
 use v5.10.1;
 use parent qw(Exporter);
 our @EXPORT = qw(lei_ls_external lei_add_external lei_forget_external);
+use PublicInbox::Config;
 
 sub _externals_each {
         my ($self, $cb, @arg) = @_;
@@ -30,7 +31,6 @@ sub _externals_each {
 
 sub lei_ls_external {
         my ($self, @argv) = @_;
-        my $stor = $self->_lei_store(0);
         my $out = $self->{1};
         my ($OFS, $ORS) = $self->{opt}->{z} ? ("\0", "\0\0") : (" ", "\n");
         $self->_externals_each(sub {
@@ -39,24 +39,58 @@ sub lei_ls_external {
         });
 }
 
+sub _canonicalize {
+        my ($location) = @_;
+        if ($location !~ m!\Ahttps?://!) {
+                PublicInbox::Config::rel2abs_collapsed($location);
+        } else {
+                require URI;
+                my $uri = URI->new($location)->canonical;
+                my $path = $uri->path . '/';
+                $path =~ tr!/!/!s; # squeeze redundant '/'
+                $uri->path($path);
+                $uri->as_string;
+        }
+}
+
 sub lei_add_external {
-        my ($self, $url_or_dir) = @_;
+        my ($self, $location) = @_;
         my $cfg = $self->_lei_cfg(1);
-        if ($url_or_dir !~ m!\Ahttps?://!) {
-                $url_or_dir = File::Spec->canonpath($url_or_dir);
-        }
         my $new_boost = $self->{opt}->{boost} // 0;
-        my $key = "external.$url_or_dir.boost";
+        $location = _canonicalize($location);
+        my $key = "external.$location.boost";
         my $cur_boost = $cfg->{$key};
         return if defined($cur_boost) && $cur_boost == $new_boost; # idempotent
         $self->lei_config($key, $new_boost);
-        my $stor = $self->_lei_store(1);
-        # TODO: add to MiscIdx
-        $stor->done;
+        $self->_lei_store(1)->done; # just create the store
 }
 
 sub lei_forget_external {
-        # TODO
+        my ($self, @locations) = @_;
+        my $cfg = $self->_lei_cfg(1);
+        my $quiet = $self->{opt}->{quiet};
+        for my $loc (@locations) {
+                my (@unset, @not_found);
+                for my $l ($loc, _canonicalize($loc)) {
+                        my $key = "external.$l.boost";
+                        delete($cfg->{$key});
+                        $self->_config('--unset', $key);
+                        if ($? == 0) {
+                                push @unset, $key;
+                        } elsif (($? >> 8) == 5) {
+                                push @not_found, $key;
+                        } else {
+                                $self->err("# --unset $key error");
+                                return $self->x_it($?);
+                        }
+                }
+                if (@unset) {
+                        next if $quiet;
+                        $self->err("# $_ unset") for @unset;
+                } elsif (@not_found) {
+                        $self->err("# $_ not found") for @not_found;
+                } # else { already exited
+        }
 }
 
 1;