1 package EPrints::Plugin::Import::MARC;
5 EPrints::Plugin::Import::MARC -- allows to import MARC records
9 This plugin allows you to import MARC and MARC XML records into GNU EPrints.
13 Configuration might be changed in cfg.d/marc.pl. Webserver needs to be restarted after any configuration changes.
15 =head1 COPYRIGHT AND LICENSE
17 (C) 2008 Jose Miguel Parrella Romero <bureado@cpan.org>
18 (C) 2013 Dobrica Pavlinušić <dpavlin@rot13.org>
19 This module is free software under the same terms of Perl.
23 use Data::Dump qw(dump);
28 our @ISA = qw/EPrints::Plugin::Import/;
32 my( $class, %params ) = @_;
34 my $self = $class->SUPER::new( %params );
36 $self->{name} = "MARC";
37 $self->{visible} = "all";
38 $self->{produce} = [ 'list/eprint' ];
40 my $rc = EPrints::Utils::require_if_exists("MARC::Record") and EPrints::Utils::require_if_exists("MARC::File::USMARC");
43 $self->{visible} = "";
44 $self->{error} = "Failed to load required modules.";
47 warn "XXX marc import with callbacks";
54 my( $plugin, %opts ) = @_;
57 my $file = MARC::File::USMARC->in( $opts{fh} );
59 while ( my $marc = $file->next() ) {
60 my $epdata = $plugin->convert_input( $marc );
61 next unless( defined $epdata );
63 my $dataobj = $plugin->epdata_to_dataobj( $opts{dataset}, $epdata );
64 if( defined $dataobj )
66 push @ids, $dataobj->get_id;
70 return EPrints::List->new(
71 dataset => $opts{dataset},
72 session => $plugin->{session},
80 my( $plugin, %opts ) = @_;
82 if( $opts{filename} eq '-' )
84 $plugin->error("Does not support input from STDIN");
90 my $file = MARC::File::USMARC->in( $opts{filename} );
92 while ( my $marc = $file->next() ) {
93 my $epdata = $plugin->convert_input( $marc );
94 next unless( defined $epdata );
96 my $dataobj = $plugin->epdata_to_dataobj( $opts{dataset}, $epdata );
97 if( defined $dataobj )
100 if ( my $code = $plugin->{session}->get_repository->get_conf( "marc" )->{dataobj_callback} ) {
101 $epdata = $code->($dataobj);
104 push @ids, $dataobj->get_id;
108 return EPrints::List->new(
109 dataset => $opts{dataset},
110 session => $plugin->{session},
119 my ( $plugin, $marc ) = @_;
120 my $epdata = (); # to be returned
122 # Taken from cfg.d/marc.pl
123 my %mappings = %{$plugin->{session}->get_repository->get_conf( "marc" )->{marc2ep}};
125 my $dataset = $plugin->{session}->get_dataset('archive');
127 foreach my $field ( $marc->fields() ) { # each field of the record
128 my $t = $field->tag();
129 my @list = grep ( /^$t/, keys %mappings ); # lookup for mappings
130 foreach my $i ( sort @list ) {
131 ( my $s ) = $i =~ /$t(.)/; # mapped subfield
132 my $ts = $t . $s; # complete tag+subfield
133 my $value = $field->as_string($s);
135 my $field = $mappings{$ts} || $plugin->error("no mapping for $ts");
136 my $metafield = $dataset->get_field($field);
138 $plugin->error("no filed $field") unless $metafield;
140 if ($metafield->get_property('multiple')) {
141 warn "# multiple $field in ",ref( $metafield ), "inserting name" if ! $debug->{$field}++;
142 $epdata->{$field} = [ { name => $value } ];
143 } elsif ( defined $epdata->{$field} ) {
144 $epdata->{$field} .= "\n" . $value;
146 $epdata->{$field} = $value; # bye!
152 my $field = $marc->field('100');
153 if ( defined $field ) {
154 foreach my $i ( $field->subfield('a') ) {
156 ( $name->{family}, $name->{given} ) = split ( "," , $i );
157 push @{ $epdata->{creators_name} }, $name if defined $name;
162 if ( $plugin->{session}->get_repository->get_conf( "marc" )->{importSubjects} ) {
164 if ( $field = $marc->field('650') ) {
165 foreach my $i ( $field->subfield('a') ) {
166 push @{ $epdata->{subjects} }, $i;
173 if ( my $code = $plugin->{session}->get_repository->get_conf( "marc" )->{epdata_callback} ) {
174 $epdata = $code->($epdata);