@@ -, +, @@ --- C4/ImportBatch.pm | 24 ++++++++++++------------ misc/stage_file.pl | 4 ++-- tools/stage-marc-import.pl | 8 ++++++-- 3 files changed, 20 insertions(+), 16 deletions(-) --- a/C4/ImportBatch.pm +++ a/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); } --- a/misc/stage_file.pl +++ a/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"; --- a/tools/stage-marc-import.pl +++ a/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; --