From 178e0bdfc8934154164cfbe79e2a5299b991086c Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Fri, 18 Dec 2020 15:43:57 +1300 Subject: [PATCH] Bug 27269: Move GetMarcSubjects to Koha namespace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch moves C4::Biblio::GetMarcSubjects to Koha::Biblio->get_marc_subjects. This is so get_marc_subjects can be used in templates and notices. To test: 1. Find a record that has a subject (6xx). 2. Add the record to the cart and a list. 3. View your cart and click 'more details'. Confirm subjects show as normal. 4. Click 'send' and confirm the email sent shows the subjects as normal. 5. Go to the list you added the record to and click 'send list'. Confirm the email sent shows the ubjectsas normal. 6. Set the OPACXSLTDetailsDisplay system preference to empty (for no xslt) 7. Log in to the OPAC. Find the record and add it to the cart and a list 8. View the cart and click 'more details'. Confirm subjects show as normal. 9. Click 'send' and confirm the email sent shows the subjects as normal. 10. Go to the list you added the record to and click 'send list'. Confirm the email sent shows the subjects as normal. 11. View the record detail page and confirm the subjects show as normal. 12. Confirm tests pass: - t/Biblio.t - t/db_dependent/Koha/Biblio.t Sponsored-by: Bibliotheksservice-Zentrum Baden-Württemberg (BSZ) --- C4/Biblio.pm | 90 -------------------------------------------- Koha/Biblio.pm | 87 ++++++++++++++++++++++++++++++++++++++++++ basket/basket.pl | 2 +- basket/sendbasket.pl | 2 +- opac/opac-basket.pl | 2 +- opac/opac-detail.pl | 2 + opac/opac-sendbasket.pl | 2 +- opac/opac-sendshelf.pl | 3 +- t/Biblio.t | 8 +--- t/db_dependent/Koha/Biblio.t | 24 +++++++++++- virtualshelves/sendshelf.pl | 2 +- 11 files changed, 120 insertions(+), 104 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 9790febdaa2..24251676191 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -33,7 +33,6 @@ BEGIN { GetMarcControlnumber GetMarcISBN GetMarcISSN - GetMarcSubjects GetMarcSeries GetMarcUrls GetUsedMarcStructure @@ -1499,95 +1498,6 @@ sub GetMarcISSN { return \@marcissns; } # end GetMarcISSN -=head2 GetMarcSubjects - - $marcsubjcts = GetMarcSubjects($record,$marcflavour); - -Get all subjects from the MARC record and returns them in an array. -The subjects are stored in different fields depending on MARC flavour - -=cut - -sub GetMarcSubjects { - my ( $record, $marcflavour ) = @_; - if (!$record) { - carp 'GetMarcSubjects called on undefined record'; - return; - } - my ( $mintag, $maxtag, $fields_filter ); - if ( $marcflavour eq "UNIMARC" ) { - $mintag = "600"; - $maxtag = "611"; - $fields_filter = '6..'; - } else { # marc21 - $mintag = "600"; - $maxtag = "699"; - $fields_filter = '6..'; - } - - my @marcsubjects; - - my $subject_limit = C4::Context->preference("TraceCompleteSubfields") ? 'su,complete-subfield' : 'su'; - my $AuthoritySeparator = C4::Context->preference('AuthoritySeparator'); - - foreach my $field ( $record->field($fields_filter) ) { - next unless ($field->tag() >= $mintag && $field->tag() <= $maxtag); - my @subfields_loop; - my @subfields = $field->subfields(); - my @link_loop; - - # if there is an authority link, build the links with an= subfield9 - my $subfield9 = $field->subfield('9'); - my $authoritylink; - if ($subfield9) { - my $linkvalue = $subfield9; - $linkvalue =~ s/(\(|\))//g; - @link_loop = ( { limit => 'an', 'link' => $linkvalue } ); - $authoritylink = $linkvalue - } - - # other subfields - for my $subject_subfield (@subfields) { - next if ( $subject_subfield->[0] eq '9' ); - - # don't load unimarc subfields 3,4,5 - next if ( ( $marcflavour eq "UNIMARC" ) and ( $subject_subfield->[0] =~ /2|3|4|5/ ) ); - # don't load MARC21 subfields 2 (FIXME: any more subfields??) - next if ( ( $marcflavour eq "MARC21" ) and ( $subject_subfield->[0] =~ /2/ ) ); - - my $code = $subject_subfield->[0]; - my $value = $subject_subfield->[1]; - my $linkvalue = $value; - $linkvalue =~ s/(\(|\))//g; - # if no authority link, build a search query - unless ($subfield9) { - push @link_loop, { - limit => $subject_limit, - 'link' => $linkvalue, - operator => (scalar @link_loop) ? ' AND ' : undef - }; - } - my @this_link_loop = @link_loop; - # do not display $0 - unless ( $code eq '0' ) { - push @subfields_loop, { - code => $code, - value => $value, - link_loop => \@this_link_loop, - separator => (scalar @subfields_loop) ? $AuthoritySeparator : '' - }; - } - } - - push @marcsubjects, { - MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop, - authoritylink => $authoritylink, - } if $authoritylink || @subfields_loop; - - } - return \@marcsubjects; -} #end getMARCsubjects - =head2 GetMarcUrls $marcurls = GetMarcUrls($record,$marcflavour); diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 5bf369cd6a1..1443e98d991 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -1162,6 +1162,93 @@ sub get_marc_authors { return [@first_authors, @other_authors]; } +=head3 get_marc_subjects + + $marcsubjcts = $biblio->get_marc_subjects({ marcflavour => $marcflavour }); + +Get all subjects from the MARC record and returns them in an array. +The subjects are stored in different fields depending on MARC flavour + +=cut + +sub get_marc_subjects { + my ( $self, $params ) = @_; + my ( $mintag, $maxtag, $fields_filter ); + + my $marcflavour = $params->{marcflavour}; + if ( $marcflavour eq "UNIMARC" ) { + $mintag = "600"; + $maxtag = "611"; + $fields_filter = '6..'; + } else { # marc21/normarc + $mintag = "600"; + $maxtag = "699"; + $fields_filter = '6..'; + } + + my @marcsubjects; + + my $subject_limit = C4::Context->preference("TraceCompleteSubfields") ? 'su,complete-subfield' : 'su'; + my $AuthoritySeparator = C4::Context->preference('AuthoritySeparator'); + + foreach my $field ( $self->metadata->record->field($fields_filter) ) { + next unless ($field->tag() >= $mintag && $field->tag() <= $maxtag); + my @subfields_loop; + my @subfields = $field->subfields(); + my @link_loop; + + # if there is an authority link, build the links with an= subfield9 + my $subfield9 = $field->subfield('9'); + my $authoritylink; + if ($subfield9) { + my $linkvalue = $subfield9; + $linkvalue =~ s/(\(|\))//g; + @link_loop = ( { limit => 'an', 'link' => $linkvalue } ); + $authoritylink = $linkvalue + } + + # other subfields + for my $subject_subfield (@subfields) { + next if ( $subject_subfield->[0] eq '9' ); + + # don't load unimarc subfields 3,4,5 + next if ( ( $marcflavour eq "UNIMARC" ) and ( $subject_subfield->[0] =~ /2|3|4|5/ ) ); + # don't load MARC21 subfields 2 (FIXME: any more subfields??) + next if ( ( $marcflavour eq "MARC21" ) and ( $subject_subfield->[0] =~ /2/ ) ); + + my $code = $subject_subfield->[0]; + my $value = $subject_subfield->[1]; + my $linkvalue = $value; + $linkvalue =~ s/(\(|\))//g; + # if no authority link, build a search query + unless ($subfield9) { + push @link_loop, { + limit => $subject_limit, + 'link' => $linkvalue, + operator => (scalar @link_loop) ? ' and ' : undef + }; + } + my @this_link_loop = @link_loop; + # do not display $0 + unless ( $code eq '0') { + push @subfields_loop, { + tag => $field->tag(), + code => $code, + value => $value, + link_loop => \@this_link_loop, + separator => (scalar @subfields_loop) ? $AuthoritySeparator : '' + }; + } + } + + push @marcsubjects, { + MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop, + authoritylink => $authoritylink, + } if $authoritylink || @subfields_loop; + + } + return \@marcsubjects; +} =head3 to_api diff --git a/basket/basket.pl b/basket/basket.pl index 54fabe44d48..4bd0bd69849 100755 --- a/basket/basket.pl +++ b/basket/basket.pl @@ -68,7 +68,7 @@ foreach my $biblionumber ( @bibs ) { my $record = $biblio->metadata->record; my $marcnotesarray = $biblio->get_marc_notes; my $marcauthorsarray = $biblio->get_marc_contributors; - my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour ); + my $marcsubjctsarray = $biblio->get_marc_subjects({ marcflavour => $marcflavour }); my $marcseriesarray = GetMarcSeries ($record,$marcflavour); my $marcurlsarray = GetMarcUrls ($record,$marcflavour); diff --git a/basket/sendbasket.pl b/basket/sendbasket.pl index af47302d5c2..b40505a7f7d 100755 --- a/basket/sendbasket.pl +++ b/basket/sendbasket.pl @@ -73,7 +73,7 @@ if ( $email_add ) { my $dat = $biblio->unblessed; my $record = $biblio->metadata->record({ embed_items => 1 }); my $marcauthorsarray = $biblio->get_marc_contributors; - my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour ); + my $marcsubjctsarray = $biblio->get_marc_subjects({ marcflavour => $marcflavour }); my $hasauthors = 0; if($dat->{'author'} || @$marcauthorsarray) { diff --git a/opac/opac-basket.pl b/opac/opac-basket.pl index 31096332b6a..b72f009172b 100755 --- a/opac/opac-basket.pl +++ b/opac/opac-basket.pl @@ -85,7 +85,7 @@ foreach my $biblionumber ( @bibs ) { next unless $record; my $marcnotesarray = $biblio->get_marc_notes({ opac => 1 }); my $marcauthorsarray = $biblio->get_marc_contributors; - my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour ); + my $marcsubjctsarray = $biblio->get_marc_subjects({ marcflavour => $marcflavour }); my $marcseriesarray = GetMarcSeries ($record,$marcflavour); my $marcurlsarray = GetMarcUrls ($record,$marcflavour); diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl index 98c4f94a8cb..6377252d402 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -777,6 +777,7 @@ if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) { } my $marcnotesarray = $biblio->get_marc_notes({ opac => 1, record => $record }); +my $marcsubjctsarray = $biblio->get_marc_subjects({ marcflavour => $marcflavour }); if( C4::Context->preference('ArticleRequests') ) { my $patron = $borrowernumber ? Koha::Patrons->find($borrowernumber) : undef; @@ -792,6 +793,7 @@ if( C4::Context->preference('ArticleRequests') ) { my $norequests = ! $biblio->items->filter_by_for_hold->count; $template->param( MARCNOTES => $marcnotesarray, + MARCSUBJCTS => $marcsubjctsarray, norequests => $norequests, itemdata_ccode => $itemfields{ccode}, itemdata_materials => $itemfields{materials}, diff --git a/opac/opac-sendbasket.pl b/opac/opac-sendbasket.pl index 278cfd34a6a..07654a53c01 100755 --- a/opac/opac-sendbasket.pl +++ b/opac/opac-sendbasket.pl @@ -83,7 +83,7 @@ if ( $email_add ) { } ); my $marcauthorsarray = $biblio->get_marc_contributors; - my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour ); + my $marcsubjctsarray = $biblio->get_marc_subjects({ marcflavour => $marcflavour }); my $items = $biblio->items->search_ordered->filter_by_visible_in_opac({ patron => $patron }); diff --git a/opac/opac-sendshelf.pl b/opac/opac-sendshelf.pl index 3d3d9232eda..5cff877782e 100755 --- a/opac/opac-sendshelf.pl +++ b/opac/opac-sendshelf.pl @@ -89,10 +89,11 @@ if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) { } ); next unless $record; + my $biblio = Koha::Biblios->find( $biblionumber ); my $fw = GetFrameworkCode($biblionumber); my $marcauthorsarray = $biblio->get_marc_contributors; - my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour ); + my $marcsubjctsarray = $biblio->get_marc_subjects({ marcflavour => $marcflavour }); my $items = $biblio->items->search_ordered->filter_by_visible_in_opac({ patron => $patron }); diff --git a/t/Biblio.t b/t/Biblio.t index c298349e18f..19732ea23ea 100755 --- a/t/Biblio.t +++ b/t/Biblio.t @@ -21,7 +21,7 @@ use Test::More; use Test::MockModule; use Test::Warn; -plan tests => 34; +plan tests => 32; use_ok('C4::Biblio', qw( AddBiblio ModBiblio BiblioAutoLink LinkBibHeadingsToAuthorities GetMarcPrice GetMarcQuantity GetMarcControlnumber GetMarcISBN GetMarcISSN GetMarcSubjects GetMarcUrls GetMarcSeries TransformMarcToKoha ModBiblioMarc RemoveAllNsb UpdateTotalIssues )); @@ -88,12 +88,6 @@ warning_is { $ret = GetMarcISSN() } ok( !defined $ret, 'GetMarcISSN returns undef if not passed rec'); -warning_is { $ret = GetMarcSubjects() } - { carped => 'GetMarcSubjects called on undefined record'}, - "GetMarcSubjects returns carped warning on undef record"; - -ok( !defined $ret, 'GetMarcSubjects returns undef if not passed rec'); - warning_is { $ret = GetMarcUrls() } { carped => 'GetMarcUrls called on undefined record'}, "GetMarcUrls returns carped warning on undef record"; diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 67513cd09fd..a649a0c62b8 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 22; # +1 +use Test::More tests => 23; # +1 use Test::Warn; use C4::Biblio qw( AddBiblio ModBiblio ModBiblioMarc ); @@ -1053,6 +1053,28 @@ subtest 'item_groups() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'get_marc_subjects() tests' => sub { + + plan tests => 1; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio; + my $record = $biblio->metadata->record; + + # add author information + my $field = MARC::Field->new('600','1','0','a' => 'Nixon, Richard M.'); + $record->append_fields($field); + $field = MARC::Field->new('600','1','0','q' => '(Richard Milhouse),'); + $record->append_fields($field); + $field = MARC::Field->new('600','1','0','d' => '1913-'); + $record->append_fields($field); + $field = MARC::Field->new('600','1','0','x' => 'Psychology.'); + is( 4, @{$biblio->get_marc_subjects({ marcflavour => 'MARC21' })}, 'get_marc_subjects retrieves correct number of subject subfields' ); + + $schema->storage->txn_rollback; +}; + sub component_record1 { my $marc = MARC::Record->new; $marc->append_fields( diff --git a/virtualshelves/sendshelf.pl b/virtualshelves/sendshelf.pl index d3514159a69..57b589bbc62 100755 --- a/virtualshelves/sendshelf.pl +++ b/virtualshelves/sendshelf.pl @@ -80,7 +80,7 @@ if ($to_address) { my $dat = $biblio->unblessed; my $record = $biblio->metadata->record({ embed_items => 1 }); my $marcauthorsarray = $biblio->get_marc_contributors; - my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour ); + my $marcsubjctsarray = $biblio->get_marc_subjects({ marcflavour => $marcflavour }); my $items = $biblio->items->search_ordered; -- 2.11.0