]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiUp.pm
a39d6047a26cab1c5e7db999271118f5b513aabe
[public-inbox.git] / lib / PublicInbox / LeiUp.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # "lei up" - updates the result of "lei q --save"
5 package PublicInbox::LeiUp;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::LeiSavedSearch;
9 use parent qw(PublicInbox::IPC);
10
11 sub up1 ($$) {
12         my ($lei, $out) = @_;
13         my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
14         my $f = $lss->{'-f'};
15         my $mset_opt = $lei->{mset_opt} = { relevance => -2 };
16         $mset_opt->{limit} = $lei->{opt}->{limit} // 10000;
17         my $q = $mset_opt->{q_raw} = $lss->{-cfg}->{'lei.q'} //
18                                 return $lei->fail("lei.q unset in $f");
19         my $lse = $lei->{lse} // die 'BUG: {lse} missing';
20         if (ref($q)) {
21                 $mset_opt->{qstr} = $lse->query_argv_to_string($lse->git, $q);
22         } else {
23                 $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q);
24         }
25         my $o = $lei->{opt}->{output} = $lss->{-cfg}->{'lei.q.output'} //
26                 return $lei->fail("lei.q.output unset in $f");
27         ref($o) and return $lei->fail("multiple values of lei.q.output in $f");
28         if (defined(my $dd = $lss->{-cfg}->{'lei.q.dedupe'})) {
29                 $lss->translate_dedupe($lei, $dd) or return;
30                 $lei->{opt}->{dedupe} = $dd;
31         }
32         for my $k (qw(only include exclude)) {
33                 my $v = $lss->{-cfg}->get_all("lei.q.$k") // next;
34                 $lei->{opt}->{$k} = $v;
35         }
36         for my $k (qw(external local remote
37                         import-remote import-before threads)) {
38                 my $c = "lei.q.$k";
39                 my $v = $lss->{-cfg}->{$c} // next;
40                 ref($v) and return $lei->fail("multiple values of $c in $f");
41                 $lei->{opt}->{$k} = $v;
42         }
43         $lei->{lss} = $lss; # for LeiOverview->new
44         my $lxs = $lei->lxs_prepare or return;
45         $lei->ale->refresh_externals($lxs, $lei);
46         $lei->_start_query;
47 }
48
49 sub up1_redispatch {
50         my ($lei, $out, $op_p) = @_;
51         require PublicInbox::LeiFinmsg;
52         $lei->{fmsg} //= PublicInbox::LeiFinmsg->new($lei->{2});
53         my $l = bless { %$lei }, ref($lei);
54         $l->{opt} = { %{$l->{opt}} };
55         delete $l->{sock};
56         $l->{''} = $op_p; # daemon only
57
58         # make close($l->{1}) happy in lei->dclose
59         open my $fh, '>&', $l->{1} or return $l->child_error(0, "dup: $!");
60         $l->{1} = $fh;
61         eval {
62                 $l->qerr("# updating $out");
63                 up1($l, $out);
64                 $l->qerr("# $out done");
65         };
66         $l->child_error(0, $@) if $@;
67 }
68
69 sub lei_up {
70         my ($lei, @outs) = @_;
71         $lei->{lse} = $lei->_lei_store(1)->search;
72         my $opt = $lei->{opt};
73         my @local;
74         if (defined $opt->{all}) {
75                 return $lei->fail("--all and @outs incompatible") if @outs;
76                 length($opt->{mua}//'') and return
77                         $lei->fail('--all and --mua= are incompatible');
78
79                 # supporting IMAP outputs is more involved due to
80                 # git-credential prompts.  TODO: add this in 1.8
81                 $opt->{all} eq 'local' or return
82                         $lei->fail('only --all=local works at the moment');
83                 my @all = PublicInbox::LeiSavedSearch::list($lei);
84                 @local = grep(!m!\Aimaps?://!i, @all);
85         } else {
86                 @local = @outs;
87         }
88         if (scalar(@outs) > 1) {
89                 length($opt->{mua}//'') and return $lei->fail(<<EOM);
90 multiple outputs and --mua= are incompatible
91 EOM
92                 # TODO:
93                 return $lei->fail(<<EOM) if grep(m!\Aimaps?://!i, @outs);
94 multiple destinations only supported for local outputs (FIXME)
95 EOM
96         }
97         if (scalar(@local) > 1) {
98                 $lei->_lei_store->write_prepare($lei); # share early
99                 # daemon mode, re-dispatch into our event loop w/o
100                 # creating an extra fork-level
101                 require PublicInbox::DS;
102                 require PublicInbox::PktOp;
103                 my ($op_c, $op_p) = PublicInbox::PktOp->pair;
104                 for my $o (@local) {
105                         PublicInbox::DS::requeue(sub {
106                                 up1_redispatch($lei, $o, $op_p);
107                         });
108                 }
109                 $lei->event_step_init;
110                 $op_c->{ops} = { '' => [$lei->can('dclose'), $lei] };
111         } else {
112                 up1($lei, $local[0]);
113         }
114 }
115
116 sub _complete_up {
117         my ($lei, @argv) = @_;
118         my $match_cb = $lei->complete_url_prepare(\@argv);
119         map { $match_cb->($_) } PublicInbox::LeiSavedSearch::list($lei);
120 }
121
122 1;