]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiInspect.pm
48da826b89b65f6bf87d74eeffb24a77030ead72
[public-inbox.git] / lib / PublicInbox / LeiInspect.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 # "lei inspect" general purpose inspector for stuff in SQLite and
5 # Xapian.  Will eventually be useful with plain public-inboxes,
6 # not just lei/store.  This is totally half-baked at the moment
7 # but useful for testing.
8 package PublicInbox::LeiInspect;
9 use strict;
10 use v5.10.1;
11 use parent qw(PublicInbox::IPC);
12 use PublicInbox::Config;
13 use PublicInbox::MID qw(mids);
14
15 sub inspect_blob ($$) {
16         my ($lei, $oidhex) = @_;
17         my $ent = {};
18         if (my $lse = $lei->{lse}) {
19                 my $oidbin = pack('H*', $oidhex);
20                 my @docids = $lse ? $lse->over->oidbin_exists($oidbin) : ();
21                 $ent->{'lei/store'} = \@docids if @docids;
22                 my $lms = $lei->lms;
23                 if (my $loc = $lms ? $lms->locations_for($oidbin) : undef) {
24                         $ent->{'mail-sync'} = $loc;
25                 }
26         }
27         $ent;
28 }
29
30 sub inspect_imap_uid ($$) {
31         my ($lei, $uid_uri) = @_;
32         my $ent = {};
33         my $lms = $lei->lms or return $ent;
34         my $oidhex = $lms->imap_oid($lei, $uid_uri);
35         if (ref(my $err = $oidhex)) { # art2folder error
36                 $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
37         }
38         $ent->{$$uid_uri} = $oidhex;
39         $ent;
40 }
41
42 sub inspect_sync_folder ($$) {
43         my ($lei, $folder) = @_;
44         my $ent = {};
45         my $lms = $lei->lms or return $ent;
46         my $folders = [ $folder ];
47         my $err = $lms->arg2folder($lei, $folders);
48         if ($err) {
49                 if ($err->{fail}) {
50                         $lei->qerr("# no folders match $folder (non-fatal)");
51                         @$folders = ();
52                 }
53                 $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
54         }
55         for my $f (@$folders) {
56                 $ent->{$f} = $lms->location_stats($f); # may be undef
57         }
58         $ent
59 }
60
61 sub inspect_docid ($$;$) {
62         my ($lei, $docid, $ent) = @_;
63         require PublicInbox::Search;
64         $ent //= {};
65         my $xdb;
66         if ($xdb = delete $ent->{xdb}) { # from inspect_num
67         } elsif (defined(my $dir = $lei->{opt}->{dir})) {
68                 no warnings 'once';
69                 $xdb = $PublicInbox::Search::X{Database}->new($dir);
70         } else {
71                 $xdb = $lei->{lse}->xdb;
72         }
73         $xdb or return $lei->fail('no Xapian DB');
74         my $doc = $xdb->get_document($docid); # raises
75         my $data = $doc->get_data;
76         $ent->{docid} = $docid;
77         $ent->{data_length} = length($data);
78         $ent->{description} = $doc->get_description;
79         $ent->{$_} = $doc->$_ for (qw(termlist_count values_count));
80         my $cur = $doc->termlist_begin;
81         my $end = $doc->termlist_end;
82         for (; $cur != $end; $cur++) {
83                 my $tn = $cur->get_termname;
84                 $tn =~ s/\A([A-Z]+)// or warn "$tn no prefix! (???)";
85                 my $term = ($1 // '');
86                 push @{$ent->{terms}->{$term}}, $tn;
87         }
88         @$_ = sort(@$_) for values %{$ent->{terms} // {}};
89         $cur = $doc->values_begin;
90         $end = $doc->values_end;
91         for (; $cur != $end; $cur++) {
92                 my $n = $cur->get_valueno;
93                 my $v = $cur->get_value;
94                 my $iv = PublicInbox::Search::sortable_unserialise($v);
95                 $v = $iv + 0 if defined $iv;
96                 # not using ->[$n] since we may have large gaps in $n
97                 $ent->{'values'}->{$n} = $v;
98         }
99         $ent;
100 }
101
102 sub dir2ibx ($$) {
103         my ($lei, $dir) = @_;
104         if (-f "$dir/ei.lock") {
105                 require PublicInbox::ExtSearch;
106                 PublicInbox::ExtSearch->new($dir);
107         } elsif (-f "$dir/inbox.lock" || -d "$dir/public-inbox") {
108                 require PublicInbox::Inbox; # v2, v1
109                 bless { inboxdir => $dir }, 'PublicInbox::Inbox';
110         } else {
111                 $lei->fail("no (indexed) inbox or extindex at $dir");
112         }
113 }
114
115 sub inspect_num ($$) {
116         my ($lei, $num) = @_;
117         my ($docid, $ibx);
118         my $ent = { num => $num };
119         if (defined(my $dir = $lei->{opt}->{dir})) {
120                 $ibx = dir2ibx($lei, $dir) or return;
121                 if ($ent->{xdb} = $ibx->xdb) {
122                         my $num2docid = $lei->{lse}->can('num2docid');
123                         $docid = $num2docid->($ibx, $num);
124                 }
125         } else {
126                 $ibx = $lei->{lse};
127                 $lei->{lse}->xdb; # set {nshard} for num2docid
128                 $docid = $lei->{lse}->num2docid($num);
129         }
130         if ($ibx && $ibx->over) {
131                 my $smsg = $ibx->over->get_art($num);
132                 $ent->{smsg} = { %$smsg } if $smsg;
133         }
134         defined($docid) ? inspect_docid($lei, $docid, $ent) : $ent;
135 }
136
137 sub inspect_mid ($$) {
138         my ($lei, $mid) = @_;
139         my ($ibx, $over);
140         my $ent = { mid => $mid };
141         if (defined(my $dir = $lei->{opt}->{dir})) {
142                 my $num2docid = $lei->{lse}->can('num mid => [ $mid ] 2docid');
143                 $ibx = dir2ibx($lei, $dir) or return;
144                 # $ent->{xdb} = $ibx->xdb //
145                         # return $lei->fail("no Xapian DB for $dir");
146         } else {
147                 $ibx = $lei->{lse};
148                 $lei->{lse}->xdb; # set {nshard} for num2docid
149         }
150         if ($ibx && $ibx->over) {
151                 my ($id, $prev);
152                 while (my $smsg = $ibx->over->next_by_mid($mid, \$id, \$prev)) {
153                         push @{$ent->{smsg}}, { %$smsg }
154                 }
155         }
156         $ent;
157 }
158
159 sub inspect1 ($$$) {
160         my ($lei, $item, $more) = @_;
161         my $ent;
162         if ($item =~ /\Ablob:(.+)/) {
163                 $ent = inspect_blob($lei, $1);
164         } elsif ($item =~ m!\Aimaps?://!i) {
165                 require PublicInbox::URIimap;
166                 my $uri = PublicInbox::URIimap->new($item);
167                 if (defined($uri->uid)) {
168                         $ent = inspect_imap_uid($lei, $uri);
169                 } else {
170                         $ent = inspect_sync_folder($lei, $item);
171                 }
172         } elsif ($item =~ m!\A(?:maildir|mh):!i || -d $item) {
173                 $ent = inspect_sync_folder($lei, $item);
174         } elsif ($item =~ m!\Adocid:([0-9]+)\z!) {
175                 $ent = inspect_docid($lei, $1 + 0);
176         } elsif ($item =~ m!\Anum:([0-9]+)\z!) {
177                 $ent = inspect_num($lei, $1 + 0);
178         } elsif ($item =~ m!\A(?:mid|m):(.+)\z!) {
179                 $ent = inspect_mid($lei, $1);
180         } else { # TODO: more things
181                 return $lei->fail("$item not understood");
182         }
183         $lei->out($lei->{json}->encode($ent));
184         $lei->out(',') if $more;
185         1;
186 }
187
188 sub inspect_argv { # via wq_do
189         my ($self) = @_;
190         my ($lei, $argv) = delete @$self{qw(lei argv)};
191         my $multi = scalar(@$argv) > 1;
192         $lei->{1}->autoflush(0);
193         $lei->out('[') if $multi;
194         while (defined(my $x = shift @$argv)) {
195                 inspect1($lei, $x, scalar(@$argv)) or return;
196         }
197         $lei->out(']') if $multi;
198 }
199
200 sub inspect_start ($$) {
201         my ($lei, $argv) = @_;
202         my $self = bless { lei => $lei, argv => $argv }, __PACKAGE__;
203         my ($op_c, $ops) = $lei->workers_start($self, 1);
204         $lei->{wq1} = $self;
205         $lei->wait_wq_events($op_c, $ops);
206         $self->wq_do('inspect_argv');
207         $self->wq_close(1);
208 }
209
210 sub ins_add { # InputPipe->consume callback
211         my ($lei) = @_; # $_[1] = $rbuf
212         if (defined $_[1]) {
213                 $_[1] eq '' and return eval {
214                         my $str = delete $lei->{istr};
215                         $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
216                         my $eml = PublicInbox::Eml->new(\$str);
217                         inspect_start($lei, [
218                                 'blob:'.$lei->git_oid($eml)->hexdigest,
219                                 map { "mid:$_" } @{mids($eml)} ]);
220                 };
221                 $lei->{istr} .= $_[1];
222         } else {
223                 $lei->fail("error reading stdin: $!");
224         }
225 }
226
227 sub lei_inspect {
228         my ($lei, @argv) = @_;
229         $lei->{json} = ref(PublicInbox::Config::json())->new->utf8->canonical;
230         $lei->{lse} = ($lei->{opt}->{external} // 1) ? do {
231                 my $sto = $lei->_lei_store;
232                 $sto ? $sto->search : undef;
233         } : undef;
234         my $isatty = -t $lei->{1};
235         $lei->{json}->pretty(1)->indent(2) if $lei->{opt}->{pretty} || $isatty;
236         $lei->start_pager if $isatty;
237         if ($lei->{opt}->{stdin}) {
238                 return $lei->fail(<<'') if @argv;
239 no args allowed on command-line with --stdin
240
241                 require PublicInbox::InputPipe;
242                 PublicInbox::InputPipe::consume($lei->{0}, \&ins_add, $lei);
243         } else {
244                 inspect_start($lei, \@argv);
245         }
246 }
247
248 sub _complete_inspect {
249         my ($lei, @argv) = @_;
250         my $lms = $lei->lms or return;
251         my $match_cb = $lei->complete_url_prepare(\@argv);
252         map { $match_cb->($_) } $lms->folders;
253 }
254
255 1;