| 1 | =head1 NAME |
| 2 | |
| 3 | Catmandu::Exporter::MARC::ALEPHSEQ - Exporter for MARC records to Ex Libris' Aleph sequential |
| 4 | |
| 5 | =head1 SYNOPSIS |
| 6 | |
| 7 | # From the command line |
| 8 | $ catmandu convert MARC to MARC --type ALEPHSEQ < /foo/data.mrc |
| 9 | |
| 10 | # From Perl |
| 11 | use Catmandu; |
| 12 | |
| 13 | my $importer = Catmandu->importer('MARC', file => "/foo/bar.mrc" , type => 'ISO'); |
| 14 | my $exporter = Catmandu->exporter('MARC', file => "marc.txt", type => 'ALEPHSEQ' ); |
| 15 | |
| 16 | $exporter->add($importer); |
| 17 | $exporter->commit; |
| 18 | |
| 19 | =head1 CONFIGURATION |
| 20 | |
| 21 | =over |
| 22 | |
| 23 | =item file |
| 24 | |
| 25 | Write output to a local file given by its path or file handle. Alternatively a |
| 26 | scalar reference can be passed to write to a string and a code reference can be |
| 27 | used to write to a callback function. |
| 28 | |
| 29 | =item fh |
| 30 | |
| 31 | Write the output to an L<IO::Handle>. If not specified, |
| 32 | L<Catmandu::Util::io|Catmandu::Util/IO-functions> is used to create the output |
| 33 | handle from the C<file> argument or by using STDOUT. |
| 34 | |
| 35 | =item fix |
| 36 | |
| 37 | An ARRAY of one or more fixes or file scripts to be applied to exported items. |
| 38 | |
| 39 | =item encoding |
| 40 | |
| 41 | Binmode of the output stream C<fh>. Set to "C<:utf8>" by default. |
| 42 | |
| 43 | =item default_fmt |
| 44 | |
| 45 | Set the value of the default C<FMT> field when none is given. Set to C<BK> by default. |
| 46 | |
| 47 | =back |
| 48 | |
| 49 | =head1 METHODS |
| 50 | |
| 51 | See L<Catmandu::Exporter>, L<Catmandu::Addable>, L<Catmandu::Fixable>, |
| 52 | L<Catmandu::Counter>, and L<Catmandu::Logger> for a full list of methods. |
| 53 | |
| 54 | =head1 SEE ALSO |
| 55 | |
| 56 | L<Catmandu::Exporter> |
| 57 | |
| 58 | =cut |
| 59 | package Catmandu::Exporter::MARC::ALEPHSEQscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/; |
| 60 | use Catmandu::Sanescip-perl cpan Catmandu-MARC 1.32 Catmandu/Sane/; |
| 61 | use Catmandu::Utilscip-perl cpan Catmandu-MARC 1.32 Catmandu/Util/ qw(xml_escape is_different :array :is); |
| 62 | use List::Utilscip-perl cpan Catmandu-MARC 1.32 List/Util/; |
| 63 | use Mooscip-perl cpan Catmandu-MARC 1.32 Moo/; |
| 64 | |
| 65 | our $VERSIONscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/$VERSION. = '1.32'; |
| 66 | |
| 67 | withscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/with(). 'Catmandu::Exporter', 'Catmandu::Exporter::MARC::Base'; |
| 68 | |
| 69 | hasscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/has(). record => (is => 'ro' , default => sub { 'record'}); |
| 70 | hasscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/has(). record_format => (is => 'ro' , default => sub { 'raw'} ); |
| 71 | hasscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/has(). skip_empty_subfields => (is => 'ro' , default => sub { 0 }); |
| 72 | hasscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/has(). default_fmt => (is => 'ro' , default => sub { 'BK'} ); |
| 73 | |
| 74 | sub addscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/add(). { |
| 75 | my ($self,$data) = @_scip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/`@_`.; |
| 76 | |
| 77 | if ($self->record_formatscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/record_format(). eq 'MARC-in-JSON') { |
| 78 | $data = $self->_json_to_rawscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/_json_to_raw().($data); |
| 79 | } |
| 80 | |
| 81 | my $id_str = $data->{_id}; |
| 82 | $id_str =~ s{\D}{0}g if definedscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/defined(). $id_str; |
| 83 | my $_id = sprintfscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/sprintf().("%-9.9d", $id_str // 0); |
| 84 | my $record = $data->{$self->recordscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/record().}; |
| 85 | |
| 86 | my @lines = (); |
| 87 | |
| 88 | # Check required FMT field |
| 89 | if (@$record > 0 && $record->[0]->[0] ne 'FMT') { |
| 90 | pushscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/push(). @lines , joinscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/join().('',$_id, ' ' , 'FMT', ' ', ' ' , ' L ' , $self->default_fmtscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/default_fmt().); |
| 91 | } |
| 92 | |
| 93 | for my $field (@$record) { |
| 94 | my ($tag,$ind1,$ind2,@data) = @$field; |
| 95 | |
| 96 | $ind1 = ' ' unless definedscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/defined(). $ind1; |
| 97 | $ind2 = ' ' unless definedscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/defined(). $ind2; |
| 98 | |
| 99 | @data = $self->_clean_raw_datascip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/_clean_raw_data().($tag,@data) if $self->skip_empty_subfieldsscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/skip_empty_subfields().; |
| 100 | |
| 101 | next if $#datascip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/`$#data`. == -1; |
| 102 | |
| 103 | # Joins are faster than perl string concatenation |
| 104 | if (@data < 2) { |
| 105 | $self->logscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/log().->warnscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/warn().("$tag doesn't have any data"); |
| 106 | } |
| 107 | if (indexscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/index().($tag,'LDR') == 0) { |
| 108 | my $ldr = $data[1]; |
| 109 | $ldr =~ s/ /^/og; |
| 110 | pushscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/push(). @lines , joinscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/join().('', $_id , ' ' , $tag , $ind1 , $ind2 , ' L ', $ldr ); |
| 111 | } |
| 112 | elsif (indexscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/index().($tag,'008') == 0) { |
| 113 | my $f008 = $data[1]; |
| 114 | $f008 =~ s/ /^/og; |
| 115 | pushscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/push(). @lines , joinscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/join().('', $_id , ' ' , $tag , $ind1 , $ind2 , ' L ', $f008 ); |
| 116 | } |
| 117 | elsif (indexscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/index().($tag,'FMT') == 0 || indexscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/index().($tag,'00') == 0) { |
| 118 | pushscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/push(). @lines , joinscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/join().('', $_id , ' ' , $tag , $ind1 , $ind2 , ' L ', $data[1] ); |
| 119 | } |
| 120 | else { |
| 121 | my @line = ('', $_id , ' ' , $tag , $ind1 , $ind2 , ' L '); |
| 122 | while (@data) { |
| 123 | my ($code,$val) = splicescip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/splice().(@data, 0, 2); |
| 124 | # some special characters are allowed as subfiled codes in local defined field |
| 125 | # see https://www.loc.gov/marc/96principl.html#eight 8.4.2.3. |
| 126 | next unless $code =~ /[a-z0-9!"#\$%&'\(\)\*\+'-\.\/:;<=>]/o; |
| 127 | next unless is_stringscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/is_string().($val); |
| 128 | $val =~ s{[[:cntrl:]]}{}g; |
| 129 | pushscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/push(). @line , '$$' , $code , $val; |
| 130 | } |
| 131 | pushscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/push(). @lines , joinscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/join().('', @line); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | $self->fhscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/fh().->printscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/print().(joinscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/join().("\n",@lines) , "\n"); |
| 136 | } |
| 137 | |
| 138 | sub commitscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/commit(). { |
| 139 | my $self = shiftscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/shift().; |
| 140 | $self->fhscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/fh().->flushscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/ALEPHSEQ/flush().; |
| 141 | 1; |
| 142 | } |
| 143 | |
| 144 | 1; |