Skip to content

Instantly share code, notes, and snippets.

@jesusbagpuss
Created June 29, 2023 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesusbagpuss/0266631121c1a894bbb6404221ca603b to your computer and use it in GitHub Desktop.
Save jesusbagpuss/0266631121c1a894bbb6404221ca603b to your computer and use it in GitHub Desktop.
EPrints - update division field values
#!/usr/bin/perl -w
##########################################################################
#
# Save this into [EPRINTS_ROOT]/bin/local/JLRS_remap_eprint_divisions
# ...or adjust the 'use lib' line below in relation to where you've saved it.
#
##########################################################################
use FindBin;
use lib "$FindBin::Bin/../../perl_lib";
use EPrints;
use strict;
##########################################################################
# Adjust these values...
my $fieldname = 'divisions';
my $old_division = 'div_a';
my $new_divisions = {
'div_a1' => [ 1, 3, 5, 7, 9 ],
'div_a2' => [ 1, 2, 4, 8 ],
};
#
##########################################################################
our $noise = 1;
if ( scalar @ARGV < 1 ){
print "Usage: $0 ARCHIVEID [make_changes]\n";
exit 1;
}
$|=1;
my $repoid = $ARGV[0];
my $make_changes = $ARGV[1];
$make_changes ||= 0;
my $session = new EPrints::Session( 1 , $repoid , $noise );
if( !defined $session )
{
print STDERR "Failed to load repository: $repoid\n";
exit 1;
}
# Get all the records in the old division.
# The 'EX' match will not match child-nodes of the division.
my $list = $session->get_repository->dataset( 'eprint' )->search(
filters => [
{meta_fields => [ $fieldname ], value => $old_division, match=> 'EX' }
]
);
print "There are ", $list->count, " eprints in $old_division\n";
$list->map( sub {
my $eprint = $_[2] or return;
my $ep_id = $eprint->get_id;
print "EPrint: $ep_id matched $old_division\n";
# get existing divisions
my @divs = @{ $eprint->value( $fieldname ) || [] };
# find out which new divisions to add to this.
print "Old $fieldname:\t", ( join ", ", @divs ), "\n";
my @replacement_divs; # to store the new (re-mapped) divs.
my @new_divs;
# find out if this eprint is mapped into one (or more) of the new divisions
foreach my $n_d ( keys $new_divisions ){
# if the eprintid is in the array, map it!
if (grep { $ep_id == $_ } @{$new_divisions->{$n_d}} )
{
print "$ep_id matches $n_d\n";
push @replacement_divs, $n_d;
}
}
# if there are new mapped divisions
if( @replacement_divs )
{
# keep the other divisions, replace the division we're interested in with the new one(s)
foreach( @divs )
{
if( $_ eq $old_division )
{
push @new_divs, @replacement_divs;
}
else
{
push @new_divs, $_;
}
}
print "New divs: ", join( ", ", @new_divs), "\n";
if( $make_changes ){
$eprint->set_value( $fieldname, \@new_divs );
if( $eprint->commit )
{
print "Updated eprint with new divs.\n\n";
}
else
{
print "Eprint failed to update\n\n";
}
}
else
{
print "\$make_changes not set. EPrint has not been updated.\n\n";
}
}
else
{
print "EPrint not in list for any new divisions\n\n";
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment