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