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 # "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;
11 use PublicInbox::Config;
13 sub inspect_blob ($$) {
14 my ($lei, $oidhex) = @_;
16 if (my $lse = $lei->{lse}) {
17 my @docids = $lse ? $lse->over->blob_exists($oidhex) : ();
18 $ent->{'lei/store'} = \@docids if @docids;
20 if (my $loc = $lms ? $lms->locations_for($oidhex) : undef) {
27 sub inspect_sync_folder ($$) {
28 my ($lei, $folder) = @_;
30 my $lse = $lei->{lse} or return $ent;
31 my $lms = $lse->lms or return $ent;
33 if ($folder =~ m!\Aimaps?://!i) {
34 require PublicInbox::URIimap;
35 my $uri = PublicInbox::URIimap->new($folder)->canonical;
36 if (defined($uri->uidvalidity)) {
39 my @maybe = $lms->folders($$uri);
41 my $u = PublicInbox::URIimap->new($_);
42 $uri->uidvalidity($u->uidvalidity);
46 } elsif ($folder =~ m!\A(maildir|mh):(.+)!i) {
48 $folders[0] = "$type:".$lei->abs_path($2);
49 } elsif (-d $folder) {
50 $folders[0] = 'maildir:'.$lei->abs_path($folder);
52 $lei->fail("$folder not understood");
54 $lei->qerr("# no folders match $folder (non-fatal)") if !@folders;
55 for my $f (@folders) {
56 $ent->{$f} = $lms->location_stats($f); # may be undef
62 my ($lei, $item, $more) = @_;
64 if ($item =~ /\Ablob:(.+)/) {
65 $ent = inspect_blob($lei, $1);
66 } elsif ($item =~ m!\Aimaps?://!i ||
67 $item =~ m!\A(?:maildir|mh):!i || -d $item) {
68 $ent = inspect_sync_folder($lei, $item);
69 } else { # TODO: more things
70 return $lei->fail("$item not understood");
72 $lei->out($lei->{json}->encode($ent));
73 $lei->out(',') if $more;
78 my ($lei, @argv) = @_;
79 $lei->{1}->autoflush(0);
80 my $multi = scalar(@argv) > 1;
81 $lei->out('[') if $multi;
82 $lei->{json} = ref(PublicInbox::Config::json())->new->utf8->canonical;
83 $lei->{lse} = ($lei->{opt}->{external} // 1) ? do {
84 my $sto = $lei->_lei_store;
85 $sto ? $sto->search : undef;
87 if ($lei->{opt}->{pretty} || -t $lei->{1}) {
88 $lei->{json}->pretty(1)->indent(2);
90 while (defined(my $x = shift @argv)) {
91 inspect1($lei, $x, scalar(@argv)) or return;
93 $lei->out(']') if $multi;