From 06dc3ddfe706e9e7f6ce99d794da9c1148ff2f2b Mon Sep 17 00:00:00 2001 From: Paul Derscheid Date: Fri, 19 Aug 2022 11:26:55 +0200 Subject: [PATCH] Bug 25870 - (follow-up) Add a q_ccl query parameter to /biblios I refactored the controller code but unfortunately still haven't figured out why Koha::Biblios->search doesn't work properly. For the time being I still use Koha::Biblios->find. That aside I think it has become much cleaner now. To test: 1) Apply the patch 2) Pick an endpoint tester of your choice, e.g. Insomnia or the ThunderClient if you use VSCode or derivatives. 3) Run a query while using Zebra. 4) Observe the marc-in-json response and check for validity. 5) Run a query while using Elasticsearch. 6) Again, observe the marc-in-json response and check for validity. 7) Not ready for sign-off but please leave a comment or help me on the Koha::Biblios->search thing. --- Koha/REST/V1/Biblios.pm | 176 ++++++++++++++-------------------------- 1 file changed, 62 insertions(+), 114 deletions(-) diff --git a/Koha/REST/V1/Biblios.pm b/Koha/REST/V1/Biblios.pm index 115a6a1df6..1b45f8078b 100644 --- a/Koha/REST/V1/Biblios.pm +++ b/Koha/REST/V1/Biblios.pm @@ -28,6 +28,8 @@ use List::MoreUtils qw( any ); use MARC::Record::MiJ; use Try::Tiny qw( catch try ); +use JSON; +use Data::Dumper; =head1 API @@ -43,12 +45,14 @@ sub get { my $c = shift->openapi->valid_input or return; my $attributes; - $attributes = { prefetch => [ 'metadata' ] } # don't prefetch metadata if not needed - unless $c->req->headers->accept =~ m/application\/json/; + $attributes = + { prefetch => ['metadata'] } # don't prefetch metadata if not needed + unless $c->req->headers->accept =~ m/application\/json/; - my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, $attributes ); + my $biblio = Koha::Biblios->find( + { biblionumber => $c->validation->param('biblio_id') }, $attributes ); - unless ( $biblio ) { + unless ($biblio) { return $c->render( status => 404, openapi => { @@ -92,10 +96,8 @@ sub get { any => { status => 406, openapi => [ - "application/json", - "application/marcxml+xml", - "application/marc-in-json", - "application/marc", + "application/json", "application/marcxml+xml", + "application/marc-in-json", "application/marc", "text/plain" ] } @@ -154,103 +156,38 @@ sub get_biblios_public { my $c = shift->openapi->valid_input or return; - my $requested_content_type = $c->req->headers->header('Accept'); my $searcher = Koha::SearchEngine::Search->new( { index => 'biblios' } ); my $query = $c->validation->param('q_ccl'); - my $record_processor = Koha::RecordProcessor->new( - { - filters => 'ViewPolicy', - options => { - interface => 'opac', - } - } - ); - my ( $error, $results, $total_hits ) = - $searcher->simple_search_compat( $query, 0, undef ); + warn Dumper($query); + + my ( $error, $results, $total_hits ) = $searcher->simple_search_compat( $query, 0, undef ); if ( !$total_hits ) { return $c->render( status => 404, - openapi => { - error => 'Nothing found.' - } + openapi => { error => 'Nothing found.' } ); } - sub format_record_by_content_type { - my ($args) = @_; - - if ( $args->{'content_type'} eq 'application/marc-in-json' ) { - for my $record ( @{ $args->{'records'} } ) { - $record = $record->to_mij; - } - return ( q{[} . ( join q{,}, @{ $args->{'records'} } ) . q{]} ); - } - - # Insert additional content types here or above, you know .. alphabetically - } - - sub process_record { - my ($args) = @_; - - my $biblionumber = - $args->{'searcher'}->extract_biblionumber( $args->{'record'} ); - my $biblio = Koha::Biblios->find( { biblionumber => $biblionumber } ); - - if ( - !( - $args->{'patron'} - && $args->{'patron'}->category->override_hidden_items - ) - ) - { - if ( $biblio->hidden_in_opac( { rules => $args->{'rules'} } ) ) { - next; - } - } - - $args->{'processor'}->process( $args->{'record'} ); - - return $args->{'record'}; - } - return try { - my $marcflavour = C4::Context->preference('marcflavour'); - - # Apply framework's filtering to MARC::Record object - - my $patron = $c->stash('koha.user'); - my $opachiddenitems_rules = - C4::Context->yaml_preference('OpacHiddenItems'); - - my @records; - for my $result ( @{$results} ) { - - if ( ref($result) eq q{} ) { next; } - - push @records, - process_record( - { - searcher => $searcher, - processor => $record_processor, - record => $result, - patron => $patron, - rules => $opachiddenitems_rules - } - ); - } - - my $response = - format_record_by_content_type( - { content_type => $requested_content_type, records => \@records } ); + my $patron = $c->stash('koha.user'); + my $is_public = $c->stash('is_public'); + my $opachiddenitems_rules = C4::Context->yaml_preference('OpacHiddenItems'); + my @biblionumbers = map { $_->field('999')->subfield('c') } $results->@*; + my @biblios = map { Koha::Biblios->find( { biblionumber => $_ } ) } @biblionumbers; + my @records = map { + next if ( $is_public && !( $patron && $patron->category->override_hidden_items ) + && $_->hidden_in_opac( { rules => $opachiddenitems_rules } ) ); + $_->metadata->record; + } @biblios; $c->respond_to( mij => { status => 200, format => 'mij', - data => $response + data => '[' . ( join ',', map { $_->to_mij } @records ) . ']', }, any => { status => 406, @@ -263,6 +200,7 @@ sub get_biblios_public { }; } + =head3 get_public Controller function that handles retrieving a single biblio object @@ -289,14 +227,16 @@ sub get_public { my $record = $biblio->metadata->record; - my $opachiddenitems_rules = C4::Context->yaml_preference('OpacHiddenItems'); + my $opachiddenitems_rules = + C4::Context->yaml_preference('OpacHiddenItems'); my $patron = $c->stash('koha.user'); # Check if the biblio should be hidden for unprivileged access # unless there's a logged in user, and there's an exception for it's # category unless ( $patron and $patron->category->override_hidden_items ) { - if ( $biblio->hidden_in_opac({ rules => $opachiddenitems_rules }) ) + if ( + $biblio->hidden_in_opac( { rules => $opachiddenitems_rules } ) ) { return $c->render( status => 404, @@ -309,13 +249,16 @@ sub get_public { my $marcflavour = C4::Context->preference("marcflavour"); - my $record_processor = Koha::RecordProcessor->new({ - filters => 'ViewPolicy', - options => { - interface => 'opac', - frameworkcode => $biblio->frameworkcode + my $record_processor = Koha::RecordProcessor->new( + { + filters => 'ViewPolicy', + options => { + interface => 'opac', + frameworkcode => $biblio->frameworkcode + } } - }); + ); + # Apply framework's filtering to MARC::Record object $record_processor->process($record); @@ -343,10 +286,8 @@ sub get_public { any => { status => 406, openapi => [ - "application/marcxml+xml", - "application/marc-in-json", - "application/marc", - "text/plain" + "application/marcxml+xml", "application/marc-in-json", + "application/marc", "text/plain" ] } ); @@ -365,9 +306,11 @@ Controller function that handles retrieving biblio's items sub get_items { my $c = shift->openapi->valid_input or return; - my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, { prefetch => ['items'] } ); + my $biblio = Koha::Biblios->find( + { biblionumber => $c->validation->param('biblio_id') }, + { prefetch => ['items'] } ); - unless ( $biblio ) { + unless ($biblio) { return $c->render( status => 404, openapi => { @@ -379,7 +322,7 @@ sub get_items { return try { my $items_rs = $biblio->items; - my $items = $c->objects->search( $items_rs ); + my $items = $c->objects->search($items_rs); return $c->render( status => 200, openapi => $items @@ -437,7 +380,7 @@ sub pickup_locations { my $c = shift->openapi->valid_input or return; my $biblio_id = $c->validation->param('biblio_id'); - my $biblio = Koha::Biblios->find( $biblio_id ); + my $biblio = Koha::Biblios->find($biblio_id); unless ($biblio) { return $c->render( @@ -447,7 +390,7 @@ sub pickup_locations { } my $patron_id = delete $c->validation->output->{patron_id}; - my $patron = Koha::Patrons->find( $patron_id ); + my $patron = Koha::Patrons->find($patron_id); unless ($patron) { return $c->render( @@ -463,14 +406,15 @@ sub pickup_locations { my @response = (); if ( C4::Context->preference('AllowHoldPolicyOverride') ) { - my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } ); - my $libraries = $c->objects->search($libraries_rs); + my $libraries_rs = + Koha::Libraries->search( { pickup_location => 1 } ); + my $libraries = $c->objects->search($libraries_rs); @response = map { my $library = $_; $library->{needs_override} = ( any { $_->branchcode eq $library->{library_id} } - @{$pl_set->as_list} + @{ $pl_set->as_list } ) ? Mojo::JSON->false : Mojo::JSON->true; @@ -480,7 +424,8 @@ sub pickup_locations { else { my $pickup_locations = $c->objects->search($pl_set); - @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations}; + @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } + @{$pickup_locations}; } return $c->render( @@ -503,9 +448,11 @@ access. sub get_items_public { my $c = shift->openapi->valid_input or return; - my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, { prefetch => ['items'] } ); + my $biblio = Koha::Biblios->find( + { biblionumber => $c->validation->param('biblio_id') }, + { prefetch => ['items'] } ); - unless ( $biblio ) { + unless ($biblio) { return $c->render( status => 404, openapi => { @@ -518,8 +465,9 @@ sub get_items_public { my $patron = $c->stash('koha.user'); - my $items_rs = $biblio->items->filter_by_visible_in_opac({ patron => $patron }); - my $items = $c->objects->search( $items_rs ); + my $items_rs = + $biblio->items->filter_by_visible_in_opac( { patron => $patron } ); + my $items = $c->objects->search($items_rs); return $c->render( status => 200, openapi => $items @@ -530,4 +478,4 @@ sub get_items_public { }; } -1; +1; \ No newline at end of file -- 2.31.1