@@ -, +, @@
RecordsFromMARCXMLFile in stage-marc-import. Use format param.
If you use .xml or .marcxml as extension, MARCXML is selected.
---
C4/ImportBatch.pm | 25 ++++++++++----------
.../prog/en/modules/tools/stage-marc-import.tt | 13 +++++++++-
misc/stage_file.pl | 15 +++++-------
tools/stage-marc-import.pl | 15 ++++++++----
4 files changed, 41 insertions(+), 27 deletions(-)
--- a/C4/ImportBatch.pm
+++ a/C4/ImportBatch.pm
@@ -404,7 +404,7 @@ sub BatchStageMarcRecords {
method => 'to_marc',
params => { data => $marc_records }
}
- ) if $to_marc_plugin;
+ ) if $to_marc_plugin && @$marc_records;
my $marc_type = C4::Context->preference('marcflavour');
$marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth');
@@ -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,37 @@ 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";
}
}
close IN;
- return ($errors, \@marc_records);
+ return ( \@errors, \@marc_records );
}
=head2 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, $@;
}
+ push @marcRecords, $record if $record;
} while( $record );
return (\@errors, \@marcRecords);
}
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt
+++ a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt
@@ -68,6 +68,10 @@ function cbUpload( status, fileid ) {
$('#fileuploadbutton').prop('disabled',true);
$('#fileuploadbutton').show();
$("#fileuploadcancel").hide();
+ var filename=$('#fileToUpload').prop('files')[0].name;
+ if( filename.match( new RegExp(/\.[^.]+xml$/) ) ) {
+ $('#format').val('MARCXML');
+ }
$("#processfile").show();
} else {
$('#fileuploadbutton').show();
@@ -183,7 +187,7 @@ function cbUpload( status, fileid ) {
+
+
+
+
[% IF plugins %]
--- a/misc/stage_file.pl
+++ a/misc/stage_file.pl
@@ -37,7 +37,7 @@ $| = 1;
# command-line parameters
my $record_type = "biblio";
-my $encoding = "";
+my $encoding = "UTF-8";
my $authorities = 0;
my $match = 0;
my $add_items = 0;
@@ -65,10 +65,6 @@ my $result = GetOptions(
$record_type = 'auth' if ($authorities);
-if ($encoding eq "") {
- $encoding = "utf8";
-}
-
if (not $result or $input_file eq "" or $want_help) {
print_usage();
exit 0;
@@ -93,9 +89,10 @@ exit 0;
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';
+ my ( $errors, $marc_records );
+ ( $errors, $marc_records ) = C4::ImportBatch::RecordsFromISO2709File($input_file, $record_type, $encoding) if $format eq 'ISO2709';
+ ( $errors, $marc_records ) = C4::ImportBatch::RecordsFromMARCXMLFile($input_file, $encoding) if $format eq 'MARCXML';
+ warn ( join ',', @$errors ) if @$errors;
my $num_input_records = ($marc_records) ? scalar(@$marc_records) : 0;
print "... staging MARC records -- please wait\n";
@@ -176,7 +173,7 @@ records into the main Koha database.
Parameters:
--file name of input MARC bib file
--authorities stage authority records instead of bibs
- --encoding encoding of MARC records, default is utf8.
+ --encoding encoding of MARC records, default is UTF-8.
Other possible options are: MARC-8,
ISO_5426, ISO_6937, ISO_8859-1, EUC-KR
--format The MARC transport format to use?
--- a/tools/stage-marc-import.pl
+++ a/tools/stage-marc-import.pl
@@ -56,7 +56,7 @@ my $parse_items = $input->param('parse_items');
my $item_action = $input->param('item_action');
my $comments = $input->param('comments');
my $record_type = $input->param('record_type');
-my $encoding = $input->param('encoding') || 'utf8';
+my $encoding = $input->param('encoding') || 'UTF-8';
my $format = $input->param('format') || 'ISO2709';
my $to_marc_plugin = $input->param('to_marc_plugin');
my $marc_modification_template = $input->param('marc_modification_template_id');
@@ -86,9 +86,16 @@ if ($completedJobID) {
$template->param(map { $_ => $results->{$_} } keys %{ $results });
} 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 ( $file, $filename ) = ( $upload->{path}, $upload->{name} );
+ my ( $errors, $marcrecords );
+ if( $format eq 'MARCXML' ) {
+ ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, $encoding);
+ } else {
+ ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File( $file, $record_type, $encoding );
+ }
+ warn "$filename: " . ( join ',', @$errors ) if @$errors;
+ # no need to exit if we have no records (or only errors) here
+ # BatchStageMarcRecords can handle that
my $job = undef;
my $dbh;
--