]> Sergey Matveev's repositories - public-inbox.git/blob - scripts/dupe-finder
use more idiomatic internal API for ->over access
[public-inbox.git] / scripts / dupe-finder
1 #!/usr/bin/perl -w
2 # Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 #
5 # ad-hoc tool for finding duplicates, unstable!
6 use strict;
7 use warnings;
8 use PublicInbox::Inbox;
9 use PublicInbox::Over;
10 use PublicInbox::Search;
11 use PublicInbox::Config;
12 my $repo = shift;
13 my $ibx;
14 if (index($repo, '@') > 0) {
15         $ibx = PublicInbox::Config->new->lookup($repo);
16 } elsif (-d $repo) {
17         $ibx = { inboxdir => $repo, address => 'unnamed@example.com' };
18         $ibx = PublicInbox::Inbox->new($ibx);
19 } else {
20         $ibx = PublicInbox::Config->new->lookup_name($repo);
21 }
22 $ibx or die "No inbox";
23 $ibx->search or die "search not available for inbox";
24 my $over = $ibx->over;
25
26 sub emit ($) {
27         my ($nums) = @_;
28         foreach my $n (@$nums) {
29                 my $smsg = $over->get_art($n) or next;
30                 print STDERR "$n $smsg->{blob} $smsg->{mid}\n";
31                 my $msg = $ibx->msg_by_smsg($smsg) or next;
32                 print "From $smsg->{blob}\@$n Thu Jan  1 00:00:00 1970\n";
33                 $$msg =~ s/^(>*From )/>$1/gm;
34                 print $$msg, "\n";
35         }
36 }
37
38 my $sth = $dbh->prepare(<<'');
39 SELECT id,num FROM id2num WHERE num > 0 ORDER BY id
40
41 $sth->execute;
42 my $prev_id = -1;
43 my ($id, $num, @nums);
44 while (1) {
45         ($id, $num) = $sth->fetchrow_array;
46         defined $id or last;
47         if ($prev_id != $id) {
48                 emit(\@nums) if scalar(@nums) > 1;
49                 @nums = ();
50         }
51         $prev_id = $id;
52         push @nums, $num;
53 }