Source.pm

filter: Statementstmt 56.1% 87 / 155 Branchbran 34.5% 29 / 84 Conditioncond 35.7% 10 / 28 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 70.5% 12 / 17 Podpod 100% 10 / 10 totaltotal 47.9% 149 / 311 CC 54 SCAR 37.0
CC
54
Cov
47%
z3950Query
156.0
getPPN
25.6
new
23.6
CRAP 40.5
« SOAPClient.pm ↑ Summary SQLiteStore.pm »
1 package PICA::Source;
2 {
3 1 $PICA::Source::VERSION = '0.585';
4 }
5 #ABSTRACT: Data source that can be queried for PICA+ records
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use PICA::PlainParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use PICA::SRUSearchParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use PICA::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Unicode::Normalize qw(NFC);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 2 ▶sub new {
Subroutine new: called CC=16 SCAR=31.6
Pod documented
18 2 my ($class, %params) = @_;
19 2 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
20  
21 PICA::Store::readconfigfile( \%params, $ENV{PICASOURCE} )
22 2 ▶ if exists $params{config} or exists $params{conf} ;
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"config"} or exists $params{"conf"} 0 2 2
Condition33%1/3
exists $params{"config"} or exists $params{"conf"}
l!l&&r!l&&!r
002
MC/DC0%0/2
exists $params{"config"} or exists $params{"conf"}
exists $params{"config"} exists $params{"conf"}
Truth table33%1/3
A or B
A= exists $params{"config"}B= exists $params{"conf"}
ABresult
1X1
011
000
23  
24 my $self = {
25 SRU => $params{SRU} ? $params{SRU} : undef,
26 Z3950 => $params{Z3950} ? $params{Z3950} : undef,
27 unAPI => $params{unAPI} ? $params{unAPI} : undef,
28 PSI => $params{PSI} ? $params{PSI} : undef,
29 user => $params{user} ? $params{user} : undef,
30 password => $params{password} ? $params{password} : undef,
31 idprefix => ($params{idprefix} || undef),
32 prev_record => undef,
33 2 ▶ Limit => ($params{Limit} || 10),
Branch: 6 branches
BranchTrueFalseTotal
$params{'SRU'} ? : 1 1 2
$params{'Z3950'} ? : 0 2 2
$params{'unAPI'} ? : 0 2 2
$params{'PSI'} ? : 1 1 2
$params{'user'} ? : 0 2 2
$params{'password'} ? : 0 2 2
Condition50%2/4
$params{'idprefix'} || undef
l!l
02
$params{'Limit'} || 10
l!l
02
MC/DC0%0/1
$params{'idprefix'} || undef
$params{'idprefix'}
MC/DC0%0/1
$params{'Limit'} || 10
$params{'Limit'}
Truth table50%1/2
A
A= $params{'idprefix'}
Aresult
11
00
Truth table50%1/2
A
A= $params{'Limit'}
Aresult
11
00
34 };
35  
36 2 ▶ if ($self->{SRU} and not $self->{SRU} =~ /[\?&]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"SRU"} and not $self->{"SRU"} =~ /[\?&]$/) 1 1 2
Condition66%2/3
$self->{"SRU"} and not $self->{"SRU"} =~ /[\?&]$/
!ll&&!rl&&r
101
MC/DC50%1/2
$self->{"SRU"} and not $self->{"SRU"} =~ /[\?&]$/
$self->{"SRU"} not $self->{"SRU"} =~ /[\?&]$/
Truth table66%2/3
A and not(B)
A= $self->{"SRU"}B= not $self->{"SRU"} =~ /[\?&]$/
ABresult
0X0
100
111
37 1 ▶ $self->{SRU} .= ($self->{SRU} =~ /\?/) ? '&' : '?';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'SRU'} =~ /\?/ ? : 0 1 1
38 }
39 2 ▶ if ($self->{PSI}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"PSI"}) 1 1 2
40 1 $self->{PSI} =~ s/\/$//;
41 }
42  
43 2 bless $self, $class;
44 }
45  
46  
47 3 ▶sub getPPN {
Subroutine getPPN: called CC=13 SCAR=32.4
Pod documented
48 3 my ($self, $id) = @_;
49  
50 croak("No SRU, PSI or unAPI interface defined")
51 3 ▶ unless $self->{SRU} or $self->{unAPI} or $self->{PSI};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"SRU"} or $self->{"unAPI"} or $self->{"PSI"} 0 3 3
Condition33%2/6
$self->{"SRU"} or $self->{"unAPI"} or $self->{"PSI"}
l!l&&r!l&&!r
030
$self->{"SRU"} or $self->{"unAPI"}
l!l&&r!l&&!r
003
MC/DC0%0/3
$self->{"SRU"} or $self->{"unAPI"} or $self->{"PSI"}
$self->{"SRU"} $self->{"unAPI"} $self->{"PSI"}
Truth table25%1/4
A or B or C
A= $self->{"SRU"}B= $self->{"unAPI"}C= $self->{"PSI"}
ABCresult
1XX1
01X1
0011
0000
52  
53 3 ▶ if ( $self->{PSI} or $self->{unAPI} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'PSI'} or $self->{'unAPI'}) { } 3 0 3
Condition33%1/3
$self->{"PSI"} or $self->{"unAPI"}
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->{"PSI"} or $self->{"unAPI"}
$self->{"PSI"} $self->{"unAPI"}
Truth table33%1/3
A or B
A= $self->{"PSI"}B= $self->{"unAPI"}
ABresult
1X1
011
000
54 3 my $url;
55  
56 3 ▶ if ( $self->{PSI} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'PSI'}) { } 3 0 3
57 3 $url = $self->{PSI} . "/PLAIN=ON/CHARSET=UTF8/PLAINTTLCHARSET=UTF8/URLENCODE=Y/PPN?PPN=$id";
58 } else {
59 $url = $self->{unAPI}
60 0 ▶ . ((index($self->{unAPI},'?') == -1) ? '?' : '&')
Branch: 1 branch
BranchTrueFalseTotal
(index($self->{'unAPI'}, '?') == -1) ? : 0 0 0
61 . "format=pp&id=";
62 0 ▶ if ( !($id =~ /ppn:/) and $self->{idprefix} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $id =~ /ppn:/ and $self->{'idprefix'}) { } 0 0 0
Condition0%0/3
not $id =~ /ppn:/ and $self->{"idprefix"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $id =~ /ppn:/ and $self->{"idprefix"}
not $id =~ /ppn:/ $self->{"idprefix"}
Truth table0%0/3
not(A) and B
A= not $id =~ /ppn:/B= $self->{"idprefix"}
ABresult
0X0
100
111
63 0 $url .= $self->{idprefix} . ":ppn:$id";
64 } else {
65 0 $url .= $id;
66 }
67 # TODO: unapi server does not set encoding header (utf8)?
68 }
69  
70 3 my $data = LWP::Simple::get( $url );
71 3 ▶ if (not $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 1 2 3
72 1 $@ = "HTTP request failed: $url";
73 1 return;
74 }
75  
76 2 ▶ if ( $self->{PSI} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"PSI"}) 2 0 2
77 2 utf8::downgrade( $data ); # make sure that the UTF-8 flag is off
78 2 $data = url_decode($data);
79 2 utf8::decode($data);
80 2 $data = NFC($data); # compose combining chars
81 2 utf8::upgrade($data);
82 }
83 2 my $record = eval { PICA::Record->new( $data ) } ;
84 2 ▶ if ($record) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record) { } 1 1 2
85 1 return $record;
86 } else {
87 1 $@ = "Failed to parse PICA::Record";
88 1 return;
89 }
90 } else {
91 0 my $result = $self->cqlQuery( "pica.ppn=$id", Limit => 1 );
92 0 my ($record) = $result->records();
93 0 return $record;
94 } # TODO: use z3950
95 }
96  
97  
98 1 ▶sub cqlQuery {
Subroutine cqlQuery: called CC=10 SCAR=25.7
Pod documented
99 1 my $self = shift;
100 1 my $cql = shift;
101  
102 1 ▶ croak("No SRU interface defined") unless $self->{SRU};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"SRU"} 0 1 1
103  
104 1 ▶ my $xmlparser = UNIVERSAL::isa( $_[0], "PICA::XMLParser" )
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], 'PICA::XMLParser') ? : 0 1 1
105 ? $_[0] : PICA::XMLParser->new( @_ );
106 1 my $sruparser = PICA::SRUSearchParser->new( $xmlparser );
107 1 ▶ shift if ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 1 1
108 1 my %params = (@_);
109 1 ▶ my $limit = $params{Limit} || $self->{Limit};
Condition33%1/3
$params{'Limit'} || $self->{'Limit'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$params{'Limit'} || $self->{'Limit'}
$params{'Limit'} $self->{'Limit'}
Truth table33%1/3
A || B
A= $params{'Limit'}B= $self->{'Limit'}
ABresult
1X1
011
000
110  
111 1 my $options = "";
112 1 $cql = url_encode($cql); #url_unicode_encode($cql);
113 1 my $baseurl = $self->{SRU} . "recordSchema=picaxml&version=1.1&operation=searchRetrieve&maximumRecords=$limit";
114  
115 1 my $startRecord = 1;
116 1 ▶ if ($xmlparser->{offset} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmlparser->{"offset"} > 0) 0 1 1
117 0 $startRecord += $xmlparser->{offset};
118 0 $xmlparser->{offset} = 0;
119 }
120 1 while(1) {
121 1 my $options = "&startRecord=$startRecord";
122 1 my $url = $baseurl . "&query=" . $cql . $options;
123  
124 1 print "$url\n"; # TODO: logging
125  
126 1 my $xml = LWP::Simple::get( $url );
127 1 ▶ croak("SRU Request failed $url") unless $xml; # TODO: don't croak?
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 1 1
128 1 $xmlparser = $sruparser->parse($xml);
129  
130 #print "numberOfRecords " . $sruparser->numberOfRecords() . "\n";
131 #print "resultSetId " . $sruparser->resultSetId() . "\n";
132 #print "current counter " . $xmlparser->counter() . "\n";
133  
134 1 ▶ return $xmlparser unless $sruparser->currentNumber(); # zero results
Branch: 1 branch
BranchTrueFalseTotal
unless $sruparser->currentNumber 0 1 1
135 1 $startRecord += $sruparser->currentNumber();
136 1 ▶ return $xmlparser if $sruparser->numberOfRecords() < $startRecord;
Branch: 1 branch
BranchTrueFalseTotal
if $sruparser->numberOfRecords < $startRecord 1 0 1
137 0 ▶ return $xmlparser if $xmlparser->finished();
Branch: 1 branch
BranchTrueFalseTotal
if $xmlparser->finished 0 0 0
138 }
139 }
140  
141  
142 0 ▶sub z3950Query {
Subroutine z3950Query: not called CC=12 SCAR=50.5
Pod documented
143 0 my ($self, $query, %handlers) = @_;
144  
145 0 eval { require ZOOM; require ZOOM::Options; require ZOOM::Connection; };
146 croak("Please load package ZOOM to use Z39.50!")
147 0 ▶ unless defined $INC{'ZOOM.pm'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $INC{"ZOOM.pm"} 0 0 0
148 0 ▶ croak("No Z3950 interface defined") unless $self->{Z3950};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Z3950"} 0 0 0
149 croak("Z3950 interface have host and database")
150 0 ▶ unless $self->{Z3950} =~ /^(tcp:|ssl:)?([^\/:]+)(:[0-9]+)?\/(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Z3950"} =~ m[^(tcp:|ssl:)?([^/:]+)(:[0-9]+)?/(.*)] 0 0 0
151  
152 0 my $options = new ZOOM::Options();
153 0 $options->option( preferredRecordSyntax => "picamarc" );
154 0 ▶ $options->option( user => $self->{user} ) if defined $self->{user};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"user"} 0 0 0
155 0 ▶ $options->option( password => $self->{password} ) if defined $self->{password};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"password"} 0 0 0
156  
157 0 my ($conn, $rs);
158 0 eval {
159 0 $conn = ZOOM::Connection->create( $options );
160 0 $conn->connect( $self->{Z3950} );
161 };
162 0 ▶ eval { $rs = $conn->search_pqf($query); } unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
163 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
164 0 croak("Z39.50 error " . $@->code(), ": ", $@->message());
165 }
166  
167 0 ▶ %handlers = () unless %handlers;
Branch: 1 branch
BranchTrueFalseTotal
unless %handlers 0 0 0
168 0 $handlers{Proceed} = 1;
169  
170 0 my $parser = PICA::PlainParser->new( %handlers );
171 0 my $n = $rs->size();
172 0 for my $i (0..$n-1) {
173 0 my $raw;
174 0 eval {
175 0 $raw = $rs->record($i)->raw();
176 };
177 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
178 0 croak("Z39.50 error " . $@->code(), ": ", $@->message());
179 }
180 #print "$raw\n";
181 0 $parser->parsedata($raw);
182 0 ▶ return $parser if $parser->finished();
Branch: 1 branch
BranchTrueFalseTotal
if $parser->finished 0 0 0
183 }
184 0 return $parser;
185 }
186  
187  
188 0 ▶sub iktQuery {
Subroutine iktQuery: not called CC=2 SCAR=17.9
Pod documented
189 0 my ($self, $ikt, $term) = @_;
190  
191 0 ▶ croak('No PSI interface defined') unless $self->{PSI};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"PSI"} 0 0 0
192  
193 0 $ikt = url_encode($ikt);
194 0 $term = url_encode($term);
195 # $term =~ s/\//\\\//; # escape / => \/
196  
197 my $url = $self->{PSI}
198 0 . "/PLAIN=ON/CHARSET=UTF8/PLAINTTLCHARSET=UTF8/"
199 . "CMD?ACT=SRCHA&IKT=$ikt&TRM=$term";
200 0 my $raw = get($url);
201 0 utf8::decode($raw);
202 0 $raw = NFC($raw); # compose combining chars
203 0 utf8::upgrade($raw);
204 0 my $record = eval { PICA::Record->new( $raw ); };
205
206 0 return ($record);
207 }
208  
209  
210 0 ▶sub iktLink {
Subroutine iktLink: not called CC=2 SCAR=17.9
Pod documented
211 0 my ($self, $ikt, $term) = @_;
212  
213 0 ▶ croak('No PSI interface defined') unless $self->{PSI};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"PSI"} 0 0 0
214  
215 0 $ikt = url_encode($ikt);
216 0 $term = url_encode($term);
217  
218 0 return $self->{PSI} . "/CMD?ACT=SRCHA&IKT=$ikt&TRM=$term";
219 }
220  
221  
222 0 ▶sub ppnLink {
Subroutine ppnLink: not called CC=2 SCAR=17.9
Pod documented
223 0 my ($self, $ppn) = @_;
224  
225 0 ▶ croak('No PSI interface defined') unless $self->{PSI};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"PSI"} 0 0 0
226  
227 0 return $self->{PSI} . "/PPNSET?PPN=$ppn";
228 }
229  
230  
231 0 ▶sub baseURL {
Subroutine baseURL: not called CC=4 SCAR=30.0
Pod documented
232 0 my $self = shift;
233  
234 0 ▶ return $self->{PSI} if $self->{PSI};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"PSI"} 0 0 0
235 0 ▶ return $self->{unAPI} if $self->{unAPI};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"unAPI"} 0 0 0
236 0 ▶ return $self->{SRU} if $self->{SRU};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"SRU"} 0 0 0
237  
238 0 return "";
239 }
240  
241  
242 1 ▶sub url_encode {
Subroutine url_encode: called CC=1 SCAR=0.0
Pod documented
243 1 my $str = shift;
244 1 $str =~ s{([^A-Za-z0-9_\.\*])}{sprintf("%%%02x", ord($1))}eg;
245 1 return $str;
246 }
247  
248  
249 2 ▶sub url_decode {
Subroutine url_decode: called CC=1 SCAR=0.0
Pod documented
250 2 my $str = shift;
251 2 $str =~ tr/+/ /;
252 2 $str =~ s|%([A-Fa-f0-9]{2})|chr(hex($1))|eg;
253 2 return $str;
254 }
255  
256  
257 # Returns the fully URL-encoded version of the given string as
258 # unicode characters. It does not convert space characters to
259 # '+' characters.
260 # sub url_unicode_encode {
261 # my $str = shift;
262 # $str =~ s{([^A-Za-z0-9_\.\*])}{sprintf("%%u%04x", ord($1))}eg;
263 # return $str;
264 #}
265  
266 1 1;
267  
268 __END__
« SOAPClient.pm ↑ Summary SQLiteStore.pm »