12ff7355bb
svc/import_bib: * takes POST request with parameters in url and MARC XML as DATA * pushes MARC XML to an impoort bach queue of type 'webservice' * returns status and imported record XML * is a drop-in replacement for svc/new_bib misc/cronjobs/import_webservice_batch.pl: * a cron job for processing impoort bach queues of type 'webservice' * batches can also be processed through the UI misc/bin/connexion_import_daemon.pl: * a daemon that listens for OCLC Connexion requests and is compliant with OCLC Gateway spec * takes request with MARC XML * takes import batch params from a config file and forwards the lot to svc/import_bib * returns status ImportBatches: * Added new import batch type of 'webservice' * Changed interface to AddImportBatch() - now it takes a hashref * Replaced batch_type = 'batch' with batch_type IN ( 'batch', 'webservice' ) in some SELECTs Signed-off-by: MJ Ray <mjr@phonecoop.coop>
101 lines
3.2 KiB
Perl
Executable file
101 lines
3.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 2 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, write to the Free Software Foundation, Inc.,
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
#
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
use CGI;
|
|
use C4::Auth qw/check_api_auth/;
|
|
use C4::Context;
|
|
use C4::ImportBatch;
|
|
use C4::Matcher;
|
|
use XML::Simple;
|
|
# use Carp::Always;
|
|
|
|
my $query = new CGI;
|
|
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('POSTDATA');
|
|
}
|
|
if ($xml) {
|
|
my %params = map { $_ => $query->url_param($_) } $query->url_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} || '';
|
|
|
|
if (my $matcher_code = delete $params->{matcher}) {
|
|
$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, "utf8", $marcflavour)};
|
|
if ($@) {
|
|
$result->{'status'} = "failed";
|
|
$result->{'error'} = $@;
|
|
return $result;
|
|
}
|
|
|
|
my $import_record_id = AddBiblioToBatch($batch_id, 0, $marc_record, "utf8", int(rand(99999)));
|
|
my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 'UPDATE COUNTS');
|
|
my $marcxml = GetImportRecordMarcXML($import_record_id);
|
|
unless ($marcxml) {
|
|
$result->{'status'} = "failed";
|
|
$result->{'error'} = "database write error";
|
|
return $result;
|
|
}
|
|
$marcxml =~ s/<\?xml.*?\?>//i;
|
|
|
|
# XXX we are ignoring the result of this;
|
|
BatchCommitBibRecords($batch_id, $framework) if lc($import_mode) eq 'direct';
|
|
|
|
$result->{'status'} = "ok";
|
|
$result->{'import_batch_id'} = $batch_id;
|
|
$result->{'marcxml'} = $marcxml;
|
|
return $result;
|
|
}
|