Koha/svc/import_bib
Nick Clemens d7f4a93008
Bug 33418: Add overlay_framework option to connexion scripts
This patch adds the option to specify a framework to be used when overlaying records from webservices/connexion

To test:
1 - vim /etc/koha/sites/kohadev/connexion.cnf
2 - Set content:
    host:
    port: 8888
    koha:http://localhost:8081
    log:/var/log/koha/kohadev/connexion.log
    match:ISBN
    user:kohauser
    password:kohapass
    overlay_action:replace
    nomatch_action:create_new
    item_action:always_add
    import_mode:direct
    framework:BKS
    overlay_framework:
    debug:1
3 - Save the sample file from this bug into your kohaclone (or copy and paste into a file your koha test site can reach)
4 - On the command line:
    perl misc/bin/connexion_import_daemon.pl -c /etc/koha/sites/kohadev/connexion.cnf
5 - In another terminal:
    cat bug_33418.test  | nc -v localhost 8888
6 - It should report success and a biblionumber
7 - In Koha: Cataloguing->Manage staged MARC record
8 - View the most recent batch with file name (webservice)
9 - Confirm it was imported, no match
10 - Click 'View' under the 'Record' column
11 - Confirm record loads correctly and 'MARC framework' detail is 'Books, Booklets, Workbooks'
12 - On the terminal repeat the command:
    cat bug_33418.test  | nc -v localhost 8888
13 - It should succed
14 - View the new batch, confirm the record matched this time
15 - View the record details, confirm framework is now 'default'
16 - On the first terminal hit Ctrl+C to stop the daemon
16 - Edit connexion.cnf and change:
    import_mode:stage
    framework:ACQ
    overlay_framework:IR
17 - Restart daemon:
     perl misc/bin/connexion_import_daemon.pl -c /etc/koha/sites/kohadev/connexion.cnf
18 - Delete the record created above
19 - On the second terminal repeat the command:
    cat bug_33418.test  | nc -v localhost 8888
20 - Confirm the batch is created, but not imported
21 - In terminal:
     perl misc/cronjobs/import_webservice_batch.pl --framework=ACQ --overlay_framework=BKS
22 - Confirm batch imported, and record in ACQ framework
23 - In terminal:
     cat bug_33418.test  | nc -v localhost 8888
     perl misc/cronjobs/import_webservice_batch.pl --framework=ACQ --overlay_framework=BKS
24 - Confirm batch added, record matched, record imported, and record now in Books framework
25 - Stop the deamon, edit connexion.cnf:
     import_mode:direct
26 - Start the daemon, and on other terminal repeat:
     cat bug_33418.test  | nc -v localhost 8888
27 - Confirm record in Binders framework
28 - Set record framework to Books
29 - Stop daemon, edit cnf and remove 'overlay_framework' setting
30 - Start daemon and cat the file again
31 - Confirm the record remains in Books framework

Signed-off-by: Brendan Lawlor <blawlor@clamsnet.org>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2024-05-10 16:45:50 +02:00

119 lines
4.2 KiB
Perl
Executable file

#!/usr/bin/perl
# Copyright 2012 CatalystIT Ltd
#
# 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 CGI qw ( -utf8 );
use C4::Auth qw/check_api_auth/;
use C4::Context;
use C4::ImportBatch qw( GetWebserviceBatchId AddBiblioToBatch AddItemsToImportBiblio BatchFindDuplicates BatchCommitRecords );
use C4::Matcher;
use XML::Simple;
# use Carp::Always;
my $query = CGI->new;
binmode STDOUT, ':encoding(UTF-8)';
my ($status, $cookie, $sessionID) = check_api_auth($query, { editcatalogue => 'edit_catalogue'} );
unless ($status eq "ok") {
print $query->header(-type => 'text/xml', -status => '403 Forbidden');
print XMLout({ auth_status => $status }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
exit 0;
}
my $xml;
if ($query->request_method eq "POST") {
$xml = $query->param('xml');
}
if ($xml) {
my %params = map { $_ => scalar $query->param($_) } $query->param;
my $result = import_bib($xml, \%params );
print $query->header(-type => 'text/xml');
print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1);
} else {
print $query->header(-type => 'text/xml', -status => '400 Bad Request');
}
exit 0;
sub import_bib {
my ($inxml, $params) = @_;
my $result = {};
my $import_mode = delete $params->{import_mode} || '';
my $framework = delete $params->{framework} || '';
my $overlay_framework = $params->{overlay_framework};
if (my $matcher_code = delete $params->{match}) {
$params->{matcher_id} = C4::Matcher::GetMatcherId($matcher_code);
}
my $batch_id = GetWebserviceBatchId($params);
unless ($batch_id) {
$result->{'status'} = "failed";
$result->{'error'} = "Batch create error";
return $result;
}
my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
my $marc_record = eval {MARC::Record::new_from_xml( $inxml, "UTF-8", $marcflavour)};
if ($@) {
$result->{'status'} = "failed";
$result->{'error'} = $@;
return $result;
}
if(C4::Context->preference('autoControlNumber') eq 'biblionumber'){
my @control_num = $marc_record->field('001');
$marc_record->delete_fields(@control_num);
}
my $import_record_id = AddBiblioToBatch($batch_id, 0, $marc_record, "utf8", 1);
my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 'UPDATE COUNTS');
my $matcher = C4::Matcher->new($params->{record_type} || 'biblio');
$matcher = C4::Matcher->fetch($params->{matcher_id});
my $number_of_matches = BatchFindDuplicates($batch_id, $matcher);
# XXX we are ignoring the result of this;
BatchCommitRecords(
{
batch_id => $batch_id,
framework => $framework,
overlay_framework => $overlay_framework
}
) if lc($import_mode) eq 'direct';
my $dbh = C4::Context->dbh();
my $sth = $dbh->prepare("SELECT matched_biblionumber FROM import_biblios WHERE import_record_id =?");
$sth->execute($import_record_id);
my $biblionumber=$sth->fetchrow_arrayref->[0] || '';
$sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id =?");
$sth->execute($import_record_id);
my $match_status = $sth->fetchrow_arrayref->[0] || 'no_match';
my $url = C4::Context->preference('staffClientBaseURL') .'/cgi-bin/koha/catalogue/detail.pl?biblionumber='. $biblionumber;
$result->{'status'} = "ok";
$result->{'import_batch_id'} = $batch_id;
$result->{'match_status'} = $match_status;
$result->{'biblionumber'} = $biblionumber;
$result->{'url'} = $url;
return $result;
}