|
Lines 511-517
sub BiblioAutoLink {
Link Here
|
| 511 |
|
511 |
|
| 512 |
=head2 LinkBibHeadingsToAuthorities |
512 |
=head2 LinkBibHeadingsToAuthorities |
| 513 |
|
513 |
|
| 514 |
my $num_headings_changed, %results = LinkBibHeadingsToAuthorities($linker, $marc, $frameworkcode, [$allowrelink]); |
514 |
my $num_headings_changed, %results = LinkBibHeadingsToAuthorities($linker, $marc, $frameworkcode, [$allowrelink, $verbose]); |
| 515 |
|
515 |
|
| 516 |
Links bib headings to authority records by checking |
516 |
Links bib headings to authority records by checking |
| 517 |
each authority-controlled field in the C<MARC::Record> |
517 |
each authority-controlled field in the C<MARC::Record> |
|
Lines 533-538
sub LinkBibHeadingsToAuthorities {
Link Here
|
| 533 |
my $bib = shift; |
533 |
my $bib = shift; |
| 534 |
my $frameworkcode = shift; |
534 |
my $frameworkcode = shift; |
| 535 |
my $allowrelink = shift; |
535 |
my $allowrelink = shift; |
|
|
536 |
my $verbose = shift; |
| 536 |
my %results; |
537 |
my %results; |
| 537 |
if (!$bib) { |
538 |
if (!$bib) { |
| 538 |
carp 'LinkBibHeadingsToAuthorities called on undefined bib record'; |
539 |
carp 'LinkBibHeadingsToAuthorities called on undefined bib record'; |
|
Lines 553-576
sub LinkBibHeadingsToAuthorities {
Link Here
|
| 553 |
if ( defined $current_link && (!$allowrelink || !C4::Context->preference('LinkerRelink')) ) |
554 |
if ( defined $current_link && (!$allowrelink || !C4::Context->preference('LinkerRelink')) ) |
| 554 |
{ |
555 |
{ |
| 555 |
$results{'linked'}->{ $heading->display_form() }++; |
556 |
$results{'linked'}->{ $heading->display_form() }++; |
|
|
557 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $current_link, status => 'UNCHANGED'}) if $verbose; |
| 556 |
next; |
558 |
next; |
| 557 |
} |
559 |
} |
| 558 |
|
560 |
|
| 559 |
my ( $authid, $fuzzy ) = $linker->get_link($heading); |
561 |
my ( $authid, $fuzzy, $status ) = $linker->get_link($heading); |
| 560 |
if ($authid) { |
562 |
if ($authid) { |
| 561 |
$results{ $fuzzy ? 'fuzzy' : 'linked' } |
563 |
$results{ $fuzzy ? 'fuzzy' : 'linked' } |
| 562 |
->{ $heading->display_form() }++; |
564 |
->{ $heading->display_form() }++; |
| 563 |
next if defined $current_link and $current_link == $authid; |
565 |
if(defined $current_link and $current_link == $authid) { |
|
|
566 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $current_link, status => 'UNCHANGED'}) if $verbose; |
| 567 |
next; |
| 568 |
} |
| 564 |
|
569 |
|
| 565 |
$field->delete_subfield( code => '9' ) if defined $current_link; |
570 |
$field->delete_subfield( code => '9' ) if defined $current_link; |
| 566 |
$field->add_subfields( '9', $authid ); |
571 |
$field->add_subfields( '9', $authid ); |
| 567 |
$num_headings_changed++; |
572 |
$num_headings_changed++; |
|
|
573 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => $status || 'LOCAL_FOUND'}) if $verbose; |
| 568 |
} |
574 |
} |
| 569 |
else { |
575 |
else { |
| 570 |
if ( defined $current_link |
576 |
if ( defined $current_link |
| 571 |
&& (!$allowrelink || C4::Context->preference('LinkerKeepStale')) ) |
577 |
&& (!$allowrelink || C4::Context->preference('LinkerKeepStale')) ) |
| 572 |
{ |
578 |
{ |
| 573 |
$results{'fuzzy'}->{ $heading->display_form() }++; |
579 |
$results{'fuzzy'}->{ $heading->display_form() }++; |
|
|
580 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $current_link, status => 'UNCHANGED'}) if $verbose; |
| 574 |
} |
581 |
} |
| 575 |
elsif ( C4::Context->preference('AutoCreateAuthorities') ) { |
582 |
elsif ( C4::Context->preference('AutoCreateAuthorities') ) { |
| 576 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
583 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
|
Lines 624-651
sub LinkBibHeadingsToAuthorities {
Link Here
|
| 624 |
} |
631 |
} |
| 625 |
|
632 |
|
| 626 |
# warn "AUTH RECORD ADDED : ".$marcrecordauth->as_formatted; |
633 |
# warn "AUTH RECORD ADDED : ".$marcrecordauth->as_formatted; |
|
|
634 |
$authid = AddAuthorityFromHeading($heading, $field, $bib); |
| 627 |
|
635 |
|
| 628 |
$authid = |
|
|
| 629 |
C4::AuthoritiesMarc::AddAuthority( $marcrecordauth, '', |
| 630 |
$heading->auth_type() ); |
| 631 |
$field->add_subfields( '9', $authid ); |
636 |
$field->add_subfields( '9', $authid ); |
| 632 |
$num_headings_changed++; |
637 |
$num_headings_changed++; |
| 633 |
$linker->update_cache($heading, $authid); |
638 |
$linker->update_cache($heading, $authid); |
| 634 |
$results{'added'}->{ $heading->display_form() }++; |
639 |
$results{'added'}->{ $heading->display_form() }++; |
|
|
640 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => 'CREATED'}) if $verbose; |
| 635 |
} |
641 |
} |
| 636 |
} |
642 |
} |
| 637 |
elsif ( defined $current_link ) { |
643 |
elsif ( defined $current_link ) { |
| 638 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
644 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
| 639 |
$results{'linked'}->{ $heading->display_form() }++; |
645 |
$results{'linked'}->{ $heading->display_form() }++; |
|
|
646 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => 'UNCHANGED'}) if $verbose; |
| 640 |
} |
647 |
} |
| 641 |
else { |
648 |
else { |
| 642 |
$field->delete_subfield( code => '9' ); |
649 |
$field->delete_subfield( code => '9' ); |
| 643 |
$num_headings_changed++; |
650 |
$num_headings_changed++; |
| 644 |
$results{'unlinked'}->{ $heading->display_form() }++; |
651 |
$results{'unlinked'}->{ $heading->display_form() }++; |
|
|
652 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => undef, status => 'NONE_FOUND'}) if $verbose; |
| 645 |
} |
653 |
} |
| 646 |
} |
654 |
} |
| 647 |
else { |
655 |
else { |
| 648 |
$results{'unlinked'}->{ $heading->display_form() }++; |
656 |
$results{'unlinked'}->{ $heading->display_form() }++; |
|
|
657 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => undef, status => 'NONE_FOUND'}) if $verbose; |
| 649 |
} |
658 |
} |
| 650 |
} |
659 |
} |
| 651 |
|
660 |
|
|
Lines 677-682
sub _check_valid_auth_link {
Link Here
|
| 677 |
return ($field->as_string('abcdefghijklmnopqrstuvwxyz') eq $authorized_heading); |
686 |
return ($field->as_string('abcdefghijklmnopqrstuvwxyz') eq $authorized_heading); |
| 678 |
} |
687 |
} |
| 679 |
|
688 |
|
|
|
689 |
=head2 AddAuthorityFromHeading |
| 690 |
|
| 691 |
my $authid = AddAuthorityFromHeading($heading, $field, [$bib]); |
| 692 |
|
| 693 |
Creates a new authority from an heading and its associated field. |
| 694 |
|
| 695 |
Returns the id of the newly created authority. |
| 696 |
|
| 697 |
=cut |
| 698 |
|
| 699 |
sub AddAuthorityFromHeading { |
| 700 |
my $heading = shift; |
| 701 |
my $field = shift; |
| 702 |
my $bib = shift; |
| 703 |
|
| 704 |
if(!defined $heading || !defined $field || !defined $bib){ |
| 705 |
return 0; |
| 706 |
} |
| 707 |
my $authtypedata = |
| 708 |
C4::AuthoritiesMarc::GetAuthType( $heading->auth_type() ); |
| 709 |
my $marcrecordauth = MARC::Record->new(); |
| 710 |
if ( C4::Context->preference('marcflavour') eq 'MARC21' ) { |
| 711 |
$marcrecordauth->leader(' nz a22 o 4500'); |
| 712 |
SetMarcUnicodeFlag( $marcrecordauth, 'MARC21' ); |
| 713 |
} |
| 714 |
my $authfield = |
| 715 |
MARC::Field->new( $authtypedata->{auth_tag_to_report}, |
| 716 |
'', '', "a" => "" . $field->subfield('a') ); |
| 717 |
map { |
| 718 |
$authfield->add_subfields( $_->[0] => $_->[1] ) |
| 719 |
if ( $_->[0] =~ /[A-z]/ && $_->[0] ne "a" ) |
| 720 |
} $field->subfields(); |
| 721 |
$marcrecordauth->insert_fields_ordered($authfield); |
| 722 |
|
| 723 |
# bug 2317: ensure new authority knows it's using UTF-8; currently |
| 724 |
# only need to do this for MARC21, as MARC::Record->as_xml_record() handles |
| 725 |
# automatically for UNIMARC (by not transcoding) |
| 726 |
# FIXME: AddAuthority() instead should simply explicitly require that the MARC::Record |
| 727 |
# use UTF-8, but as of 2008-08-05, did not want to introduce that kind |
| 728 |
# of change to a core API just before the 3.0 release. |
| 729 |
|
| 730 |
if ( C4::Context->preference('marcflavour') eq 'MARC21' ) { |
| 731 |
$marcrecordauth->insert_fields_ordered( |
| 732 |
MARC::Field->new( |
| 733 |
'667', '', '', |
| 734 |
'a' => "Machine generated authority record." |
| 735 |
) |
| 736 |
); |
| 737 |
if(defined $bib) { |
| 738 |
my $cite = |
| 739 |
$bib->author() . ", " |
| 740 |
. $bib->title_proper() . ", " |
| 741 |
. $bib->publication_date() . " "; |
| 742 |
$cite =~ s/^[\s\,]*//; |
| 743 |
$cite =~ s/[\s\,]*$//; |
| 744 |
$cite = |
| 745 |
"Work cat.: (" |
| 746 |
. C4::Context->preference('MARCOrgCode') . ")" |
| 747 |
. $bib->subfield( '999', 'c' ) . ": " |
| 748 |
. $cite; |
| 749 |
$marcrecordauth->insert_fields_ordered( |
| 750 |
MARC::Field->new( '670', '', '', 'a' => $cite ) ); |
| 751 |
} |
| 752 |
|
| 753 |
# Add correct informations in field 008 and 040 so the authority can be linked correctly in future linkings |
| 754 |
my $date=POSIX::strftime("%y%m%d",localtime); |
| 755 |
my @heading_use = qw(b b b); |
| 756 |
my $thesaurus = '|'; |
| 757 |
|
| 758 |
if ($heading->{'subject_added_entry'}) { |
| 759 |
$heading_use[1] = 'a'; |
| 760 |
# Thesaurus treatment |
| 761 |
my %thesaurus_conv = ('lcsh'=>'a','lcac'=>'b','mesh'=>'c','nal'=>'d','notspecified'=>'n','cash'=>'k','rvm'=>'v'); |
| 762 |
$thesaurus = 'z'; |
| 763 |
$thesaurus = $thesaurus_conv{$heading->{'thesaurus'}} if exists $thesaurus_conv{$heading->{'thesaurus'}}; |
| 764 |
if($thesaurus eq 'z') |
| 765 |
{ |
| 766 |
$marcrecordauth->insert_fields_ordered( |
| 767 |
MARC::Field->new('040','','','f'=>$heading->{'thesaurus'}) |
| 768 |
); |
| 769 |
} |
| 770 |
} |
| 771 |
if ($heading->{'series_added_entry'}) { |
| 772 |
$heading_use[2] = 'a'; |
| 773 |
} |
| 774 |
if (not $heading->{'subject_added_entry'} and not $heading->{'series_added_entry'}) { |
| 775 |
$heading_use[0] = 'a'; |
| 776 |
} |
| 777 |
|
| 778 |
# Get a valid default value for field 008 |
| 779 |
my $default_008 = C4::Context->preference('MARCAuthorityControlField008'); |
| 780 |
if(!$default_008 or length($default_008)<34) { |
| 781 |
$default_008 = '|| aca||aabn | a|a d'; |
| 782 |
} |
| 783 |
else { |
| 784 |
$default_008 = substr($default_008,0,34) |
| 785 |
} |
| 786 |
|
| 787 |
# Insert the date, thesaurus and heading_use into field 008 |
| 788 |
my $f008 = $date . $default_008; |
| 789 |
substr($f008, 11, 1) = $thesaurus; |
| 790 |
substr($f008, 14, 3) = join('', @heading_use); |
| 791 |
|
| 792 |
$marcrecordauth->insert_fields_ordered( |
| 793 |
MARC::Field->new('008',$f008) |
| 794 |
); |
| 795 |
} |
| 796 |
|
| 797 |
# warn "AUTH RECORD ADDED : ".$marcrecordauth->as_formatted; |
| 798 |
|
| 799 |
my $authid = C4::AuthoritiesMarc::AddAuthority( $marcrecordauth, '', $heading->auth_type() ); |
| 800 |
return $authid; |
| 801 |
} |
| 802 |
|
| 803 |
|
| 680 |
=head2 GetRecordValue |
804 |
=head2 GetRecordValue |
| 681 |
|
805 |
|
| 682 |
my $values = GetRecordValue($field, $record, $frameworkcode); |
806 |
my $values = GetRecordValue($field, $record, $frameworkcode); |