Skip to content

Commit

Permalink
Bug 24269: Adjust C4::Heading to generate headings from auth tags
Browse files Browse the repository at this point in the history
To test:
1 - Be using Elasticsearch
2 - Check that field 150 is mapped to 'Match-heading' or add it (the subfields don't matter)
3 - Add a topic term authority record like "150 $aCats$vFiction"
4 - Add a 650 with $aCats and $vFiction and  $e depicted to a bibliographic record
5 - Run the linker for the bib
    perl misc/link_bibs_to_authorities.pl --bib-limit "biblionumber=89" -v
6 - Confirm the record is not correctly linked to the record
7 - Apply patch
8 - Reindex authorities for ES
    perl misc/search_tools/rebuild_elasticsearch.pl -v -d -a
9 - Run the linker and confirm record is correctly linked
    perl misc/link_bibs_to_authorities.pl --bib-limit "biblionumber=89" -v

Signed-off-by: Martin Renvoize <[email protected]>
Signed-off-by: Bouzid Fergani <[email protected]>
Signed-off-by: Martin Renvoize <[email protected]>
  • Loading branch information
kidclamp authored and mrenvoize committed Feb 19, 2020
1 parent 9079146 commit d4783f2
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 56 deletions.
4 changes: 2 additions & 2 deletions C4/Biblio.pm
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ sub LinkBibHeadingsToAuthorities {
$allowrelink = 1 unless defined $allowrelink;
my $num_headings_changed = 0;
foreach my $field ( $bib->fields() ) {
my $heading = C4::Heading->new_from_bib_field( $field, $frameworkcode );
my $heading = C4::Heading->new_from_field( $field, $frameworkcode );
next unless defined $heading;

# check existing $9
Expand Down Expand Up @@ -545,7 +545,7 @@ sub LinkBibHeadingsToAuthorities {
my @auth_subfields;
foreach my $subfield ( $field->subfields() ){
if ( $subfield->[0] =~ /[A-z]/
&& C4::Heading::valid_bib_heading_subfield(
&& C4::Heading::valid_heading_subfield(
$field->tag, $subfield->[0] )
){
push @auth_subfields, $subfield->[0] => $subfield->[1];
Expand Down
34 changes: 16 additions & 18 deletions C4/Heading.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ C4::Heading
=head1 SYNOPSIS
use C4::Heading;
my $heading = C4::Heading->new_from_bib_field($field, $frameworkcode);
my $heading = C4::Heading->new_from_field($field, $frameworkcode);
my $thesaurus = $heading->thesaurus();
my $type = $heading->type();
my $display_heading = $heading->display_form();
Expand All @@ -46,40 +46,38 @@ headings found in bibliographic and authority records.
=head1 METHODS
=head2 new_from_bib_field
=head2 new_from_field
my $heading = C4::Heading->new_from_bib_field($field, $frameworkcode, [, $marc_flavour]);
my $heading = C4::Heading->new_from_field($field, $frameworkcode, [, $auth]);
Given a C<MARC::Field> object containing a heading from a
bib record, create a C<C4::Heading> object.
The optional second parameter is the MARC flavour (i.e., MARC21
or UNIMARC); if this parameter is not supplied, it is
taken from the Koha application context.
The optional third parameter is 'auth' - it is handled as boolean. If supplied we treat the field as an auth record field. Otherwise if it is a bib field. The fields checked are the same in a UNIMARC system and this parameter is ignored
If the MARC field supplied is not a valid heading, undef
is returned.
=cut

sub new_from_bib_field {
sub new_from_field {
my $class = shift;
my $field = shift;
my $frameworkcode = shift;
my $marcflavour = @_ ? shift : C4::Context->preference('marcflavour');

my $frameworkcode = shift; #FIXME this is not used?
my $auth = shift;
my $marcflavour = C4::Context->preference('marcflavour');
my $marc_handler = _marc_format_handler($marcflavour);

my $tag = $field->tag();
return unless $marc_handler->valid_bib_heading_tag( $tag, $frameworkcode );
return unless $marc_handler->valid_heading_tag( $tag, $frameworkcode, $auth );
my $self = {};

$self->{'field'} = $field;
(
$self->{'auth_type'}, $self->{'thesaurus'},
$self->{'search_form'}, $self->{'display_form'},
$self->{'match_type'}
) = $marc_handler->parse_heading($field);
) = $marc_handler->parse_heading($field, $auth );

bless $self, $class;
return $self;
Expand Down Expand Up @@ -170,20 +168,20 @@ sub preferred_authorities {
return $results;
}

=head2 valid_bib_heading_subfield
=head2 valid_heading_subfield
if (C4::Heading::valid_bib_heading_subfield('100', 'e', '')) ...
if (C4::Heading::valid_heading_subfield('100', 'e', '')) ...
=cut

sub valid_bib_heading_subfield {
sub valid_heading_subfield {
my $tag = shift;
my $subfield = shift;
my $marcflavour = @_ ? shift : C4::Context->preference('marcflavour');
my $marcflavour = C4::Context->preference('marcflavour');
my $auth = shift;

my $marc_handler = _marc_format_handler($marcflavour);

return $marc_handler->valid_bib_heading_subfield( $tag, $subfield );
return $marc_handler->valid_heading_subfield( $tag, $subfield, $auth );
}

=head1 INTERNAL METHODS
Expand Down
69 changes: 61 additions & 8 deletions C4/Heading/MARC21.pm
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,49 @@ my $bib_heading_fields = {
{ auth_type => 'UNIF_TITLE', subfields => 'adfghklmnoprst', series => 1 },
};

my $auth_heading_fields = {
'100' => {
auth_type => 'PERSO_NAME',
subfields => 'abcdfghjklmnopqrstvxyz',
main_entry => 1
},
'110' => {
auth_type => 'CORPO_NAME',
subfields => 'abcdfghklmnoprstvxyz',
main_entry => 1
},
'111' => {
auth_type => 'MEETI_NAME',
subfields => 'acdfghjklnpqstvxyz',
main_entry => 1
},
'130' => {
auth_type => 'UNIF_TITLE',
subfields => 'adfghklmnoprstvxyz',
main_entry => 1
},
'148' => {
auth_type => 'CHRON_TERM',
subfields => 'avxyz',
main_entry => 1
},
'150' => {
auth_type => 'TOPIC_TERM',
subfields => 'abgvxyz',
main_entry => 1
},
'151' => {
auth_type => 'GEOG_NAME',
subfields => 'agvxyz',
main_entry => 1
},
'155' => {
auth_type => 'GENRE/FORM',
subfields => 'agvxyz',
main_entry => 1
}
};

=head2 subdivisions
=cut
Expand All @@ -143,16 +186,18 @@ sub new {
return bless {}, $class;
}

=head2 valid_bib_heading_tag
=head2 valid_heading_tag
=cut

sub valid_bib_heading_tag {
sub valid_heading_tag {
my $self = shift;
my $tag = shift;
my $frameworkcode = shift;
my $auth = shift;
my $heading_fields = $auth ? { %$auth_heading_fields } : { %$bib_heading_fields };

if ( exists $bib_heading_fields->{$tag} ) {
if ( exists $heading_fields->{$tag} ) {
return 1;
}
else {
Expand All @@ -161,32 +206,40 @@ sub valid_bib_heading_tag {

}

=head2 valid_bib_heading_subfield
=head2 valid_heading_subfield
=cut

sub valid_bib_heading_subfield {
sub valid_heading_subfield {
my $self = shift;
my $tag = shift;
my $subfield = shift;
my $auth = shift;

my $heading_fields = $auth ? { %$auth_heading_fields } : { %$bib_heading_fields };

if ( exists $bib_heading_fields->{$tag} ) {
return 1 if ($bib_heading_fields->{$tag}->{subfields} =~ /$subfield/);
if ( exists $heading_fields->{$tag} ) {
return 1 if ($heading_fields->{$tag}->{subfields} =~ /$subfield/);
}
return 0;
}

=head2 parse_heading
Given a field and an indicator to specify if it is an authority field or biblio field we return
the correct type, thesauarus, search form, and display form of the heading.
=cut

sub parse_heading {
my $self = shift;
my $field = shift;
my $auth = shift;

my $tag = $field->tag;
my $field_info = $bib_heading_fields->{$tag};
my $heading_fields = $auth ? { %$auth_heading_fields } : { %$bib_heading_fields };

my $field_info = $heading_fields->{$tag};
my $auth_type = $field_info->{'auth_type'};
my $thesaurus =
$tag =~ m/6../
Expand Down
8 changes: 4 additions & 4 deletions C4/Heading/UNIMARC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,20 @@ sub new {
return bless {}, $class;
}

=head2 valid_bib_heading_tag
=head2 valid_heading_tag
=cut

sub valid_bib_heading_tag {
sub valid_heading_tag {
my ( $self, $tag ) = @_;
return $bib_heading_fields->{$tag};
}

=head2 valid_bib_heading_subfield
=head2 valid_heading_subfield
=cut

sub valid_bib_heading_subfield {
sub valid_heading_subfield {
my $self = shift;
my $tag = shift;
my $subfield = shift;
Expand Down
2 changes: 1 addition & 1 deletion C4/Linker/Default.pm
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ sub get_link {
map { $_->[0] => $_->[1] } @subfields
);
( $authid, $fuzzy ) =
$self->get_link( C4::Heading->new_from_bib_field($field),
$self->get_link( C4::Heading->new_from_field($field),
$behavior );
}
}
Expand Down
16 changes: 15 additions & 1 deletion Koha/SearchEngine/Elasticsearch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use Koha::Exceptions::Config;
use Koha::Exceptions::Elasticsearch;
use Koha::SearchFields;
use Koha::SearchMarcMaps;
use C4::Heading;

use Carp;
use Clone qw(clone);
Expand Down Expand Up @@ -470,7 +471,6 @@ sub marc_records_to_documents {
}

my $data_field_rules = $data_fields_rules->{$tag};

if ($data_field_rules) {
my $subfields_mappings = $data_field_rules->{subfields};
my $wildcard_mappings = $subfields_mappings->{'*'};
Expand All @@ -483,6 +483,13 @@ sub marc_records_to_documents {
if (@{$mappings}) {
$self->_process_mappings($mappings, $data, $record_document, $altscript);
}
if ( defined @{$mappings}[0] && grep /match-heading/, @{@{$mappings}[0]} ){
# Used by the authority linker the match-heading field requires a specific syntax
# that is specified in C4/Heading
my $heading = C4::Heading->new_from_field( $field, undef, 1 ); #new auth heading
next unless $heading;
push @{$record_document->{'match-heading'}}, $heading->search_form;
}
}

my $subfields_join_mappings = $data_field_rules->{subfields_join};
Expand All @@ -499,6 +506,13 @@ sub marc_records_to_documents {
if ($data) {
$self->_process_mappings($subfields_join_mappings->{$subfields_group}, $data, $record_document, $altscript);
}
if ( grep { $_->[0] eq 'match-heading' } @{$subfields_join_mappings->{$subfields_group}} ){
# Used by the authority linker the match-heading field requires a specific syntax
# that is specified in C4/Heading
my $heading = C4::Heading->new_from_field( $field, undef, 1 ); #new auth heading
next unless $heading;
push @{$record_document->{'match-heading'}}, $heading->search_form;
}
}
}
}
Expand Down
Loading

0 comments on commit d4783f2

Please sign in to comment.