|
Lines 460-466
sub BiblioAutoLink {
Link Here
|
| 460 |
|
460 |
|
| 461 |
=head2 LinkBibHeadingsToAuthorities |
461 |
=head2 LinkBibHeadingsToAuthorities |
| 462 |
|
462 |
|
| 463 |
my $num_headings_changed, %results = LinkBibHeadingsToAuthorities($linker, $marc, $frameworkcode, [$allowrelink]); |
463 |
my $num_headings_changed, %results = LinkBibHeadingsToAuthorities($linker, $marc, $frameworkcode, [$allowrelink, $verbose]); |
| 464 |
|
464 |
|
| 465 |
Links bib headings to authority records by checking |
465 |
Links bib headings to authority records by checking |
| 466 |
each authority-controlled field in the C<MARC::Record> |
466 |
each authority-controlled field in the C<MARC::Record> |
|
Lines 482-487
sub LinkBibHeadingsToAuthorities {
Link Here
|
| 482 |
my $bib = shift; |
482 |
my $bib = shift; |
| 483 |
my $frameworkcode = shift; |
483 |
my $frameworkcode = shift; |
| 484 |
my $allowrelink = shift; |
484 |
my $allowrelink = shift; |
|
|
485 |
my $verbose = shift; |
| 485 |
my %results; |
486 |
my %results; |
| 486 |
if (!$bib) { |
487 |
if (!$bib) { |
| 487 |
carp 'LinkBibHeadingsToAuthorities called on undefined bib record'; |
488 |
carp 'LinkBibHeadingsToAuthorities called on undefined bib record'; |
|
Lines 489-495
sub LinkBibHeadingsToAuthorities {
Link Here
|
| 489 |
} |
490 |
} |
| 490 |
require C4::Heading; |
491 |
require C4::Heading; |
| 491 |
require C4::AuthoritiesMarc; |
492 |
require C4::AuthoritiesMarc; |
| 492 |
|
|
|
| 493 |
$allowrelink = 1 unless defined $allowrelink; |
493 |
$allowrelink = 1 unless defined $allowrelink; |
| 494 |
my $num_headings_changed = 0; |
494 |
my $num_headings_changed = 0; |
| 495 |
foreach my $field ( $bib->fields() ) { |
495 |
foreach my $field ( $bib->fields() ) { |
|
Lines 502-507
sub LinkBibHeadingsToAuthorities {
Link Here
|
| 502 |
if ( defined $current_link && (!$allowrelink || !C4::Context->preference('LinkerRelink')) ) |
502 |
if ( defined $current_link && (!$allowrelink || !C4::Context->preference('LinkerRelink')) ) |
| 503 |
{ |
503 |
{ |
| 504 |
$results{'linked'}->{ $heading->display_form() }++; |
504 |
$results{'linked'}->{ $heading->display_form() }++; |
|
|
505 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $current_link, status => 'UNCHANGED'}) if $verbose; |
| 505 |
next; |
506 |
next; |
| 506 |
} |
507 |
} |
| 507 |
|
508 |
|
|
Lines 509-525
sub LinkBibHeadingsToAuthorities {
Link Here
|
| 509 |
if ($authid) { |
510 |
if ($authid) { |
| 510 |
$results{ $fuzzy ? 'fuzzy' : 'linked' } |
511 |
$results{ $fuzzy ? 'fuzzy' : 'linked' } |
| 511 |
->{ $heading->display_form() }++; |
512 |
->{ $heading->display_form() }++; |
| 512 |
next if defined $current_link and $current_link == $authid; |
513 |
if(defined $current_link and $current_link == $authid) { |
|
|
514 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $current_link, status => 'UNCHANGED'}) if $verbose; |
| 515 |
next; |
| 516 |
} |
| 513 |
|
517 |
|
| 514 |
$field->delete_subfield( code => '9' ) if defined $current_link; |
518 |
$field->delete_subfield( code => '9' ) if defined $current_link; |
| 515 |
$field->add_subfields( '9', $authid ); |
519 |
$field->add_subfields( '9', $authid ); |
| 516 |
$num_headings_changed++; |
520 |
$num_headings_changed++; |
|
|
521 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => $status || 'LOCAL_FOUND'}) if $verbose; |
| 517 |
} |
522 |
} |
| 518 |
else { |
523 |
else { |
|
|
524 |
my $authority_type = Koha::Authority::Types->find( $heading->auth_type() ); |
| 519 |
if ( defined $current_link |
525 |
if ( defined $current_link |
| 520 |
&& (!$allowrelink || C4::Context->preference('LinkerKeepStale')) ) |
526 |
&& (!$allowrelink || C4::Context->preference('LinkerKeepStale')) ) |
| 521 |
{ |
527 |
{ |
| 522 |
$results{'fuzzy'}->{ $heading->display_form() }++; |
528 |
$results{'fuzzy'}->{ $heading->display_form() }++; |
|
|
529 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $current_link, status => 'UNCHANGED'}) if $verbose; |
| 523 |
} |
530 |
} |
| 524 |
elsif ( C4::Context->preference('AutoCreateAuthorities') ) { |
531 |
elsif ( C4::Context->preference('AutoCreateAuthorities') ) { |
| 525 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
532 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
|
Lines 595-618
sub LinkBibHeadingsToAuthorities {
Link Here
|
| 595 |
$num_headings_changed++; |
602 |
$num_headings_changed++; |
| 596 |
$linker->update_cache($heading, $authid); |
603 |
$linker->update_cache($heading, $authid); |
| 597 |
$results{'added'}->{ $heading->display_form() }++; |
604 |
$results{'added'}->{ $heading->display_form() }++; |
|
|
605 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => 'CREATED'}) if $verbose; |
| 598 |
} |
606 |
} |
| 599 |
} |
607 |
} |
| 600 |
elsif ( defined $current_link ) { |
608 |
elsif ( defined $current_link ) { |
| 601 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
609 |
if ( _check_valid_auth_link( $current_link, $field ) ) { |
| 602 |
$results{'linked'}->{ $heading->display_form() }++; |
610 |
$results{'linked'}->{ $heading->display_form() }++; |
|
|
611 |
push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => 'UNCHANGED'}) if $verbose; |
| 603 |
} |
612 |
} |
| 604 |
else { |
613 |
else { |
| 605 |
$field->delete_subfield( code => '9' ); |
614 |
$field->delete_subfield( code => '9' ); |
| 606 |
$num_headings_changed++; |
615 |
$num_headings_changed++; |
| 607 |
$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; |
| 608 |
} |
618 |
} |
| 609 |
} |
619 |
} |
| 610 |
else { |
620 |
else { |
| 611 |
$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; |
| 612 |
} |
623 |
} |
| 613 |
} |
624 |
} |
| 614 |
|
625 |
|
| 615 |
} |
626 |
} |
|
|
627 |
push(@{$results{'details'}}, { tag => '', authid => undef, status => 'UNCHANGED'}) unless %results; |
| 616 |
return $num_headings_changed, \%results; |
628 |
return $num_headings_changed, \%results; |
| 617 |
} |
629 |
} |
| 618 |
|
630 |
|
|
Lines 638-643
sub _check_valid_auth_link {
Link Here
|
| 638 |
return ($field->as_string('abcdefghijklmnopqrstuvwxyz') eq $authorized_heading); |
650 |
return ($field->as_string('abcdefghijklmnopqrstuvwxyz') eq $authorized_heading); |
| 639 |
} |
651 |
} |
| 640 |
|
652 |
|
|
|
653 |
=head2 AddAuthorityFromHeading |
| 654 |
|
| 655 |
my $authid = AddAuthorityFromHeading($marcrecordauth, $heading); |
| 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 $marcrecordauth = shift; |
| 665 |
my $heading = shift; |
| 666 |
|
| 667 |
if(!defined $heading || !defined $marcrecordauth){ |
| 668 |
return 0; |
| 669 |
} |
| 670 |
# Add correct informations in field 008 and 040 so the authority can be linked correctly in future linkings |
| 671 |
my $date=POSIX::strftime("%y%m%d",localtime); |
| 672 |
my @heading_use = qw(b b b); |
| 673 |
my $thesaurus = '|'; |
| 674 |
|
| 675 |
if ($heading->{'subject_added_entry'}) { |
| 676 |
$heading_use[1] = 'a'; |
| 677 |
# Thesaurus treatment |
| 678 |
my %thesaurus_conv = ('lcsh'=>'a','lcac'=>'b','mesh'=>'c','nal'=>'d','notspecified'=>'n','cash'=>'k','rvm'=>'v'); |
| 679 |
$thesaurus = 'z'; |
| 680 |
$thesaurus = $thesaurus_conv{$heading->{'thesaurus'}} if exists $thesaurus_conv{$heading->{'thesaurus'}}; |
| 681 |
if($thesaurus eq 'z') |
| 682 |
{ |
| 683 |
$marcrecordauth->insert_fields_ordered( |
| 684 |
MARC::Field->new('040','','','f'=>$heading->{'thesaurus'}) |
| 685 |
); |
| 686 |
} |
| 687 |
} |
| 688 |
if ($heading->{'series_added_entry'}) { |
| 689 |
$heading_use[2] = 'a'; |
| 690 |
} |
| 691 |
if (not $heading->{'subject_added_entry'} and not $heading->{'series_added_entry'}) { |
| 692 |
$heading_use[0] = 'a'; |
| 693 |
} |
| 694 |
|
| 695 |
# Get a valid default value for field 008 |
| 696 |
my $default_008 = C4::Context->preference('MARCAuthorityControlField008'); |
| 697 |
if(!$default_008 or length($default_008)<34) { |
| 698 |
$default_008 = '|| aca||aabn | a|a d'; |
| 699 |
} |
| 700 |
else { |
| 701 |
$default_008 = substr($default_008,0,34) |
| 702 |
} |
| 703 |
|
| 704 |
# Insert the date, thesaurus and heading_use into field 008 |
| 705 |
my $f008 = $date . $default_008; |
| 706 |
substr($f008, 11, 1) = $thesaurus; |
| 707 |
substr($f008, 14, 3) = join('', @heading_use); |
| 708 |
|
| 709 |
$marcrecordauth->insert_fields_ordered( |
| 710 |
MARC::Field->new('008',$f008) |
| 711 |
); |
| 712 |
|
| 713 |
my $authid = C4::AuthoritiesMarc::AddAuthority( $marcrecordauth, '', $heading->auth_type() ); |
| 714 |
return $authid; |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
=head2 GetRecordValue |
| 719 |
|
| 720 |
my $values = GetRecordValue($field, $record, $frameworkcode); |
| 721 |
|
| 722 |
Get MARC fields from a keyword defined in fieldmapping table. |
| 723 |
|
| 724 |
=cut |
| 725 |
|
| 726 |
sub GetRecordValue { |
| 727 |
my ( $field, $record, $frameworkcode ) = @_; |
| 728 |
|
| 729 |
if (!$record) { |
| 730 |
carp 'GetRecordValue called with undefined record'; |
| 731 |
return; |
| 732 |
} |
| 733 |
my $dbh = C4::Context->dbh; |
| 734 |
|
| 735 |
my $sth = $dbh->prepare('SELECT fieldcode, subfieldcode FROM fieldmapping WHERE frameworkcode = ? AND field = ?'); |
| 736 |
$sth->execute( $frameworkcode, $field ); |
| 737 |
|
| 738 |
my @result = (); |
| 739 |
|
| 740 |
while ( my $row = $sth->fetchrow_hashref ) { |
| 741 |
foreach my $field ( $record->field( $row->{fieldcode} ) ) { |
| 742 |
if ( ( $row->{subfieldcode} ne "" && $field->subfield( $row->{subfieldcode} ) ) ) { |
| 743 |
foreach my $subfield ( $field->subfield( $row->{subfieldcode} ) ) { |
| 744 |
push @result, { 'subfield' => $subfield }; |
| 745 |
} |
| 746 |
|
| 747 |
} elsif ( $row->{subfieldcode} eq "" ) { |
| 748 |
push @result, { 'subfield' => $field->as_string() }; |
| 749 |
} |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
return \@result; |
| 754 |
} |
| 755 |
|
| 641 |
=head2 GetBiblioData |
756 |
=head2 GetBiblioData |
| 642 |
|
757 |
|
| 643 |
$data = &GetBiblioData($biblionumber); |
758 |
$data = &GetBiblioData($biblionumber); |