From a96c3c6f3a60b9494a539633955a5f9385c5110c Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Thu, 7 Jul 2016 14:13:16 +0200 Subject: [PATCH] Bug 10407: Add marcxml import to stage-marc-import script Content-Type: text/plain; charset=utf-8 The previous patch did the groundwork, but did not yet add the call to RecordsFromMARCXMLFile. I will open a new report to add this to the interface, but in the meantime this patch just looks for a .xml extension in the filename. Also changed: [2] The errors of RecordsFromISO2709File are now returned to in array. --- C4/ImportBatch.pm | 24 ++++++++++++------------ misc/stage_file.pl | 4 ++-- tools/stage-marc-import.pl | 8 ++++++-- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index ec9d7c2..78604a0 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -1473,11 +1473,13 @@ Reads ISO2709 binary porridge from the given file and creates MARC::Record-objec @PARAM2, String, see stage_file.pl @PARAM3, String, should be utf8 +Returns two array refs. + =cut sub RecordsFromISO2709File { my ($input_file, $record_type, $encoding) = @_; - my $errors; + my @errors; my $marc_type = C4::Context->preference('marcflavour'); $marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth'); @@ -1493,40 +1495,38 @@ sub RecordsFromISO2709File { my ($marc_record, $charset_guessed, $char_errors) = MarcToUTF8Record($_, $marc_type, $encoding); push @marc_records, $marc_record; if ($charset_guessed ne $encoding) { - $errors = '' unless $errors; - $errors .= "Unexpected charset $charset_guessed, expecting $encoding\n"; + push @errors, + "Unexpected charset $charset_guessed, expecting $encoding\n"; } } close IN; - return ($errors, \@marc_records); + return ( \@errors, \@marc_records ); } =head RecordsFromMARCXMLFile my ($errors, $records) = C4::ImportBatch::RecordsFromMARCXMLFile($input_file, $encoding); - - Creates MARC::Record-objects out of the given MARCXML-file. @PARAM1, String, absolute path to the ISO2709 file. @PARAM2, String, should be utf8 +Returns two array refs. + =cut sub RecordsFromMARCXMLFile { my ( $filename, $encoding ) = @_; my $batch = MARC::File::XML->in( $filename ); - my @marcRecords; - my @errors; + my ( @marcRecords, @errors, $record ); do { - eval { - my $record = $batch->next($encoding); - push @marcRecords, $record if $record; - }; + eval { $record = $batch->next( $encoding ); }; if ($@) { push @errors, $@; + last; } + push @marcRecords, $record if $record; } while( $record ); return (\@errors, \@marcRecords); } diff --git a/misc/stage_file.pl b/misc/stage_file.pl index 95ac941..4374c1d 100755 --- a/misc/stage_file.pl +++ b/misc/stage_file.pl @@ -94,8 +94,8 @@ sub process_batch { my ($format, $input_file, $record_type, $match, $add_items, $batch_comment) = @_; my ($errors, $marc_records) = C4::ImportBatch::RecordsFromISO2709File($input_file, $record_type, $encoding) if $format eq 'ISO2709'; - warn $errors if $errors; - $marc_records = C4::ImportBatch::RecordsFromMARCXMLFile($input_file, $encoding) if $format eq 'MARCXML'; + warn "@$errors\n" if @$errors; + ( $errors, $marc_records ) = C4::ImportBatch::RecordsFromMARCXMLFile($input_file, $encoding) if $format eq 'MARCXML'; my $num_input_records = ($marc_records) ? scalar(@$marc_records) : 0; print "... staging MARC records -- please wait\n"; diff --git a/tools/stage-marc-import.pl b/tools/stage-marc-import.pl index e7e9954..0577016 100755 --- a/tools/stage-marc-import.pl +++ b/tools/stage-marc-import.pl @@ -87,8 +87,12 @@ if ($completedJobID) { } elsif ($fileID) { my $upload = Koha::Upload->new->get({ id => $fileID }); my $filename = $upload->{path}; - my $marcrecord=''; - my ($errors, $marcrecords) = C4::ImportBatch::RecordsFromISO2709File($uploaded_file->filename(), $record_type, $encoding); + my ( $errors, $marcrecords ); + if( $filename =~ /\.[^.]*xml$/i ) { # like .xml or .marcxml + ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $filename, $encoding); + } else { + ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File( $filename, $record_type, $encoding ); + } my $job = undef; my $dbh; -- 1.7.10.4