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 589-620
sub LinkBibHeadingsToAuthorities {
Link Here
|
589 |
} |
595 |
} |
590 |
|
596 |
|
591 |
# warn "AUTH RECORD ADDED : ".$marcrecordauth->as_formatted; |
597 |
# warn "AUTH RECORD ADDED : ".$marcrecordauth->as_formatted; |
|
|
598 |
$authid = AddAuthorityFromHeading($marcrecordauth, $heading); |
592 |
|
599 |
|
593 |
$authid = |
|
|
594 |
C4::AuthoritiesMarc::AddAuthority( $marcrecordauth, '', |
595 |
$heading->auth_type() ); |
596 |
$field->add_subfields( '9', $authid ); |
600 |
$field->add_subfields( '9', $authid ); |
597 |
$num_headings_changed++; |
601 |
$num_headings_changed++; |
598 |
$linker->update_cache($heading, $authid); |
602 |
$linker->update_cache($heading, $authid); |
599 |
$results{'added'}->{ $heading->display_form() }++; |
603 |
$results{'added'}->{ $heading->display_form() }++; |
|
|
604 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => 'CREATED'}) if $verbose; |
600 |
} |
605 |
} |
601 |
} |
606 |
} |
602 |
elsif ( defined $current_link ) { |
607 |
elsif ( defined $current_link ) { |
603 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
608 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
604 |
$results{'linked'}->{ $heading->display_form() }++; |
609 |
$results{'linked'}->{ $heading->display_form() }++; |
|
|
610 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => 'UNCHANGED'}) if $verbose; |
605 |
} |
611 |
} |
606 |
else { |
612 |
else { |
607 |
$field->delete_subfield( code => '9' ); |
613 |
$field->delete_subfield( code => '9' ); |
608 |
$num_headings_changed++; |
614 |
$num_headings_changed++; |
609 |
$results{'unlinked'}->{ $heading->display_form() }++; |
615 |
$results{'unlinked'}->{ $heading->display_form() }++; |
|
|
616 |
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; |
610 |
} |
617 |
} |
611 |
} |
618 |
} |
612 |
else { |
619 |
else { |
613 |
$results{'unlinked'}->{ $heading->display_form() }++; |
620 |
$results{'unlinked'}->{ $heading->display_form() }++; |
|
|
621 |
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; |
614 |
} |
622 |
} |
615 |
} |
623 |
} |
616 |
|
624 |
|
617 |
} |
625 |
} |
|
|
626 |
push(@{$results{'details'}}, { tag => '', authid => undef, status => 'UNCHANGED'}) unless %results; |
618 |
return $num_headings_changed, \%results; |
627 |
return $num_headings_changed, \%results; |
619 |
} |
628 |
} |
620 |
|
629 |
|
Lines 640-645
sub _check_valid_auth_link {
Link Here
|
640 |
return ($field->as_string('abcdefghijklmnopqrstuvwxyz') eq $authorized_heading); |
649 |
return ($field->as_string('abcdefghijklmnopqrstuvwxyz') eq $authorized_heading); |
641 |
} |
650 |
} |
642 |
|
651 |
|
|
|
652 |
=head2 AddAuthorityFromHeading |
653 |
|
654 |
my $authid = AddAuthorityFromHeading($marcrecordauth, $heading); |
655 |
|
656 |
Creates a new authority from an heading and its associated field. |
657 |
|
658 |
Returns the id of the newly created authority. |
659 |
|
660 |
=cut |
661 |
|
662 |
sub AddAuthorityFromHeading { |
663 |
my $marcrecordauth = shift; |
664 |
my $heading = shift; |
665 |
|
666 |
if(!defined $heading || !defined $marcrecordauth){ |
667 |
return 0; |
668 |
} |
669 |
# Add correct informations in field 008 and 040 so the authority can be linked correctly in future linkings |
670 |
my $date=POSIX::strftime("%y%m%d",localtime); |
671 |
my @heading_use = qw(b b b); |
672 |
my $thesaurus = '|'; |
673 |
|
674 |
if ($heading->{'subject_added_entry'}) { |
675 |
$heading_use[1] = 'a'; |
676 |
# Thesaurus treatment |
677 |
my %thesaurus_conv = ('lcsh'=>'a','lcac'=>'b','mesh'=>'c','nal'=>'d','notspecified'=>'n','cash'=>'k','rvm'=>'v'); |
678 |
$thesaurus = 'z'; |
679 |
$thesaurus = $thesaurus_conv{$heading->{'thesaurus'}} if exists $thesaurus_conv{$heading->{'thesaurus'}}; |
680 |
if($thesaurus eq 'z') |
681 |
{ |
682 |
$marcrecordauth->insert_fields_ordered( |
683 |
MARC::Field->new('040','','','f'=>$heading->{'thesaurus'}) |
684 |
); |
685 |
} |
686 |
} |
687 |
if ($heading->{'series_added_entry'}) { |
688 |
$heading_use[2] = 'a'; |
689 |
} |
690 |
if (not $heading->{'subject_added_entry'} and not $heading->{'series_added_entry'}) { |
691 |
$heading_use[0] = 'a'; |
692 |
} |
693 |
|
694 |
# Get a valid default value for field 008 |
695 |
my $default_008 = C4::Context->preference('MARCAuthorityControlField008'); |
696 |
if(!$default_008 or length($default_008)<34) { |
697 |
$default_008 = '|| aca||aabn | a|a d'; |
698 |
} |
699 |
else { |
700 |
$default_008 = substr($default_008,0,34) |
701 |
} |
702 |
|
703 |
# Insert the date, thesaurus and heading_use into field 008 |
704 |
my $f008 = $date . $default_008; |
705 |
substr($f008, 11, 1) = $thesaurus; |
706 |
substr($f008, 14, 3) = join('', @heading_use); |
707 |
|
708 |
$marcrecordauth->insert_fields_ordered( |
709 |
MARC::Field->new('008',$f008) |
710 |
); |
711 |
|
712 |
my $authid = C4::AuthoritiesMarc::AddAuthority( $marcrecordauth, '', $heading->auth_type() ); |
713 |
return $authid; |
714 |
} |
715 |
|
716 |
|
717 |
=head2 GetRecordValue |
718 |
|
719 |
my $values = GetRecordValue($field, $record, $frameworkcode); |
720 |
|
721 |
Get MARC fields from a keyword defined in fieldmapping table. |
722 |
|
723 |
=cut |
724 |
|
725 |
sub GetRecordValue { |
726 |
my ( $field, $record, $frameworkcode ) = @_; |
727 |
|
728 |
if (!$record) { |
729 |
carp 'GetRecordValue called with undefined record'; |
730 |
return; |
731 |
} |
732 |
my $dbh = C4::Context->dbh; |
733 |
|
734 |
my $sth = $dbh->prepare('SELECT fieldcode, subfieldcode FROM fieldmapping WHERE frameworkcode = ? AND field = ?'); |
735 |
$sth->execute( $frameworkcode, $field ); |
736 |
|
737 |
my @result = (); |
738 |
|
739 |
while ( my $row = $sth->fetchrow_hashref ) { |
740 |
foreach my $field ( $record->field( $row->{fieldcode} ) ) { |
741 |
if ( ( $row->{subfieldcode} ne "" && $field->subfield( $row->{subfieldcode} ) ) ) { |
742 |
foreach my $subfield ( $field->subfield( $row->{subfieldcode} ) ) { |
743 |
push @result, { 'subfield' => $subfield }; |
744 |
} |
745 |
|
746 |
} elsif ( $row->{subfieldcode} eq "" ) { |
747 |
push @result, { 'subfield' => $field->as_string() }; |
748 |
} |
749 |
} |
750 |
} |
751 |
|
752 |
return \@result; |
753 |
} |
754 |
|
643 |
=head2 GetBiblioData |
755 |
=head2 GetBiblioData |
644 |
|
756 |
|
645 |
$data = &GetBiblioData($biblionumber); |
757 |
$data = &GetBiblioData($biblionumber); |