]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiExternal.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / LeiExternal.pm
1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # *-external commands of lei
5 package PublicInbox::LeiExternal;
6 use strict;
7 use v5.10.1;
8 use parent qw(Exporter);
9 our @EXPORT = qw(lei_ls_external lei_add_external lei_forget_external);
10
11 sub lei_ls_external {
12         my ($self, @argv) = @_;
13         my $stor = $self->_lei_store(0);
14         my $cfg = $self->_lei_cfg(0);
15         my $out = $self->{1};
16         my ($OFS, $ORS) = $self->{opt}->{z} ? ("\0", "\0\0") : (" ", "\n");
17         my (%boost, @loc);
18         for my $sec (grep(/\Aexternal\./, @{$cfg->{-section_order}})) {
19                 my $loc = substr($sec, length('external.'));
20                 $boost{$loc} = $cfg->{"$sec.boost"};
21                 push @loc, $loc;
22         }
23         use sort 'stable';
24         # highest boost first, but stable for alphabetic tie break
25         for (sort { $boost{$b} <=> $boost{$a} } sort keys %boost) {
26                 # TODO: use miscidx and show docid so forget/set is easier
27                 print $out $_, $OFS, 'boost=', $boost{$_}, $ORS;
28         }
29 }
30
31 sub lei_add_external {
32         my ($self, $url_or_dir) = @_;
33         my $cfg = $self->_lei_cfg(1);
34         if ($url_or_dir !~ m!\Ahttps?://!) {
35                 $url_or_dir = File::Spec->canonpath($url_or_dir);
36         }
37         my $new_boost = $self->{opt}->{boost} // 0;
38         my $key = "external.$url_or_dir.boost";
39         my $cur_boost = $cfg->{$key};
40         return if defined($cur_boost) && $cur_boost == $new_boost; # idempotent
41         $self->lei_config($key, $new_boost);
42         my $stor = $self->_lei_store(1);
43         # TODO: add to MiscIdx
44         $stor->done;
45 }
46
47 sub lei_forget_external {
48         # TODO
49 }
50
51 1;