use base qw/WebPAC::Common/;
use MARC::Record;
+use MARC::File::XML;
use MARC::Lint;
use Data::Dump qw/dump/;
-use Encode qw/from_to decode/;
=head1 NAME
my $marc = new WebPAC::Output::MARC(
path => '/path/to/output.marc',
- native_encoding => 'iso-8859-2',
marc_encoding => 'utf-8',
lint => 1,
dump => 0,
+ marcxml => 0,
)
=cut
$log->warn("Can't create MARC::Lint object, linting is disabled");
}
+ $self->{marc_encoding} ||= 'utf-8';
+
if (my $path = $self->{path}) {
- open($self->{fh}, '>', $path) ||
+ open($self->{fh}, '>', $path . '.marc') ||
$log->logdie("can't open MARC output $path: $!");
binmode($self->{fh}, ':utf8');
- $log->info("Creating MARC export file $path", $self->{lint} ? ' (with lint)' : '', "\n");
+ $log->info("Creating MARC export file $path.marc", $self->{lint} ? ' (with lint)' : '', " encoding ", $self->{marc_encoding}, "\n");
+ if ( $self->{marcxml} || $ENV{MARCXML} ) {
+ open($self->{fh_marcxml}, '>:utf8', "$path.marcxml") ||
+ $log->logdie("can't open MARCXML output $path.marcxml: $!");
+ $log->info("Creating MARCXML export file $path.marcxml");
+ print {$self->{fh_marcxml}} qq{<?xml version="1.0" encoding="UTF-8"?>\n<collection>\n};
+ }
} else {
$log->logconfess("new called without path");
}
- $self->{native_encoding} ||= 'iso-8859-2';
- $self->{marc_encoding} ||= 'utf-8';
-
$self ? return $self : return undef;
}
foreach my $j ( 0 .. $#$fields ) {
foreach my $i ( 0 .. ( ( $#{$fields->[$j]} - 3 ) / 2 ) ) {
my $f = $fields->[$j]->[ ($i * 2) + 4 ];
- $f = decode( $self->{native_encoding}, $f );
$fields->[$j]->[ ($i * 2) + 4 ] = $f;
}
}
my $leader = $marc->leader;
- foreach my $o ( keys %$new_l ) {
+ foreach my $o ( sort { $a <=> $b } keys %$new_l ) {
my $insert = $new_l->{$o};
$leader = substr($leader, 0, $o) .
$insert . substr($leader, $o+length($insert));
print {$self->{fh}} $marc->as_usmarc;
+ if ( $self->{fh_marcxml} ) {
+ my $xml = $marc->as_xml_record;
+ $xml =~ s/\Q<?xml version="1.0" encoding="UTF-8"?>\E//;
+ print {$self->{fh_marcxml}} $xml;
+ }
}
=head2 finish
my $log = $self->get_logger;
- close( $self->{fh} ) or $log->logdie("can't close ", $self->{path}, ": $!");
+ close( $self->{fh} ) or $log->logdie("can't close ", $self->{path}, ".marc: $!");
+ if ( $self->{fh_marcxml} ) {
+ print {$self->{fh_marcxml}} qq{</collection>\n};
+ $log->info("MARCXML file ", $self->{path}, ".marcxml ", -s $self->{fh_marcxml}, " bytes");
+ close( $self->{fh_marcxml} );
+ }
if (my $w = $self->{_marc_lint_warnings}) {
$log->error("MARC lint warnings summary:\n",
join ("\n",