Koha 24.05 is here!
[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     my $overlay_framework = $params->{overlay_framework};
64
65     if (my $matcher_code = delete $params->{match}) {
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, "UTF-8", $marcflavour)};
78     if ($@) {
79         $result->{'status'} = "failed";
80         $result->{'error'} = $@;
81         return $result;
82     }
83     if(C4::Context->preference('autoControlNumber') eq 'biblionumber'){
84         my @control_num = $marc_record->field('001');
85         $marc_record->delete_fields(@control_num);
86     }
87
88     my $import_record_id = AddBiblioToBatch($batch_id, 0, $marc_record, "utf8", 1);
89     my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 'UPDATE COUNTS');
90
91     my $matcher = C4::Matcher->new($params->{record_type} || 'biblio');
92     $matcher = C4::Matcher->fetch($params->{matcher_id});
93     my $number_of_matches =  BatchFindDuplicates($batch_id, $matcher);
94
95     # XXX we are ignoring the result of this;
96     BatchCommitRecords(
97         {
98             batch_id          => $batch_id,
99             framework         => $framework,
100             overlay_framework => $overlay_framework
101         }
102     ) if lc($import_mode) eq 'direct';
103
104     my $dbh = C4::Context->dbh();
105     my $sth = $dbh->prepare("SELECT matched_biblionumber FROM import_biblios WHERE import_record_id =?");
106     $sth->execute($import_record_id);
107     my $biblionumber=$sth->fetchrow_arrayref->[0] || '';
108     $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id =?");
109     $sth->execute($import_record_id);
110     my $match_status = $sth->fetchrow_arrayref->[0] || 'no_match';
111     my $url = C4::Context->preference('staffClientBaseURL') .'/cgi-bin/koha/catalogue/detail.pl?biblionumber='. $biblionumber;
112
113     $result->{'status'} = "ok";
114     $result->{'import_batch_id'} = $batch_id;
115     $result->{'match_status'} = $match_status;
116     $result->{'biblionumber'} = $biblionumber;
117     $result->{'url'} = $url;
118     return $result;
119 }