experiment: use PazPar2 to group related works
[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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 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
106     my $uri = URI->new($self->{'endpoint'});
107     $uri->query_param(command => 'show');
108     $uri->query_param(start => $start);
109     $uri->query_param(num => $count);
110     $uri->query_param(block => 1);
111     $uri->query_param(session => $self->{'session'});
112     my $response = $self->{'ua'}->get($uri);
113     if ($response->is_success) {
114         return $response->content;
115     } else {
116         warn $response->status_line;
117         return;
118     }
119     
120 }
121
122 sub record {
123     my $self = shift;
124     my $id = shift;
125     my $offset = shift;
126
127     my $uri = URI->new($self->{'endpoint'});
128     $uri->query_param(command => 'record');
129     $uri->query_param(id => $id);
130     $uri->query_param(offset => $offset);
131     $uri->query_param(binary => 1);
132     $uri->query_param(session => $self->{'session'});
133     my $response = $self->{'ua'}->get($uri);
134     if ($response->is_success) {
135         return $response->content;
136     } else {
137         warn $response->status_line;
138         return;
139     }
140 }
141
142 sub termlist {
143     my $self = shift;
144     my $name = shift;
145
146     my $uri = URI->new($self->{'endpoint'});
147     $uri->query_param(command => 'termlist');
148     $uri->query_param(name => $name);
149     $uri->query_param(session => $self->{'session'});
150     my $response = $self->{'ua'}->get($uri);
151     if ($response->is_success) {
152         return $response->content;
153     } else {
154         warn $response->status_line;
155         return;
156     }
157
158 }
159
160 1;
161
162 =head1 AUTHOR
163
164 Koha Development Team <info@koha.org>
165
166 Galen Charlton <galen.charlton@liblime.com>
167
168 =cut