Jump to content
Wiki Home
EPrints Home
Bazaar
Files
Main menu
Main menu
move to sidebar
hide
Navigation
Front Page
EPrints 3 Manual
EPrints Training Course
EPrints Glossary
Installing EPrints
How-to Configure EPrints
EPrints 3.2+ API
EPrints 3 Reference
EPrints Plugins
Wiki
All categories
Recent changes
Main Page
Help
EPrints Documentation
Search
Search
Appearance
Log in
Request account
Personal tools
Log in
Request account
Pages for logged out editors
learn more
Contributions
Talk
Editing
Remove subjectid script
(section)
Page
Discussion
British English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Instructions== Save this script into <code>~/bin/local/remove_subjectid_from_eprint</code> Edit the perl path as necessary. To use the script: <code>~/bin/local/remove_subjectid_from_eprint ARCHIVEID FIELDNAME SUBJECTID</code> this will list the changes that the script might make (a dry-run by default) <code>~/bin/local/remove_subjectid_from_eprint ARCHIVEID FIELDNAME SUBJECTID yes</code> adding 'yes' to the end of the parameters will actually make the changes. <source lang="perl"> #!/usr/bin/perl -w use FindBin; use lib "$FindBin::Bin/../../perl_lib"; use EPrints; use strict; our $noise = 1; if ( scalar @ARGV < 3 ){ print "Usage: $0 ARCHIVEID SUBJ_FIELDNAME SUBJECTID [yes]\n"; exit 1; } # Set STDOUT to auto flush (without needing a \n) $|=1; my $repoid = $ARGV[0]; my $fieldname = $ARGV[1]; my $subjectid = $ARGV[2]; my $doit = $ARGV[3]; $doit ||= 0; my $session = new EPrints::Session( 1 , $repoid , $noise ); if( !defined $session ) { print STDERR "Failed to load repository: $repoid\n"; exit 1; } #check eprint has a field that matches the FIELDNAME param if( !$session->get_repository->dataset( 'eprint' )->has_field( $fieldname ) ){ print STDERR "ERROR: EPrint dataset doesn't have a field called $fieldname.\n"; exit 1; } #check the field FIELDNAME is a Subject field if( !$session->get_repository->dataset( 'eprint' )->get_field( $fieldname )->isa( "EPrints::MetaField::Subject" ) ){ print STDERR "ERROR: Field $fieldname is not a Subject type field.\n"; exit 1; }; my $list = $session->get_repository->dataset( 'eprint' )->search( filters => [ { meta_fields => [ $fieldname ], value => $subjectid } ] ); # map function onto all matched EPrints $list->map( sub { my $eprint = $_[2] or return; print "EPrint: ",$eprint->get_id,"\n"; my @subjects = @{ $eprint->value( $fieldname ) || [] }; my $done_any = 0; print "Old subjects:\t", ( join ", ", @subjects ), "\n"; my @new_subjects; foreach my $subject (@subjects) { if( $subject ne $subjectid ) { #this subject isn;t the one we're trying to delete - keep it! push @new_subjects, $subject; $done_any++; } else { #we've matched the subjectid we want to delete print "Removing $subject\n"; } } return if !$done_any; print " New subjects:\t", ( join ", ", @new_subjects ), "\n"; if( $doit eq "yes" ){ print "Updating $fieldname\n"; $eprint->set_value( $fieldname, \@new_subjects ); $eprint->commit; } print "\n"; } ); $session->terminate(); exit; </source> [[Category:Howto]] [[Category:Snippets]] [[Category:Subject Trees]]
Summary:
Please note that all contributions to EPrints Documentation may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
EPrints Documentation:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)