From aef09e15bff1bc95fb79cadc358dd67166738239 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 | 86 ++++++++++++++++++++++++++++++++++++++++++ basket/basket.pl | 3 +- basket/sendbasket.pl | 3 +- opac/opac-basket.pl | 3 +- opac/opac-detail.pl | 2 +- opac/opac-sendbasket.pl | 3 +- opac/opac-sendshelf.pl | 3 +- t/Biblio.t | 8 +--- t/db_dependent/Koha/Biblio.t | 30 ++++++++++++++- virtualshelves/sendshelf.pl | 3 +- 11 files changed, 129 insertions(+), 105 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 6e53842ee1..6829c3d535 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -35,7 +35,6 @@ BEGIN { GetMarcNotes GetMarcISBN GetMarcISSN - GetMarcSubjects GetMarcAuthors GetMarcSeries GetMarcUrls @@ -1605,95 +1604,6 @@ sub GetMarcNotes { return \@marcnotes; } -=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/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 ( $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 GetMarcAuthors authors = GetMarcAuthors($record,$marcflavour); diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 4d335e2c23..033fb61241 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -793,6 +793,92 @@ sub cover_images { return Koha::CoverImages->_new_from_dbic($cover_images_rs); } +=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, { + 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 aa4cd97dcf..6a741ae2e9 100755 --- a/basket/basket.pl +++ b/basket/basket.pl @@ -61,10 +61,11 @@ foreach my $biblionumber ( @bibs ) { my $dat = &GetBiblioData($biblionumber); next unless $dat; + my $biblio = Koha::Biblios->find( $biblionumber ); my $record = &GetMarcBiblio({ biblionumber => $biblionumber }); my $marcnotesarray = GetMarcNotes( $record, $marcflavour ); my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour ); - my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour ); + my $marcsubjctsarray = $biblio->get_marc_subjects({ marcflavour => $marcflavour }); my $marcseriesarray = GetMarcSeries ($record,$marcflavour); my $marcurlsarray = GetMarcUrls ($record,$marcflavour); my @items = GetItemsInfo( $biblionumber ); diff --git a/basket/sendbasket.pl b/basket/sendbasket.pl index 9e273b1f45..21d35b24b6 100755 --- a/basket/sendbasket.pl +++ b/basket/sendbasket.pl @@ -69,11 +69,12 @@ if ( $email_add ) { my $dat = GetBiblioData($biblionumber); next unless $dat; + my $biblio = Koha::Biblios->find( $biblionumber ); my $record = GetMarcBiblio({ biblionumber => $biblionumber, embed_items => 1 }); my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour ); - my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour ); + my $marcsubjctsarray = $biblio->get_marc_subjects({ marcflavour => $marcflavour }); my @items = GetItemsInfo( $biblionumber ); diff --git a/opac/opac-basket.pl b/opac/opac-basket.pl index 14c44c4bee..dee2ffa176 100755 --- a/opac/opac-basket.pl +++ b/opac/opac-basket.pl @@ -81,9 +81,10 @@ foreach my $biblionumber ( @bibs ) { }); $record_processor->process($record); next unless $record; + my $biblio = Koha::Biblios->find( $biblionumber ); my $marcnotesarray = GetMarcNotes( $record, $marcflavour, 1 ); my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour ); - 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 8b935af155..865b5ec9e6 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -803,7 +803,7 @@ if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) { if (!C4::Context->preference("OPACXSLTDetailsDisplay") ) { my $marcisbnsarray = GetMarcISBN ($record,$marcflavour); my $marcauthorsarray = GetMarcAuthors ($record,$marcflavour); - 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-sendbasket.pl b/opac/opac-sendbasket.pl index eee5e2c29b..1f1dd920a6 100755 --- a/opac/opac-sendbasket.pl +++ b/opac/opac-sendbasket.pl @@ -77,13 +77,14 @@ if ( $email_add ) { my $dat = GetBiblioData($biblionumber); next unless $dat; + my $biblio = Koha::Biblios->find( $biblionumber ); my $record = GetMarcBiblio({ biblionumber => $biblionumber, embed_items => 1, opac => 1, borcat => $borcat }); my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour ); - my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour ); + my $marcsubjctsarray = $biblio->get_marc_subjects({ marcflavour => $marcflavour }); my @items = GetItemsInfo( $biblionumber ); diff --git a/opac/opac-sendshelf.pl b/opac/opac-sendshelf.pl index c97aa450dc..c529cbee5c 100755 --- a/opac/opac-sendshelf.pl +++ b/opac/opac-sendshelf.pl @@ -86,11 +86,12 @@ if ( $email ) { opac => 1, borcat => $borcat }); next unless $record; + my $biblio = Koha::Biblios->find( $biblionumber ); my $fw = GetFrameworkCode($biblionumber); my $dat = GetBiblioData($biblionumber); my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour ); - my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour ); + my $marcsubjctsarray = $biblio->get_marc_subjects({ marcflavour => $marcflavour }); my @items = GetItemsInfo( $biblionumber ); diff --git a/t/Biblio.t b/t/Biblio.t index 5596b4bf19..4fdab71d94 100755 --- a/t/Biblio.t +++ b/t/Biblio.t @@ -21,7 +21,7 @@ use Test::More; use Test::MockModule; use Test::Warn; -plan tests => 41; +plan tests => 39; use_ok('C4::Biblio'); @@ -93,12 +93,6 @@ warning_is { $ret = GetMarcNotes() } ok( !defined $ret, 'GetMarcNotes 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 = GetMarcAuthors() } { carped => 'GetMarcAuthors called on undefined record'}, "GetMarcAuthors returns carped warning on undef record"; diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 718b69ebf5..56454c3ab1 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 => 12; +use Test::More tests => 13; use C4::Biblio; use Koha::Database; @@ -551,3 +551,31 @@ subtest 'subscriptions() 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.'); + $record->append_fields($field); + + # get record + C4::Biblio::ModBiblio( $record, $biblio->biblionumber ); + $biblio = Koha::Biblios->find( $biblio->biblionumber ); + + is( 4, @{$biblio->get_marc_subjects({ marcflavour => 'MARC21' })}, 'get_marc_subjects retrieves correct number of subject subfields' ); + + $schema->storage->txn_rollback; +}; diff --git a/virtualshelves/sendshelf.pl b/virtualshelves/sendshelf.pl index f15e2c9f64..6eb988cca7 100755 --- a/virtualshelves/sendshelf.pl +++ b/virtualshelves/sendshelf.pl @@ -67,12 +67,13 @@ if ($to_address) { while ( my $content = $contents->next ) { my $biblionumber = $content->biblionumber; + my $biblio = Koha::Biblios->find( $biblionumber ); my $dat = GetBiblioData($biblionumber); my $record = GetMarcBiblio({ biblionumber => $biblionumber, embed_items => 1 }); my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour ); - my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour ); + my $marcsubjctsarray = $biblio->get_marc_subjects({ marcflavour => $marcflavour }); my @items = GetItemsInfo($biblionumber); -- 2.11.0