Merge commit 'kc/master'
[koha.git] / C4 / Search / PazPar2.pm
1 package C4::Search::PazPar2;
2
3 # Copyright (C) 2007 LibLime
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22
23 use LWP::UserAgent;
24 use URI;
25 use URI::QueryParam;
26 use XML::Simple;
27
28 =head1 NAME
29
30 C4::Search::PazPar2 - implement client for PazPar2
31
32 [Note: may rename to Net::PazPar2 or somesuch if decide to put on CPAN separate
33  from Koha]
34
35 =head1 SYNOPSIS
36
37 =head1 DESCRIPTION
38
39 =cut
40
41 sub new {
42     my $class = shift;
43     my $endpoint = shift;
44
45     my $self = {};
46     $self->{'endpoint'} = $endpoint;
47     $self->{'session'} = '';
48     $self->{'ua'} = LWP::UserAgent->new;
49     bless $self, $class;
50
51     return $self;
52 }
53
54 sub init {
55     my $self = shift;
56
57     my $uri = URI->new($self->{'endpoint'});
58     $uri->query_param(command => 'init');
59     my $response = $self->{'ua'}->get($uri);
60     if ($response->is_success) {
61         my $message = XMLin($response->content);
62         if ($message->{'status'} eq 'OK') {
63             $self->{'session'} = $message->{'session'};
64         }
65     } else {
66         warn $response->status_line;
67     }
68 }
69
70 sub search {
71     my $self = shift;
72     my $query = shift;
73
74     my $uri = URI->new($self->{'endpoint'});
75     $uri->query_param(command => 'search');
76     $uri->query_param(session => $self->{'session'});
77     $uri->query_param(query => $query);
78     my $response = $self->{'ua'}->get($uri);
79     if ($response->is_success) {
80         #print $response->content, "\n";
81     } else {
82         warn $response->status_line;
83     }
84
85 }
86
87 sub stat {
88     my $self = shift;
89
90     my $uri = URI->new($self->{'endpoint'});
91     $uri->query_param(command => 'stat');
92     $uri->query_param(session => $self->{'session'});
93     my $response = $self->{'ua'}->get($uri);
94     if ($response->is_success) {
95         return $response->content;
96     } else {
97         warn $response->status_line;
98         return;
99     }
100 }
101
102 sub show {
103     my $self = shift;
104     my $start = shift;
105     my $count = shift;
106     my $sort = shift;
107
108     my $uri = URI->new($self->{'endpoint'});
109     $uri->query_param(command => 'show');
110     $uri->query_param(start => $start);
111     $uri->query_param(num => $count);
112     $uri->query_param(block => 1);
113     $uri->query_param(session => $self->{'session'});
114     $uri->query_param(sort => $sort);
115     my $response = $self->{'ua'}->get($uri);
116     if ($response->is_success) {
117         return $response->content;
118     } else {
119         warn $response->status_line;
120         return;
121     }
122     
123 }
124
125 sub record {
126     my $self = shift;
127     my $id = shift;
128     my $offset = shift;
129
130     my $uri = URI->new($self->{'endpoint'});
131     $uri->query_param(command => 'record');
132     $uri->query_param(id => $id);
133     $uri->query_param(offset => $offset);
134     $uri->query_param(binary => 1);
135     $uri->query_param(session => $self->{'session'});
136     my $response = $self->{'ua'}->get($uri);
137     if ($response->is_success) {
138         return $response->content;
139     } else {
140         warn $response->status_line;
141         return;
142     }
143 }
144
145 sub termlist {
146     my $self = shift;
147     my $name = shift;
148
149     my $uri = URI->new($self->{'endpoint'});
150     $uri->query_param(command => 'termlist');
151     $uri->query_param(name => $name);
152     $uri->query_param(session => $self->{'session'});
153     my $response = $self->{'ua'}->get($uri);
154     if ($response->is_success) {
155         return $response->content;
156     } else {
157         warn $response->status_line;
158         return;
159     }
160
161 }
162
163 1;
164
165 =head1 AUTHOR
166
167 Koha Development Team <http://koha-community.org/>
168
169 Galen Charlton <galen.charlton@liblime.com>
170
171 =cut