Bug 11175: (QA follow-up) Take account of bug 15851

We can simplify the code introduced by bug 15851 by moving the
'show_analytics_link' variable assignment into C4::XSLT and thus making
the code more DRY.

Taking the code in bug 15851 as inspiration this patch also adds proper
handling for UseControlNumber vs EasyAnalytics style 773 linking and
ensures we only return analytic component parts and no other records
containing 773's.

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Andrew Nugged <nugged@gmail.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Martin Renvoize 2021-08-03 14:29:27 +01:00 committed by Jonathan Druart
parent acdbbcd40c
commit 37c7b9358d
6 changed files with 74 additions and 102 deletions

View File

@ -284,31 +284,37 @@ sub XSLTParse4Display {
my $partsxml = '';
# possibly insert component records into Detail views
if ( $xslsyspref eq "OPACXSLTDetailsDisplay" || $xslsyspref eq "XSLTDetailsDisplay" ) {
my $biblio = Koha::Biblios->find( $biblionumber );
my $components = $biblio->get_marc_components(300);
$variables->{show_analytics_link} = ( scalar @{$components} == 0 ) ? 0 : 1;
my $showcomp = C4::Context->preference('ShowComponentRecords');
if ( $showcomp eq 'both' ||
($showcomp eq 'staff' && $xslsyspref !~ m/OPAC/ ) ||
($showcomp eq 'opac' && $xslsyspref =~ m/OPAC/ ) ) {
my $biblio = Koha::Biblios->find( $biblionumber );
my $max_results = 300;
if (
$variables->{show_analytics_link}
&& ( $showcomp eq 'both'
|| ( $showcomp eq 'staff' && $xslsyspref !~ m/OPAC/ )
|| ( $showcomp eq 'opac' && $xslsyspref =~ m/OPAC/ ) )
)
{
if ( $biblio->get_marc_components($max_results) ) {
my $search_query = Koha::Util::Search::get_component_part_query($biblionumber);
$variables->{ComponentPartQuery} = $search_query;
$variables->{show_analytics_link} = 0;
my @componentPartRecordXML = ('<componentPartRecords>');
for my $cb ( @{ $biblio->get_marc_components($max_results) } ) {
if( ref $cb eq 'MARC::Record'){
$cb = $cb->as_xml_record();
} else {
$cb = decode('utf8', $cb);
}
# Remove the xml header
$cb =~ s/^<\?xml.*?\?>//;
push @componentPartRecordXML,$cb;
}
push @componentPartRecordXML, '</componentPartRecords>';
$partsxml = join "\n", @componentPartRecordXML;
}
my $search_query = Koha::Util::Search::get_component_part_query($biblionumber);
$variables->{ComponentPartQuery} = $search_query;
my @componentPartRecordXML = ('<componentPartRecords>');
for my $cb ( @{ $components } ) {
if( ref $cb eq 'MARC::Record'){
$cb = $cb->as_xml_record();
} else {
$cb = decode('utf8', $cb);
}
# Remove the xml header
$cb =~ s/^<\?xml.*?\?>//;
push @componentPartRecordXML,$cb;
}
push @componentPartRecordXML, '</componentPartRecords>';
$partsxml = join "\n", @componentPartRecordXML;
}
}

View File

@ -491,7 +491,7 @@ this object (MARC21 773$w points to this)
sub get_marc_components {
my ($self, $max_results) = @_;
return if (C4::Context->preference('marcflavour') ne 'MARC21');
return [] if (C4::Context->preference('marcflavour') ne 'MARC21');
my $searchstr = Koha::Util::Search::get_component_part_query($self->id);
@ -501,7 +501,7 @@ sub get_marc_components {
$self->{_components} = $results if ( defined($results) && scalar(@$results) );
}
return $self->{_components} || ();
return $self->{_components} || [];
}
=head3 subscriptions

View File

@ -19,7 +19,7 @@ package Koha::Util::Search;
use Modern::Perl;
use C4::Biblio;
use C4::Biblio qw( GetMarcBiblio );
=head1 NAME
@ -36,22 +36,38 @@ Returns a query which can be used to search for all component parts of MARC21 bi
sub get_component_part_query {
my ($biblionumber) = @_;
my $marc = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
my $pf001 = $marc->field('001') || undef;
my $marc = GetMarcBiblio( { biblionumber => $biblionumber } );
if (defined($pf001)) {
my $pf003 = $marc->field('003') || undef;
my $searchstr;
my $searchstr;
if ( C4::Context->preference('UseControlNumber') ) {
my $pf001 = $marc->field('001') || undef;
if (!defined($pf003)) {
# search for 773$w='Host001'
$searchstr = "rcn=\"".$pf001->data()."\"";
} else {
# search for (773$w='Host001' and 003='Host003') or 773$w='Host003 Host001')
$searchstr = "(rcn=\"".$pf001->data()."\" AND cni=\"".$pf003->data()."\")";
$searchstr .= " OR rcn=\"".$pf003->data()." ".$pf001->data()."\"";
if ( defined($pf001) ) {
my $pf003 = $marc->field('003') || undef;
if ( !defined($pf003) ) {
# search for 773$w='Host001'
$searchstr = "rcn:" . $pf001->data();
}
else {
$searchstr = "(";
# search for (773$w='Host001' and 003='Host003') or 773$w='Host003 Host001')
$searchstr .= "(rcn:" . $pf001->data() . " AND cni:" . $pf003->data() . ")";
$searchstr .= " OR rcn:" . $pf003->data() . " " . $pf001->data();
$searchstr .= ")";
}
# limit to monograph and serial component part records
$searchstr .= " AND (bib-level:a OR bib-level:b)";
}
}
else {
my $cleaned_title = $marc->title;
$cleaned_title =~ tr|/||;
$searchstr = "Host-item:($cleaned_title)";
}
return $searchstr;
}
1;

View File

@ -126,38 +126,6 @@ my $marcflavour = C4::Context->preference("marcflavour");
{
# XSLT processing of some stuff
my $searcher = Koha::SearchEngine::Search->new(
{ index => $Koha::SearchEngine::BIBLIOS_INDEX }
);
my $builder = Koha::SearchEngine::QueryBuilder->new(
{ index => $Koha::SearchEngine::BIBLIOS_INDEX } );
my $cleaned_title = $biblio->title;
$cleaned_title =~ tr|/||;
$cleaned_title = $builder->clean_search_term($cleaned_title);
my $query =
( C4::Context->preference('UseControlNumber') and $record->field('001') )
? 'rcn:'. $record->field('001')->data . ' AND (bib-level:a OR bib-level:b)'
: "Host-item:($cleaned_title)";
my ( $err, $result, $count );
eval {
( $err, $result, $count ) =
$searcher->simple_search_compat( $query, 0, 0 );
};
if ($err || $@){
my $error = q{};
$error .= $err if $err;
$error .= $@ if $@;
warn "Warning from simple_search_compat: $error";
$template->param( analytics_error => 1 );
}
my $variables = {
show_analytics_link => defined $count && $count > 0 ? 1 : 0
};
$template->param(
XSLTDetailsDisplay => '1',
XSLTBloc => XSLTParse4Display(
@ -166,7 +134,6 @@ my $marcflavour = C4::Context->preference("marcflavour");
record => $record,
xsl_syspref => "XSLTDetailsDisplay",
fix_amps => 1,
xslt_variables => $variables
}
),
);

View File

@ -195,37 +195,8 @@ my $marcflavour = C4::Context->preference("marcflavour");
my $ean = GetNormalizedEAN( $record, $marcflavour );
{
my $searcher = Koha::SearchEngine::Search->new(
{ index => $Koha::SearchEngine::BIBLIOS_INDEX }
);
my $builder = Koha::SearchEngine::QueryBuilder->new(
{ index => $Koha::SearchEngine::BIBLIOS_INDEX }
);
my $cleaned_title = $biblio->title;
$cleaned_title =~ tr|/||;
$cleaned_title = $builder->clean_search_term($cleaned_title);
my $query =
( C4::Context->preference('UseControlNumber') and $record->field('001') )
? 'rcn:'. $record->field('001')->data . ' AND (bib-level:a OR bib-level:b)'
: "Host-item:($cleaned_title)";
my ( $err, $result, $count );
eval {
( $err, $result, $count ) =
$searcher->simple_search_compat( $query, 0, 0 );
};
if ($err || $@){
my $error = q{};
$error .= $err if $err;
$error .= $@ if $@;
warn "Warning from simple_search_compat: $error";
}
my $variables = {
anonymous_session => ($borrowernumber) ? 0 : 1,
show_analytics_link => defined $count && $count > 0 ? 1 : 0
};
my $lang = C4::Languages::getlanguage();

View File

@ -20,23 +20,35 @@
use Modern::Perl;
use Test::More tests => 1;
use t::lib::Mocks;
use t::lib::TestBuilder;
use C4::Biblio;
use C4::Biblio qw( GetMarcBiblio ModBiblioMarc );
use Koha::Util::Search;
use MARC::Field;
my $builder = t::lib::TestBuilder->new;
subtest 'get_component_part_query' => sub {
plan tests => 1;
plan tests => 3;
my $biblio = $builder->build_sample_biblio();
my $biblionumber = $biblio->biblionumber;
my $record = GetMarcBiblio({ biblionumber => $biblionumber });
t::lib::Mocks::mock_preference( 'UseControlNumber', '0' );
is(Koha::Util::Search::get_component_part_query($biblionumber), "Host-item:(Some boring read)", "UseControlNumber disabled");
t::lib::Mocks::mock_preference( 'UseControlNumber', '1' );
my $marc_001_field = MARC::Field->new('001', $biblionumber);
$record->append_fields($marc_001_field);
ModBiblioMarc($record, $biblionumber);
is(Koha::Util::Search::get_component_part_query($biblionumber), "rcn=\"$biblionumber\"");
is(Koha::Util::Search::get_component_part_query($biblionumber), "rcn:$biblionumber AND (bib-level:a OR bib-level:b)", "UseControlNumber enabled without MarcOrgCode");
my $marc_003_field = MARC::Field->new('003', 'OSt');
$record->append_fields($marc_003_field);
ModBiblioMarc($record, $biblionumber);
is(Koha::Util::Search::get_component_part_query($biblionumber), "((rcn:$biblionumber AND cni:OSt) OR rcn:OSt $biblionumber) AND (bib-level:a OR bib-level:b)", "UseControlNumber enabled with MarcOrgCode");
};