Merge remote-tracking branch 'origin/new/bug_7781'
[koha.git] / svc / import_bib
1 #!/usr/bin/perl
2
3 # Copyright 2012 CatalystIT Ltd
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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.
19 #
20
21 use strict;
22 use warnings;
23
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;
31
32 my $query = new CGI;
33 binmode STDOUT, ':encoding(UTF-8)';
34
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;
40 }
41
42 my $xml;
43 if ($query->request_method eq "POST") {
44     $xml = $query->param('POSTDATA');
45 }
46 if ($xml) {
47     my %params = map { $_ => $query->url_param($_) } $query->url_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');
53 }
54
55 exit 0;
56
57 sub import_bib {
58     my ($inxml, $params) = @_;
59
60     my $result = {};
61
62     my $import_mode = delete $params->{import_mode} || '';
63     my $framework   = delete $params->{framework}   || '';
64
65     if (my $matcher_code = delete $params->{matcher}) {
66         $params->{matcher_id} = C4::Matcher::GetMatcherId($matcher_code);
67     }
68
69     my $batch_id = GetWebserviceBatchId($params);
70     unless ($batch_id) {
71         $result->{'status'} = "failed";
72         $result->{'error'} = "Batch create error";
73         return $result;
74     }
75
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;
82     }
83
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');
86     my $marcxml = GetImportRecordMarcXML($import_record_id);
87     unless ($marcxml) {
88         $result->{'status'} = "failed";
89         $result->{'error'} = "database write error";
90         return $result;
91     }
92     $marcxml =~ s/<\?xml.*?\?>//i;
93
94     # XXX we are ignoring the result of this;
95     BatchCommitBibRecords($batch_id, $framework) if lc($import_mode) eq 'direct';
96
97     $result->{'status'} = "ok";
98     $result->{'import_batch_id'} =  $batch_id;
99     $result->{'marcxml'} =  $marcxml;
100     return $result;
101 }