]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiSavedSearch.pm
avoid wrong/needless squote_maybe shortcuts
[public-inbox.git] / lib / PublicInbox / LeiSavedSearch.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 # pretends to be like LeiDedupe and also PublicInbox::Inbox
5 package PublicInbox::LeiSavedSearch;
6 use strict;
7 use v5.10.1;
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);
15
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)
19         my ($val) = @_;
20         $val =~ s/([\n\t\b])/$cquote{$1}/g;
21         $val;
22 }
23
24 sub lss_dir_for ($$) {
25         my ($lei, $dstref) = @_;
26         my @n;
27         if ($$dstref =~ m,\Aimaps?://,i) { # already canonicalized
28                 require PublicInbox::URIimap;
29                 my $uri = PublicInbox::URIimap->new($$dstref)->canonical;
30                 $$dstref = $$uri;
31                 @n = ($uri->mailbox);
32         } else { # basename
33                 $$dstref = $lei->rel2abs($$dstref);
34                 $$dstref .= '/' if -d $$dstref;
35                 $$dstref =~ tr!/!/!s;
36                 @n = ($$dstref =~ m{([^/]+)/*\z});
37         }
38         push @n, sha256_hex($$dstref);
39         $lei->share_path . '/saved-searches/' . join('-', @n);
40 }
41
42 sub new {
43         my ($cls, $lei, $dst) = @_;
44         my $self = bless { ale => $lei->ale }, $cls;
45         my $dir;
46         if (defined $dst) { # updating existing saved search via "lei up"
47                 my $f;
48                 $dir = $dst;
49                 output2lssdir($self, $lei, \$dir, \$f) or
50                         return $lei->fail("--save was not used with $dst cwd=".
51                                                 $lei->rel2abs('.'));
52                 $self->{-cfg} //= PublicInbox::Config::git_config_dump($f);
53                 $self->{'-f'} = $f;
54         } else { # new saved search "lei q --save"
55                 my $dst = $lei->{ovv}->{dst};
56                 $dir = lss_dir_for($lei, \$dst);
57                 require File::Path;
58                 File::Path::make_path($dir); # raises on error
59                 $self->{-cfg} = {};
60                 my $f = $self->{'-f'} = "$dir/lei.saved-search";
61                 open my $fh, '>', $f or return $lei->fail("open $f: $!");
62                 my $sq_dst = PublicInbox::Config::squote_maybe($dst);
63                 my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
64                 if (ref $q) {
65                         $q = join("\n", map { "\tq = ".cquote_val($_) } @$q);
66                 } else {
67                         $q = "\tq = ".cquote_val($q);
68                 }
69                 $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i;
70                 print $fh <<EOM;
71 ; to refresh with new results, run: lei up $sq_dst
72 [lei]
73 $q
74 [lei "q"]
75         output = $dst
76 EOM
77                 for my $k (qw(only include exclude)) {
78                         my $ary = $lei->{opt}->{$k} // next;
79                         for my $x (@$ary) {
80                                 print $fh "\t$k = ".cquote_val($x)."\n";
81                         }
82                 }
83                 for my $k (qw(external local remote import-remote
84                                 import-before threads)) {
85                         my $val = $lei->{opt}->{$k} // next;
86                         print $fh "\t$k = ".cquote_val($val)."\n";
87                 }
88                 close($fh) or return $lei->fail("close $f: $!");
89         }
90         bless $self->{-cfg}, 'PublicInbox::Config';
91         $self->{lock_path} = "$self->{-f}.flock";
92         $self->{-ovf} = "$dir/over.sqlite3";
93         $self;
94 }
95
96 sub description { $_[0]->{qstr} } # for WWW
97
98 sub cfg_set { # called by LeiXSearch
99         my ($self, @args) = @_;
100         my $lk = $self->lock_for_scope; # git-config doesn't wait
101         run_die([qw(git config -f), $self->{'-f'}, @args]);
102 }
103
104 # drop-in for LeiDedupe API
105 sub is_dup {
106         my ($self, $eml, $smsg) = @_;
107         my $oidx = $self->{oidx} // die 'BUG: no {oidx}';
108         my $blob = $smsg ? $smsg->{blob} : undef;
109         return 1 if $blob && $oidx->blob_exists($blob);
110         my $lk = $self->lock_for_scope_fast;
111         if (my $xoids = PublicInbox::LeiSearch::xoids_for($self, $eml, 1)) {
112                 for my $docid (values %$xoids) {
113                         $oidx->add_xref3($docid, -1, $blob, '.');
114                 }
115                 $oidx->commit_lazy;
116                 1;
117         } else {
118                 # n.b. above xoids_for fills out eml->{-lei_fake_mid} if needed
119                 unless ($smsg) {
120                         $smsg = bless {}, 'PublicInbox::Smsg';
121                         $smsg->{bytes} = 0;
122                         $smsg->populate($eml);
123                 }
124                 $oidx->begin_lazy;
125                 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
126                 $smsg->{blob} //= git_sha(1, $eml)->hexdigest;
127                 $oidx->add_overview($eml, $smsg);
128                 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
129                 $oidx->commit_lazy;
130                 undef;
131         }
132 }
133
134 sub prepare_dedupe {
135         my ($self) = @_;
136         $self->{oidx} //= do {
137                 my $creat = !-f $self->{-ovf};
138                 my $lk = $self->lock_for_scope; # git-config doesn't wait
139                 my $oidx = PublicInbox::OverIdx->new($self->{-ovf});
140                 $oidx->{-no_fsync} = 1;
141                 $oidx->dbh;
142                 if ($creat) {
143                         $oidx->{dbh}->do('PRAGMA journal_mode = WAL');
144                         $oidx->eidx_prep; # for xref3
145                 }
146                 $oidx
147         };
148 }
149
150 sub over { $_[0]->{oidx} } # for xoids_for
151
152 sub git { $_[0]->{ale}->git }
153
154 sub pause_dedupe {
155         my ($self) = @_;
156         $self->{ale}->git->cleanup;
157         my $oidx = delete($self->{oidx}) // return;
158         $oidx->commit_lazy;
159 }
160
161 sub mm { undef }
162
163 sub altid_map { {} }
164
165 sub cloneurl { [] }
166
167 # find existing directory containing a `lei.saved-search' file based on
168 # $dir_ref which is an output
169 sub output2lssdir {
170         my ($self, $lei, $dir_ref, $fn_ref) = @_;
171         my $dst = $$dir_ref; # imap://$MAILBOX, /path/to/maildir, /path/to/mbox
172         my $dir = lss_dir_for($lei, \$dst);
173         my $f = "$dir/lei.saved-search";
174         if (-f $f && -r _) {
175                 $self->{-cfg} = PublicInbox::Config::git_config_dump($f);
176                 $$dir_ref = $dir;
177                 $$fn_ref = $f;
178                 return 1;
179         }
180         undef;
181 }
182
183 no warnings 'once';
184 *nntp_url = \&cloneurl;
185 *base_url = \&PublicInbox::Inbox::base_url;
186 *smsg_eml = \&PublicInbox::Inbox::smsg_eml;
187 *smsg_by_mid = \&PublicInbox::Inbox::smsg_by_mid;
188 *msg_by_mid = \&PublicInbox::Inbox::msg_by_mid;
189 *modified = \&PublicInbox::Inbox::modified;
190 *recent = \&PublicInbox::Inbox::recent;
191 *max_git_epoch = *nntp_usable = *msg_by_path = \&mm; # undef
192 *isrch = *search = \&mm; # TODO
193 *DESTROY = \&pause_dedupe;
194
195 1;