Lines 416-421
sub marc_records_to_documents {
Link Here
|
416 |
my $control_fields_rules = $rules->{control_fields}; |
416 |
my $control_fields_rules = $rules->{control_fields}; |
417 |
my $data_fields_rules = $rules->{data_fields}; |
417 |
my $data_fields_rules = $rules->{data_fields}; |
418 |
my $marcflavour = lc C4::Context->preference('marcflavour'); |
418 |
my $marcflavour = lc C4::Context->preference('marcflavour'); |
|
|
419 |
my $use_array = C4::Context->preference('ElasticsearchMARCFormat') eq 'ARRAY'; |
419 |
|
420 |
|
420 |
my @record_documents; |
421 |
my @record_documents; |
421 |
|
422 |
|
Lines 535-560
sub marc_records_to_documents {
Link Here
|
535 |
|
536 |
|
536 |
# TODO: Perhaps should check if $records_document non empty, but really should never be the case |
537 |
# TODO: Perhaps should check if $records_document non empty, but really should never be the case |
537 |
$record->encoding('UTF-8'); |
538 |
$record->encoding('UTF-8'); |
538 |
my @warnings; |
539 |
if ($use_array) { |
539 |
{ |
540 |
$record_document->{'marc_data_array'} = $self->_marc_to_array($record); |
540 |
# Temporarily intercept all warn signals (MARC::Record carps when record length > 99999) |
541 |
$record_document->{'marc_format'} = 'ARRAY'; |
541 |
local $SIG{__WARN__} = sub { |
542 |
} else { |
542 |
push @warnings, $_[0]; |
543 |
my @warnings; |
543 |
}; |
544 |
{ |
544 |
$record_document->{'marc_data'} = encode_base64(encode('UTF-8', $record->as_usmarc())); |
545 |
# Temporarily intercept all warn signals (MARC::Record carps when record length > 99999) |
545 |
} |
546 |
local $SIG{__WARN__} = sub { |
546 |
if (@warnings) { |
547 |
push @warnings, $_[0]; |
547 |
# Suppress warnings if record length exceeded |
548 |
}; |
548 |
unless (substr($record->leader(), 0, 5) eq '99999') { |
549 |
$record_document->{'marc_data'} = encode_base64(encode('UTF-8', $record->as_usmarc())); |
549 |
foreach my $warning (@warnings) { |
550 |
} |
550 |
carp $warning; |
551 |
if (@warnings) { |
|
|
552 |
# Suppress warnings if record length exceeded |
553 |
unless (substr($record->leader(), 0, 5) eq '99999') { |
554 |
foreach my $warning (@warnings) { |
555 |
carp $warning; |
556 |
} |
551 |
} |
557 |
} |
|
|
558 |
$record_document->{'marc_data'} = $record->as_xml_record($marcflavour); |
559 |
$record_document->{'marc_format'} = 'MARCXML'; |
560 |
} |
561 |
else { |
562 |
$record_document->{'marc_format'} = 'base64ISO2709'; |
552 |
} |
563 |
} |
553 |
$record_document->{'marc_data'} = $record->as_xml_record($marcflavour); |
|
|
554 |
$record_document->{'marc_format'} = 'MARCXML'; |
555 |
} |
556 |
else { |
557 |
$record_document->{'marc_format'} = 'base64ISO2709'; |
558 |
} |
564 |
} |
559 |
my $id = $record->subfield('999', 'c'); |
565 |
my $id = $record->subfield('999', 'c'); |
560 |
push @record_documents, [$id, $record_document]; |
566 |
push @record_documents, [$id, $record_document]; |
Lines 562-567
sub marc_records_to_documents {
Link Here
|
562 |
return \@record_documents; |
568 |
return \@record_documents; |
563 |
} |
569 |
} |
564 |
|
570 |
|
|
|
571 |
=head2 _marc_to_array($record) |
572 |
|
573 |
my @fields = _marc_to_array($record) |
574 |
|
575 |
Convert a MARC::Record to an array modeled after MARC-in-JSON |
576 |
(see https://github.com/marc4j/marc4j/wiki/MARC-in-JSON-Description) |
577 |
|
578 |
=over 4 |
579 |
|
580 |
=item C<$record> |
581 |
|
582 |
A MARC::Record object |
583 |
|
584 |
=back |
585 |
|
586 |
=cut |
587 |
|
588 |
sub _marc_to_array { |
589 |
my ($self, $record) = @_; |
590 |
|
591 |
my $data = { |
592 |
leader => $record->leader(), |
593 |
fields => [] |
594 |
}; |
595 |
for my $field ($record->fields()) { |
596 |
my $tag = $field->tag(); |
597 |
if ($field->is_control_field()) { |
598 |
push @{$data->{fields}}, {$tag => $field->data()}; |
599 |
} else { |
600 |
my $subfields = (); |
601 |
foreach my $subfield ($field->subfields()) { |
602 |
my ($code, $contents) = @{$subfield}; |
603 |
push @{$subfields}, {$code => $contents}; |
604 |
} |
605 |
push @{$data->{fields}}, { |
606 |
$tag => { |
607 |
ind1 => $field->indicator(1), |
608 |
ind2 => $field->indicator(2), |
609 |
subfields => $subfields |
610 |
} |
611 |
}; |
612 |
} |
613 |
} |
614 |
return $data; |
615 |
} |
616 |
|
617 |
=head2 _array_to_marc($data) |
618 |
|
619 |
my $record = _array_to_marc($data) |
620 |
|
621 |
Convert an array modeled after MARC-in-JSON to a MARC::Record |
622 |
|
623 |
=over 4 |
624 |
|
625 |
=item C<$data> |
626 |
|
627 |
An array modeled after MARC-in-JSON |
628 |
(see https://github.com/marc4j/marc4j/wiki/MARC-in-JSON-Description) |
629 |
|
630 |
=back |
631 |
|
632 |
=cut |
633 |
|
634 |
sub _array_to_marc { |
635 |
my ($self, $data) = @_; |
636 |
|
637 |
my $record = MARC::Record->new(); |
638 |
|
639 |
$record->leader($data->{leader}); |
640 |
for my $field (@{$data->{fields}}) { |
641 |
my $tag = (keys %{$field})[0]; |
642 |
$field = $field->{$tag}; |
643 |
my $marc_field; |
644 |
if (ref($field) eq 'HASH') { |
645 |
my @subfields; |
646 |
foreach my $subfield (@{$field->{subfields}}) { |
647 |
my $code = (keys %{$subfield})[0]; |
648 |
push @subfields, $code; |
649 |
push @subfields, $subfield->{$code}; |
650 |
} |
651 |
$marc_field = MARC::Field->new($tag, $field->{ind1}, $field->{ind2}, @subfields); |
652 |
} else { |
653 |
$marc_field = MARC::Field->new($tag, $field) |
654 |
} |
655 |
$record->append_fields($marc_field); |
656 |
} |
657 |
; |
658 |
return $record; |
659 |
} |
660 |
|
565 |
=head2 _field_mappings($facet, $suggestible, $sort, $target_name, $target_type, $range) |
661 |
=head2 _field_mappings($facet, $suggestible, $sort, $target_name, $target_type, $range) |
566 |
|
662 |
|
567 |
my @mappings = _field_mappings($facet, $suggestible, $sort, $target_name, $target_type, $range) |
663 |
my @mappings = _field_mappings($facet, $suggestible, $sort, $target_name, $target_type, $range) |