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 # move this to PublicInbox::Config if other things use it:
19 my %cquote = ("\n" => '\\n', "\t" => '\\t', "\b" => '\\b');
20 sub cquote_val ($) { # cf. git-config(1)
22 $val =~ s/([\n\t\b])/$cquote{$1}/g;
26 sub lss_dir_for ($$) {
27 my ($lei, $dstref) = @_;
29 if ($$dstref =~ m,\Aimaps?://,i) { # already canonicalized
30 require PublicInbox::URIimap;
31 my $uri = PublicInbox::URIimap->new($$dstref)->canonical;
35 $$dstref = $lei->rel2abs($$dstref);
36 $$dstref .= '/' if -d $$dstref;
38 @n = ($$dstref =~ m{([^/]+)/*\z});
40 push @n, sha256_hex($$dstref);
41 $lei->share_path . '/saved-searches/' . join('-', @n);
45 my ($cls, $lei, $dst) = @_;
46 my $self = bless { ale => $lei->ale }, $cls;
48 if (defined $dst) { # updating existing saved search via "lei up"
51 output2lssdir($self, $lei, \$dir, \$f) or
52 return $lei->fail("--save was not used with $dst cwd=".
54 $self->{-cfg} //= PublicInbox::Config::git_config_dump($f);
56 } else { # new saved search "lei q --save"
57 my $dst = $lei->{ovv}->{dst};
58 $dir = lss_dir_for($lei, \$dst);
60 File::Path::make_path($dir); # raises on error
62 my $f = $self->{'-f'} = "$dir/lei.saved-search";
63 open my $fh, '>', $f or return $lei->fail("open $f: $!");
64 my $sq_dst = squote_maybe($dst);
65 my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
67 $q = join("\n", map { "\tq = ".cquote_val($_) } @$q);
69 $q = "\tq = ".cquote_val($q);
71 $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i;
73 ; to refresh with new results, run: lei up $sq_dst
79 for my $k (qw(only include exclude)) {
80 my $ary = $lei->{opt}->{$k} // next;
82 print $fh "\t$k = ".cquote_val($x)."\n";
85 for my $k (qw(external local remote import-remote
86 import-before threads)) {
87 my $val = $lei->{opt}->{$k} // next;
88 print $fh "\t$k = ".cquote_val($val)."\n";
90 close($fh) or return $lei->fail("close $f: $!");
92 bless $self->{-cfg}, 'PublicInbox::Config';
93 $self->{lock_path} = "$self->{-f}.flock";
94 $self->{-ovf} = "$dir/over.sqlite3";
98 sub description { $_[0]->{qstr} } # for WWW
100 sub cfg_set { # called by LeiXSearch
101 my ($self, @args) = @_;
102 my $lk = $self->lock_for_scope; # git-config doesn't wait
103 run_die([qw(git config -f), $self->{'-f'}, @args]);
106 # drop-in for LeiDedupe API
108 my ($self, $eml, $smsg) = @_;
109 my $oidx = $self->{oidx} // die 'BUG: no {oidx}';
110 my $blob = $smsg ? $smsg->{blob} : undef;
111 return 1 if $blob && $oidx->blob_exists($blob);
112 my $lk = $self->lock_for_scope_fast;
113 if (my $xoids = PublicInbox::LeiSearch::xoids_for($self, $eml, 1)) {
114 for my $docid (values %$xoids) {
115 $oidx->add_xref3($docid, -1, $blob, '.');
120 # n.b. above xoids_for fills out eml->{-lei_fake_mid} if needed
122 $smsg = bless {}, 'PublicInbox::Smsg';
124 $smsg->populate($eml);
127 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
128 $smsg->{blob} //= git_sha(1, $eml)->hexdigest;
129 $oidx->add_overview($eml, $smsg);
130 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
138 $self->{oidx} //= do {
139 my $creat = !-f $self->{-ovf};
140 my $lk = $self->lock_for_scope; # git-config doesn't wait
141 my $oidx = PublicInbox::OverIdx->new($self->{-ovf});
142 $oidx->{-no_fsync} = 1;
145 $oidx->{dbh}->do('PRAGMA journal_mode = WAL');
146 $oidx->eidx_prep; # for xref3
152 sub over { $_[0]->{oidx} } # for xoids_for
154 sub git { $_[0]->{ale}->git }
158 $self->{ale}->git->cleanup;
159 my $oidx = delete($self->{oidx}) // return;
169 # find existing directory containing a `lei.saved-search' file based on
170 # $dir_ref which is an output
172 my ($self, $lei, $dir_ref, $fn_ref) = @_;
173 my $dst = $$dir_ref; # imap://$MAILBOX, /path/to/maildir, /path/to/mbox
174 my $dir = lss_dir_for($lei, \$dst);
175 my $f = "$dir/lei.saved-search";
177 $self->{-cfg} = PublicInbox::Config::git_config_dump($f);
186 *nntp_url = \&cloneurl;
187 *base_url = \&PublicInbox::Inbox::base_url;
188 *smsg_eml = \&PublicInbox::Inbox::smsg_eml;
189 *smsg_by_mid = \&PublicInbox::Inbox::smsg_by_mid;
190 *msg_by_mid = \&PublicInbox::Inbox::msg_by_mid;
191 *modified = \&PublicInbox::Inbox::modified;
192 *recent = \&PublicInbox::Inbox::recent;
193 *max_git_epoch = *nntp_usable = *msg_by_path = \&mm; # undef
194 *isrch = *search = \&mm; # TODO
195 *DESTROY = \&pause_dedupe;