Bug 16214: Fix typo 'To.jon' -> 'To.json'
[koha.git] / svc / import_bib
blobb02e5f2671114f16009ccb74e9ae24029ffb40dd
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 strict;
22 use warnings;
24 use CGI qw ( -utf8 );
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;