From 78520e8876988ebe54a97bb2998629a3cb002c8e 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) Signed-off-by: Owen Leonard --- C4/Biblio.pm | 90 ------------------- Koha/Biblio.pm | 87 ++++++++++++++++++ basket/basket.pl | 3 +- basket/sendbasket.pl | 3 - .../mysql/en/mandatory/sample_notices.yml | 2 + opac/opac-basket.pl | 3 +- opac/opac-detail.pl | 3 +- opac/opac-sendbasket.pl | 3 - opac/opac-sendshelf.pl | 1 - t/Biblio.t | 10 +-- t/db_dependent/Koha/Biblio.t | 31 ++++++- virtualshelves/sendshelf.pl | 1 - 12 files changed, 125 insertions(+), 112 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index cc88b20871..fbb7a9cda2 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -33,7 +33,6 @@ BEGIN { GetMarcControlnumber GetMarcISBN GetMarcISSN - GetMarcSubjects GetMarcSeries GetMarcUrls GetUsedMarcStructure @@ -1560,95 +1559,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 50d60c8a69..e2b40d10de 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -1218,6 +1218,93 @@ sub get_marc_authors { return [@first_authors, @other_authors]; } +=head3 get_marc_subjects + + $marcsubjcts = $biblio->get_marc_subjects; + +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 = C4::Context->preference('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 4220f0a629..bf87f16e6b 100755 --- a/basket/basket.pl +++ b/basket/basket.pl @@ -21,7 +21,6 @@ use CGI qw ( -utf8 ); use C4::Koha; use C4::Biblio qw( GetMarcSeries - GetMarcSubjects GetMarcUrls ); use C4::Auth qw( get_template_and_user ); @@ -68,7 +67,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; my $marcseriesarray = GetMarcSeries ($record,$marcflavour); my $marcurlsarray = GetMarcUrls ($record,$marcflavour); diff --git a/basket/sendbasket.pl b/basket/sendbasket.pl index 3d5438fb42..173fb52c46 100755 --- a/basket/sendbasket.pl +++ b/basket/sendbasket.pl @@ -22,9 +22,6 @@ use Encode; use Carp qw( carp ); use Try::Tiny qw( catch try ); -use C4::Biblio qw( - GetMarcSubjects -); use C4::Auth qw( get_template_and_user ); use C4::Output qw( output_and_exit output_html_with_http_headers ); use C4::Templates; diff --git a/installer/data/mysql/en/mandatory/sample_notices.yml b/installer/data/mysql/en/mandatory/sample_notices.yml index 15adee5090..59e7e777aa 100644 --- a/installer/data/mysql/en/mandatory/sample_notices.yml +++ b/installer/data/mysql/en/mandatory/sample_notices.yml @@ -2315,6 +2315,7 @@ tables: - "[% IF ( biblioitem.publishercode ) %]Published by: [% biblioitem.publishercode | html %][% IF ( biblioitem.publicationyear ) %] in [% biblioitem.publicationyear | html %][% END %][% IF ( biblioitem.pages ) %], [% biblioitem.pages | html %][% END %]
[% END %]" - "[% IF ( biblio.seriestitle ) %]Collection: [% biblio.seriestitle | html %]
[% END %]" - "[% IF ( biblio.copyrightdate ) %]Copyright year: [% biblio.copyrightdate | html %]
[% END %]" + - "[% IF ( biblio.get_marc_subjects ) %]Subject(s): [% FOREACH subject IN biblio.get_marc_subjects %][% FOREACH subfield IN subject.MARCSUBJECT_SUBFIELDS_LOOP %][% subfield.separator | html %][% subfield.value | html %][% END %][% UNLESS ( loop.last ) %];[% END %][% END %][% END %]" - "[% IF ( biblio.notes ) %]Notes: [% biblio.notes | html %]
[% END %]" - "[% IF ( biblio.unititle ) %]Unified title: [% biblio.unititle | html %]
[% END %]" - "[% IF ( biblio.serial ) %]Serial: [% biblio.serial | html %]
[% END %]" @@ -2351,6 +2352,7 @@ tables: - "[% IF ( biblioitem.publishercode ) %]Published by: [% biblioitem.publishercode | html %][% IF ( biblioitem.publicationyear ) %] in [% biblioitem.publicationyear | html %][% END %][% IF ( biblioitem.pages ) %], [% biblioitem.pages | html %][% END %]
[% END %]" - "[% IF ( biblio.seriestitle ) %]Collection: [% biblio.seriestitle | html %]
[% END %]" - "[% IF ( biblio.copyrightdate ) %]Copyright year: [% biblio.copyrightdate | html %]
[% END %]" + - "[% IF ( biblio.get_marc_subjects ) %]Subject(s): [% FOREACH subject IN biblio.get_marc_subjects %][% FOREACH subfield IN subject.MARCSUBJECT_SUBFIELDS_LOOP %][% subfield.separator | html %][% subfield.value | html %][% END %][% UNLESS ( loop.last ) %];[% END %][% END %][% END %]" - "[% IF ( biblio.notes ) %]Notes: [% biblio.notes | html %]
[% END %]" - "[% IF ( biblio.unititle ) %]Unified title: [% biblio.unititle | html %]
[% END %]" - "[% IF ( biblio.serial ) %]Serial: [% biblio.serial | html %]
[% END %]" diff --git a/opac/opac-basket.pl b/opac/opac-basket.pl index e4ed044a85..7a5a2ef28e 100755 --- a/opac/opac-basket.pl +++ b/opac/opac-basket.pl @@ -23,7 +23,6 @@ use C4::Koha; use C4::Biblio qw( GetFrameworkCode GetMarcSeries - GetMarcSubjects GetMarcUrls ); use C4::Auth qw( get_template_and_user ); @@ -85,7 +84,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; my $marcseriesarray = GetMarcSeries ($record,$marcflavour); my $marcurlsarray = GetMarcUrls ($record,$marcflavour); diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl index 116694e267..414aa765bb 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -42,7 +42,6 @@ use C4::Biblio qw( GetMarcISBN GetMarcISSN GetMarcSeries - GetMarcSubjects GetMarcUrls ); use C4::Tags qw( get_tags ); @@ -778,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; if( C4::Context->preference('ArticleRequests') ) { my $patron = $borrowernumber ? Koha::Patrons->find($borrowernumber) : undef; @@ -793,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 ae0ec81e1c..4735ff5123 100755 --- a/opac/opac-sendbasket.pl +++ b/opac/opac-sendbasket.pl @@ -24,9 +24,6 @@ use Encode; use Carp qw( carp ); use Try::Tiny qw( catch try ); -use C4::Biblio qw( - GetMarcSubjects -); use C4::Auth qw( get_template_and_user ); use C4::Output qw( output_html_with_http_headers ); use C4::Templates; diff --git a/opac/opac-sendshelf.pl b/opac/opac-sendshelf.pl index a90d2ca5f4..12b4873662 100755 --- a/opac/opac-sendshelf.pl +++ b/opac/opac-sendshelf.pl @@ -28,7 +28,6 @@ use C4::Auth qw( get_template_and_user ); use C4::Biblio qw( GetFrameworkCode GetMarcISBN - GetMarcSubjects ); use C4::Output qw( output_html_with_http_headers ); use Koha::Biblios; diff --git a/t/Biblio.t b/t/Biblio.t index c298349e18..bff4bf40eb 100755 --- a/t/Biblio.t +++ b/t/Biblio.t @@ -21,10 +21,10 @@ 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 )); +use_ok('C4::Biblio', qw( AddBiblio ModBiblio BiblioAutoLink LinkBibHeadingsToAuthorities GetMarcPrice GetMarcQuantity GetMarcControlnumber GetMarcISBN GetMarcISSN GetMarcUrls GetMarcSeries TransformMarcToKoha ModBiblioMarc RemoveAllNsb UpdateTotalIssues )); my $db = Test::MockModule->new('Koha::Database'); $db->mock( _new_schema => sub { return Schema(); } ); @@ -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 ef41c7636a..3069d4e4db 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 => 23; # +1 +use Test::More tests => 24; # +1 use Test::Exception; use Test::Warn; @@ -1088,6 +1088,35 @@ 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; + t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' ); + + # 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->id ); + $biblio = Koha::Biblios->find( $biblio->id ); + + is( @{$biblio->get_marc_subjects}, 4, '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 3a2738c2d8..82238c9665 100755 --- a/virtualshelves/sendshelf.pl +++ b/virtualshelves/sendshelf.pl @@ -27,7 +27,6 @@ use Try::Tiny qw( catch try ); use C4::Auth qw( get_template_and_user ); use C4::Biblio qw( GetMarcISBN - GetMarcSubjects ); use C4::Output qw( output_html_with_http_headers -- 2.30.2