blib/lib/Bib/CrossRef.pm

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