f1119b4543a7978e68797d092db13aacb0a975b5
[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
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.
11 #
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.
16 #
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>.
19 #
20
21 use Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use C4::Auth qw/check_api_auth/;
25 use C4::Context;
26 use C4::ImportBatch qw( GetWebserviceBatchId AddBiblioToBatch AddItemsToImportBiblio BatchFindDuplicates BatchCommitRecords );
27 use C4::Matcher;
28 use XML::Simple;
29 # use Carp::Always;
30
31 my $query = CGI->new;
32 binmode STDOUT, ':encoding(UTF-8)';
33
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;
39 }
40
41 my $xml;
42 if ($query->request_method eq "POST") {
43     $xml = $query->param('xml');
44 }
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');
52 }
53
54 exit 0;
55
56 sub import_bib {
57     my ($inxml, $params) = @_;
58
59     my $result = {};
60
61     my $import_mode = delete $params->{import_mode} || '';
62     my $framework   = delete $params->{framework}   || '';
63
64     if (my $matcher_code = delete $params->{match}) {
65         $params->{matcher_id} = C4::Matcher::GetMatcherId($matcher_code);
66     }
67
68     my $batch_id = GetWebserviceBatchId($params);
69     unless ($batch_id) {
70         $result->{'status'} = "failed";
71         $result->{'error'} = "Batch create error";
72         return $result;
73     }
74
75     my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
76     my $marc_record = eval {MARC::Record::new_from_xml( $inxml, "UTF-8", $marcflavour)};
77     if ($@) {
78         $result->{'status'} = "failed";
79         $result->{'error'} = $@;
80         return $result;
81     }
82     if(C4::Context->preference('autoControlNumber') eq 'biblionumber'){
83         my @control_num = $marc_record->field('001');
84         $marc_record->delete_fields(@control_num);
85     }
86
87     my $import_record_id = AddBiblioToBatch($batch_id, 0, $marc_record, "utf8", 1);
88     my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 'UPDATE COUNTS');
89
90     my $matcher = C4::Matcher->new($params->{record_type} || 'biblio');
91     $matcher = C4::Matcher->fetch($params->{matcher_id});
92     my $number_of_matches =  BatchFindDuplicates($batch_id, $matcher);
93
94     # XXX we are ignoring the result of this;
95     BatchCommitRecords($batch_id, $framework) if lc($import_mode) eq 'direct';
96
97     my $dbh = C4::Context->dbh();
98     my $sth = $dbh->prepare("SELECT matched_biblionumber FROM import_biblios WHERE import_record_id =?");
99     $sth->execute($import_record_id);
100     my $biblionumber=$sth->fetchrow_arrayref->[0] || '';
101     $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id =?");
102     $sth->execute($import_record_id);
103     my $match_status = $sth->fetchrow_arrayref->[0] || 'no_match';
104     my $url = C4::Context->preference('staffClientBaseURL') .'/cgi-bin/koha/catalogue/detail.pl?biblionumber='. $biblionumber;
105
106     $result->{'status'} = "ok";
107     $result->{'import_batch_id'} = $batch_id;
108     $result->{'match_status'} = $match_status;
109     $result->{'biblionumber'} = $biblionumber;
110     $result->{'url'} = $url;
111     return $result;
112 }