Bug 22043: SIP Checkin Response alert flag set to often set to Y incorrectly
[koha.git] / svc / import_bib
blob8577036ed9f4e6a2cd688d944fdbd814f5e7646e
1 #!/usr/bin/perl
3 # Copyright 2012 CatalystIT Ltd
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Modern::Perl;
23 use CGI qw ( -utf8 );
24 use C4::Auth qw/check_api_auth/;
25 use C4::Context;
26 use C4::ImportBatch;
27 use C4::Matcher;
28 use XML::Simple;
29 # use Carp::Always;
31 my $query = new CGI;
32 binmode STDOUT, ':encoding(UTF-8)';
34 my ($status, $cookie, $sessionID) = check_api_auth($query, { editcatalogue => 'edit_catalogue'} );
35 unless ($status eq "ok") {
36 print $query->header(-type => 'text/xml', -status => '403 Forbidden');
37 print XMLout({ auth_status => $status }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
38 exit 0;
41 my $xml;
42 if ($query->request_method eq "POST") {
43 $xml = $query->param('xml');
45 if ($xml) {
46 my %params = map { $_ => scalar $query->param($_) } $query->param;
47 my $result = import_bib($xml, \%params );
48 print $query->header(-type => 'text/xml');
49 print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1);
50 } else {
51 print $query->header(-type => 'text/xml', -status => '400 Bad Request');
54 exit 0;
56 sub import_bib {
57 my ($inxml, $params) = @_;
59 my $result = {};
61 my $import_mode = delete $params->{import_mode} || '';
62 my $framework = delete $params->{framework} || '';
64 if (my $matcher_code = delete $params->{match}) {
65 $params->{matcher_id} = C4::Matcher::GetMatcherId($matcher_code);
68 my $batch_id = GetWebserviceBatchId($params);
69 unless ($batch_id) {
70 $result->{'status'} = "failed";
71 $result->{'error'} = "Batch create error";
72 return $result;
75 my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
76 my $marc_record = eval {MARC::Record::new_from_xml( $inxml, "utf8", $marcflavour)};
77 if ($@) {
78 $result->{'status'} = "failed";
79 $result->{'error'} = $@;
80 return $result;
83 my $import_record_id = AddBiblioToBatch($batch_id, 0, $marc_record, "utf8", int(rand(99999)));
84 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 'UPDATE COUNTS');
86 my $matcher = C4::Matcher->new($params->{record_type} || 'biblio');
87 $matcher = C4::Matcher->fetch($params->{matcher_id});
88 my $number_of_matches = BatchFindDuplicates($batch_id, $matcher);
90 # XXX we are ignoring the result of this;
91 BatchCommitRecords($batch_id, $framework) if lc($import_mode) eq 'direct';
93 my $dbh = C4::Context->dbh();
94 my $sth = $dbh->prepare("SELECT matched_biblionumber FROM import_biblios WHERE import_record_id =?");
95 $sth->execute($import_record_id);
96 my $biblionumber=$sth->fetchrow_arrayref->[0] || '';
97 $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id =?");
98 $sth->execute($import_record_id);
99 my $match_status = $sth->fetchrow_arrayref->[0] || 'no_match';
100 my $url = C4::Context->preference('staffClientBaseURL') .'/cgi-bin/koha/catalogue/detail.pl?biblionumber='. $biblionumber;
102 $result->{'status'} = "ok";
103 $result->{'import_batch_id'} = $batch_id;
104 $result->{'match_status'} = $match_status;
105 $result->{'biblionumber'} = $biblionumber;
106 $result->{'url'} = $url;
107 return $result;