blib/lib/Net/WoRMS.pm

filter: Statementstmt 92.6% 163 / 176 Branchbran 67.5% 50 / 74 Conditioncond 46.1% 6 / 13 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 97.0% 33 / 34 Podpod 100% 19 / 19 totaltotal 84.2% 273 / 324 CC 46 SCAR 25.1
CC
46
Cov
83%
printRecord
27.9
searchSpeciesID
19.0
getChildrenByID
7.3
CRAP 12.3
↑ Summary
1 package Net::WoRMS;
2 # ======================================================================
3 # (C) 2012 by A. Weidauer
4 # Contact: alex.weidauer@huckfinn.de
5 # ======================================================================
6 # ABSTRACT: Service object to query the "World Register of Marine Species" WoRMS.
7 # ======================================================================
8  
9 =encoding utf8
10  
11 =head1 NAME
12  
13 Net::WoRMS - query the "World Register of Marine Species" WoRMS
14  
15 =head1 SYNOPSIS
16  
17 #!/usr/bin/env perl
18 use strict;
19 use warnings;
20 use Net::WoRMS;
21  
22 # Example settings
23 my $GENUS = 'Abra';
24 my $SPECIES = 'Abra alba';
25  
26 # Initialize the service
27 my $WORMS = Net::WoRMS->new();
28 my $WORMS->init();
29 die "Cannot open service!"
30 if not $WORMS->checkSoap();
31  
32 # Single species info, output CSV
33 $WORMS->changeFormat('CSV');
34 my $id = $WORMS->searchSpeciesID($SPECIES, 0);
35 my $rec = $WORMS->getRecordByID($id);
36 $WORMS->printRecordDef($rec, ' ');
37  
38 # Species info for GENUS, output plain TEXT
39 my $gid = $WORMS->searchSpeciesID($GENUS, 0);
40 $WORMS->changeFormat('CSV');
41 $WORMS->getChildrenByID($gid) or
42 die "No species found for GENUS: '$GENUS'!";
43  
44 =head1 DESCRIPTION
45  
46 Net::WoRMS bundles WEB access, parsing and formatting capabilities for
47 the tool L<query-worms|https://metacpan.org>, to harvest information
48 form the "World Register of Marine Species" --
49 L<http://www.marinespecies.org>. The module was built around the module
50 L<SOAP::Lite> and is OO implemented using L<Moo>.
51  
52 =cut
53  
54 # ======================================================================
55 4 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 4 ▶use open qw(:std :utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 4 our $VERSION = 1.901;
62  
63 # ----------------------------------------------------------------------
64  
65 =head1 DEPENDENCIES
66  
67 The module uses the packages:
68  
69 =over
70  
71 =item L<Data::Dumper>
72  
73 Debugging purposes
74  
75 =item L<Moo>
76  
77 Object oriented extension for the module
78  
79 =item L<MooX::Types::MooseLike::Base>
80  
81 Minimal type system for L<Moo>
82  
83 =item L<SOAP::Lite>
84  
85 is used for the WEB access and parsing capabilities to retrieve data
86 from L<http://www.marinespecies.org>.
87  
88 =item L<Syntax::Keyword::Match>
89  
90 to end the switch-case/while-given madness
91  
92 =back
93  
94 =cut
95  
96 # Modules -------------------------------------------------------------
97 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 4 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
99 4 ▶use SOAP::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 # Modern Perl ----------------------------------------------------------
103 4 ▶use Syntax::Keyword::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 4 ▶use feature qw(signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
105 4 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
106 4 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
107 4 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
108  
109 # ---------------------------------------------------------------------
110  
111 =head1 DEFAULT SETTINGS
112  
113 =over
114  
115 =item $END_POINT = 'https://www.marinespecies.org/aphia.php?p=soap'
116  
117 The access point to retrieve data via the Simple Object Access Protocol.
118  
119 =item $APHIA_NS = 'https://aphia/v1.0'
120  
121 Namespace and corresponding URI. In the development context these
122 variables are considered as a constants. There are methods to change
123 these values but never tested.
124  
125 =item $FORMAT = 'TEXT'
126  
127 FORMAT defines the default output format.
128  
129 =item @ORDER_SCORE = qw/APHIA_ID ... /
130  
131 The array holds field names for SQL related output.
132  
133 =item @ORDER_DOT = qw/APHIA.ID ... /
134  
135 The array holds field names for TEXT and CSV related output.
136  
137 =item %FORMATS = ( TEXT => 1, SQL => 2, CSV => 3)
138  
139 The format hash mostly for validation.
140  
141 =item %ORDER HASH
142  
143 Dictionary to sort, parse and translate the retrieved field names from the
144 WoRMS database and bring them into a consistent form.
145  
146 =back
147  
148 =cut
149  
150 # GLOBALS ==============================================================
151  
152 4 my $END_POINT = 'https://www.marinespecies.org/aphia.php?p=soap';
153 4 my $APHIA_NS = 'https://aphia/v1.0';
154 4 my $APHIA_URI = $APHIA_NS;
155 4 my $FORMAT = 'TEXT';
156  
157 4 my $RI = qr/^\d+$/;
158 4 my $RS = qr/^(\(?[\w-]+\)?\.?(?:\s+\(?[\w-]+\)?\.?){0,4})$/;
159 # my $RA = qr/^\(?\[?([\w.&'-]+(?:\s+[\w.&'-]+){0,20})(?:,?\s*(\d+))?\)?\]?$/;
160 4 my $RA = qr/^(?=.*\p{L}).{1,250}$/s;
161 4 my $RW = qr/^[\w+\-\.\s+]+$/;
162  
163 4 my %TYPES = ($RI => 'APHIA.ID', $RS => 'TAXON',
164 $RA => 'AUTHOR', $RW => 'WORDS');
165  
166 # Field name dump output
167 4 my %ORDER = (
168 # WoRMS.KEY ORDER TEXT|CSV SQL REX SUBS
169 'AphiaID' => [ 0, 'APHIA.ID', 'APHIA_ID', $RI, 0],
170 'scientificname' => [ 1, 'NAME', 'NAME', $RS, 1],
171 'authority' => [ 2, 'AUTHOR', 'AUTHOR', $RA, 2],
172 'rank' => [ 3, 'RANK', 'RANK', $RW, 1],
173 'status' => [ 4, 'STATUS', 'STATUS', $RW, 1],
174 'valid_AphiaID' => [ 5, 'VALID.APHIA.ID', 'VALID_APHIA_ID', $RI, 0],
175 'valid_name' => [ 6, 'VALID.NAME', 'VALID_NAME', $RS, 1],
176 'valid_authority' => [ 7, 'VALID.AUTHOR', 'VALID_AUTHOR', $RA, 2],
177 'kingdom' => [ 8, 'KINGDOM', 'RNK_KINGDOM', $RW, 1],
178 'phylum' => [ 9, 'PHYLUM', 'RNK_PHYLUM', $RW, 1],
179 'class' => [10, 'CLASS' , 'RNK_CLASS', $RW, 1],
180 'order' => [11, 'ORDER', 'RNK_ORDER', $RW, 1],
181 'family' => [12, 'FAMILY', 'RNK_FAMILY', $RW, 1],
182 'genus' => [13, 'GENUS', 'RNK_GENUS', $RW, 1],
183 'citation' => [14, 'CITATION', 'CITATION', undef, 3],
184 );
185  
186 4 my %FORMATS = ( TEXT => 1, SQL => 2, CSV => 3);
187  
188 4 my @CTRL = qw(
189 NUL SOH STX ETX EOT ENQ ACK BEL BS TAB LF VT FF CR SO SI
190 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US
191 );
192 # ----------------------------------------------------------------------
193  
194 =head1 FIELDS
195  
196 =over
197  
198 =item Debug: BOOL
199  
200 Flag to switch to the debugging mode.
201  
202 =item Format: STR
203  
204 Field holds the current output format. The tags "TEXT|SQL|CSV" are
205 allowed.
206  
207 =item EndPoint: STR (URI)
208  
209 The current SOAP endpoint defaults to C<$END_POINT>.
210  
211 =item AphiaNS and AphiaURI: STR (URI)
212  
213 The current namespace and corresponding URI, defaulting to $APHIA_NS.
214  
215 =item Soap
216  
217 The SOAP access client instance generate by L<SOAP::Lite>.
218  
219 =back
220  
221 =cut
222  
223 # FIELDS ===============================================================
224 4 has Debug => ( is => 'rwp', isa => Int, default => 0 );
225 4 has Format => ( is => 'rwp', isa => Str, default => $FORMAT );
226 4 has EndPoint => ( is => 'rwp', isa => Str, default => $END_POINT );
227 4 has AphiaNS => ( is => 'rwp', isa => Str, default => $APHIA_NS );
228 4 has AphiaURI => ( is => 'rwp', isa => Str, default => $APHIA_URI );
229 4 has Soap => (
230 is => 'rwp',
231 );
232  
233 # METHODS ==============================================================
234  
235 =head1 METHODS
236  
237 =over
238  
239 =item init()
240  
241 Initializes the SOAP client.
242  
243 Note: All C<change*(...)> functions must be used before the
244 initialization phase.
245  
246 =item _checkInit($what)
247  
248 Internal initialization test to assert the task C<$what> was not
249 recycled.
250  
251 =back
252  
253 =cut
254  
255 1 ▶sub init($self) {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
256 1 $self->_set_Soap( SOAP::Lite->new(
257 uri => $self->AphiaURI,
258 proxy => $self->EndPoint));
259 }
260  
261 4 ▶sub _checkInit($self, $what) {
Subroutine _checkInit: called CC=2 SCAR=7.6
262 4 ▶ die "You have to change the '$what' before the ".
Branch: 1 branch
BranchTrueFalseTotal
if $self->Soap 0 4 4
263 "initialization of the connection!" if $self->Soap;
264 }
265  
266 # ----------------------------------------------------------------------
267  
268 =head2 CONFIGURATION
269  
270 =over
271  
272 =item changeEndPoint($uri)
273  
274 Change the end point for the soap client (don't change).
275  
276 =item changeAphiaURI($uri)
277  
278 Change the namespace URI for the soap client (don't change).
279  
280 =item changeAphiaNS($uri)
281  
282 Change the namespace soap soap client (don't change).
283  
284 =item changeDebug($flag)
285  
286 Demand debugging capabilities.
287  
288 =item changeFormat($format)
289  
290 Changes the output format. Valid formats are TEXT|CSV|SQL.
291  
292 =item getFormats()
293  
294 Get the format dictionary for validation.
295  
296 =back
297  
298 =cut
299  
300 # Function to configure the net access --------------------------------
301  
302 1 ▶sub changeEndPoint($self, $endPoint) {
Subroutine changeEndPoint: called CC=1 SCAR=0.0
Pod documented
303 1 $self->_checkInit('END.POINT');
304 1 $self->_set_EndPoint($endPoint);
305 }
306  
307 1 ▶sub changeAphiaURI($self, $aphiaUri) {
Subroutine changeAphiaURI: called CC=1 SCAR=0.0
Pod documented
308 1 $self->_checkInit('APHIA.URI');
309 1 $self->_set_AphiaURI($aphiaUri);
310 }
311  
312 1 ▶sub changeAphiaNS($self, $aphiaNS) {
Subroutine changeAphiaNS: called CC=1 SCAR=0.0
Pod documented
313 1 $self->_checkInit('APHIA.NS');
314 1 $self->_set_AphiaNS($aphiaNS);
315 }
316  
317 1 ▶sub changeDebug($self, $debug) {
Subroutine changeDebug: called CC=1 SCAR=0.0
Pod documented
318 1 $self->_checkInit('DEBUG');
319 1 $self->_set_Debug($debug);
320 }
321  
322 3 ▶sub changeFormat($self, $format) {
Subroutine changeFormat: called CC=1 SCAR=0.0
Pod documented
323 # $self->_checkInit('FORMAT');
324 3 $self->_set_Format($format);
325 }
326  
327 3 ▶sub getFormats($self) { return \%FORMATS; }
Subroutine getFormats: called CC=1 SCAR=0.0
Pod documented
328  
329 # ----------------------------------------------------------------------
330  
331 =head2 ASSERTATIONS
332  
333 =over
334  
335 =item checkSoap()
336  
337 Check if the soap client is running. The function stops the calling
338 program C<(die ...)> in case of an error.
339  
340 =item checkError($soapResponse)
341  
342 Checks the SOAP query result. The function stops the calling program in
343 case of an communication error.
344  
345 =item checkDebug($queryResult)
346  
347 Prints the query result (dictionary) via L<Data::Dumper> to C<STDERR> if
348 the debug flag is set.
349  
350 =back
351  
352 =cut
353  
354 7 ▶sub checkSoap($self) {
Subroutine checkSoap: called CC=2 SCAR=7.1
Pod documented
355 7 my $res = $self->Soap;
356 7 ▶ die "Missing connection!" if not $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 7 7
357 7 return $res;
358 }
359  
360 6 ▶sub checkError($self, $response) {
Subroutine checkError: called CC=2 SCAR=7.6
Pod documented
361 6 ▶ die "ERROR: ".$response->faultstring."!\n" if $response->fault;
Branch: 1 branch
BranchTrueFalseTotal
if $response->fault 0 6 6
362 }
363  
364 4 ▶sub checkDebug($self, $result) {
Subroutine checkDebug: called CC=2 SCAR=7.6
Pod documented
365 4 ▶ print STDERR "DGB BEGIN:\n",Dumper($result),"EOF\n" if $self->Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->Debug 0 4 4
366 }
367  
368 # ----------------------------------------------------------------
369  
370 =head2 QUERIES
371  
372 =over
373  
374 =item searchSpeciesID($name, $print)
375  
376 Function to find a species by a certain name. The if the flag C<$print>
377 is set. The result will be written to C<STDOUT> on the given format.
378  
379 =item getRecordByID($aphiaID)
380  
381 Retrieves a record for a given AphiaID and returns a dictionary or
382 'dies' with an error.
383  
384 =item getBlockChildrenByID($AphiaID, $offs)
385  
386 Retrieve a set of taxon records under an given AphiaID for a defined
387 offset in the query ensemble. The offset addresses a paging pattern.
388  
389 =item getChildrenByID($aphiaID)
390  
391 Retrieves all children under a given AphiaID using the method
392 C<getBlockChildrenID(...)> and prints the result to C<STDOUT> using the
393 method C<printRecord(...)>.
394  
395 =item searchFuzzy($name, $offs, $fuzzy) EXPERIMENTAL
396  
397 The fuzzy search function is experimantal and does not give
398 proper results due to offset maping issues.
399  
400 =back
401  
402 =cut
403  
404 # Query functions ------------------------------------------------------
405  
406  
407 # Request to WORMS for a specific taxon by name ------------------------
408 2 ▶sub searchSpeciesID($self, $name, $print) {
Subroutine searchSpeciesID: called CC=7 SCAR=29.4
Pod documented
409 2 my $con = $self->checkSoap;
410 2 my $response = $con->call(
411 SOAP::Data->name("getAphiaID")->attr({ "xmlns" => $APHIA_NS})
412 => SOAP::Data->name("ScientificName" => $name)) ;
413 2 $self->checkError($response);
414 2 my $aphia_id = $response->result;
415 2 ▶ die "ERROR: Unknown species with the NAME: '$name'!" if not $aphia_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $aphia_id 0 2 2
416 2 ▶ if ($print) {
Branch: 1 branch
BranchTrueFalseTotal
if ($print) 0 2 2
417 match($self->Format: eq) {
418 case('TEXT') {
419 0 ▶ print "INT APHIA.ID ", $aphia_id, "\n";
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'SQL' ? : 0 0 0
420 }
421 case('SQL') {
422 0 ▶ print "APHIA_ID = ", $aphia_id, "\n"
Condition0%0/3
$(Syntax::Keyword::Match/topic) eq "CSV" and do { print $aphia_id, "\n" if $aphia_id }
!ll&&!rl&&r
000
MC/DC0%0/2
$(Syntax::Keyword::Match/topic) eq "CSV" and do { print $aphia_id, "\n" if $aphia_id }
$(Syntax::Keyword::Match/topic) eq "CSV" do { print $aphia_id, "\n" if $aphia_id }
Truth table0%0/3
A and B
A= $(Syntax::Keyword::Match/topic) eq "CSV"B= do { print $aphia_id, "\n" if $aphia_id }
ABresult
0X0
100
111
423 }
424 0 ▶ case ('CSV') {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'TEXT' ? : 0 0 0
425 0 ▶ print $aphia_id, "\n" if $aphia_id;
Branch: 1 branch
BranchTrueFalseTotal
if $aphia_id 0 0 0
426 }
427 }
428 }
429 2 return $aphia_id;
430 }
431  
432 # Request to WORMS for a specific taxon by AphiaID ---------------------
433 1 ▶sub getRecordByID($self, $id) {
Subroutine getRecordByID: called CC=1 SCAR=0.0
Pod documented
434 1 my $con = $self->checkSoap;
435 1 my $response = $con->call(
436 SOAP::Data->name("getAphiaRecordByID")->attr({ "xmlns" => $APHIA_NS})
437 => SOAP::Data->name("AphiaID") -> value($id)) ;
438 1 $self->checkError($response);
439 1 my $result = $response->result;
440 1 $self->checkDebug($result);
441 1 return $result;
442 }
443  
444 # @TODO THIS REQUEST DONT WORK -----------------------------------------
445 3 ▶sub getBlockChildrenByID($self, $id, $offs) {
Subroutine getBlockChildrenByID: called CC=1 SCAR=0.0
Pod documented
446 3 my $con = $self->checkSoap;
447 3 my $response = $con->call(
448 SOAP::Data->name("getAphiaChildrenByID")->attr({ 'xmlns' => $APHIA_NS})
449 => SOAP::Data->name("AphiaID") -> value($id),
450 => SOAP::Data->name("offset") -> value($offs),
451 => SOAP::Data->name("marine_only") -> value("false")
452 ) ;
453 3 $self->checkError($response);
454 3 my $result = $response->result;
455 3 $self->checkDebug($result);
456 3 return $result;
457 }
458  
459 # DONT WORK ------------------------------------------------------------
460 0 ▶sub searchFuzzy($self, $name, $offs, $fuzzy) {
Subroutine searchFuzzy: not called CC=1 SCAR=6.9
Pod documented
461 0 my $con = $self->checkSoap;
462 # print $name, $offs, $FUZZY, "\n";
463 0 my $response = $con->call(
464 SOAP::Data->name("getAphiaRecords")->attr({ 'xmlns' => $APHIA_NS})
465 => SOAP::Data->name("scientificname") -> value($name),
466 => SOAP::Data->name("offset") -> value($offs),
467 => SOAP::Data->name("marine_only") -> value("false"),
468 => SOAP::Data->name("like") -> value($fuzzy));
469 0 $self->checkError($response);
470 0 my $result = $response->result;
471 0 $self->checkDebug($result);
472 0 return $result;
473 }
474  
475 # ======================================================================
476  
477 1 ▶sub getChildrenByID($self, $sid) {
Subroutine getChildrenByID: called CC=7 SCAR=19.8
Pod documented
478 1 my $offs=0; my $total = 0; my $num=1;
479 1 ▶ print "\nWORMS.CHILDREN ID $sid \n" if $self->Format eq "TEXT";
Branch: 1 branch
BranchTrueFalseTotal
if $self->Format eq "TEXT" 1 0 1
480 1 ▶ while ($num > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num > 0) 3 0 3
481 3 my $found = $self->getBlockChildrenByID($sid, $offs);
482 3 ▶ last if not $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 1 2 3
483 2 my @recs = @$found;
484 2 ▶ if (@recs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@recs) { } 2 0 2
485 2 $num = @recs;
486 2 for my $ix (0..$#recs) {
487 74 $total++;
488 74 my %rec = %{ $recs[$ix] };
489 74 $self->printRecord(\%rec, ($ix==0), ($ix==$#recs) ," ");
490 }
491 2 $offs +=$num+1;
492 0 } else { $num=0; }
493 # print "---- $num $offs $total -----\n";
494 }
495 1 ▶ print "\nEOF\n# WORMS.CHILDREN ID $sid TOTAL: $total\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->Format eq "TEXT" 1 0 1
496 if $self->Format eq "TEXT";
497 1 $total;
498 }
499  
500 # Output service routine record DUMP, CSV and SQL ----------------------
501  
502 =head2 OUTPUT
503  
504 =over
505  
506 =item quoteAuthor($author)
507  
508 The function will translate abbrevation in names like <D'Alambert>
509 to the UTF8 form <D’Alambert> to avoid qouting in SQL fields.
510  
511 =item quoteString($str, $fmt)
512  
513 The function will quote $str to prevent SQL injection problems with:
514  
515 =over
516  
517 =item undef
518  
519 $str = $fmt eq 'SQL' ? 'NULL' : 'NA';
520  
521 =item backspaced control sequences
522  
523 $str = '\r'; # becomes '\\r'
524  
525 =item control sequences
526  
527 $str = "\r"; # becomes '<CR>'
528  
529 =item embedded single quotes
530  
531 $str = "'"; # becomes "''"
532  
533 =back
534  
535 =item printRecord($record, $first, $last, $spc)
536  
537 Prints a record to C<STDOUT> and handles indentation by C<$spc> as well
538 as header and trailing aspects by using the flags C<$first> and C<$last>.
539  
540 =item printRecordDef($record, $record, $spc)
541  
542 Prints the default plain TEXT record to C<STDOUT> and handles
543 indentation by C<$spc>.
544  
545 =back
546  
547 =cut
548  
549 1006 ▶sub quoteString($self, $str, $fmt) {
Subroutine quoteString: called CC=4 SCAR=13.9
Pod documented
550 1006 ▶ unless (defined $str) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $str) 3 1003 1006
551 3 ▶ $str = $fmt eq 'SQL' ? 'NULL' : 'NA';
Branch: 1 branch
BranchTrueFalseTotal
$fmt eq 'SQL' ? : 1 2 3
552 3 return $str;
553 }
554  
555 # Remove control sequences
556 # $str =~ s/\x00/<NUL>/g;
557 # $str =~ s/\r/<CR>/g;
558 # $str =~ s/\n/<NL>/g;
559  
560 # Mask backslashes
561 1003 $str =~ s/\\/\\\\/g;
562  
563 # Remove control sequences
564 1003 ▶ $str =~ s/([\x00-\x1f\x7f])/ '<'.($CTRL[ord($1)] \/\/ 'DEL').'>' /ge;
Condition50%1/2
$CTRL[ord $1] // "DEL"
l!l
20
MC/DC0%0/1
$CTRL[ord $1] // "DEL"
$CTRL[ord $1]
Truth table50%1/2
A
A= $CTRL[ord $1]
Aresult
11
00
565  
566 # Single Quotes
567 1003 $str =~ s/'/''/g;
568 1003 return "'$str'";
569 }
570  
571 155 ▶sub quoteAuthor($self, $author) {
Subroutine quoteAuthor: called CC=2 SCAR=7.1
Pod documented
572 155 ▶ return unless defined $author;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $author 0 155 155
573 155 $author =~ s/'/\x{2019}/g;
574 155 $author;
575 }
576  
577 77 ▶sub printRecord($self, $record, $first, $last, $spc) {
Subroutine printRecord: called CC=26 SCAR=33.3
Pod documented
578 77 my @fields; my @data; my @type; my $fmt = $self->Format;
579 77 my $id = $record->{AphiaID};
580 77 my $nm = $record->{scientificname};
581 77 for my $field (keys %ORDER) {
582  
583 # Get the pasing and substitution rules
584 1155 my ($order, $txtField, $sqlField, $rex, $subs) = @{ $ORDER{$field} };
585 1155 ▶ print("F: $field: O: $order FT: $txtField FS: $sqlField RX: $rex SB: $subs \n")
Branch: 1 branch
BranchTrueFalseTotal
if $self->Debug 0 1155 1155
586 if $self->Debug;
587 # check, escape and pack the data content
588 # the worms field xsd is not very expressive here
589 1155 my $value = $record->{$field};
590 1155 ▶ if (defined $value and defined $rex) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and defined $rex) 1078 77 1155
Condition66%2/3
defined $value and defined $rex
!ll&&!rl&&r
0771078
MC/DC50%1/2
defined $value and defined $rex
defined $value defined $rex
Truth table66%2/3
A and B
A= defined $valueB= defined $rex
ABresult
0X0
100
111
591 1078 ▶ if ($value !~ /$rex/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$rex/u) 0 1078 1078
592 print STDERR "ERROR ID: $id NAME: '$nm' parsing ".
593 "FIELD: '$field' VALUE: '$value' is not a valid ".
594 0 $TYPES{$rex}." descriptor!\n" ;
595 0 return 0;
596 }
597 }
598 # Replace ' by ’ in Author stuff like D'Alambert
599 1155 ▶ $value = $self->quoteAuthor($value)
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and $subs == 2 154 1001 1155
Condition66%2/3
defined $value and $subs == 2
!ll&&!rl&&r
01001154
MC/DC50%1/2
defined $value and $subs == 2
defined $value $subs == 2
Truth table66%2/3
A and B
A= defined $valueB= $subs == 2
ABresult
0X0
100
111
600 if (defined $value) and ($subs == 2);
601  
602 # Add, NULL/NA and sanitize and quote strings
603 1155 ▶ if ($subs != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subs != 0) { } 1001 154 1155
604 1001 $value = $self->quoteString($value, $fmt);
605 } else {
606 # Nulling integers
607 154 ▶ $value //= $fmt eq 'SQL' ? 'NULL' : 'NA';
Branch: 1 branch
BranchTrueFalseTotal
$fmt eq 'SQL' ? : 0 0 0
Condition50%1/2
$value //= $fmt eq "SQL" ? "NULL" : "NA"
l!l
1540
MC/DC0%0/1
$value //= $fmt eq "SQL" ? "NULL" : "NA"
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
608 }
609 # Pack it in the predefined order
610 1155 $data[$order] = $value;
611 1155 ▶ $fields[$order] = ($fmt eq 'SQL') ? $sqlField : $txtField;
Branch: 1 branch
BranchTrueFalseTotal
$fmt eq 'SQL' ? : 15 1140 1155
612  
613 # The WoRMS xsd makes no deeper differentiation here
614 1155 ▶ $type[$order] = $subs ? 'STR' : 'INT';
Branch: 1 branch
BranchTrueFalseTotal
$subs ? : 1001 154 1155
615 }
616 77 ▶ return 0 if not @data;
Branch: 1 branch
BranchTrueFalseTotal
unless @data 0 77 77
617  
618 # return if $record{status} =~ "deleted";
619
620 # header section ---------------------------------------------------
621 77 ▶ if ($first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) 5 72 77
622 5 ▶ print join(",",@fields), "\n" if $fmt eq "CSV";
Branch: 1 branch
BranchTrueFalseTotal
if $fmt eq "CSV" 1 4 5
623 5 ▶ print "INSERT INTO WORMS_TABLE\n(",join(",",@fields),") ",
Branch: 1 branch
BranchTrueFalseTotal
if $fmt eq "SQL" 1 4 5
624 "VALUES\n" if $fmt eq "SQL";
625 }
626 # data section -----------------------------------------------------
627 77 ▶ print join(",", @data),"\n" if $fmt eq "CSV";
Branch: 1 branch
BranchTrueFalseTotal
if $fmt eq "CSV" 1 76 77
628 77 ▶ print "(",join(",", @data),")" if $fmt eq "SQL";
Branch: 1 branch
BranchTrueFalseTotal
if $fmt eq "SQL" 1 76 77
629 77 ▶ if ($self->Format eq "TEXT") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Format eq "TEXT") 75 2 77
630 75 ▶ print "\n", $spc, "WORMS ID $id \n" if $fmt eq 'TEXT';
Branch: 1 branch
BranchTrueFalseTotal
if $fmt eq "TEXT" 75 0 75
631 75 for my $ix (0..$#fields) {
632 1125 printf "%s\t%s\t%-16s\t%s\n",
633 $spc, $type[$ix], $fields[$ix], $data[$ix];
634 }
635 75 print $spc,"EOF\n";
636 }
637 # footer section ---------------------------------------------------
638 77 ▶ if ($last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last) { } 5 72 77
639 5 ▶ print "\n", if $self->Format eq "TEXT";
Branch: 1 branch
BranchTrueFalseTotal
if $self->Format eq "TEXT" 3 2 5
640 5 ▶ print ";\n" if $self->Format eq "SQL";
Branch: 1 branch
BranchTrueFalseTotal
if $self->Format eq "SQL" 1 4 5
641 } else {
642 72 ▶ print ",\n" if $self->Format eq "SQL";
Branch: 1 branch
BranchTrueFalseTotal
if $self->Format eq "SQL" 0 72 72
643 }
644 77 1
645 }
646  
647 # WORMS dump Data ------------------------------------------------------
648 3 ▶sub printRecordDef($self, $record, $spc) {
Subroutine printRecordDef: called CC=1 SCAR=0.0
Pod documented
649 3 $self->printRecord($record, 1, 1, $spc);
650 3 1
651 }
652  
653 =head1 BUGS
654  
655 Please report bugs via the issue tracker: L<https://rt.cpan.org>.
656  
657 =cut
658  
659 =head1 AUTHOR
660  
661 Alexander Weidauer E<lt>alex.weidauer@huckfinn.deE<gt>
662  
663 =head1 COPYRIGHT AND LICENSE
664  
665 Copyright (C) 2012 by Alexander Weidauer
666  
667 This program is free software: you can redistribute it and/or modify it
668 under the terms of the GNU General Public License as published by the
669 Free Software Foundation, either version 3 of the License, or any later
670 version.
671  
672 This program is distributed in the hope that it will be useful, but
673 WITHOUT ANY WARRANTY; without even the implied warranty of
674 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
675 General Public License for more details.
676  
677 You should have received a copy of the GNU General Public License along
678 with this program. If not, see <http://www.gnu.org/licenses/>.
679  
680 =cut
681  
682 4 1;
683  
684 # EOF ==================================================================
↑ Summary