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
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.
25 use C4
::Auth qw
/check_api_auth/;
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);
43 if ($query->request_method eq "POST") {
44 $xml = $query->param('POSTDATA');
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);
52 print $query->header(-type
=> 'text/xml', -status
=> '400 Bad Request');
58 my ($inxml, $params) = @_;
62 my $import_mode = delete $params->{import_mode
} || '';
63 my $framework = delete $params->{framework
} || '';
65 if (my $matcher_code = delete $params->{matcher
}) {
66 $params->{matcher_id
} = C4
::Matcher
::GetMatcherId
($matcher_code);
69 my $batch_id = GetWebserviceBatchId
($params);
71 $result->{'status'} = "failed";
72 $result->{'error'} = "Batch create error";
76 my $marcflavour = C4
::Context
->preference('marcflavour') || 'MARC21';
77 my $marc_record = eval {MARC
::Record
::new_from_xml
( $inxml, "utf8", $marcflavour)};
79 $result->{'status'} = "failed";
80 $result->{'error'} = $@
;
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);
88 $result->{'status'} = "failed";
89 $result->{'error'} = "database write error";
92 $marcxml =~ s/<\?xml.*?\?>//i;
94 # XXX we are ignoring the result of this;
95 BatchCommitBibRecords
($batch_id, $framework) if lc($import_mode) eq 'direct';
97 $result->{'status'} = "ok";
98 $result->{'import_batch_id'} = $batch_id;
99 $result->{'marcxml'} = $marcxml;