blib/lib/Bib/CrossRef.pm

filter: Statementstmt 49.3% 161 / 326 Branchbran 13.8% 15 / 108 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 42 / 48 Podpod 100% 22 / 22 totaltotal 47.1% 240 / 509 CC 59 SCAR 59.0
CC
59
Cov
40%
parse_doi
1122.0
print
71.5
parse_text
16.4
CRAP 363.6
↑ Summary
1 ############################################################
2 #
3 # Bib::CrossRef - Uses crossref to robustly parse bibliometric references.
4 #
5 ############################################################
6  
7 package Bib::CrossRef;
8  
9 2 ▶use 5.8.8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 require Exporter;
15 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use JSON qw/decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use URI::Escape qw(uri_escape_utf8 uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use HTML::Entities qw(decode_entities encode_entities);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 #use Data::Dumper;
23  
24 2 $VERSION = '0.10';
25 2 @ISA = qw(Exporter);
26 2 @EXPORT = qw();
27 2 @EXPORT_OK = qw(
28 sethtml clearhtml parse_text parse_doi print printheader printfooter
29 doi score date atitle jtitle volume issue genre spage epage authcount auth query
30 );
31 2 %EXPORT_TAGS = (all => \@EXPORT_OK);
32  
33 sub new {
34 2 ▶ my $self;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
35 2 $self->{html} = 0; # use html for error messages ?
36 2 $self->{ref} = {}; # the reference itself
37 2 bless $self;
38 2 return $self;
39 }
40  
41 sub sethtml {
42 1 ▶ $_[0]->{html} = 1;
Subroutine sethtml: called CC=1 SCAR=0.0
Pod documented
43 }
44  
45 sub clearhtml {
46 0 ▶ $_[0]->{html} = 0;
Subroutine clearhtml: not called CC=1 SCAR=6.9
Pod documented
47 }
48  
49 sub _err {
50 1 ▶ my ($self, $str) = @_;
Subroutine _err: called CC=2 SCAR=7.6
51 1 ▶ if ($self->{html}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'html'}) { } 0 1 1
52 0 print "<p style='color:red'>",$str,"</p>";
53 } else {
54 1 print $str,"\n";
55 }
56 }
57  
58 sub doi {
59 3 ▶ my $self = shift @_;
Subroutine doi: called CC=1 SCAR=0.0
Pod documented
60 3 return $self->{ref}->{'doi'};
61 }
62  
63 sub _setdoi {
64 1 ▶ my $self = shift @_;
Subroutine _setdoi: called CC=1 SCAR=0.0
65 1 my $val = shift @_;
66 1 $self->{ref}->{'doi'}=$val;
67 }
68  
69 sub url {
70 1 ▶ my $self = shift @_;
Subroutine url: called CC=1 SCAR=0.0
Pod documented
71 1 return $self->{ref}->{'url'};
72 }
73  
74 sub _seturl {
75 0 ▶ my $self = shift @_;
Subroutine _seturl: not called CC=1 SCAR=6.9
76 0 my $val = shift @_;
77 0 $self->{ref}->{'url'}=$val;
78 }
79  
80 sub score {
81 2 ▶ my $self = shift @_;
Subroutine score: called CC=1 SCAR=0.0
Pod documented
82 2 return $self->{ref}->{'score'};
83 }
84  
85 sub _setscore {
86 1 ▶ my $self = shift @_;
Subroutine _setscore: called CC=1 SCAR=0.0
87 1 my $val = shift @_;
88 1 $self->{ref}->{'score'}=$val;
89 }
90  
91 sub atitle {
92 2 ▶ my $self = shift @_;
Subroutine atitle: called CC=1 SCAR=0.0
Pod documented
93 2 return $self->{ref}->{'atitle'};
94 }
95  
96 sub _setatitle {
97 1 ▶ my $self = shift @_;
Subroutine _setatitle: called CC=1 SCAR=0.0
98 1 my $val = shift @_;
99 1 $self->{ref}->{'atitle'}=$val;
100 }
101  
102 sub jtitle {
103 2 ▶ my $self = shift @_;
Subroutine jtitle: called CC=1 SCAR=0.0
Pod documented
104 2 return $self->{ref}->{'jtitle'};
105 }
106  
107 sub _setjtitle {
108 1 ▶ my $self = shift @_;
Subroutine _setjtitle: called CC=1 SCAR=0.0
109 1 my $val = shift @_;
110 1 $self->{ref}->{'jtitle'}=$val;
111 }
112  
113 sub volume {
114 3 ▶ my $self = shift @_;
Subroutine volume: called CC=1 SCAR=0.0
Pod documented
115 3 return $self->{ref}->{'volume'};
116 }
117  
118 sub _setvolume {
119 1 ▶ my $self = shift @_;
Subroutine _setvolume: called CC=1 SCAR=0.0
120 1 my $val = shift @_;
121 1 $self->{ref}->{'volume'}=$val;
122 }
123  
124 sub issue {
125 3 ▶ my $self = shift @_;
Subroutine issue: called CC=1 SCAR=0.0
Pod documented
126 3 return $self->{ref}->{'issue'};
127 }
128  
129 sub _setissue {
130 1 ▶ my $self = shift @_;
Subroutine _setissue: called CC=1 SCAR=0.0
131 1 my $val = shift @_;
132 1 $self->{ref}->{'issue'}=$val;
133 }
134  
135 sub date {
136 2 ▶ my $self = shift @_;
Subroutine date: called CC=1 SCAR=0.0
Pod documented
137 2 return $self->{ref}->{'date'};
138 }
139  
140 sub _setdate {
141 1 ▶ my $self = shift @_;
Subroutine _setdate: called CC=1 SCAR=0.0
142 1 my $val = shift @_;
143 1 $self->{ref}->{'date'}=$val;
144 }
145  
146 sub genre {
147 2 ▶ my $self = shift @_;
Subroutine genre: called CC=1 SCAR=0.0
Pod documented
148 2 return $self->{ref}->{'genre'};
149 }
150  
151 sub _setgenre {
152 1 ▶ my $self = shift @_;
Subroutine _setgenre: called CC=1 SCAR=0.0
153 1 my $val = shift @_;
154 1 $self->{ref}->{'genre'}=$val;
155 }
156  
157 sub spage {
158 3 ▶ my $self = shift @_;
Subroutine spage: called CC=1 SCAR=0.0
Pod documented
159 3 return $self->{ref}->{'spage'};
160 }
161  
162 sub _setspage {
163 1 ▶ my $self = shift @_;
Subroutine _setspage: called CC=1 SCAR=0.0
164 1 my $val = shift @_;
165 1 $self->{ref}->{'spage'}=$val;
166 }
167  
168 sub epage {
169 3 ▶ my $self = shift @_;
Subroutine epage: called CC=1 SCAR=0.0
Pod documented
170 3 return $self->{ref}->{'epage'};
171 }
172  
173 sub _setepage {
174 1 ▶ my $self = shift @_;
Subroutine _setepage: called CC=1 SCAR=0.0
175 1 my $val = shift @_;
176 1 $self->{ref}->{'epage'}=$val;
177 }
178  
179 sub authcount {
180 4 ▶ my $self = shift @_;
Subroutine authcount: called CC=1 SCAR=0.0
Pod documented
181 4 return $self->{ref}->{'authcount'};
182 }
183  
184 sub _setauthcount {
185 1 ▶ my $self = shift @_;
Subroutine _setauthcount: called CC=1 SCAR=0.0
186 1 my $val = shift @_;
187 1 $self->{ref}->{'authcount'}=$val;
188 }
189  
190 sub auth {
191 4 ▶ my ($self, $num) = @_;
Subroutine auth: called CC=1 SCAR=0.0
Pod documented
192 4 return $self->{ref}->{'au'.$num};
193 }
194  
195 sub _setauth {
196 2 ▶ my $self = shift @_;
Subroutine _setauth: called CC=1 SCAR=0.0
197 2 my $i = shift @_;
198 2 my $val = shift @_;
199 2 $self->{ref}->{'au'.$i}=$val;
200 }
201  
202 sub query {
203 1 ▶ my $self = shift @_;
Subroutine query: called CC=1 SCAR=0.0
Pod documented
204 1 return $self->{ref}->{'query'};
205 }
206  
207 sub _setquery {
208 0 ▶ my $self = shift @_;
Subroutine _setquery: not called CC=1 SCAR=6.9
209 0 my $val = shift @_;
210 0 $self->{ref}->{'query'}=$val;
211 }
212  
213 sub parse_text {
214 # given free format text, use crossref.org to try to convert into a paper reference and doi
215 1 ▶ my ($self, $cites) = @_;
Subroutine parse_text: called CC=5 SCAR=28.0
Pod documented
216
217 1 my $cites_clean = $cites;
218 # tidy up string, escape nasty characters etc.
219 1 $cites_clean =~ s/\s+/+/g; #$cites_clean = uri_escape_utf8($cites_clean);
220 1 my $req = HTTP::Request->new(GET => 'http://search.crossref.org/dois?q='.$cites_clean);
221 1 my $ua = LWP::UserAgent->new;
222 1 my $res = $ua->request($req);
223 1 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 1 1
224 # extract json response
225 0 my $json = decode_json($res->decoded_content);
226 0 my $ref={};
227 # keep a record of the query string we used
228 0 $ref->{'query'} = $cites;
229 # extract doi and matching score
230 0 $ref->{'doi'} = $json->[0]{'doi'};
231 0 $ref->{'url'} = $json->[0]{'doi'};
232 0 $ref->{'score'} = $json->[0]{'score'}; #$json->[0]{'normalizedScore'};
233 # and get the rest of the details from the coins encoded payload ...
234 0 ▶ if (exists $json->[0]{'coins'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $json->[0]{"coins"}) 0 0 0
235 0 my $coins = $json->[0]{'coins'};
236 0 my @list = split(';',$coins);
237 0 my $authcount=0;
238 0 foreach my $val (@list) {
239 0 my @pieces = split('=',$val);
240 0 $pieces[0] =~ s/rft\.//;
241 0 ▶ if ($pieces[0] =~ m/au$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pieces[0] =~ /au$/) 0 0 0
242 0 $authcount++;
243 0 $pieces[0] = 'au'.$authcount;
244 }
245 0 $pieces[1] = uri_unescape($pieces[1]);
246 0 $pieces[1] = decode_entities($pieces[1]); # shouldn't be needed, but some html can creep into titles etc
247 0 $pieces[1] =~ s/\&$//; $pieces[1] =~ s/\s+//g; $pieces[1] =~ s/\+/ /g;
248 0 $pieces[1] =~ s/^\s+//;
249 0 $ref->{$pieces[0]} = $pieces[1];
250 }
251 0 $ref->{'authcount'} = $authcount;
252 0 $self->{ref} = $ref;
253 }
254 } else {
255 1 $self->_err("Problem with search.crossref.org: ".$res->status_line);
256 }
257 }
258  
259 sub parse_doi {
260 # given a DOI, use unixref interface to convert into a full citation
261 0 ▶ my ($self, $doi) = @_;
Subroutine parse_doi: not called CC=33 SCAR=70.2
Pod documented
262
263 0 my $req = HTTP::Request->new(GET =>'http://dx.doi.org/'.$doi,['Accept' =>'application/vnd.crossref.unixsd+xml']);
264 0 my $ua = LWP::UserAgent->new;
265 0 my $res = $ua->request($req);
266 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
267 # now parse the xml
268 0 my $xs = XML::Simple->new();
269 0 my $data = $xs->XMLin($res->decoded_content);
270 0 my $cite = $data->{'query_result'}->{'body'}->{'query'}->{'doi_record'}->{'crossref'};
271 0 my $cc = undef;
272 0 ▶ if (exists($cite->{'conference'})) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $cite->{'conference'}) { } 0 0 0
elsif (exists $cite->{'journal'}) { } 0 0 0
elsif (exists $cite->{'book'}) { } 0 0 0
273 0 $self->_setgenre('proceeding');
274 0 ▶ if (exists($cite->{'conference'}->{'proceedings_metadata'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cite->{'conference'}{'proceedings_metadata'}) { } 0 0 0
275 0 $self->_setjtitle($cite->{'conference'}->{'proceedings_metadata'}->{'proceedings_title'});
276 0 ▶ if (exists $cite->{'conference'}->{'proceedings_metadata'}->{'publication_date'}) {$self->_setdate($cite->{'conference'}->{'proceedings_metadata'}->{'publication_date'}->{'year'});}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cite->{"conference"}{"proceedings_metadata"}{"publication_date"}) 0 0 0
277 } else {
278 0 $self->_setjtitle($cite->{'conference'}->{'proceedings_series_metadata'}->{'series_metadata'}->{'proceedings_title'});
279 0 $self->_setvolume($cite->{'conference'}->{'proceedings_series_metadata'}->{'series_metadata'}->{'volume'});
280 0 ▶ if (exists $cite->{'conference'}->{'proceedings_series_metadata'}->{'publication_date'}) {$self->_setdate($cite->{'conference'}->{'proceedings_series_metadata'}->{'publication_date'}->{'year'});}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cite->{"conference"}{"proceedings_series_metadata"}{"publication_date"}) 0 0 0
281 }
282 0 $cc = $cite->{'conference'}->{'conference_paper'};
283 } elsif (exists($cite->{'journal'})) {
284 0 $self->_setgenre('article');
285 0 $self->_setjtitle($cite->{'journal'}->{'journal_metadata'}->{'full_title'});
286 0 $cc = $cite->{'journal'}->{'journal_issue'};
287 0 ▶ if (exists($cc->{'journal_volume'})) {$self->_setvolume($cc->{'journal_volume'}->{'volume'});}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cc->{"journal_volume"}) 0 0 0
288 0 ▶ if (exists($cc->{'issue'})) {$self->_setissue($cc->{'issue'});}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cc->{"issue"}) 0 0 0
289 0 $cc = $cite->{'journal'}->{'journal_article'};
290 } elsif (exists($cite->{'book'})) {
291 0 ▶ if ($cite->{'book'}->{'book_type'} ne 'other' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cite->{'book'}{'book_type'} ne 'other') { } 0 0 0
292 0 $self->_setgenre('book');
293 } else {
294 0 $self->_setgenre('bookitem');
295 }
296 0 my $jtitle = '';
297 0 ▶ if (exists($cite->{'book'}->{'book_series_metadata'})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $cite->{'book'}{'book_series_metadata'}) { } 0 0 0
elsif (exists $cite->{'book'}{'book_metadata'}) { } 0 0 0
298 0 ▶ if (exists($cite->{'book'}->{'book_series_metadata'}->{'titles'}->{'title'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cite->{"book"}{"book_series_metadata"}{"titles"}{"title"}) 0 0 0
299 0 $jtitle .= $cite->{'book'}->{'book_series_metadata'}->{'titles'}->{'title'}.': ';
300 }
301 0 $jtitle .= $cite->{'book'}->{'book_series_metadata'}->{'series_metadata'}->{'titles'}->{'title'};
302 0 $self->_setvolume($cite->{'book'}->{'book_series_metadata'}->{'volume'});
303 0 ▶ if (exists $cite->{'book'}->{'book_series_metadata'}->{'publication_date'}) {$self->_setdate($cite->{'book'}->{'book_series_metadata'}->{'publication_date'}->{'year'});}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cite->{"book"}{"book_series_metadata"}{"publication_date"}) 0 0 0
304 } elsif (exists($cite->{'book'}->{'book_metadata'})) {
305 0 $jtitle .= $cite->{'book'}->{'book_metadata'}->{'titles'}->{'title'};
306 0 ▶ if (exists($cite->{'book'}->{'book_metadata'}->{'series_metadata'}->{'titles'}->{'title'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cite->{"book"}{"book_metadata"}{"series_metadata"}{"titles"}{"title"}) 0 0 0
307 0 $jtitle .= ": ".$cite->{'book'}->{'book_metadata'}->{'series_metadata'}->{'titles'}->{'title'};
308 0 ▶ if (exists $cite->{'book'}->{'book_metadata'}->{'series_metadata'}->{'volume'}) {$self->_setvolume($cite->{'book'}->{'book_metadata'}->{'series_metadata'}->{'volume'});}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cite->{"book"}{"book_metadata"}{"series_metadata"}{"volume"}) 0 0 0
309 }
310 0 ▶ if (exists $cite->{'book'}->{'book_metadata'}->{'volume'}) {$self->_setvolume($cite->{'book'}->{'book_metadata'}->{'volume'});}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cite->{"book"}{"book_metadata"}{"volume"}) 0 0 0
311 0 ▶ if (exists $cite->{'book'}->{'book_metadata'}->{'publication_date'}) {$self->_setdate($cite->{'book'}->{'book_metadata'}->{'publication_date'}->{'year'});}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cite->{"book"}{"book_metadata"}{"publication_date"}) 0 0 0
312 } else {
313 0 ▶ if (exists($cite->{'book'}->{'book_set_metadata'}->{'titles'}->{'title'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cite->{"book"}{"book_set_metadata"}{"titles"}{"title"}) 0 0 0
314 0 $jtitle .= $cite->{'book'}->{'book_set_metadata'}->{'titles'}->{'title'}.': ';
315 }
316 0 $jtitle .= $cite->{'book'}->{'book_set_metadata'}->{'set_metadata'}->{'titles'}->{'title'};
317 }
318 0 $self->_setjtitle($jtitle);
319 0 $cc = $cite->{'book'}->{'content_item'};
320 } else {
321 # something else -- might be dissertation, report-paper, standard, sa-component, database
322 # fall back to alternative interface for now
323 0 $self->parse_text($doi);
324 0 return; # stop here
325 }
326 0 $self->_setscore(1);
327 0 $self->_setquery($doi);
328 0 $self->_setdoi($doi);
329 0 ▶ if (!defined $cc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cc) 0 0 0
330 # seems like an incomplete entry
331 0 return;
332 }
333 #$self->_setatitle($cc->{'titles'}->{'title'});
334 0 my $title;
335 0 ▶ if (ref $cc->{titles} ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cc->{'titles'} ne 'HASH') { } 0 0 0
336 0 $title = $cc->{titles}->[0];
337 } else {
338 0 $title = $cc->{'titles'}->{'title'};
339 }
340 0 $self->_setatitle($title);
341 0 $self->_setdoi($cc->{'doi_data'}->{'doi'});
342 0 ▶ if (ref($cc->{'publication_date'}) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cc->{'publication_date'} eq 'HASH') { } 0 0 0
343 0 $self->_setdate($cc->{'publication_date'}->{'year'});
344 } else { # we have multiple dates, lets try and pick put the print date
345 0 my $found = 0; my $count=0;
346 0 foreach my $d (@{$cc->{'publication_date'}}) {
347 0 ▶ if ($d->{'media_type'} eq 'print') {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->{"media_type"} eq "print") 0 0 0
348 0 $self->_setdate($d->{'year'}); $found = 1;
349 }
350 0 $count++;
351 }
352 0 ▶ if ((!$found) && ($count>0)) {$self->_setdate(${$cc->{'publication_date'}}[0]->{'year'});}
Branch: 1 branch
BranchTrueFalseTotal
if (not $found and $count > 0) 0 0 0
Condition0%0/3
not $found and $count > 0
!ll&&!rl&&r
000
MC/DC0%0/2
not $found and $count > 0
not $found $count > 0
Truth table0%0/3
not(A) and B
A= not $foundB= $count > 0
ABresult
0X0
100
111
353 }
354 0 ▶ if (exists(${$cc}{'pages'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $cc }->{"pages"}) 0 0 0
355 0 ▶ if (exists(${$cc->{'pages'}}{'first_page'})) {$self->_setspage($cc->{'pages'}->{'first_page'});}
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $$cc{"pages"} }->{"first_page"}) 0 0 0
356 0 ▶ if (exists(${$cc->{'pages'}}{'last_page'})) {$self->_setepage($cc->{'pages'}->{'last_page'});}
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $$cc{"pages"} }->{"last_page"}) 0 0 0
357 }
358 0 $cc = $cc->{'contributors'}->{'person_name'};
359 0 ▶ if (ref($cc) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cc eq 'HASH') { } 0 0 0
360 0 $self->_setauthcount(1);
361 0 $self->_setauth(1,$cc->{'given_name'}.' '.$cc->{'surname'});
362 } else {
363 0 my $count = 0;
364 0 foreach my $au (@{$cc}) {
365 0 ▶ if ($au->{'contributor_role'} ne 'author') {next;}
Branch: 1 branch
BranchTrueFalseTotal
if ($au->{"contributor_role"} ne "author") 0 0 0
366 0 $count++;
367 0 $self->_setauth($count, $au->{'given_name'}.' '.$au->{'surname'});
368 }
369 0 $self->_setauthcount($count);
370 }
371 } else {
372 0 $self->_err("Problem with search.crossref.org/guestquery: ".$res->status_line);
373 }
374 }
375  
376 sub printheader {
377 0 ▶ my $self = shift @_;
Subroutine printheader: not called CC=2 SCAR=17.9
Pod documented
378 0 my $id = shift @_;
379 0 my $str = '';
380 0 ▶ if (defined $id) {$str = 'id="'.$id.'"';}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 0 0 0
381 0 return '<table '.$str.'><tr style="font-weight:bold"><td></td><td>Use</td><td></td><td>Type</td><td>Year</td><td>Authors</td><td>Title</td><td>Journal</td><td>Volume</td><td>Issue</td><td>Pages</td><td>DOI</td><td>url</td><td></td></tr>'."\n";
382 }
383  
384 sub printfooter {
385 0 ▶ return "</table>\n";
Subroutine printfooter: not called CC=1 SCAR=6.9
Pod documented
386 }
387  
388 sub _authstring {
389 1 ▶ my $self = shift @_;
Subroutine _authstring: called CC=3 SCAR=11.0
390 1 my $out='';
391 1 ▶ if ($self->authcount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->authcount > 0) 1 0 1
392 1 $out = $self->auth(1);
393 1 ▶ for (my $j = 2; $j <= $self->authcount; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $self->authcount) 1 1 2
394 1 $out.=' and '.$self->auth($j);
395 }
396 }
397 1 return $out;
398 }
399  
400 sub print {
401 # return a reference in human readable form
402 1 ▶ my ($self, $id, $add) = @_;
Subroutine print: called CC=19 SCAR=42.7
Pod documented
403 1 my $ref = $self->{ref};
404 1 ▶ if (!defined $id) {$id='';}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 1 1
405 1 ▶ if (!defined $add) {$add='';}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $add) 1 0 1
406
407 1 my $out='';
408 1 ▶ if ($self->{html}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'html'}) { } 1 0 1
409 1 $out.=sprintf "%s", '<tr id="cite">';
410 1 $out.=sprintf "%s", '<td>'.$id.'</td>';
411 1 ▶ if ($self->score<1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->score < 1) { } 0 1 1
412 0 $out.=sprintf "%s", '<td><input type="checkbox" name="'.$id.'" value=""></td>';
413 0 $out.=sprintf "%s", '<td style="color:red">Poor match</td>';
414 } else {
415 1 $out.=sprintf "%s", '<td><input type="checkbox" name="'.$id.'" value="" checked></td><td></td>';
416 }
417 1 $out.=sprintf "%s", '<td contenteditable="true">'.$self->genre.'</td><td contenteditable="true">'.$self->date.'</td>';
418 1 $out.=sprintf "%s", '<td contenteditable="true">'.$self->_authstring.'</td>';
419 1 $out.=sprintf "%s", '<td contenteditable="true">'.$self->atitle.'</td><td contenteditable="true">'.encode_entities($self->jtitle).'</td>';
420 1 $out.=sprintf "%s", '<td contenteditable="true">';
421 1 ▶ if (defined $self->volume) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->volume) 1 0 1
422 1 $out.=sprintf "%s", $self->volume;
423 }
424 1 $out.=sprintf "%s", '</td><td contenteditable="true">';
425 1 ▶ if (defined $self->issue) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->issue) 1 0 1
426 1 $out.=sprintf "%s", $self->issue;
427 }
428 1 $out.=sprintf "%s", '</td><td contenteditable="true">';
429 1 ▶ if (defined $self->spage) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->spage) 1 0 1
430 1 $out.=sprintf "%s", $self->spage;
431 }
432 1 ▶ if (defined $self->epage) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->epage) 1 0 1
433 1 $out.=sprintf "%s", '-'.$self->epage;
434 }
435 1 $out.=sprintf "%s", '</td><td contenteditable="true">';
436 1 ▶ if (defined $self->doi) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->doi) 1 0 1
437 1 my $doi = $self->doi;
438 1 $doi =~ s/http:\/\/dx.doi.org\///;
439 1 $out.=$doi;
440 }
441 1 $out.=sprintf "%s", '</td><td contenteditable="true">';
442 1 ▶ if (defined $self->url) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->url) 0 1 1
443 0 $out.=sprintf "%s", '<a href='.$self->url.'>'.$self->url.'</a>';
444 }
445 1 $out.= '</td><td>'.$add;
446 1 $out.=sprintf "%s", '</td></tr>'."\n";
447 1 $out.=sprintf "%s", '<tr><td colspan=12 style="color:#C0C0C0">'.encode_entities($self->query).'</td></tr>'."\n";
448 } else {
449 0 ▶ if (length($id)>0) {$out .= $id.". ";}
Branch: 1 branch
BranchTrueFalseTotal
if (length $id > 0) 0 0 0
450 0 ▶ if ($self->score<1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->score < 1) { } 0 0 0
451 0 $out.=sprintf "%s", 'Poor match (score='.$self->score."):\n";
452 0 $out.=sprintf "%s", $self->query."\n";
453 } else {
454 #print "$count. ";
455 }
456 0 $out.=sprintf "%s", $self->genre.': '.$self->date.", ".$self->_authstring.", ";
457 0 $out.=sprintf "%s", "\'".$self->atitle."\'. ".$self->jtitle;
458 0 ▶ if (defined $self->volume) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->volume) 0 0 0
459 0 $out.=sprintf "%s", ", ".$self->volume;
460 0 ▶ if (defined $self->issue) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->issue) 0 0 0
461 0 $out.=sprintf "%s", "(".$self->issue.")";
462 }
463 }
464 0 ▶ if (defined $self->spage) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->spage) 0 0 0
465 0 $out.=sprintf "%s", ",pp".$self->spage;
466 }
467 0 ▶ if (defined $self->epage) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->epage) 0 0 0
468 0 $out.=sprintf "%s", '-'.$self->epage;
469 }
470 0 ▶ if (defined $self->doi) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->doi) 0 0 0
471 0 my $doi = $self->doi;
472 0 $doi =~ s/http:\/\/dx.doi.org\///;
473 0 $out.=sprintf "%s", ", DOI: ".$doi;
474 }
475 0 ▶ if (defined $self->url) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->url) 0 0 0
476 0 $out.=sprintf "%s", ", ".$self->url;
477 }
478 }
479 1 return $out;
480 }
481  
482 2 1;
483  
484 =pod
485
486 =head1 NAME
487
488 Bib::CrossRef - Uses crossref to robustly parse bibliometric references.
489
490 =head1 SYNOPSIS
491  
492 use strict;
493 use Bib::CrossRef;
494  
495 # Create a new object
496  
497 my $ref = Bib::CrossRef->new();
498  
499 # Supply some details, Bib::CrossRef will do its best to use this to derive full citation details e.g. the DOI of a document ...
500  
501 $ref->parse_text('10.1109/jstsp.2013.2251604');
502
503 # Show the full citation details, in human readable form
504  
505 print $ref->print();
506  
507 article: 2013, Alessandro Checco and Douglas J. Leith, 'Learning-Based Constraint Satisfaction With Sensing Restrictions'. IEEE Journal of Selected Topics in Signal Processing, 7(5),pp811-820, DOI: http://dx.doi.org/10.1109/jstsp.2013.2251604
508  
509 # Show the full citation details, in html format
510  
511 $ref->sethtml;
512 print $ref->printheader;
513 print $ref->print;
514 print $ref->printfooter;
515  
516  
517 =head1 EXAMPLES
518  
519 A valid DOI will always be resolved to a full citation
520 e.g.
521  
522 $ref->sparse_text('10.1109/jstsp.2013.2251604');
523 print $ref->print();
524
525 article: 2013, Alessandro Checco and Douglas J. Leith, 'Learning-Based Constraint Satisfaction With Sensing Restrictions'. IEEE Journal of Selected Topics in Signal Processing, 7(5),pp811-820, DOI: http://dx.doi.org/10.1109/jstsp.2013.2251604
526  
527 An attempt will be made to resolve almost any text containing citation info
528 e.g. article title only
529  
530 $ref->parse_text('Learning-Based Constraint Satisfaction With Sensing Restrictions');
531  
532 e.g. author and journal
533  
534 $ref->parse_text('Alessandro Checco, Douglas J. Leith, IEEE Journal of Selected Topics in Signal Processing, 7(5)');
535  
536 Please bear in mind that crossref provides a great service for free -- don't abuse it by making excessive queries. If making many queries, be
537 sure to rate limit them to a sensible level or you will likely get blocked.
538  
539 =head1 METHODS
540
541 =head2 new
542  
543 my $ref = Bib::CrossRef->new();
544  
545 Creates a new Bib::CrossRef object
546  
547 =head2 parse_text
548  
549 $ref->parse_text($string)
550  
551 Given a text string, Bib::CrossRef will try to resolve into a full citation with the help of crossref.org
552  
553 =head2 parse_doi
554  
555 $ref->parse_doi($doi)
556  
557 Given a string containing a DOI e.g. 10.1109/tnet.2012.2202686, resolve into a full citation with the unixref
558 interface of crossref.org. This should be definitive publishers DOI and is usually
559 much as the same as calling $ref->parse_text($doi), but is included completeness.
560  
561 =head2 doi
562  
563 my $info = $ref->doi
564  
565 Returns a string containg the DOI (digital object identifier) field from a full citation. If present, this
566 should be unique to the document.
567  
568 =head2 score
569  
570 my $info = $ref->score
571  
572 Returns a matching score from crossref.org. If less than 1, the text provided to set_details() was likely
573 insufficient to allow the correct full citation to be obtained.
574  
575 =head2 genre
576  
577 my $info = $ref->genre
578  
579 Returns the type of publication e.g. jounal paper, conference paper etc
580  
581 =head2 date
582  
583 my $info = $ref->date
584  
585 Returns the year of publication
586  
587 =head2 atitle
588  
589 my $info = $ref->atitle
590  
591 Returns the article title
592  
593 =head2 jtitle
594  
595 my $info = $ref->jtitle
596  
597 Returns the name of the journal (in long form)
598  
599 =head2 authcount
600  
601 my $info = $ref->authcount
602  
603 Returns the number of authors
604  
605 =head2 auth
606  
607 my $info = $ref->auth($num)
608  
609 Get the name of author number $num (first author is $ref->auth(1))
610  
611 =head2 volume
612  
613 my $info = $ref->volume
614  
615 Returns the volume number in which paper appeared
616  
617 =head2 issue
618  
619 my $info = $ref->issue
620  
621 Returns the issue number in which paper appeared
622  
623 =head2 spage
624  
625 my $info = $ref->spage
626  
627 Returns the start page
628  
629 =head2 epage
630  
631 my $info = $ref->epage
632  
633 Returns the end page
634  
635 =head2 url
636  
637 Return the url, if any
638  
639 =head2 query
640  
641 my $info = $ref->query
642
643 Returns the free form string from which full citation is derived
644  
645 =head2 print
646  
647 print $ref->printheader;
648  
649 Prints full citation in human readable form.
650  
651 =head2 sethtml
652  
653 $ref->sethtml
654  
655 Set output format to be html
656  
657 =head2 clearhtml
658  
659 $ref->clearhtml
660  
661 Set output format to be plain text
662  
663 =head2 printheader
664  
665 print $ref->printheader;
666  
667 When html formatting is enabled, prints some html header tags
668  
669 =head2 printfooter
670  
671 print $ref->printfooter;
672  
673 When html formatting is enabled, prints some html footer tags
674  
675 =head1 EXPORTS
676
677 You can export the following functions if you do not want to use the object orientated interface:
678  
679 parse_text parse_doi sethtml clearhtml set_details print printheader printfooter
680 doi score date atitle jtitle volume issue genre spage epage authcount auth query url
681  
682 The tag C<all> is available to easily export everything:
683
684 use Bib::CrossRef qw(:all);
685  
686 =head1 VERSION
687
688 Ver 0.09
689
690 =head1 AUTHOR
691
692 Doug Leith
693
694 =head1 BUGS
695
696 Please report any bugs or feature requests to C<bug-rrd-db at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Bib-CrossRef>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
697
698 =head1 COPYRIGHT
699
700 Copyright 2015 D.J.Leith.
701
702 This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
703
704 See http://dev.perl.org/licenses/ for more information.
705
706 =cut
707  
708  
709 __END__
↑ Summary