Lines 468-474
sub BiblioAutoLink {
Link Here
|
468 |
|
468 |
|
469 |
=head2 LinkBibHeadingsToAuthorities |
469 |
=head2 LinkBibHeadingsToAuthorities |
470 |
|
470 |
|
471 |
my $num_headings_changed, %results = LinkBibHeadingsToAuthorities($linker, $marc, $frameworkcode, [$allowrelink]); |
471 |
my $num_headings_changed, %results = LinkBibHeadingsToAuthorities($linker, $marc, $frameworkcode, [$allowrelink, $verbose]); |
472 |
|
472 |
|
473 |
Links bib headings to authority records by checking |
473 |
Links bib headings to authority records by checking |
474 |
each authority-controlled field in the C<MARC::Record> |
474 |
each authority-controlled field in the C<MARC::Record> |
Lines 490-495
sub LinkBibHeadingsToAuthorities {
Link Here
|
490 |
my $bib = shift; |
490 |
my $bib = shift; |
491 |
my $frameworkcode = shift; |
491 |
my $frameworkcode = shift; |
492 |
my $allowrelink = shift; |
492 |
my $allowrelink = shift; |
|
|
493 |
my $verbose = shift; |
493 |
my %results; |
494 |
my %results; |
494 |
if (!$bib) { |
495 |
if (!$bib) { |
495 |
carp 'LinkBibHeadingsToAuthorities called on undefined bib record'; |
496 |
carp 'LinkBibHeadingsToAuthorities called on undefined bib record'; |
Lines 497-503
sub LinkBibHeadingsToAuthorities {
Link Here
|
497 |
} |
498 |
} |
498 |
require C4::Heading; |
499 |
require C4::Heading; |
499 |
require C4::AuthoritiesMarc; |
500 |
require C4::AuthoritiesMarc; |
500 |
|
|
|
501 |
$allowrelink = 1 unless defined $allowrelink; |
501 |
$allowrelink = 1 unless defined $allowrelink; |
502 |
my $num_headings_changed = 0; |
502 |
my $num_headings_changed = 0; |
503 |
foreach my $field ( $bib->fields() ) { |
503 |
foreach my $field ( $bib->fields() ) { |
Lines 510-540
sub LinkBibHeadingsToAuthorities {
Link Here
|
510 |
if ( defined $current_link && (!$allowrelink || !C4::Context->preference('LinkerRelink')) ) |
510 |
if ( defined $current_link && (!$allowrelink || !C4::Context->preference('LinkerRelink')) ) |
511 |
{ |
511 |
{ |
512 |
$results{'linked'}->{ $heading->display_form() }++; |
512 |
$results{'linked'}->{ $heading->display_form() }++; |
|
|
513 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $current_link, status => 'UNCHANGED'}) if $verbose; |
513 |
next; |
514 |
next; |
514 |
} |
515 |
} |
515 |
|
516 |
|
516 |
my ( $authid, $fuzzy ) = $linker->get_link($heading); |
517 |
my ( $authid, $fuzzy, $status ) = $linker->get_link($heading); |
517 |
if ($authid) { |
518 |
if ($authid) { |
518 |
$results{ $fuzzy ? 'fuzzy' : 'linked' } |
519 |
$results{ $fuzzy ? 'fuzzy' : 'linked' } |
519 |
->{ $heading->display_form() }++; |
520 |
->{ $heading->display_form() }++; |
520 |
next if defined $current_link and $current_link == $authid; |
521 |
if(defined $current_link and $current_link == $authid) { |
|
|
522 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $current_link, status => 'UNCHANGED'}) if $verbose; |
523 |
next; |
524 |
} |
521 |
|
525 |
|
522 |
$field->delete_subfield( code => '9' ) if defined $current_link; |
526 |
$field->delete_subfield( code => '9' ) if defined $current_link; |
523 |
$field->add_subfields( '9', $authid ); |
527 |
$field->add_subfields( '9', $authid ); |
524 |
$num_headings_changed++; |
528 |
$num_headings_changed++; |
|
|
529 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => $status || 'LOCAL_FOUND'}) if $verbose; |
525 |
} |
530 |
} |
526 |
else { |
531 |
else { |
|
|
532 |
my $authority_type = Koha::Authority::Types->find( $heading->auth_type() ); |
527 |
if ( defined $current_link |
533 |
if ( defined $current_link |
528 |
&& (!$allowrelink || C4::Context->preference('LinkerKeepStale')) ) |
534 |
&& (!$allowrelink || C4::Context->preference('LinkerKeepStale')) ) |
529 |
{ |
535 |
{ |
530 |
$results{'fuzzy'}->{ $heading->display_form() }++; |
536 |
$results{'fuzzy'}->{ $heading->display_form() }++; |
|
|
537 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $current_link, status => 'UNCHANGED'}) if $verbose; |
531 |
} |
538 |
} |
532 |
elsif ( C4::Context->preference('AutoCreateAuthorities') ) { |
539 |
elsif ( C4::Context->preference('AutoCreateAuthorities') ) { |
533 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
540 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
534 |
$results{'linked'}->{ $heading->display_form() }++; |
541 |
$results{'linked'}->{ $heading->display_form() }++; |
535 |
} |
542 |
} |
536 |
else { |
543 |
else { |
537 |
my $authority_type = Koha::Authority::Types->find( $heading->auth_type() ); |
|
|
538 |
my $marcrecordauth = MARC::Record->new(); |
544 |
my $marcrecordauth = MARC::Record->new(); |
539 |
if ( C4::Context->preference('marcflavour') eq 'MARC21' ) { |
545 |
if ( C4::Context->preference('marcflavour') eq 'MARC21' ) { |
540 |
$marcrecordauth->leader(' nz a22 o 4500'); |
546 |
$marcrecordauth->leader(' nz a22 o 4500'); |
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($marcrecordauth, $heading); |
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', auth_type => $heading->auth_type(), tag_to_report => $authority_type->auth_tag_to_report}) 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', auth_type => $heading->auth_type(), tag_to_report => $authority_type->auth_tag_to_report}) 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 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($marcrecordauth, $heading); |
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 $marcrecordauth = shift; |
670 |
my $heading = shift; |
671 |
|
672 |
if(!defined $heading || !defined $marcrecordauth){ |
673 |
return 0; |
674 |
} |
675 |
# Add correct informations in field 008 and 040 so the authority can be linked correctly in future linkings |
676 |
my $date=POSIX::strftime("%y%m%d",localtime); |
677 |
my @heading_use = qw(b b b); |
678 |
my $thesaurus = '|'; |
679 |
|
680 |
if ($heading->{'subject_added_entry'}) { |
681 |
$heading_use[1] = 'a'; |
682 |
# Thesaurus treatment |
683 |
my %thesaurus_conv = ('lcsh'=>'a','lcac'=>'b','mesh'=>'c','nal'=>'d','notspecified'=>'n','cash'=>'k','rvm'=>'v'); |
684 |
$thesaurus = 'z'; |
685 |
$thesaurus = $thesaurus_conv{$heading->{'thesaurus'}} if exists $thesaurus_conv{$heading->{'thesaurus'}}; |
686 |
if($thesaurus eq 'z') |
687 |
{ |
688 |
$marcrecordauth->insert_fields_ordered( |
689 |
MARC::Field->new('040','','','f'=>$heading->{'thesaurus'}) |
690 |
); |
691 |
} |
692 |
} |
693 |
if ($heading->{'series_added_entry'}) { |
694 |
$heading_use[2] = 'a'; |
695 |
} |
696 |
if (not $heading->{'subject_added_entry'} and not $heading->{'series_added_entry'}) { |
697 |
$heading_use[0] = 'a'; |
698 |
} |
699 |
|
700 |
# Get a valid default value for field 008 |
701 |
my $default_008 = C4::Context->preference('MARCAuthorityControlField008'); |
702 |
if(!$default_008 or length($default_008)<34) { |
703 |
$default_008 = '|| aca||aabn | a|a d'; |
704 |
} |
705 |
else { |
706 |
$default_008 = substr($default_008,0,34) |
707 |
} |
708 |
|
709 |
# Insert the date, thesaurus and heading_use into field 008 |
710 |
my $f008 = $date . $default_008; |
711 |
substr($f008, 11, 1) = $thesaurus; |
712 |
substr($f008, 14, 3) = join('', @heading_use); |
713 |
|
714 |
$marcrecordauth->insert_fields_ordered( |
715 |
MARC::Field->new('008',$f008) |
716 |
); |
717 |
|
718 |
my $authid = C4::AuthoritiesMarc::AddAuthority( $marcrecordauth, '', $heading->auth_type() ); |
719 |
return $authid; |
720 |
} |
721 |
|
722 |
|
723 |
=head2 GetRecordValue |
724 |
|
725 |
my $values = GetRecordValue($field, $record, $frameworkcode); |
726 |
|
727 |
Get MARC fields from a keyword defined in fieldmapping table. |
728 |
|
729 |
=cut |
730 |
|
731 |
sub GetRecordValue { |
732 |
my ( $field, $record, $frameworkcode ) = @_; |
733 |
|
734 |
if (!$record) { |
735 |
carp 'GetRecordValue called with undefined record'; |
736 |
return; |
737 |
} |
738 |
my $dbh = C4::Context->dbh; |
739 |
|
740 |
my $sth = $dbh->prepare('SELECT fieldcode, subfieldcode FROM fieldmapping WHERE frameworkcode = ? AND field = ?'); |
741 |
$sth->execute( $frameworkcode, $field ); |
742 |
|
743 |
my @result = (); |
744 |
|
745 |
while ( my $row = $sth->fetchrow_hashref ) { |
746 |
foreach my $field ( $record->field( $row->{fieldcode} ) ) { |
747 |
if ( ( $row->{subfieldcode} ne "" && $field->subfield( $row->{subfieldcode} ) ) ) { |
748 |
foreach my $subfield ( $field->subfield( $row->{subfieldcode} ) ) { |
749 |
push @result, { 'subfield' => $subfield }; |
750 |
} |
751 |
|
752 |
} elsif ( $row->{subfieldcode} eq "" ) { |
753 |
push @result, { 'subfield' => $field->as_string() }; |
754 |
} |
755 |
} |
756 |
} |
757 |
|
758 |
return \@result; |
759 |
} |
760 |
|
649 |
=head2 GetBiblioData |
761 |
=head2 GetBiblioData |
650 |
|
762 |
|
651 |
$data = &GetBiblioData($biblionumber); |
763 |
$data = &GetBiblioData($biblionumber); |