Merge branch 'bug_10106' into 3.14-master
[koha.git] / svc / import_bib
blobf93428d71461731c6535fbb198412b4941975243
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 use strict;
22 use warnings;
24 use CGI;
25 use C4::Auth qw/check_api_auth/;
26 use C4::Context;
27 use C4::ImportBatch;
28 use C4::Matcher;
29 use XML::Simple;
30 # use Carp::Always;
32 my $query = new CGI;
33 binmode STDOUT, ':encoding(UTF-8)';
35 my ($status, $cookie, $sessionID) = check_api_auth($query, { editcatalogue => 'edit_catalogue'} );
36 unless ($status eq "ok") {
37 print $query->header(-type => 'text/xml', -status => '403 Forbidden');
38 print XMLout({ auth_status => $status }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
39 exit 0;
42 my $xml;
43 if ($query->request_method eq "POST") {
44 $xml = $query->param('xml');
46 if ($xml) {
47 my %params = map { $_ => $query->param($_) } $query->param;
48 my $result = import_bib($xml, \%params );
49 print $query->header(-type => 'text/xml');
50 print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1);
51 } else {
52 print $query->header(-type => 'text/xml', -status => '400 Bad Request');
55 exit 0;
57 sub import_bib {
58 my ($inxml, $params) = @_;
60 my $result = {};
62 my $import_mode = delete $params->{import_mode} || '';
63 my $framework = delete $params->{framework} || '';
65 if (my $matcher_code = delete $params->{match}) {
66 $params->{matcher_id} = C4::Matcher::GetMatcherId($matcher_code);
69 my $batch_id = GetWebserviceBatchId($params);
70 unless ($batch_id) {
71 $result->{'status'} = "failed";
72 $result->{'error'} = "Batch create error";
73 return $result;
76 my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
77 my $marc_record = eval {MARC::Record::new_from_xml( $inxml, "utf8", $marcflavour)};
78 if ($@) {
79 $result->{'status'} = "failed";
80 $result->{'error'} = $@;
81 return $result;
84 my $import_record_id = AddBiblioToBatch($batch_id, 0, $marc_record, "utf8", int(rand(99999)));
85 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 'UPDATE COUNTS');
87 my $matcher = C4::Matcher->new($params->{record_type} || 'biblio');
88 $matcher = C4::Matcher->fetch($params->{matcher_id});
89 my $number_of_matches = BatchFindDuplicates($batch_id, $matcher);
91 # XXX we are ignoring the result of this;
92 BatchCommitRecords($batch_id, $framework) if lc($import_mode) eq 'direct';
94 my $dbh = C4::Context->dbh();
95 my $sth = $dbh->prepare("SELECT matched_biblionumber FROM import_biblios WHERE import_record_id =?");
96 $sth->execute($import_record_id);
97 my $biblionumber=$sth->fetchrow_arrayref->[0] || '';
98 $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id =?");
99 $sth->execute($import_record_id);
100 my $match_status = $sth->fetchrow_arrayref->[0] || 'no_match';
101 my $url = 'http://'. C4::Context->preference('staffClientBaseURL') .'/cgi-bin/koha/catalogue/detail.pl?biblionumber='. $biblionumber;
103 $result->{'status'} = "ok";
104 $result->{'import_batch_id'} = $batch_id;
105 $result->{'match_status'} = $match_status;
106 $result->{'biblionumber'} = $biblionumber;
107 $result->{'url'} = $url;
108 return $result;