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