Bug 35181: Don't pass undef to header
[koha.git] / svc / bib
1 #!/usr/bin/perl
2
3 # Copyright 2007 LibLime
4 # Copyright 2012 software.coop and MJ Ray
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 #
21
22 use Modern::Perl;
23
24 use CGI qw ( -utf8 );
25 use C4::Auth qw( check_api_auth );
26 use C4::Biblio qw( GetFrameworkCode );
27 use C4::Items;
28 use XML::Simple;
29 use Koha::Biblios;
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 # do initial validation
42 my $path_info = $query->path_info();
43
44 my $biblionumber = undef;
45 if ($path_info =~ m!^/(\d+)$!) {
46     $biblionumber = $1;
47 } else {
48     print $query->header(-type => 'text/xml', -status => '400 Bad Request');
49 }
50
51 # are we retrieving, updating or deleting a bib?
52 if ($query->request_method eq "GET") {
53     fetch_bib($query, $biblionumber);
54 } elsif ($query->request_method eq "POST") {
55     update_bib($query, $biblionumber);
56 } elsif ($query->request_method eq "DELETE") {
57     delete_bib($query, $biblionumber);
58 } else {
59     print $query->header(-type => 'text/xml', -status => '405 Method not allowed');
60     print XMLout({ error => 'Method not allowed' }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
61     exit 0;
62 }
63
64 exit 0;
65
66 sub fetch_bib {
67     my $query = shift;
68     my $biblionumber = shift;
69     my $biblio = Koha::Biblios->find( $biblionumber );
70     my $record;
71     my $exception;
72     my $invalid_metadata = 0;
73     eval { $record = $biblio->metadata->record( { embed_items => scalar $query->param('items') } ) };
74     if ($@) {
75         $exception = $@;
76         $exception->rethrow unless ( $exception->isa('Koha::Exceptions::Metadata::Invalid') );
77         $record           = $biblio->metadata->record_strip_nonxml( { embed_items => scalar $query->param('items') } );
78         $invalid_metadata = 1;
79     }
80     if ( defined $record ) {
81         print $query->header( -type => 'text/xml', -charset => 'utf-8', -invalid_metadata => $invalid_metadata );
82         print $record->as_xml_record();
83     } else {
84         print $query->header( -type => 'text/xml', -status => '404 Not Found' );
85     }
86 }
87
88 sub update_bib {
89     my $query = shift;
90     my $biblionumber = shift;
91     my $biblio = Koha::Biblios->find( $biblionumber );
92     my $old_record = $biblio->metadata;
93     my $frameworkcode = $query->url_param('frameworkcode') // GetFrameworkCode($biblionumber);
94     unless  (defined $old_record) {
95         print $query->header(-type => 'text/xml', -status => '404 Not Found');
96         return;
97     }
98
99     my $result = {};
100     my $inxml = $query->param('POSTDATA');
101     print $query->header(-type => 'text/xml', -charset => 'utf-8');
102
103     my $record = eval {MARC::Record::new_from_xml( $inxml, "UTF-8", C4::Context->preference('marcflavour'))};
104     my $do_not_escape = 0;
105     if ($@) {
106         $result->{'status'} = "failed";
107         $result->{'error'} = $@;
108     } else {
109         my $fullrecord = $record->clone();
110         my ( $itemtag, $itemsubfield ) =
111           C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
112
113         # delete any item tags
114         foreach my $field ( $record->field($itemtag) ) {
115             $record->delete_field($field);
116         }
117
118         if ( $query->url_param('items') ) {
119             foreach my $field ( $fullrecord->field($itemtag) ) {
120                 my $one_item_record = $record->clone();
121                 $one_item_record->add_fields($field);
122                 C4::Items::ModItemFromMarc( $one_item_record, $biblionumber,
123                     $field->subfield($itemsubfield) );
124             }
125         }
126
127         C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode );
128         my $biblio = Koha::Biblios->find( $biblionumber );
129         my $new_record = $biblio->metadata->record({ embed_items => scalar $query->url_param('items') });
130
131         $result->{'status'} = "ok";
132         $result->{'biblionumber'} = $biblionumber;
133         my $xml = $new_record->as_xml_record();
134         $xml =~ s/<\?xml.*?\?>//i;
135         $result->{'marcxml'} =  $xml;
136         $do_not_escape = 1;
137     }
138    
139     print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1, NoEscape => $do_not_escape); 
140 }
141
142 sub delete_bib {
143     my $query = shift;
144     my $biblionumber = shift;
145     my $error = C4::Biblio::DelBiblio($biblionumber);
146
147     if (defined $error) {
148         print $query->header(-type => 'text/xml', -status => '400 Bad request');
149         print XMLout({ error => $error }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
150         exit 0;
151     }
152
153     print $query->header(-type => 'text/xml');
154     print XMLout({ status => 'OK, biblio deleted' }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
155 }