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 | 15 15 15 | 485 108 625 | use strict; | |||
21 | #use warnings; FIXME - Bug 2505 | |||||
22 | ||||||
23 | 15 15 15 | 35621 743183 658 | use LWP::UserAgent; | |||
24 | 15 15 15 | 205 88 699 | use URI; | |||
25 | 15 15 15 | 21061 11649 552 | use URI::QueryParam; | |||
26 | 15 15 15 | 292 11617 330 | 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 | 1 | 27 | my $class = shift; | |||
45 | 1 | 25 | my $endpoint = shift; | |||
46 | ||||||
47 | 1 | 24 | my $self = {}; | |||
48 | 1 | 23 | $self->{'endpoint'} = $endpoint; | |||
49 | 1 | 29 | $self->{'session'} = ''; | |||
50 | 1 | 32 | $self->{'ua'} = LWP::UserAgent->new; | |||
51 | 1 | 3346 | bless $self, $class; | |||
52 | ||||||
53 | 1 | 93 | return $self; | |||
54 | } | |||||
55 | ||||||
56 | sub init { | |||||
57 | 1 | 26 | my $self = shift; | |||
58 | ||||||
59 | 1 | 33 | my $uri = URI->new($self->{'endpoint'}); | |||
60 | 1 | 70942 | $uri->query_param(command => 'init'); | |||
61 | 1 | 145 | my $response = $self->{'ua'}->get($uri); | |||
62 | 1 | 3393 | 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 | 1 | 21 | warn $response->status_line; | |||
69 | } | |||||
70 | } | |||||
71 | ||||||
72 | sub search { | |||||
73 | 1 | 2 | my $self = shift; | |||
74 | 1 | 3 | my $query = shift; | |||
75 | ||||||
76 | 1 | 9 | my $uri = URI->new($self->{'endpoint'}); | |||
77 | 1 | 81 | $uri->query_param(command => 'search'); | |||
78 | 1 | 119 | $uri->query_param(session => $self->{'session'}); | |||
79 | 1 | 142 | $uri->query_param(query => $query); | |||
80 | 1 | 187 | my $response = $self->{'ua'}->get($uri); | |||
81 | 1 | 578 | if ($response->is_success) { | |||
82 | #print $response->content, "\n"; | |||||
83 | } else { | |||||
84 | 1 | 20 | warn $response->status_line; | |||
85 | } | |||||
86 | ||||||
87 | } | |||||
88 | ||||||
89 | sub stat { | |||||
90 | 1 | 2 | my $self = shift; | |||
91 | ||||||
92 | 1 | 9 | my $uri = URI->new($self->{'endpoint'}); | |||
93 | 1 | 48 | $uri->query_param(command => 'stat'); | |||
94 | 1 | 343 | $uri->query_param(session => $self->{'session'}); | |||
95 | 1 | 145 | my $response = $self->{'ua'}->get($uri); | |||
96 | 1 | 799 | if ($response->is_success) { | |||
97 | 0 | 0 | return $response->content; | |||
98 | } else { | |||||
99 | 1 | 20 | warn $response->status_line; | |||
100 | 1 | 109 | return; | |||
101 | } | |||||
102 | } | |||||
103 | ||||||
104 | sub show { | |||||
105 | 1 | 35 | my $self = shift; | |||
106 | 1 | 2 | my $start = shift; | |||
107 | 1 | 2 | my $count = shift; | |||
108 | 1 | 1 | my $sort = shift; | |||
109 | ||||||
110 | 1 | 9 | my $uri = URI->new($self->{'endpoint'}); | |||
111 | 1 | 93 | $uri->query_param(command => 'show'); | |||
112 | 1 | 282 | $uri->query_param(start => $start); | |||
113 | 1 | 159 | $uri->query_param(num => $count); | |||
114 | 1 | 166 | $uri->query_param(block => 1); | |||
115 | 1 | 428 | $uri->query_param(session => $self->{'session'}); | |||
116 | 1 | 1240 | $uri->query_param(sort => $sort); | |||
117 | 1 | 384 | my $response = $self->{'ua'}->get($uri); | |||
118 | 1 | 572 | if ($response->is_success) { | |||
119 | 0 | 0 | return $response->content; | |||
120 | } else { | |||||
121 | 1 | 20 | warn $response->status_line; | |||
122 | 1 | 135 | return; | |||
123 | } | |||||
124 | ||||||
125 | } | |||||
126 | ||||||
127 | sub record { | |||||
128 | 1 | 3 | my $self = shift; | |||
129 | 1 | 1 | my $id = shift; | |||
130 | 1 | 1 | my $offset = shift; | |||
131 | ||||||
132 | 1 | 8 | my $uri = URI->new($self->{'endpoint'}); | |||
133 | 1 | 50 | $uri->query_param(command => 'record'); | |||
134 | 1 | 109 | $uri->query_param(id => $id); | |||
135 | 1 | 137 | $uri->query_param(offset => $offset); | |||
136 | 1 | 169 | $uri->query_param(binary => 1); | |||
137 | 1 | 893 | $uri->query_param(session => $self->{'session'}); | |||
138 | 1 | 664 | my $response = $self->{'ua'}->get($uri); | |||
139 | 1 | 549 | if ($response->is_success) { | |||
140 | 0 | 0 | return $response->content; | |||
141 | } else { | |||||
142 | 1 | 19 | warn $response->status_line; | |||
143 | 1 | 125 | return; | |||
144 | } | |||||
145 | } | |||||
146 | ||||||
147 | sub termlist { | |||||
148 | 1 | 3 | my $self = shift; | |||
149 | 1 | 2 | my $name = shift; | |||
150 | ||||||
151 | 1 | 8 | my $uri = URI->new($self->{'endpoint'}); | |||
152 | 1 | 72 | $uri->query_param(command => 'termlist'); | |||
153 | 1 | 114 | $uri->query_param(name => $name); | |||
154 | 1 | 141 | $uri->query_param(session => $self->{'session'}); | |||
155 | 1 | 186 | my $response = $self->{'ua'}->get($uri); | |||
156 | 1 | 567 | if ($response->is_success) { | |||
157 | 0 | 0 | return $response->content; | |||
158 | } else { | |||||
159 | 1 | 20 | warn $response->status_line; | |||
160 | 1 | 501 | 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 |