Skip to content

Commit 7910662

Browse files
authored
Merge pull request #45 from metacpan/mickey/purge
Added purge script
2 parents 3fc7e6b + 4fe2368 commit 7910662

File tree

4 files changed

+111
-10
lines changed

4 files changed

+111
-10
lines changed

bin/purge.pl

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use strict;
2+
use warnings;
3+
use v5.36;
4+
5+
use Getopt::Long;
6+
use MetaCPAN::Logger qw< :log :dlog >;
7+
8+
use MetaCPAN::ES;
9+
use MetaCPAN::Ingest qw<
10+
are_you_sure
11+
author_dir
12+
>;
13+
14+
# args
15+
my ( $author, $release, $force );
16+
GetOptions(
17+
"author=s" => \$author,
18+
"release=s" => \$release,
19+
"force" => \$force,
20+
);
21+
22+
# setup
23+
my $type2index = {
24+
release => 'cpan',
25+
file => 'cpan',
26+
author => 'cpan',
27+
favorite => 'cpan',
28+
permission => 'cpan',
29+
contributor => 'contributor',
30+
};
31+
32+
33+
purge_author() if $author;
34+
35+
log_info {'Done'};
36+
37+
sub purge_author () {
38+
# confirm
39+
$release
40+
? are_you_sure( sprintf("%s's %s release is about to be purged!", $author, $release), $force )
41+
: are_you_sure( sprintf("All of %s's releases are about to be purged!", $author), $force );
42+
43+
my $query = {
44+
bool => {
45+
must => [
46+
{ term => { author => $author } },
47+
( $release
48+
? { term => { release => $release } }
49+
: ()
50+
)
51+
]
52+
}
53+
};
54+
55+
purge_ids( type => 'favorite', query => $query);
56+
purge_ids( type => 'file', query => $query);
57+
purge_ids( type => 'release', query => $query);
58+
if ( !$release ) {
59+
purge_ids( type => 'author', id => $author );
60+
purge_ids( type => 'contributor', id => $author );
61+
}
62+
}
63+
64+
sub purge_ids ( %args ) {
65+
my $type = $args{type};
66+
my $es = MetaCPAN::ES->new(
67+
index => $type2index->{$type},
68+
type => $type
69+
);
70+
71+
my $bulk = $es->bulk;
72+
73+
my $id = $args{id};
74+
my $ids = $id
75+
? [ $id ]
76+
: $es->get_ids( query => $args{query} );
77+
78+
$bulk->delete_ids(@$ids);
79+
80+
$bulk->flush;
81+
}
82+
83+
1;
84+
85+
__END__

cpanfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ requires 'Search::Elasticsearch';
3131
requires 'Search::Elasticsearch::Client::2_0';
3232
requires 'Sub::Exporter';
3333
requires 'Text::CSV_XS';
34+
requires 'Term::ANSIColor';
3435
requires 'URI';
3536
requires 'XML::Simple';
3637

lib/MetaCPAN/ES.pm

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,37 @@ sub count ( $self, %args ) {
109109
);
110110
}
111111

112-
sub clear_type ( $self ) {
113-
my $bulk = $self->bulk;
112+
sub get_ids ( $self, %args ) {
113+
my $query = $args{query};
114+
114115
my $scroll = $self->scroll(
115-
query => { match_all => {} },
116+
query => $query // { match_all => {} },
116117
sort => '_doc',
117118
);
118119

119120
my @ids;
121+
120122
while ( my $search = $scroll->next ) {
121123
push @ids => $search->{_id};
122-
log_debug { "deleting id=" . $search->{_id} };
123-
if ( @ids == 500 ) {
124-
$bulk->delete_ids(@ids);
125-
@ids = ();
126-
}
127124
}
128-
$bulk->delete_ids(@ids);
125+
126+
return \@ids;
127+
}
128+
129+
sub delete_ids ( $self, $ids ) {
130+
my $bulk = $self->bulk;
131+
132+
while ( my @batch = splice(@$ids, 0, 500) ) {
133+
$bulk->delete_ids(@batch);
134+
}
129135

130136
$bulk->flush;
131137
}
132138

139+
sub clear_type ( $self ) {
140+
my $ids = $self->get_ids();
141+
142+
$self->delete_ids(@$ids);
143+
}
144+
133145
1;

lib/MetaCPAN/Ingest.pm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use LWP::UserAgent;
1212
use Path::Tiny qw< path >;
1313
use PAUSE::Permissions ();
1414
use Ref::Util qw< is_ref is_plain_arrayref is_plain_hashref >;
15+
use Term::ANSIColor qw< colored >;
1516
use XML::Simple qw< XMLin >;
1617

1718
use MetaCPAN::Config;
@@ -56,7 +57,9 @@ $config->init_logger;
5657

5758
sub config () {$config}
5859

59-
sub are_you_sure ( $msg ) {
60+
sub are_you_sure ( $msg, $force=0 ) {
61+
return 1 if $force;
62+
6063
my $iconfirmed = 0;
6164

6265
if ( -t *STDOUT ) {

0 commit comments

Comments
 (0)