+=head2 RecordsFromISO2709File
+
+ my ($errors, $records) = C4::ImportBatch::RecordsFromISO2709File($input_file, $record_type, $encoding);
+
+Reads ISO2709 binary porridge from the given file and creates MARC::Record-objects out of it.
+
+@PARAM1, String, absolute path to the ISO2709 file.
+@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 $marc_type = C4::Context->preference('marcflavour');
+ $marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth');
+
+ open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
+ my @marc_records;
+ $/ = "\035";
+ while (<IN>) {
+ s/^\s+//;
+ s/\s+$//;
+ next unless $_; # skip if record has only whitespace, as might occur
+ # if file includes newlines between each MARC record
+ my ($marc_record, $charset_guessed, $char_errors) = MarcToUTF8Record($_, $marc_type, $encoding);
+ push @marc_records, $marc_record;
+ if ($charset_guessed ne $encoding) {
+ push @errors,
+ "Unexpected charset $charset_guessed, expecting $encoding";
+ }
+ }
+ close IN;
+ 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, @errors, $record );
+ do {
+ eval { $record = $batch->next( $encoding ); };
+ if ($@) {
+ push @errors, $@;
+ }
+ push @marcRecords, $record if $record;
+ } while( $record );
+ return (\@errors, \@marcRecords);
+}
+
+=head2 RecordsFromMarcPlugin
+
+ Converts text of input_file into array of MARC records with to_marc plugin
+
+=cut
+
+sub RecordsFromMarcPlugin {
+ my ($input_file, $plugin_class, $encoding) = @_;
+ my ( $text, @return );
+ return \@return if !$input_file || !$plugin_class;
+
+ # Read input file
+ open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
+ $/ = "\035";
+ while (<IN>) {
+ s/^\s+//;
+ s/\s+$//;
+ next unless $_;
+ $text .= $_;
+ }
+ close IN;
+
+ # Convert to large MARC blob with plugin
+ $text = Koha::Plugins::Handler->run({
+ class => $plugin_class,
+ method => 'to_marc',
+ params => { data => $text },
+ }) if $text;
+
+ # Convert to array of MARC records
+ if( $text ) {
+ my $marc_type = C4::Context->preference('marcflavour');
+ foreach my $blob ( split(/\x1D/, $text) ) {
+ next if $blob =~ /^\s*$/;
+ my ($marcrecord) = MarcToUTF8Record($blob, $marc_type, $encoding);
+ push @return, $marcrecord;
+ }
+ }
+ return \@return;
+}