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