|
| 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__ |
0 commit comments