Lines 499-505
sub BiblioAutoLink {
Link Here
|
499 |
|
499 |
|
500 |
=head2 LinkBibHeadingsToAuthorities |
500 |
=head2 LinkBibHeadingsToAuthorities |
501 |
|
501 |
|
502 |
my $num_headings_changed, %results = LinkBibHeadingsToAuthorities($linker, $marc, $frameworkcode, [$allowrelink]); |
502 |
my $num_headings_changed, %results = LinkBibHeadingsToAuthorities($linker, $marc, $frameworkcode, [$allowrelink, $verbose]); |
503 |
|
503 |
|
504 |
Links bib headings to authority records by checking |
504 |
Links bib headings to authority records by checking |
505 |
each authority-controlled field in the C<MARC::Record> |
505 |
each authority-controlled field in the C<MARC::Record> |
Lines 521-526
sub LinkBibHeadingsToAuthorities {
Link Here
|
521 |
my $bib = shift; |
521 |
my $bib = shift; |
522 |
my $frameworkcode = shift; |
522 |
my $frameworkcode = shift; |
523 |
my $allowrelink = shift; |
523 |
my $allowrelink = shift; |
|
|
524 |
my $verbose = shift; |
524 |
my %results; |
525 |
my %results; |
525 |
require C4::Heading; |
526 |
require C4::Heading; |
526 |
require C4::AuthoritiesMarc; |
527 |
require C4::AuthoritiesMarc; |
Lines 537-635
sub LinkBibHeadingsToAuthorities {
Link Here
|
537 |
if ( defined $current_link && (!$allowrelink || !C4::Context->preference('LinkerRelink')) ) |
538 |
if ( defined $current_link && (!$allowrelink || !C4::Context->preference('LinkerRelink')) ) |
538 |
{ |
539 |
{ |
539 |
$results{'linked'}->{ $heading->display_form() }++; |
540 |
$results{'linked'}->{ $heading->display_form() }++; |
|
|
541 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $current_link, status => 'UNCHANGED'}) if $verbose; |
540 |
next; |
542 |
next; |
541 |
} |
543 |
} |
542 |
|
544 |
|
543 |
my ( $authid, $fuzzy ) = $linker->get_link($heading); |
545 |
my ( $authid, $fuzzy, $status ) = $linker->get_link($heading); |
544 |
if ($authid) { |
546 |
if ($authid) { |
545 |
$results{ $fuzzy ? 'fuzzy' : 'linked' } |
547 |
$results{ $fuzzy ? 'fuzzy' : 'linked' } |
546 |
->{ $heading->display_form() }++; |
548 |
->{ $heading->display_form() }++; |
547 |
next if defined $current_link and $current_link == $authid; |
549 |
if(defined $current_link and $current_link == $authid) { |
|
|
550 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $current_link, status => 'UNCHANGED'}) if $verbose; |
551 |
next; |
552 |
} |
548 |
|
553 |
|
549 |
$field->delete_subfield( code => '9' ) if defined $current_link; |
554 |
$field->delete_subfield( code => '9' ) if defined $current_link; |
550 |
$field->add_subfields( '9', $authid ); |
555 |
$field->add_subfields( '9', $authid ); |
551 |
$num_headings_changed++; |
556 |
$num_headings_changed++; |
|
|
557 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => $status || 'LOCAL_FOUND'}) if $verbose; |
552 |
} |
558 |
} |
553 |
else { |
559 |
else { |
554 |
if ( defined $current_link |
560 |
if ( defined $current_link |
555 |
&& (!$allowrelink || C4::Context->preference('LinkerKeepStale')) ) |
561 |
&& (!$allowrelink || C4::Context->preference('LinkerKeepStale')) ) |
556 |
{ |
562 |
{ |
557 |
$results{'fuzzy'}->{ $heading->display_form() }++; |
563 |
$results{'fuzzy'}->{ $heading->display_form() }++; |
|
|
564 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $current_link, status => 'UNCHANGED'}) if $verbose; |
558 |
} |
565 |
} |
559 |
elsif ( C4::Context->preference('AutoCreateAuthorities') ) { |
566 |
elsif ( C4::Context->preference('AutoCreateAuthorities') ) { |
560 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
567 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
561 |
$results{'linked'}->{ $heading->display_form() }++; |
568 |
$results{'linked'}->{ $heading->display_form() }++; |
562 |
} |
569 |
} |
563 |
else { |
570 |
else { |
564 |
my $authtypedata = |
571 |
$authid = AddAuthorityFromHeading($heading, $field, $bib); |
565 |
C4::AuthoritiesMarc::GetAuthType( $heading->auth_type() ); |
|
|
566 |
my $marcrecordauth = MARC::Record->new(); |
567 |
if ( C4::Context->preference('marcflavour') eq 'MARC21' ) { |
568 |
$marcrecordauth->leader(' nz a22 o 4500'); |
569 |
SetMarcUnicodeFlag( $marcrecordauth, 'MARC21' ); |
570 |
} |
571 |
$field->delete_subfield( code => '9' ) |
572 |
if defined $current_link; |
573 |
my $authfield = |
574 |
MARC::Field->new( $authtypedata->{auth_tag_to_report}, |
575 |
'', '', "a" => "" . $field->subfield('a') ); |
576 |
map { |
577 |
$authfield->add_subfields( $_->[0] => $_->[1] ) |
578 |
if ( $_->[0] =~ /[A-z]/ && $_->[0] ne "a" ) |
579 |
} $field->subfields(); |
580 |
$marcrecordauth->insert_fields_ordered($authfield); |
581 |
|
582 |
# bug 2317: ensure new authority knows it's using UTF-8; currently |
583 |
# only need to do this for MARC21, as MARC::Record->as_xml_record() handles |
584 |
# automatically for UNIMARC (by not transcoding) |
585 |
# FIXME: AddAuthority() instead should simply explicitly require that the MARC::Record |
586 |
# use UTF-8, but as of 2008-08-05, did not want to introduce that kind |
587 |
# of change to a core API just before the 3.0 release. |
588 |
|
589 |
if ( C4::Context->preference('marcflavour') eq 'MARC21' ) { |
590 |
$marcrecordauth->insert_fields_ordered( |
591 |
MARC::Field->new( |
592 |
'667', '', '', |
593 |
'a' => "Machine generated authority record." |
594 |
) |
595 |
); |
596 |
my $cite = |
597 |
$bib->author() . ", " |
598 |
. $bib->title_proper() . ", " |
599 |
. $bib->publication_date() . " "; |
600 |
$cite =~ s/^[\s\,]*//; |
601 |
$cite =~ s/[\s\,]*$//; |
602 |
$cite = |
603 |
"Work cat.: (" |
604 |
. C4::Context->preference('MARCOrgCode') . ")" |
605 |
. $bib->subfield( '999', 'c' ) . ": " |
606 |
. $cite; |
607 |
$marcrecordauth->insert_fields_ordered( |
608 |
MARC::Field->new( '670', '', '', 'a' => $cite ) ); |
609 |
} |
610 |
|
611 |
# warn "AUTH RECORD ADDED : ".$marcrecordauth->as_formatted; |
612 |
|
572 |
|
613 |
$authid = |
|
|
614 |
C4::AuthoritiesMarc::AddAuthority( $marcrecordauth, '', |
615 |
$heading->auth_type() ); |
616 |
$field->add_subfields( '9', $authid ); |
573 |
$field->add_subfields( '9', $authid ); |
617 |
$num_headings_changed++; |
574 |
$num_headings_changed++; |
618 |
$results{'added'}->{ $heading->display_form() }++; |
575 |
$results{'added'}->{ $heading->display_form() }++; |
|
|
576 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => 'CREATED'}) if $verbose; |
619 |
} |
577 |
} |
620 |
} |
578 |
} |
621 |
elsif ( defined $current_link ) { |
579 |
elsif ( defined $current_link ) { |
622 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
580 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
623 |
$results{'linked'}->{ $heading->display_form() }++; |
581 |
$results{'linked'}->{ $heading->display_form() }++; |
|
|
582 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => 'UNCHANGED'}) if $verbose; |
624 |
} |
583 |
} |
625 |
else { |
584 |
else { |
626 |
$field->delete_subfield( code => '9' ); |
585 |
$field->delete_subfield( code => '9' ); |
627 |
$num_headings_changed++; |
586 |
$num_headings_changed++; |
628 |
$results{'unlinked'}->{ $heading->display_form() }++; |
587 |
$results{'unlinked'}->{ $heading->display_form() }++; |
|
|
588 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => undef, status => 'NONE_FOUND'}) if $verbose; |
629 |
} |
589 |
} |
630 |
} |
590 |
} |
631 |
else { |
591 |
else { |
632 |
$results{'unlinked'}->{ $heading->display_form() }++; |
592 |
$results{'unlinked'}->{ $heading->display_form() }++; |
|
|
593 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => undef, status => 'NONE_FOUND'}) if $verbose; |
633 |
} |
594 |
} |
634 |
} |
595 |
} |
635 |
|
596 |
|
Lines 661-666
sub _check_valid_auth_link {
Link Here
|
661 |
return ($field->as_string('abcdefghijklmnopqrstuvwxyz') eq $authorized_heading); |
622 |
return ($field->as_string('abcdefghijklmnopqrstuvwxyz') eq $authorized_heading); |
662 |
} |
623 |
} |
663 |
|
624 |
|
|
|
625 |
=head2 AddAuthorityFromHeading |
626 |
|
627 |
my $authid = AddAuthorityFromHeading($heading, $field, [$bib]); |
628 |
|
629 |
Creates a new authority from an heading and its associated field. |
630 |
|
631 |
Returns the id of the newly created authority. |
632 |
|
633 |
=cut |
634 |
|
635 |
sub AddAuthorityFromHeading { |
636 |
my $heading = shift; |
637 |
my $field = shift; |
638 |
my $bib = shift; |
639 |
|
640 |
my $authtypedata = |
641 |
C4::AuthoritiesMarc::GetAuthType( $heading->auth_type() ); |
642 |
my $marcrecordauth = MARC::Record->new(); |
643 |
if ( C4::Context->preference('marcflavour') eq 'MARC21' ) { |
644 |
$marcrecordauth->leader(' nz a22 o 4500'); |
645 |
SetMarcUnicodeFlag( $marcrecordauth, 'MARC21' ); |
646 |
} |
647 |
my $authfield = |
648 |
MARC::Field->new( $authtypedata->{auth_tag_to_report}, |
649 |
'', '', "a" => "" . $field->subfield('a') ); |
650 |
map { |
651 |
$authfield->add_subfields( $_->[0] => $_->[1] ) |
652 |
if ( $_->[0] =~ /[A-z]/ && $_->[0] ne "a" ) |
653 |
} $field->subfields(); |
654 |
$marcrecordauth->insert_fields_ordered($authfield); |
655 |
|
656 |
# bug 2317: ensure new authority knows it's using UTF-8; currently |
657 |
# only need to do this for MARC21, as MARC::Record->as_xml_record() handles |
658 |
# automatically for UNIMARC (by not transcoding) |
659 |
# FIXME: AddAuthority() instead should simply explicitly require that the MARC::Record |
660 |
# use UTF-8, but as of 2008-08-05, did not want to introduce that kind |
661 |
# of change to a core API just before the 3.0 release. |
662 |
|
663 |
if ( C4::Context->preference('marcflavour') eq 'MARC21' ) { |
664 |
$marcrecordauth->insert_fields_ordered( |
665 |
MARC::Field->new( |
666 |
'667', '', '', |
667 |
'a' => "Machine generated authority record." |
668 |
) |
669 |
); |
670 |
if(defined $bib) { |
671 |
my $cite = |
672 |
$bib->author() . ", " |
673 |
. $bib->title_proper() . ", " |
674 |
. $bib->publication_date() . " "; |
675 |
$cite =~ s/^[\s\,]*//; |
676 |
$cite =~ s/[\s\,]*$//; |
677 |
$cite = |
678 |
"Work cat.: (" |
679 |
. C4::Context->preference('MARCOrgCode') . ")" |
680 |
. $bib->subfield( '999', 'c' ) . ": " |
681 |
. $cite; |
682 |
$marcrecordauth->insert_fields_ordered( |
683 |
MARC::Field->new( '670', '', '', 'a' => $cite ) ); |
684 |
} |
685 |
|
686 |
# Add correct informations in field 008 and 040 so the authority can be linked correctly in future linkings |
687 |
my $date=POSIX::strftime("%y%m%d",localtime); |
688 |
my @heading_use = qw(b b b); |
689 |
my $thesaurus = '|'; |
690 |
|
691 |
if ($heading->{'subject_added_entry'}) { |
692 |
$heading_use[1] = 'a'; |
693 |
# Thesaurus treatment |
694 |
my %thesaurus_conv = ('lcsh'=>'a','lcac'=>'b','mesh'=>'c','nal'=>'d','notspecified'=>'n','cash'=>'k','rvm'=>'v'); |
695 |
$thesaurus = 'z'; |
696 |
$thesaurus = $thesaurus_conv{$heading->{'thesaurus'}} if exists $thesaurus_conv{$heading->{'thesaurus'}}; |
697 |
if($thesaurus eq 'z') |
698 |
{ |
699 |
$marcrecordauth->insert_fields_ordered( |
700 |
MARC::Field->new('040','','','f'=>$heading->{'thesaurus'}) |
701 |
); |
702 |
} |
703 |
} |
704 |
if ($heading->{'series_added_entry'}) { |
705 |
$heading_use[2] = 'a'; |
706 |
} |
707 |
if (not $heading->{'subject_added_entry'} and not $heading->{'series_added_entry'}) { |
708 |
$heading_use[0] = 'a'; |
709 |
} |
710 |
|
711 |
# Get a valid default value for field 008 |
712 |
my $default_008 = C4::Context->preference('MARCAuthorityControlField008'); |
713 |
if(!$default_008 or length($default_008)<34) { |
714 |
$default_008 = '|| aca||aabn | a|a d'; |
715 |
} |
716 |
else { |
717 |
$default_008 = substr($default_008,0,34) |
718 |
} |
719 |
|
720 |
# Insert the date, thesaurus and heading_use into field 008 |
721 |
my $f008 = $date . $default_008; |
722 |
substr($f008, 11, 1) = $thesaurus; |
723 |
substr($f008, 14, 3) = join('', @heading_use); |
724 |
|
725 |
$marcrecordauth->insert_fields_ordered( |
726 |
MARC::Field->new('008',$f008) |
727 |
); |
728 |
} |
729 |
|
730 |
# warn "AUTH RECORD ADDED : ".$marcrecordauth->as_formatted; |
731 |
|
732 |
my $authid = C4::AuthoritiesMarc::AddAuthority( $marcrecordauth, '', $heading->auth_type() ); |
733 |
return $authid; |
734 |
} |
735 |
|
736 |
|
664 |
=head2 GetRecordValue |
737 |
=head2 GetRecordValue |
665 |
|
738 |
|
666 |
my $values = GetRecordValue($field, $record, $frameworkcode); |
739 |
my $values = GetRecordValue($field, $record, $frameworkcode); |