From 8b12141609e5e8d29c99e0347fb24ac45b70d144 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 | 88 ++++++++++++++++++ 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 | 32 ++++++- virtualshelves/sendshelf.pl | 1 - 12 files changed, 127 insertions(+), 112 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index ab895b25753..128503c3aa7 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -33,7 +33,6 @@ BEGIN { GetMarcControlnumber GetMarcISBN GetMarcISSN - GetMarcSubjects GetMarcSeries GetMarcUrls GetUsedMarcStructure @@ -1577,95 +1576,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 7d0b2440856..74f4188c78f 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -1582,6 +1582,94 @@ sub normalized_oclc { return $self->metadata_extractor->get_normalized_oclc; } +=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 my $json = $biblio->to_api; diff --git a/basket/basket.pl b/basket/basket.pl index 4220f0a629f..bf87f16e6bd 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 e863497d32b..202a03a3255 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 9675b3eb4f1..60d932a3e6f 100644 --- a/installer/data/mysql/en/mandatory/sample_notices.yml +++ b/installer/data/mysql/en/mandatory/sample_notices.yml @@ -2380,6 +2380,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 %]" @@ -2416,6 +2417,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 e4ed044a859..7a5a2ef28ec 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 b9c644e38d7..9a8716945c1 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 ); @@ -780,6 +779,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; @@ -795,6 +795,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 d0ff9fc53a6..c3e344aec05 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 e08b474cb15..b1a32e6beb3 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 c298349e18f..bff4bf40eb8 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 13be09f4662..04ef6345dec 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 => 30; +use Test::More tests => 31; use Test::Exception; use Test::Warn; @@ -1171,6 +1171,7 @@ subtest 'item_groups() tests' => sub { $schema->storage->txn_rollback; }; +<<<<<<< HEAD subtest 'normalized_isbn' => sub { plan tests => 1; @@ -1235,6 +1236,35 @@ subtest 'opac_summary_html' => sub { sprintf( 'Replace %s, %s, %s AND %s please', $author, $title, $biblio->normalized_isbn, $biblio->biblionumber ), 'opac_summary_html replaces the different patterns' ); +======= +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; +>>>>>>> Bug 27269: Move GetMarcSubjects to Koha namespace }; sub component_record1 { diff --git a/virtualshelves/sendshelf.pl b/virtualshelves/sendshelf.pl index e50f3c3cd35..c239130bf38 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