Lines 481-487
sub BiblioAutoLink {
Link Here
|
481 |
|
481 |
|
482 |
=head2 LinkBibHeadingsToAuthorities |
482 |
=head2 LinkBibHeadingsToAuthorities |
483 |
|
483 |
|
484 |
my $num_headings_changed, %results = LinkBibHeadingsToAuthorities($linker, $marc, $frameworkcode, [$allowrelink]); |
484 |
my $num_headings_changed, %results = LinkBibHeadingsToAuthorities($linker, $marc, $frameworkcode, [$allowrelink, $verbose]); |
485 |
|
485 |
|
486 |
Links bib headings to authority records by checking |
486 |
Links bib headings to authority records by checking |
487 |
each authority-controlled field in the C<MARC::Record> |
487 |
each authority-controlled field in the C<MARC::Record> |
Lines 503-508
sub LinkBibHeadingsToAuthorities {
Link Here
|
503 |
my $bib = shift; |
503 |
my $bib = shift; |
504 |
my $frameworkcode = shift; |
504 |
my $frameworkcode = shift; |
505 |
my $allowrelink = shift; |
505 |
my $allowrelink = shift; |
|
|
506 |
my $verbose = shift; |
506 |
my %results; |
507 |
my %results; |
507 |
if (!$bib) { |
508 |
if (!$bib) { |
508 |
carp 'LinkBibHeadingsToAuthorities called on undefined bib record'; |
509 |
carp 'LinkBibHeadingsToAuthorities called on undefined bib record'; |
Lines 510-516
sub LinkBibHeadingsToAuthorities {
Link Here
|
510 |
} |
511 |
} |
511 |
require C4::Heading; |
512 |
require C4::Heading; |
512 |
require C4::AuthoritiesMarc; |
513 |
require C4::AuthoritiesMarc; |
513 |
|
|
|
514 |
$allowrelink = 1 unless defined $allowrelink; |
514 |
$allowrelink = 1 unless defined $allowrelink; |
515 |
my $num_headings_changed = 0; |
515 |
my $num_headings_changed = 0; |
516 |
foreach my $field ( $bib->fields() ) { |
516 |
foreach my $field ( $bib->fields() ) { |
Lines 523-546
sub LinkBibHeadingsToAuthorities {
Link Here
|
523 |
if ( defined $current_link && (!$allowrelink || !C4::Context->preference('LinkerRelink')) ) |
523 |
if ( defined $current_link && (!$allowrelink || !C4::Context->preference('LinkerRelink')) ) |
524 |
{ |
524 |
{ |
525 |
$results{'linked'}->{ $heading->display_form() }++; |
525 |
$results{'linked'}->{ $heading->display_form() }++; |
|
|
526 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $current_link, status => 'UNCHANGED'}) if $verbose; |
526 |
next; |
527 |
next; |
527 |
} |
528 |
} |
528 |
|
529 |
|
529 |
my ( $authid, $fuzzy ) = $linker->get_link($heading); |
530 |
my ( $authid, $fuzzy, $status ) = $linker->get_link($heading); |
530 |
if ($authid) { |
531 |
if ($authid) { |
531 |
$results{ $fuzzy ? 'fuzzy' : 'linked' } |
532 |
$results{ $fuzzy ? 'fuzzy' : 'linked' } |
532 |
->{ $heading->display_form() }++; |
533 |
->{ $heading->display_form() }++; |
533 |
next if defined $current_link and $current_link == $authid; |
534 |
if(defined $current_link and $current_link == $authid) { |
|
|
535 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $current_link, status => 'UNCHANGED'}) if $verbose; |
536 |
next; |
537 |
} |
534 |
|
538 |
|
535 |
$field->delete_subfield( code => '9' ) if defined $current_link; |
539 |
$field->delete_subfield( code => '9' ) if defined $current_link; |
536 |
$field->add_subfields( '9', $authid ); |
540 |
$field->add_subfields( '9', $authid ); |
537 |
$num_headings_changed++; |
541 |
$num_headings_changed++; |
|
|
542 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => $status || 'LOCAL_FOUND'}) if $verbose; |
538 |
} |
543 |
} |
539 |
else { |
544 |
else { |
540 |
if ( defined $current_link |
545 |
if ( defined $current_link |
541 |
&& (!$allowrelink || C4::Context->preference('LinkerKeepStale')) ) |
546 |
&& (!$allowrelink || C4::Context->preference('LinkerKeepStale')) ) |
542 |
{ |
547 |
{ |
543 |
$results{'fuzzy'}->{ $heading->display_form() }++; |
548 |
$results{'fuzzy'}->{ $heading->display_form() }++; |
|
|
549 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $current_link, status => 'UNCHANGED'}) if $verbose; |
544 |
} |
550 |
} |
545 |
elsif ( C4::Context->preference('AutoCreateAuthorities') ) { |
551 |
elsif ( C4::Context->preference('AutoCreateAuthorities') ) { |
546 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
552 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
Lines 594-625
sub LinkBibHeadingsToAuthorities {
Link Here
|
594 |
} |
600 |
} |
595 |
|
601 |
|
596 |
# warn "AUTH RECORD ADDED : ".$marcrecordauth->as_formatted; |
602 |
# warn "AUTH RECORD ADDED : ".$marcrecordauth->as_formatted; |
|
|
603 |
$authid = AddAuthorityFromHeading($heading, $field, $bib); |
597 |
|
604 |
|
598 |
$authid = |
|
|
599 |
C4::AuthoritiesMarc::AddAuthority( $marcrecordauth, '', |
600 |
$heading->auth_type() ); |
601 |
$field->add_subfields( '9', $authid ); |
605 |
$field->add_subfields( '9', $authid ); |
602 |
$num_headings_changed++; |
606 |
$num_headings_changed++; |
603 |
$linker->update_cache($heading, $authid); |
607 |
$linker->update_cache($heading, $authid); |
604 |
$results{'added'}->{ $heading->display_form() }++; |
608 |
$results{'added'}->{ $heading->display_form() }++; |
|
|
609 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => 'CREATED'}) if $verbose; |
605 |
} |
610 |
} |
606 |
} |
611 |
} |
607 |
elsif ( defined $current_link ) { |
612 |
elsif ( defined $current_link ) { |
608 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
613 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
609 |
$results{'linked'}->{ $heading->display_form() }++; |
614 |
$results{'linked'}->{ $heading->display_form() }++; |
|
|
615 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => 'UNCHANGED'}) if $verbose; |
610 |
} |
616 |
} |
611 |
else { |
617 |
else { |
612 |
$field->delete_subfield( code => '9' ); |
618 |
$field->delete_subfield( code => '9' ); |
613 |
$num_headings_changed++; |
619 |
$num_headings_changed++; |
614 |
$results{'unlinked'}->{ $heading->display_form() }++; |
620 |
$results{'unlinked'}->{ $heading->display_form() }++; |
|
|
621 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => undef, status => 'NONE_FOUND'}) if $verbose; |
615 |
} |
622 |
} |
616 |
} |
623 |
} |
617 |
else { |
624 |
else { |
618 |
$results{'unlinked'}->{ $heading->display_form() }++; |
625 |
$results{'unlinked'}->{ $heading->display_form() }++; |
|
|
626 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => undef, status => 'NONE_FOUND'}) if $verbose; |
619 |
} |
627 |
} |
620 |
} |
628 |
} |
621 |
|
629 |
|
622 |
} |
630 |
} |
|
|
631 |
push(@{$results{'details'}}, { tag => '', authid => undef, status => 'UNCHANGED'}) unless %results; |
623 |
return $num_headings_changed, \%results; |
632 |
return $num_headings_changed, \%results; |
624 |
} |
633 |
} |
625 |
|
634 |
|
Lines 647-652
sub _check_valid_auth_link {
Link Here
|
647 |
return ($field->as_string('abcdefghijklmnopqrstuvwxyz') eq $authorized_heading); |
656 |
return ($field->as_string('abcdefghijklmnopqrstuvwxyz') eq $authorized_heading); |
648 |
} |
657 |
} |
649 |
|
658 |
|
|
|
659 |
=head2 AddAuthorityFromHeading |
660 |
|
661 |
my $authid = AddAuthorityFromHeading($heading, $field, [$bib]); |
662 |
|
663 |
Creates a new authority from an heading and its associated field. |
664 |
|
665 |
Returns the id of the newly created authority. |
666 |
|
667 |
=cut |
668 |
|
669 |
sub AddAuthorityFromHeading { |
670 |
my $heading = shift; |
671 |
my $field = shift; |
672 |
my $bib = shift; |
673 |
|
674 |
if(!defined $heading || !defined $field || !defined $bib){ |
675 |
return 0; |
676 |
} |
677 |
my $authtypedata = |
678 |
Koha::Authority::Types->find( $heading->auth_type() ); |
679 |
my $marcrecordauth = MARC::Record->new(); |
680 |
if ( C4::Context->preference('marcflavour') eq 'MARC21' ) { |
681 |
$marcrecordauth->leader(' nz a22 o 4500'); |
682 |
SetMarcUnicodeFlag( $marcrecordauth, 'MARC21' ); |
683 |
} |
684 |
my $authfield = |
685 |
MARC::Field->new( $authtypedata->auth_tag_to_report, |
686 |
'', '', "a" => "" . $field->subfield('a') ); |
687 |
map { |
688 |
$authfield->add_subfields( $_->[0] => $_->[1] ) |
689 |
if ( $_->[0] =~ /[A-z]/ && $_->[0] ne "a" ) |
690 |
} $field->subfields(); |
691 |
$marcrecordauth->insert_fields_ordered($authfield); |
692 |
|
693 |
# bug 2317: ensure new authority knows it's using UTF-8; currently |
694 |
# only need to do this for MARC21, as MARC::Record->as_xml_record() handles |
695 |
# automatically for UNIMARC (by not transcoding) |
696 |
# FIXME: AddAuthority() instead should simply explicitly require that the MARC::Record |
697 |
# use UTF-8, but as of 2008-08-05, did not want to introduce that kind |
698 |
# of change to a core API just before the 3.0 release. |
699 |
|
700 |
if ( C4::Context->preference('marcflavour') eq 'MARC21' ) { |
701 |
$marcrecordauth->insert_fields_ordered( |
702 |
MARC::Field->new( |
703 |
'667', '', '', |
704 |
'a' => "Machine generated authority record." |
705 |
) |
706 |
); |
707 |
if(defined $bib) { |
708 |
my $cite = |
709 |
$bib->author() . ", " |
710 |
. $bib->title_proper() . ", " |
711 |
. $bib->publication_date() . " "; |
712 |
$cite =~ s/^[\s\,]*//; |
713 |
$cite =~ s/[\s\,]*$//; |
714 |
$cite = |
715 |
"Work cat.: (" |
716 |
. C4::Context->preference('MARCOrgCode') . ")" |
717 |
. $bib->subfield( '999', 'c' ) . ": " |
718 |
. $cite; |
719 |
$marcrecordauth->insert_fields_ordered( |
720 |
MARC::Field->new( '670', '', '', 'a' => $cite ) ); |
721 |
} |
722 |
|
723 |
# Add correct informations in field 008 and 040 so the authority can be linked correctly in future linkings |
724 |
my $date=POSIX::strftime("%y%m%d",localtime); |
725 |
my @heading_use = qw(b b b); |
726 |
my $thesaurus = '|'; |
727 |
|
728 |
if ($heading->{'subject_added_entry'}) { |
729 |
$heading_use[1] = 'a'; |
730 |
# Thesaurus treatment |
731 |
my %thesaurus_conv = ('lcsh'=>'a','lcac'=>'b','mesh'=>'c','nal'=>'d','notspecified'=>'n','cash'=>'k','rvm'=>'v'); |
732 |
$thesaurus = 'z'; |
733 |
$thesaurus = $thesaurus_conv{$heading->{'thesaurus'}} if exists $thesaurus_conv{$heading->{'thesaurus'}}; |
734 |
if($thesaurus eq 'z') |
735 |
{ |
736 |
$marcrecordauth->insert_fields_ordered( |
737 |
MARC::Field->new('040','','','f'=>$heading->{'thesaurus'}) |
738 |
); |
739 |
} |
740 |
} |
741 |
if ($heading->{'series_added_entry'}) { |
742 |
$heading_use[2] = 'a'; |
743 |
} |
744 |
if (not $heading->{'subject_added_entry'} and not $heading->{'series_added_entry'}) { |
745 |
$heading_use[0] = 'a'; |
746 |
} |
747 |
|
748 |
# Get a valid default value for field 008 |
749 |
my $default_008 = C4::Context->preference('MARCAuthorityControlField008'); |
750 |
if(!$default_008 or length($default_008)<34) { |
751 |
$default_008 = '|| aca||aabn | a|a d'; |
752 |
} |
753 |
else { |
754 |
$default_008 = substr($default_008,0,34) |
755 |
} |
756 |
|
757 |
# Insert the date, thesaurus and heading_use into field 008 |
758 |
my $f008 = $date . $default_008; |
759 |
substr($f008, 11, 1) = $thesaurus; |
760 |
substr($f008, 14, 3) = join('', @heading_use); |
761 |
|
762 |
$marcrecordauth->insert_fields_ordered( |
763 |
MARC::Field->new('008',$f008) |
764 |
); |
765 |
} |
766 |
|
767 |
# warn "AUTH RECORD ADDED : ".$marcrecordauth->as_formatted; |
768 |
|
769 |
my $authid = C4::AuthoritiesMarc::AddAuthority( $marcrecordauth, '', $heading->auth_type() ); |
770 |
return $authid; |
771 |
} |
772 |
|
773 |
|
650 |
=head2 GetRecordValue |
774 |
=head2 GetRecordValue |
651 |
|
775 |
|
652 |
my $values = GetRecordValue($field, $record, $frameworkcode); |
776 |
my $values = GetRecordValue($field, $record, $frameworkcode); |