Koha/svc/cataloguing/metasearch
Nick Clemens 7bbf4f7b5f Bug 17515: Order Z3950 server by rank and preserve ordering
Previously we put all the servers into an object with keys of the server id

This patch converts it to an array of objects to preserve order, and adjusts code to use the array index
where necessary and store the server id within the array

To test:
1 - Add some new Z3950 servers, they don't need to be valid
    FIRST
    SECOND
    THIRD
    FOURTH
2 - Adjust the ranking so FOURTH:1 THIRD:2 SECOND:3 FIRST:4
3 - Enable and launch the advanced editor
4 - Click 'Advanced' under search on the left
5 - Note the list displays in the order you entered the servers
6 - Apply patch
7 - Reload
8 - Order is correct
9 - With valid servers, confirm that searching still works and servers can be checked or unchecked to include/remove from results

https://bugs.koha-community.org/show_bug.cgi?id=17515

Signed-off-by: B Johnson <barbara.johnson@bedfordtx.gov>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2020-11-02 11:03:09 +01:00

77 lines
2.2 KiB
Perl
Executable file

#!/usr/bin/perl
#
# Copyright 2014 ByWater Solutions
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use C4::Service;
use Encode qw( encode_utf8 );
use Koha::MetaSearcher;
my ( $query, $response ) = C4::Service->init( catalogue => 1 );
my ( $query_string, $servers ) = C4::Service->require_params( 'q', 'servers' );
my $server_errors = {};
my $sort_key = $query->param( 'sort_key' ) || 'title';
my $sort_direction = $query->param( 'sort_direction' ) || 'asc';
my $offset = $query->param( 'offset' ) || 0;
my $page_size = $query->param( 'page_size' ) || 20;
my $fetched = $query->param( 'fetched' ) || 100;
my $searcher = Koha::MetaSearcher->new( {
fetched => $fetched,
on_error => sub {
my ( $server, $exception ) = @_;
$server_errors->{ $server->{id} } = $exception->message;
},
} );
$searcher->resultset( $query->param('resultset') ) if ( $query->param('resultset') );
my @server_ids = split( /,/, $servers );
my $stats = $searcher->search( \@server_ids, $query_string );
$searcher->sort( $sort_key, $sort_direction eq 'desc' ? -1 : 1 );
my @hits;
foreach my $hit ( $searcher->results( $offset, $page_size ) ) {
push @hits, {
server => $hit->{server}->{id},
servername => $hit->{server}->{servername},
index => $hit->{index},
record => $hit->{record}->as_xml_record(),
metadata => $hit->{metadata}
};
}
$response->param(
resultset => $searcher->resultset,
sort_key => $sort_key,
sort_direction => $sort_direction,
offset => $offset,
page_size => $page_size,
errors => $server_errors,
hits => \@hits,
%$stats
);
C4::Service->return_success( $response );