]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiQuery.pm
lei: complete basenames for include|exclude|only
[public-inbox.git] / lib / PublicInbox / LeiQuery.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 # handles lei <q|ls-query|rm-query|mv-query> commands
5 package PublicInbox::LeiQuery;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::DS qw(dwaitpid);
9
10 sub prep_ext { # externals_each callback
11         my ($lxs, $exclude, $loc) = @_;
12         $lxs->prepare_external($loc) unless $exclude->{$loc};
13 }
14
15 # the main "lei q SEARCH_TERMS" method
16 sub lei_q {
17         my ($self, @argv) = @_;
18         require PublicInbox::LeiXSearch;
19         require PublicInbox::LeiOverview;
20         require PublicInbox::V2Writable;
21         PublicInbox::Config->json; # preload before forking
22         my $opt = $self->{opt};
23         # prepare any number of LeiXSearch || LeiSearch || Inbox || URL
24         my $lxs = $self->{lxs} = PublicInbox::LeiXSearch->new;
25         my @only = @{$opt->{only} // []};
26         # --local is enabled by default unless --only is used
27         # we'll allow "--only $LOCATION --local"
28         if ($opt->{'local'} //= scalar(@only) ? 0 : 1) {
29                 my $sto = $self->_lei_store(1);
30                 $lxs->prepare_external($sto->search);
31         }
32         if (@only) {
33                 for my $loc (@only) {
34                         my @loc = $self->get_externals($loc) or return;
35                         $lxs->prepare_external($_) for @loc;
36                 }
37         } else {
38                 for my $loc (@{$opt->{include} // []}) {
39                         my @loc = $self->get_externals($loc) or return;
40                         $lxs->prepare_external($_) for @loc;
41                 }
42                 # --external is enabled by default, but allow --no-external
43                 if ($opt->{external} //= 1) {
44                         my %x;
45                         for my $loc (@{$opt->{exclude} // []}) {
46                                 my @l = $self->get_externals($loc, 1) or return;
47                                 $x{$_} = 1 for @l;
48                         }
49                         my $ne = $self->externals_each(\&prep_ext, $lxs, \%x);
50                         $opt->{remote} //= !($lxs->locals - $opt->{'local'});
51                         if ($opt->{'local'}) {
52                                 delete($lxs->{remotes}) if !$opt->{remote};
53                         } else {
54                                 delete($lxs->{locals});
55                         }
56                 }
57         }
58         unless ($lxs->locals || $lxs->remotes) {
59                 return $self->fail('no local or remote inboxes to search');
60         }
61         my ($xj, $mj) = split(/,/, $opt->{jobs} // '');
62         if (defined($xj) && $xj ne '' && $xj !~ /\A[1-9][0-9]*\z/) {
63                 return $self->fail("`$xj' search jobs must be >= 1");
64         }
65         $xj ||= $lxs->concurrency($opt); # allow: "--jobs ,$WRITER_ONLY"
66         my $nproc = $lxs->detect_nproc; # don't memoize, schedtool(1) exists
67         $xj = $nproc if $xj > $nproc;
68         PublicInbox::LeiOverview->new($self) or return;
69         $self->atfork_prepare_wq($lxs);
70         $lxs->wq_workers_start('lei_xsearch', $xj, $self->oldset);
71         delete $lxs->{-ipc_atfork_child_close};
72         if (my $l2m = $self->{l2m}) {
73                 if (defined($mj) && $mj !~ /\A[1-9][0-9]*\z/) {
74                         return $self->fail("`$mj' writer jobs must be >= 1");
75                 }
76                 $mj //= $nproc;
77                 $self->atfork_prepare_wq($l2m);
78                 $l2m->wq_workers_start('lei2mail', $mj, $self->oldset);
79                 delete $l2m->{-ipc_atfork_child_close};
80         }
81
82         # no forking workers after this
83
84         my %mset_opt = map { $_ => $opt->{$_} } qw(thread limit offset);
85         $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
86         $mset_opt{limit} //= 10000;
87         $mset_opt{qstr} = join(' ', map {;
88                 # Consider spaces in argv to be for phrase search in Xapian.
89                 # In other words, the users should need only care about
90                 # normal shell quotes and not have to learn Xapian quoting.
91                 /\s/ ? (s/\A(\w+:)// ? qq{$1"$_"} : qq{"$_"}) : $_
92         } @argv);
93         if (defined(my $sort = $opt->{'sort'})) {
94                 if ($sort eq 'relevance') {
95                         $mset_opt{relevance} = 1;
96                 } elsif ($sort eq 'docid') {
97                         $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
98                 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
99                         # the default
100                 } else {
101                         die "unrecognized --sort=$sort\n";
102                 }
103         }
104         # descending docid order
105         $mset_opt{relevance} //= -2 if $opt->{thread};
106         $self->{mset_opt} = \%mset_opt;
107         $self->{ovv}->ovv_begin($self);
108         $lxs->do_query($self);
109 }
110
111 # shell completion helper called by lei__complete
112 sub _complete_q {
113         my ($self, @argv) = @_;
114         my $ext = qr/\A(?:-I|(?:--(?:include|exclude|only)))\z/;
115         my @cur;
116         while (@argv) {
117                 if ($argv[-1] =~ $ext) {
118                         my @c = $self->_complete_forget_external(@cur);
119                         # try basename match:
120                         if (scalar(@cur) == 1 && index($cur[0], '/') < 0) {
121                                 my $all = $self->externals_each;
122                                 my %bn;
123                                 for my $loc (keys %$all) {
124                                         my $bn = (split(m!/!, $loc))[-1];
125                                         ++$bn{$bn};
126                                 }
127                                 push @c, grep {
128                                         $bn{$_} == 1 && /\A\Q$cur[0]/
129                                 } keys %bn;
130                         }
131                         return @c if @c;
132                 }
133                 unshift(@cur, pop @argv);
134         }
135         ();
136 }
137
138 # Stuff we may pass through to curl (as of 7.64.0), see curl manpage for
139 # details, so most options which make sense for HTTP/HTTPS (including proxy
140 # support for Tor and other methods of getting past weird networks).
141 # Most of these are untested by us, some may not make sense for our use case
142 # and typos below are likely.
143 # n.b. some short options (-$NUMBER) are not supported since they conflict
144 # with other "lei q" switches.
145 # FIXME: Getopt::Long doesn't easily let us support support options with
146 # '.' in them (e.g. --http1.1)
147 sub curl_opt { qw(
148         abstract-unix-socket=s anyauth basic cacert=s capath=s
149         cert-status cert-type cert|E=s ciphers=s config|K=s@
150         connect-timeout=s connect-to=s cookie-jar|c=s cookie|b=s crlfile=s
151         digest disable dns-interface=s dns-ipv4-addr=s dns-ipv6-addr=s
152         dns-servers=s doh-url=s egd-file=s engine=s false-start
153         happy-eyeballs-timeout-ms=s haproxy-protocol header|H=s@
154         http2-prior-knowledge http2 insecure|k
155         interface=s ipv4 ipv6 junk-session-cookies
156         key-type=s key=s limit-rate=s local-port=s location-trusted location|L
157         max-redirs=i max-time=s negotiate netrc-file=s netrc-optional netrc
158         no-alpn no-buffer|N no-npn no-sessionid noproxy=s ntlm-wb ntlm
159         pass=s pinnedpubkey=s post301 post302 post303 preproxy=s
160         proxy-anyauth proxy-basic proxy-cacert=s proxy-capath=s
161         proxy-cert-type=s proxy-cert=s proxy-ciphers=s proxy-crlfile=s
162         proxy-digest proxy-header=s@ proxy-insecure
163         proxy-key-type=s proxy-key proxy-negotiate proxy-ntlm proxy-pass=s
164         proxy-pinnedpubkey=s proxy-service-name=s proxy-ssl-allow-beast
165         proxy-tls13-ciphers=s proxy-tlsauthtype=s proxy-tlspassword=s
166         proxy-tlsuser=s proxy-tlsv1 proxy-user|U=s proxy=s
167         proxytunnel=s pubkey=s random-file=s referer=s resolve=s
168         retry-connrefused retry-delay=s retry-max-time=s retry=i
169         sasl-ir service-name=s socks4=s socks4a=s socks5-basic
170         socks5-gssapi-service-name=s socks5-gssapi socks5-hostname=s socks5=s
171         speed-limit|Y speed-type|y ssl-allow-beast sslv2 sslv3
172         suppress-connect-headers tcp-fastopen tls-max=s
173         tls13-ciphers=s tlsauthtype=s tlspassword=s tlsuser=s
174         tlsv1 trace-ascii=s trace-time trace=s
175         unix-socket=s user-agent|A=s user|u=s
176 )
177 }
178
179 1;