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