File: | C4/Search/PazPar2.pm |
Coverage: | 87.2% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
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 | 10 10 10 | 43468 58 310 | use strict; | |||
21 | #use warnings; FIXME - Bug 2505 | |||||
22 | ||||||
23 | 10 10 10 | 29681 582774 441 | use LWP::UserAgent; | |||
24 | 10 10 10 | 113 18 244 | use URI; | |||
25 | 10 10 10 | 33146 8315 351 | use URI::QueryParam; | |||
26 | 10 10 10 | 354 21832 168 | use XML::Simple; | |||
27 | ||||||
28 - 37 | =head1 NAME C4::Search::PazPar2 - implement client for PazPar2 [Note: may rename to Net::PazPar2 or somesuch if decide to put on CPAN separate from Koha] =head1 SYNOPSIS =cut | |||||
38 | ||||||
39 - 41 | =head1 DESCRIPTION =cut | |||||
42 | ||||||
43 | sub new { | |||||
44 | 2 | 483930 | my $class = shift; | |||
45 | 2 | 4 | my $endpoint = shift; | |||
46 | ||||||
47 | 2 | 4 | my $self = {}; | |||
48 | 2 | 4 | $self->{'endpoint'} = $endpoint; | |||
49 | 2 | 5 | $self->{'session'} = ''; | |||
50 | 2 | 20 | $self->{'ua'} = LWP::UserAgent->new; | |||
51 | 2 | 7337 | bless $self, $class; | |||
52 | ||||||
53 | 2 | 15 | return $self; | |||
54 | } | |||||
55 | ||||||
56 | sub init { | |||||
57 | 2 | 1482 | my $self = shift; | |||
58 | ||||||
59 | 2 | 17 | my $uri = URI->new($self->{'endpoint'}); | |||
60 | 2 | 87107 | $uri->query_param(command => 'init'); | |||
61 | 2 | 332 | my $response = $self->{'ua'}->get($uri); | |||
62 | 2 | 7423 | if ($response->is_success) { | |||
63 | 0 | 0 | my $message = XMLin($response->content); | |||
64 | 0 | 0 | if ($message->{'status'} eq 'OK') { | |||
65 | 0 | 0 | $self->{'session'} = $message->{'session'}; | |||
66 | } | |||||
67 | } else { | |||||
68 | 2 | 44 | warn $response->status_line; | |||
69 | } | |||||
70 | } | |||||
71 | ||||||
72 | sub search { | |||||
73 | 2 | 1822 | my $self = shift; | |||
74 | 2 | 4 | my $query = shift; | |||
75 | ||||||
76 | 2 | 52 | my $uri = URI->new($self->{'endpoint'}); | |||
77 | 2 | 197 | $uri->query_param(command => 'search'); | |||
78 | 2 | 283 | $uri->query_param(session => $self->{'session'}); | |||
79 | 2 | 345 | $uri->query_param(query => $query); | |||
80 | 2 | 411 | my $response = $self->{'ua'}->get($uri); | |||
81 | 2 | 1739 | if ($response->is_success) { | |||
82 | #print $response->content, "\n"; | |||||
83 | } else { | |||||
84 | 2 | 42 | warn $response->status_line; | |||
85 | } | |||||
86 | ||||||
87 | } | |||||
88 | ||||||
89 | sub stat { | |||||
90 | 2 | 1990 | my $self = shift; | |||
91 | ||||||
92 | 2 | 225 | my $uri = URI->new($self->{'endpoint'}); | |||
93 | 2 | 102 | $uri->query_param(command => 'stat'); | |||
94 | 2 | 224 | $uri->query_param(session => $self->{'session'}); | |||
95 | 2 | 297 | my $response = $self->{'ua'}->get($uri); | |||
96 | 2 | 980 | if ($response->is_success) { | |||
97 | 0 | 0 | return $response->content; | |||
98 | } else { | |||||
99 | 2 | 38 | warn $response->status_line; | |||
100 | 2 | 161 | return; | |||
101 | } | |||||
102 | } | |||||
103 | ||||||
104 | sub show { | |||||
105 | 2 | 4 | my $self = shift; | |||
106 | 2 | 4 | my $start = shift; | |||
107 | 2 | 2 | my $count = shift; | |||
108 | 2 | 3 | my $sort = shift; | |||
109 | ||||||
110 | 2 | 30 | my $uri = URI->new($self->{'endpoint'}); | |||
111 | 2 | 80 | $uri->query_param(command => 'show'); | |||
112 | 2 | 201 | $uri->query_param(start => $start); | |||
113 | 2 | 525 | $uri->query_param(num => $count); | |||
114 | 2 | 357 | $uri->query_param(block => 1); | |||
115 | 2 | 422 | $uri->query_param(session => $self->{'session'}); | |||
116 | 2 | 493 | $uri->query_param(sort => $sort); | |||
117 | 2 | 559 | my $response = $self->{'ua'}->get($uri); | |||
118 | 2 | 995 | if ($response->is_success) { | |||
119 | 0 | 0 | return $response->content; | |||
120 | } else { | |||||
121 | 2 | 34 | warn $response->status_line; | |||
122 | 2 | 1064 | return; | |||
123 | } | |||||
124 | ||||||
125 | } | |||||
126 | ||||||
127 | sub record { | |||||
128 | 2 | 12 | my $self = shift; | |||
129 | 2 | 10 | my $id = shift; | |||
130 | 2 | 8 | my $offset = shift; | |||
131 | ||||||
132 | 2 | 20 | my $uri = URI->new($self->{'endpoint'}); | |||
133 | 2 | 94 | $uri->query_param(command => 'record'); | |||
134 | 2 | 293 | $uri->query_param(id => $id); | |||
135 | 2 | 302 | $uri->query_param(offset => $offset); | |||
136 | 2 | 347 | $uri->query_param(binary => 1); | |||
137 | 2 | 431 | $uri->query_param(session => $self->{'session'}); | |||
138 | 2 | 512 | my $response = $self->{'ua'}->get($uri); | |||
139 | 2 | 2275 | if ($response->is_success) { | |||
140 | 0 | 0 | return $response->content; | |||
141 | } else { | |||||
142 | 2 | 299 | warn $response->status_line; | |||
143 | 2 | 167 | return; | |||
144 | } | |||||
145 | } | |||||
146 | ||||||
147 | sub termlist { | |||||
148 | 2 | 6 | my $self = shift; | |||
149 | 2 | 2 | my $name = shift; | |||
150 | ||||||
151 | 2 | 15 | my $uri = URI->new($self->{'endpoint'}); | |||
152 | 2 | 84 | $uri->query_param(command => 'termlist'); | |||
153 | 2 | 223 | $uri->query_param(name => $name); | |||
154 | 2 | 280 | $uri->query_param(session => $self->{'session'}); | |||
155 | 2 | 364 | my $response = $self->{'ua'}->get($uri); | |||
156 | 2 | 1021 | if ($response->is_success) { | |||
157 | 0 | 0 | return $response->content; | |||
158 | } else { | |||||
159 | 2 | 40 | warn $response->status_line; | |||
160 | 2 | 155 | return; | |||
161 | } | |||||
162 | ||||||
163 | } | |||||
164 | ||||||
165 | 1; | |||||
166 | ||||||
167 - 173 | =head1 AUTHOR Koha Development Team <http://koha-community.org/> Galen Charlton <galen.charlton@liblime.com> =cut |