3 # Copyright 2011 - Dobrica Pavlinusic
5 # This file is part of Koha.
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.
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.
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>.
26 if ( $#ARGV >= 3 && ! caller ) { # process command-line params only if not called as module!
27 my ( $url, $user, $password, $biblionumber, $file ) = @ARGV;
29 my $svc = Koha::SVC->new(
32 password => $password,
37 my $marcxml = $svc->get( $biblionumber );
38 my $file = "bib-$biblionumber.xml";
39 write_file $file , $marcxml;
40 print "saved $file ", -s $file, " bytes\n";
43 print "update $biblionumber from $file\n";
44 $svc->post( $biblionumber, scalar read_file($file) );
60 Call Koha's C</svc/> API to fetch/update records
62 This script can be used from other scripts as C<Koha::SVC> module or run
63 directly using syntax:
65 koha-svc.pl http://koha-dev:8080/cgi-bin/koha/svc svc-user svc-password $biblionumber [bib-42.xml]
67 If called without last argument (MARCXML filename) it will fetch C<$biblionumber> from Koha and create
68 C<bib-$biblionumber.xml> file from it. When called with xml filename, it will update record in Koha.
70 This script is intentionally separate from Koha itself and dependencies which Koha has under
71 assumption that you might want to run it on another machine (or create custom script which mungles
72 Koha's records from other machine without bringing all Koha dependencies with it).
76 This same script can be used as module (as it defines T<Koha::SVC> package) using
80 at beginning of script. Rest of API is described below. Example of its usage is at beginning of this script.
84 my $svc = Koha::SVC->new(
85 url => 'http://koha-dev:8080/cgi-bin/koha/svc',
87 password => 'svc-password',
91 URL must point to Koha's B<intranet> address and port.
93 Specified user must have C<editcatalogue> permission.
102 my $url = $self->{url} || die "no url found";
103 my $user = $self->{user} || die "no user specified";
104 my $password = $self->{password} || die "no password";
106 my $ua = LWP::UserAgent->new();
108 my $resp = $ua->post( "$url/authentication", {userid =>$user, password => $password} );
109 die $resp->status_line unless $resp->is_success;
111 warn "# $user $url = ", $resp->decoded_content, "\n" if $self->{debug};
120 my $marcxml = $svc->get( $biblionumber );
125 my ($self,$biblionumber) = @_;
127 my $url = $self->{url};
128 warn "# get $url/bib/$biblionumber\n" if $self->{debug};
129 my $resp = $self->{ua}->get( "$url/bib/$biblionumber" );
130 die $resp->status_line unless $resp->is_success;
131 return $resp->decoded_content;
136 my $marcxml = $svc->post( $biblionumber, $marcxml );
141 my ($self,$biblionumber,$marcxml) = @_;
142 my $url = $self->{url};
143 warn "# post $url/bib/$biblionumber\n" if $self->{debug};
144 my $resp = $self->{ua}->post( "$url/bib/$biblionumber", 'Content_type' => 'text/xml', Content => $marcxml );
145 die $resp->status_line unless $resp->is_success;
146 return $resp->decoded_content;