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 # move this to PublicInbox::Config if other things use it:
17 my %cquote = ("\n" => '\\n', "\t" => '\\t', "\b" => '\\b');
18 sub cquote_val ($) { # cf. git-config(1)
20 $val =~ s/([\n\t\b])/$cquote{$1}/g;
24 sub ARRAY_FIELDS () { qw(only include exclude) }
26 qw(external local remote import-remote import-before threads)
29 sub lss_dir_for ($$) {
30 my ($lei, $dstref) = @_;
32 if ($$dstref =~ m,\Aimaps?://,i) { # already canonicalized
33 require PublicInbox::URIimap;
34 my $uri = PublicInbox::URIimap->new($$dstref)->canonical;
38 $$dstref = $lei->rel2abs($$dstref);
39 $$dstref .= '/' if -d $$dstref;
41 @n = ($$dstref =~ m{([^/]+)/*\z});
43 push @n, sha256_hex($$dstref);
44 $lei->share_path . '/saved-searches/' . join('-', @n);
49 my $lss_dir = $lei->share_path.'/saved-searches/';
50 return () unless -d $lss_dir;
51 # TODO: persist the cache? Use another format?
52 my $f = $lei->cache_dir."/saved-tmp.$$.".time.'.config';
53 open my $fh, '>', $f or die "open $f: $!";
54 print $fh "[include]\n";
55 for my $p (glob("$lss_dir/*/lei.saved-search")) {
56 print $fh "\tpath = ", cquote_val($p), "\n";
58 close $fh or die "close $f: $!";
59 my $cfg = PublicInbox::Config::git_config_dump($f);
61 bless $cfg, 'PublicInbox::Config';
62 my $out = $cfg->get_all('lei.q.output') or return ();
64 s!\A(?:maildir|mh|mbox.+|mmdf):!!i;
70 my ($cls, $lei, $dst) = @_;
71 my $self = bless { ale => $lei->ale }, $cls;
73 if (defined $dst) { # updating existing saved search via "lei up"
76 output2lssdir($self, $lei, \$dir, \$f) or
77 return $lei->fail("--save was not used with $dst cwd=".
79 $self->{-cfg} //= PublicInbox::Config::git_config_dump($f);
81 } else { # new saved search "lei q --save"
82 $dst = $lei->{ovv}->{dst};
83 $dir = lss_dir_for($lei, \$dst);
85 File::Path::make_path($dir); # raises on error
87 my $f = $self->{'-f'} = "$dir/lei.saved-search";
88 open my $fh, '>', $f or return $lei->fail("open $f: $!");
89 my $sq_dst = PublicInbox::Config::squote_maybe($dst);
90 my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
92 $q = join("\n", map { "\tq = ".cquote_val($_) } @$q);
94 $q = "\tq = ".cquote_val($q);
96 $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i;
98 ; to refresh with new results, run: lei up $sq_dst
104 for my $k (ARRAY_FIELDS) {
105 my $ary = $lei->{opt}->{$k} // next;
107 print $fh "\t$k = ".cquote_val($x)."\n";
110 for my $k (BOOL_FIELDS) {
111 my $val = $lei->{opt}->{$k} // next;
112 print $fh "\t$k = ".($val ? 1 : 0)."\n";
114 close($fh) or return $lei->fail("close $f: $!");
116 bless $self->{-cfg}, 'PublicInbox::Config';
117 $self->{lock_path} = "$self->{-f}.flock";
118 $self->{-ovf} = "$dir/over.sqlite3";
122 sub description { $_[0]->{qstr} } # for WWW
124 sub cfg_set { # called by LeiXSearch
125 my ($self, @args) = @_;
126 my $lk = $self->lock_for_scope; # git-config doesn't wait
127 run_die([qw(git config -f), $self->{'-f'}, @args]);
130 # drop-in for LeiDedupe API
132 my ($self, $eml, $smsg) = @_;
133 my $oidx = $self->{oidx} // die 'BUG: no {oidx}';
134 my $blob = $smsg ? $smsg->{blob} : undef;
135 return 1 if $blob && $oidx->blob_exists($blob);
136 my $lk = $self->lock_for_scope_fast;
137 if (my $xoids = PublicInbox::LeiSearch::xoids_for($self, $eml, 1)) {
138 for my $docid (values %$xoids) {
139 $oidx->add_xref3($docid, -1, $blob, '.');
144 # n.b. above xoids_for fills out eml->{-lei_fake_mid} if needed
146 $smsg = bless {}, 'PublicInbox::Smsg';
148 $smsg->populate($eml);
151 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
152 $smsg->{blob} //= git_sha(1, $eml)->hexdigest;
153 $oidx->add_overview($eml, $smsg);
154 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
162 $self->{oidx} //= do {
163 my $creat = !-f $self->{-ovf};
164 my $lk = $self->lock_for_scope; # git-config doesn't wait
165 my $oidx = PublicInbox::OverIdx->new($self->{-ovf});
166 $oidx->{-no_fsync} = 1;
169 $oidx->{dbh}->do('PRAGMA journal_mode = WAL');
170 $oidx->eidx_prep; # for xref3
176 sub over { $_[0]->{oidx} } # for xoids_for
178 sub git { $_[0]->{ale}->git }
182 $self->{ale}->git->cleanup;
183 my $oidx = delete($self->{oidx}) // return;
193 # find existing directory containing a `lei.saved-search' file based on
194 # $dir_ref which is an output
196 my ($self, $lei, $dir_ref, $fn_ref) = @_;
197 my $dst = $$dir_ref; # imap://$MAILBOX, /path/to/maildir, /path/to/mbox
198 my $dir = lss_dir_for($lei, \$dst);
199 my $f = "$dir/lei.saved-search";
201 $self->{-cfg} = PublicInbox::Config::git_config_dump($f);
210 *nntp_url = \&cloneurl;
211 *base_url = \&PublicInbox::Inbox::base_url;
212 *smsg_eml = \&PublicInbox::Inbox::smsg_eml;
213 *smsg_by_mid = \&PublicInbox::Inbox::smsg_by_mid;
214 *msg_by_mid = \&PublicInbox::Inbox::msg_by_mid;
215 *modified = \&PublicInbox::Inbox::modified;
216 *recent = \&PublicInbox::Inbox::recent;
217 *max_git_epoch = *nntp_usable = *msg_by_path = \&mm; # undef
218 *isrch = *search = \&mm; # TODO
219 *DESTROY = \&pause_dedupe;