1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # pretends to be like LeiDedupe and also PublicInbox::Inbox
5 package PublicInbox::LeiSavedSearch;
8 use parent qw(PublicInbox::Lock);
9 use PublicInbox::OverIdx;
10 use PublicInbox::LeiSearch;
11 use PublicInbox::Config;
12 use PublicInbox::Spawn qw(run_die);
13 use PublicInbox::ContentHash qw(git_sha);
14 use Digest::SHA qw(sha256_hex);
16 *squote_maybe = \&PublicInbox::Config::squote_maybe;
18 sub lss_dir_for ($$) {
19 my ($lei, $dstref) = @_;
21 if ($$dstref =~ m,\Aimaps?://,i) { # already canonicalized
22 require PublicInbox::URIimap;
23 my $uri = PublicInbox::URIimap->new($$dstref)->canonical;
27 $$dstref = $lei->rel2abs($$dstref);
28 $$dstref .= '/' if -d $$dstref;
30 @n = ($$dstref =~ m{([^/]+)/*\z});
32 push @n, sha256_hex($$dstref);
33 $lei->share_path . '/saved-searches/' . join('-', @n);
37 my ($cls, $lei, $dst) = @_;
38 my $self = bless { ale => $lei->ale }, $cls;
40 if (defined $dst) { # updating existing saved search via "lei up"
43 output2lssdir($self, $lei, \$dir, \$f) or
44 return $lei->fail("--save was not used with $dst cwd=".
46 $self->{-cfg} //= PublicInbox::Config::git_config_dump($f);
48 } else { # new saved search "lei q --save"
49 my $dst = $lei->{ovv}->{dst};
50 $dir = lss_dir_for($lei, \$dst);
52 File::Path::make_path($dir); # raises on error
54 my $f = $self->{'-f'} = "$dir/lei.saved-search";
55 open my $fh, '>', $f or return $lei->fail("open $f: $!");
56 my $sq_dst = squote_maybe($dst);
57 print $fh <<EOM or return $lei->fail("print $f: $!");
58 ; to refresh with new results, run: lei up $sq_dst
60 close $fh or return $lei->fail("close $f: $!");
61 my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
63 cfg_set($self, '--add', 'lei.q', $_) for @$q;
65 cfg_set($self, 'lei.q', $q);
67 $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i;
68 cfg_set($self, 'lei.q.output', $dst);
69 for my $k (qw(only include exclude)) {
70 my $ary = $lei->{opt}->{$k} // next;
72 cfg_set($self, '--add', "lei.q.$k", $x);
75 for my $k (qw(external local remote import-remote
76 import-before threads)) {
77 my $val = $lei->{opt}->{$k} // next;
78 cfg_set($self, "lei.q.$k", $val);
81 bless $self->{-cfg}, 'PublicInbox::Config';
82 $self->{lock_path} = "$self->{-f}.flock";
83 $self->{-ovf} = "$dir/over.sqlite3";
87 sub description { $_[0]->{qstr} } # for WWW
90 my ($self, @args) = @_;
91 my $lk = $self->lock_for_scope; # git-config doesn't wait
92 run_die([qw(git config -f), $self->{'-f'}, @args]);
95 # drop-in for LeiDedupe API
97 my ($self, $eml, $smsg) = @_;
98 my $oidx = $self->{oidx} // die 'BUG: no {oidx}';
99 my $blob = $smsg ? $smsg->{blob} : undef;
100 return 1 if $blob && $oidx->blob_exists($blob);
101 my $lk = $self->lock_for_scope_fast;
102 if (my $xoids = PublicInbox::LeiSearch::xoids_for($self, $eml, 1)) {
103 for my $docid (values %$xoids) {
104 $oidx->add_xref3($docid, -1, $blob, '.');
109 # n.b. above xoids_for fills out eml->{-lei_fake_mid} if needed
111 $smsg = bless {}, 'PublicInbox::Smsg';
113 $smsg->populate($eml);
116 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
117 $smsg->{blob} //= git_sha(1, $eml)->hexdigest;
118 $oidx->add_overview($eml, $smsg);
119 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
127 $self->{oidx} //= do {
128 my $creat = !-f $self->{-ovf};
129 my $lk = $self->lock_for_scope; # git-config doesn't wait
130 my $oidx = PublicInbox::OverIdx->new($self->{-ovf});
131 $oidx->{-no_fsync} = 1;
134 $oidx->{dbh}->do('PRAGMA journal_mode = WAL');
135 $oidx->eidx_prep; # for xref3
141 sub over { $_[0]->{oidx} } # for xoids_for
143 sub git { $_[0]->{ale}->git }
147 $self->{ale}->git->cleanup;
148 my $oidx = delete($self->{oidx}) // return;
158 # find existing directory containing a `lei.saved-search' file based on
159 # $dir_ref which is an output
161 my ($self, $lei, $dir_ref, $fn_ref) = @_;
162 my $dst = $$dir_ref; # imap://$MAILBOX, /path/to/maildir, /path/to/mbox
163 my $dir = lss_dir_for($lei, \$dst);
164 my $f = "$dir/lei.saved-search";
166 $self->{-cfg} = PublicInbox::Config::git_config_dump($f);
175 *nntp_url = \&cloneurl;
176 *base_url = \&PublicInbox::Inbox::base_url;
177 *smsg_eml = \&PublicInbox::Inbox::smsg_eml;
178 *smsg_by_mid = \&PublicInbox::Inbox::smsg_by_mid;
179 *msg_by_mid = \&PublicInbox::Inbox::msg_by_mid;
180 *modified = \&PublicInbox::Inbox::modified;
181 *recent = \&PublicInbox::Inbox::recent;
182 *max_git_epoch = *nntp_usable = *msg_by_path = \&mm; # undef
183 *isrch = *search = \&mm; # TODO
184 *DESTROY = \&pause_dedupe;