blib/lib/Bib/Tools.pm

filter: Statementstmt 37.4% 206 / 550 Branchbran 21.3% 44 / 206 Conditioncond 18.3% 11 / 60 MC/DCmcdc 2.8% 1 / 35 Subroutinesub 72.0% 31 / 43 Podpod 100% 19 / 19 totaltotal 34.1% 312 / 913 SCAR 87.2
CC
138
Cov
32%
add_dblp
756.0
add_orcid
342.0
add_pubmed
110.0
CRAP 6129.9
↑ Summary
1 ############################################################
2 #
3 # Bib::Tools - For managing collections of Bib::CrossRef references.
4 #
5 ############################################################
6  
7 package Bib::Tools;
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 Bib::CrossRef;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use JSON qw/decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use URI::Escape qw(uri_escape_utf8 uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use HTML::Entities qw(decode_entities encode_entities);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use HTML::TreeBuilder::XPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use XML::Simple qw(XMLin);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use BibTeX::Parser qw(new next);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 #use LWP::Protocol::https;
27 #use Data::Dumper;
28  
29 2 $VERSION = '0.15';
30 2 @ISA = qw(Exporter);
31 2 @EXPORT = qw();
32 2 @EXPORT_OK = qw(
33 sethtml clearhtml add_details add_google add_google_search add_orcid add_fromfile add_dblp add_pubmed
34 send_resp print print_nodoi num num_nodoi getref getref_nodoi append add_bibtex
35 );
36 2 %EXPORT_TAGS = (all => \@EXPORT_OK);
37  
38 ####################################################################################
39 sub new {
40 3 ▶ my $self;
Subroutine new: called CC=3 SCAR=11.4
Pod documented
41 # defaults
42 3 $self->{refs} = []; # the references
43 3 $self->{nodoi_refs} = [];
44 3 $self->{duprefs} = [];
45 3 $self->{html}=0;
46 3 $self->{ratelimit}=5; # limit of 5 crossref queries per sec
47 3 $self->{last} = {};
48 3 bless $self;
49
50 3 my $ratelimit = $_[1];
51 3 ▶ if (defined($ratelimit) && ($ratelimit>=0)) {$self->{ratelimit}=$ratelimit};
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ratelimit and $ratelimit >= 0) 0 3 3
Condition33%1/3
defined $ratelimit and $ratelimit >= 0
!ll&&!rl&&r
300
MC/DC0%0/2
defined $ratelimit and $ratelimit >= 0
defined $ratelimit $ratelimit >= 0
Truth table33%1/3
A and B
A= defined $ratelimitB= $ratelimit >= 0
ABresult
0X0
100
111
52 3 return $self;
53 }
54  
55 ####################################################################################
56 sub sethtml {
57 1 ▶ my $self = shift @_;
Subroutine sethtml: called CC=1 SCAR=0.0
Pod documented
58 1 $self->{html}=1;
59 }
60  
61 ####################################################################################
62 sub clearhtml {
63 0 ▶ my $self = shift @_;
Subroutine clearhtml: not called CC=1 SCAR=6.9
Pod documented
64 0 $self->{html}=0;
65 }
66  
67 ####################################################################################
68 sub _err {
69 0 ▶ my ($self, $str) = @_;
Subroutine _err: not called CC=2 SCAR=17.9
70 0 ▶ if ($self->{html}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'html'}) { } 0 0 0
71 0 print "<p style='color:red'>",$str,"</p>";
72 } else {
73 0 print $str,"\n";
74 }
75 }
76  
77 ####################################################################################
78 sub _split_duplicates {
79 # split list of references into three lists: one with refs that have unique doi's, one with no doi's
80 #and one with all the rest (with duplicate doi's)
81 4 ▶ my $self = shift @_;
Subroutine _split_duplicates: called CC=7 SCAR=19.5
82 4 my @refs=@{$self->{refs}};
83
84 4 my @newrefs;
85 4 foreach my $ref (@refs) {
86 9 my $doi = $ref->doi();
87 9 ▶ if (!defined($doi) || length($doi)==0) {push @{$self->{nodoi_refs}}, $ref; next; }# skip entries with no DOI
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $doi or length $doi == 0) 1 8 9
Condition66%2/3
not defined $doi or length $doi == 0
l!l&&r!l&&!r
108
MC/DC50%1/2
not defined $doi or length $doi == 0
not defined $doi length $doi == 0
Truth table66%2/3
not(A) or B
A= not defined $doiB= length $doi == 0
ABresult
1X1
011
000
88 8 my $found = 0;
89 8 foreach my $ref2 (@newrefs) {
90 8 ▶ if ($ref2->doi() eq $doi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref2->doi eq $doi) 1 7 8
91 1 $found = 1;
92 }
93 }
94 8 ▶ if (!$found) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found) { } 7 1 8
95 7 push @newrefs, $ref;
96 } else {
97 1 push @{$self->{duprefs}}, $ref;
98 }
99 }
100 4 $self->{refs} = \@newrefs;
101 }
102  
103 ####################################################################################
104 sub append {
105 # add new reference to end of existing list
106 7 ▶ my $self = shift @_;
Subroutine append: called CC=1 SCAR=0.0
Pod documented
107 7 my $ref = shift @_;
108 7 push @{$self->{refs}}, $ref;
109 }
110  
111 ####################################################################################
112 sub add_details {
113 # given an array of raw strings, try to convert into paper references
114
115 2 ▶ my $self = shift @_;
Subroutine add_details: called CC=2 SCAR=6.9
Pod documented
116 2 foreach my $cites (@_) {
117 4 $self->{last} = Bib::CrossRef->new();
118 4 $self->{last}->parse_text($cites);
119 4 $self->append($self->{last});
120 4 sleep 1/(0.001+$self->{ratelimit}); # rate limit queries to crossref
121 }
122 2 $self->_split_duplicates();
123 }
124  
125 ####################################################################################
126 sub add_google {
127 # scrape paper details from google scholar personal page -- nb: no doi info on google, so use crossref.org to obtain this
128 # nb: doesn't work with google scholar search results
129
130 0 ▶ my $self = shift @_;
Subroutine add_google: not called CC=8 SCAR=42.8
Pod documented
131 0 my $url = shift @_;
132 0 my $ua = LWP::UserAgent->new;
133 0 $ua->agent('Mozilla/5.0');
134 0 my $req = HTTP::Request->new(GET => $url);
135 0 my $res = $ua->request($req);
136 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
137 0 my $tree= HTML::TreeBuilder::XPath->new;
138 0 $tree->parse($res->decoded_content);
139 0 my @atitles=$tree->findvalues('//tr[@class="gsc_a_tr"]/td/a[@class="gsc_a_at"]');
140 0 my @authors=$tree->findvalues('//tr[@class="gsc_a_tr"]/td/div[@class="gs_gray"][1]');
141 0 my @jtitles=$tree->findvalues('//tr[@class="gsc_a_tr"]/td/div[@class="gs_gray"][2]');
142 0 my $len1 = @atitles; my $len2 = @authors; my $len3 = @jtitles;
143 0 ▶ if (($len1 != $len2) || ($len1 != $len3) || ($len2 != $len3)) {$self->_err("Problem parsing google page: mismatched $len1 titles/$len2 authors/$len3 journals.");return []}
Branch: 1 branch
BranchTrueFalseTotal
if ($len1 != $len2 or $len1 != $len3 or $len2 != $len3) 0 0 0
Condition0%0/6
$len1 != $len2 or $len1 != $len3 or $len2 != $len3
l!l&&r!l&&!r
000
$len1 != $len2 or $len1 != $len3
l!l&&r!l&&!r
000
MC/DC0%0/3
$len1 != $len2 or $len1 != $len3 or $len2 != $len3
$len1 != $len2 $len1 != $len3 $len2 != $len3
Truth table0%0/4
A or B or C
A= $len1 != $len2B= $len1 != $len3C= $len2 != $len3
ABCresult
1XX1
01X1
0011
0000
144 0 ▶ for (my $i = 0; $i<$len1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len1) 0 0 0
145 # these are already utf8
146 0 $authors[$i] = decode_entities($authors[$i]);
147 0 $atitles[$i] = decode_entities($atitles[$i]);
148 0 $jtitles[$i] = decode_entities($jtitles[$i]);
149 0 my $temp = $authors[$i].", ".$atitles[$i].", ".$jtitles[$i];
150 0 my $r = Bib::CrossRef->new;
151 0 $r->parse_text($temp);
152 0 $jtitles[$i] =~ m/\s([0-9][0-9][0-9][0-9])$/;
153 0 my $year=$1;
154 0 ▶ if ((length($year)==4) && ($r->date ne $1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $year == 4 and $r->date ne $1) 0 0 0
Condition0%0/3
length $year == 4 and $r->date ne $1
!ll&&!rl&&r
000
MC/DC0%0/2
length $year == 4 and $r->date ne $1
length $year == 4 $r->date ne $1
Truth table0%0/3
A and B
A= length $year == 4B= $r->date ne $1
ABresult
0X0
100
111
155 0 $r->_setscore(0.5); # mismatch in year, probably bad
156 }
157 0 $self->append($r);
158 }
159 } else {
160 0 $self->_err("Problem with $url: ".$res->status_line);
161 }
162 }
163  
164 ####################################################################################
165 sub add_google_search {
166 # scrape paper details from google scholar search results -- *not* from persons scholar home page
167  
168 0 ▶ my $self = shift @_;
Subroutine add_google_search: not called CC=5 SCAR=34.0
Pod documented
169 0 my $url = shift @_;
170 0 my $ua = LWP::UserAgent->new;
171 0 $ua->agent('Mozilla/5.0');
172 0 my $req = HTTP::Request->new(GET => $url);
173 0 my $res = $ua->request($req);
174 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
175 0 my $tree= HTML::TreeBuilder::XPath->new;
176 0 $tree->parse($res->decoded_content);
177 0 my @atitles=$tree->findvalues('//div[@class="gs_ri"]/h3/a');
178 0 my @authors=$tree->findvalues('//div[@class="gs_a"]');
179 0 my $len1 = @atitles; my $len2 = @authors;
180 0 ▶ if ($len1 != $len2) {$self->_err("Problem parsing google page: mismatched $len1 titles/$len2 authors.");return [];}
Branch: 1 branch
BranchTrueFalseTotal
if ($len1 != $len2) 0 0 0
181 0 my @cites=();
182 0 ▶ for (my $i = 0; $i<$len1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len1) 0 0 0
183 0 $authors[$i] = decode_entities($authors[$i]);
184 0 $atitles[$i] = decode_entities($atitles[$i]);
185 0 my $str = $authors[$i].", ".$atitles[$i];
186 0 ▶ if (length($str)>5) { # a potentially useful entry ?
Branch: 1 branch
BranchTrueFalseTotal
if (length $str > 5) 0 0 0
187 0 push @cites, $authors[$i].", ".$atitles[$i];
188 }
189 }
190 0 $self->add_details(@cites);
191 } else {
192 0 $self->_err("Problem with $url: ".$res->status_line);
193 }
194 }
195  
196 ####################################################################################
197 sub _dblp_setauth {
198 0 ▶ my $self = shift @_; my $r = shift @_; my $cite = shift @_;
Subroutine _dblp_setauth: not called CC=3 SCAR=24.8
199
200 0 ▶ if (ref($cite->{'author'}) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cite->{'author'} eq 'HASH') { } 0 0 0
201 0 $r->_setauthcount(1);
202 0 $r->_setauth(1,$cite->{'author'});
203 } else {
204 0 my $count = 0;
205 0 foreach my $au (@{$cite->{'author'}}) {
206 0 $count++;
207 0 $r->_setauth($count, $au);
208 }
209 0 $r->_setauthcount($count);
210 }
211 }
212  
213 ####################################################################################
214 sub add_dblp {
215 # get details using DBLP XML API
216
217 0 ▶ my $self = shift @_;
Subroutine add_dblp: not called CC=27 SCAR=66.3
Pod documented
218 0 my $url = shift @_;
219 0 ▶ my $maxnum = shift @_; if (!defined($maxnum)) {$maxnum=-1;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $maxnum) 0 0 0
220
221 0 my $ua = LWP::UserAgent->new;
222 0 $ua->agent('Mozilla/5.0');
223 0 my $req = HTTP::Request->new(GET => $url);
224 0 my $res = $ua->request($req);
225 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
226 0 my $xs = XML::Simple->new();
227 0 my $data = $xs->XMLin($res->decoded_content);
228 0 my @cites; my @ctemp;
229 0 ▶ if (defined $data->{'r'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $data->{'r'}) { } 0 0 0
elsif (defined $data->{'article'}) { } 0 0 0
230 # a person page
231 0 @cites = $data->{'r'};
232 } elsif (defined $data->{'article'}) {
233 # its xml for a single article
234 0 $ctemp[0] = $data;
235 0 push @cites, \@ctemp;
236 }
237 0 my $num=0;
238 0 foreach my $c (@{$cites[0]}) {
239 0 ▶ $num++; if ($maxnum>0 && $num>$maxnum) {last;} # mainly for testing
Branch: 1 branch
BranchTrueFalseTotal
if ($maxnum > 0 and $num > $maxnum) 0 0 0
Condition0%0/3
$maxnum > 0 and $num > $maxnum
!ll&&!rl&&r
000
MC/DC0%0/2
$maxnum > 0 and $num > $maxnum
$maxnum > 0 $num > $maxnum
Truth table0%0/3
A and B
A= $maxnum > 0B= $num > $maxnum
ABresult
0X0
100
111
240 0 my @k = keys %{$c};
241 0 my $cite = $c->{$k[0]};
242 0 my $ee = $cite->{'ee'};
243 0 ▶ if ($ee =~ m/dx.doi.org/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ee =~ /dx.doi.org/) 0 0 0
244 # we have a DOI, lets call crossref
245 0 $ee =~ s/http:\/\/dx.doi.org\///;
246 0 my $r = Bib::CrossRef->new;
247 0 $r->parse_text($ee);
248 0 ▶ if ($r->score >=1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->score >= 1) 0 0 0
249 0 ▶ if (!defined $r->authcount || $r->authcount==0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $r->authcount or $r->authcount == 0) 0 0 0
Condition0%0/3
not defined $r->authcount or $r->authcount == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $r->authcount or $r->authcount == 0
not defined $r->authcount $r->authcount == 0
Truth table0%0/3
not(A) or B
A= not defined $r->authcountB= $r->authcount == 0
ABresult
1X1
011
000
250 # shouldn't happen, but sometimes doi data lacks authors so use dblp data
251 0 $self->_dblp_setauth($r,$cite);
252 }
253 0 $self->append($r);
254 0 next; # move on to next record
255 }
256 }
257 0 my $jtitle='';
258 0 ▶ if (defined $cite->{'journal'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $cite->{'journal'}) { } 0 0 0
elsif (defined $cite->{'booktitle'}) { } 0 0 0
259 0 $jtitle = $cite->{'journal'};
260 } elsif (defined $cite->{'booktitle'}) {
261 0 $jtitle = $cite->{'booktitle'};
262 }
263 0 my $temp = $cite->{'year'}.' '.$cite->{'title'}.' '.$jtitle. ' ';
264 0 ▶ if (ref($cite->{'author'}) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cite->{'author'} eq 'HASH') { } 0 0 0
265 0 $temp .= $cite->{'author'};
266 } else {
267 0 foreach my $au (@{$cite->{'author'}}) { $temp .= $au.", ";}
268 }
269 0 my $r = Bib::CrossRef->new;
270 0 $r->parse_text($temp);
271 0 ▶ if ($r->score >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->score >= 1) 0 0 0
272 # found an ok match, lets use it
273 0 $self->append($r);
274 0 next; # move on
275 }
276
277 # we got a poor match, lets use the rest of the dblp data
278 0 $r = Bib::CrossRef->new;
279 0 ▶ if (exists $cite->{'publtype'}) {
Branch: 4 branches
BranchTrueFalseTotal
if (exists $cite->{'publtype'}) { } 0 0 0
elsif ($k[0] =~ /article/) { } 0 0 0
elsif ($k[0] =~ /inproceedings/) { } 0 0 0
elsif ($k[0] =~ /informal/) { } 0 0 0
280 0 $r->_setgenre($cite->{'publtype'});
281 } elsif ($k[0] =~ m/article/) {
282 0 $r->_setgenre('article');
283 } elsif ($k[0] =~ m/inproceedings/) {
284 0 $r->_setgenre('proceeding');
285 } elsif ($k[0] =~ m/informal/) {
286 0 $r->_setgenre('preprint');
287 } else {
288 0 $r->_setgenre($k[0]);
289 }
290 0 $r->_setdate($cite->{'year'}); $r->_setatitle($cite->{'title'}); $r->_setjtitle($jtitle);
291 0 $self->_dblp_setauth($r,$cite);
292 0 ▶ if (defined $cite->{'volume'}) {$r->_setvolume($cite->{'volume'});}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cite->{"volume"}) 0 0 0
293 0 ▶ if (defined $cite->{'number'}) {$r->_setissue($cite->{'number'});}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cite->{"number"}) 0 0 0
294 0 ▶ if (defined $cite->{'pages'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cite->{"pages"}) 0 0 0
295 0 my @bits = split('-',$cite->{'pages'});
296 0 ▶ if (defined $bits[0]) {$r->_setspage($bits[0]);}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bits[0]) 0 0 0
297 0 ▶ if (defined $bits[1]) {$r->_setepage($bits[1]);}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bits[1]) 0 0 0
298 }
299 0 ▶ if (($cite->{'ee'} =~ m/^http:\/\//)) {$r->_seturl($cite->{'ee'});}
Branch: 1 branch
BranchTrueFalseTotal
if ($cite->{"ee"} =~ m[^http://]) 0 0 0
300 0 $r->_setscore(1);
301 0 $r->_setquery($temp);
302 # add manually constructed record
303 0 $self->append($r);
304 }
305 0 $self->_split_duplicates();
306 } else {
307 0 $self->_err("Problem with $url: ".$res->status_line);
308 }
309 }
310  
311 ####################################################################################
312 sub _orcid_getdoi {
313 # extract DOI from an orcid entry
314 0 ▶ my $cite = shift @_;
Subroutine _orcid_getdoi: not called CC=5 SCAR=34.0
315
316 0 my $doi='';
317 0 my $c = $cite->{'work-external-identifiers'}->{'work-external-identifier'};
318 0 ▶ if (ref($c) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq 'HASH') { } 0 0 0
319 # a single value
320 0 ▶ if ($c->{'work-external-identifier-type'} =~ m/doi/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"work-external-identifier-type"} =~ /doi/) 0 0 0
321 # and its a DOI
322 0 $doi = $c->{'work-external-identifier-id'};
323 }
324 } else {
325 # multiple values
326 0 foreach my $id (@{$c}) {
327 0 ▶ if ($id->{'work-external-identifier-type'} =~ m/doi/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id->{"work-external-identifier-type"} =~ /doi/) 0 0 0
328 # its a DOI
329 0 $doi = $id->{'work-external-identifier-id'};
330 0 last; # exit loop
331 }
332 }
333 }
334 0 return $doi;
335 }
336  
337 ####################################################################################
338 sub _orcid_getauth {
339 # generate an author string from an orcid entry
340 0 ▶ my $cite = shift @_;
Subroutine _orcid_getauth: not called CC=5 SCAR=34.0
341  
342 0 my $auth='';
343 0 my $c = $cite->{'work-contributors'}->{'contributor'};
344 0 ▶ if (ref($c) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq 'HASH') { } 0 0 0
345 # single author
346 0 ▶ if ($c->{'contributor-attributes'}->{'contributor-role'} =~ m/author/) {$auth = $c->{'credit-name'}->{'content'};}
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"contributor-attributes"}{"contributor-role"} =~ /author/) 0 0 0
347 } else {
348 # multiple authors
349 0 foreach my $au (@{$c}) {
350 0 ▶ if ($au->{'contributor-attributes'}->{'contributor-role'} =~ m/author/) {$auth .= $au->{'credit-name'}->{'content'}.", ";}
Branch: 1 branch
BranchTrueFalseTotal
if ($au->{"contributor-attributes"}{"contributor-role"} =~ /author/) 0 0 0
351 }
352 }
353 0 return $auth;
354 }
355  
356 ####################################################################################
357 sub _orcid_setauth {
358 # use an orcid entry to set citation author list (using orcid bibtex data if appropriate)
359 0 ▶ my $self = shift @_; my $r = shift @_;
Subroutine _orcid_setauth: not called CC=8 SCAR=42.8
360 0 my $cite = shift @_; my $entry = shift @_;
361
362 0 my $c = $cite->{'work-contributors'}->{'contributor'};
363 0 ▶ if (defined $c) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c) 0 0 0
364 # we have an orcid author entry
365 0 my $authcount=0;
366 0 ▶ if (ref($c) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq 'HASH') { } 0 0 0
367 # single author
368 0 ▶ if ($c->{'contributor-attributes'}->{'contributor-role'} =~ m/author/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"contributor-attributes"}{"contributor-role"} =~ /author/) 0 0 0
369 0 $authcount++;
370 0 $r->_setauth($authcount,$c->{'credit-name'}->{'content'});
371 }
372 } else {
373 # multiple authors
374 0 foreach my $au (@{$c}) {
375 0 ▶ if ($au->{'contributor-attributes'}->{'contributor-role'} =~ m/author/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($au->{"contributor-attributes"}{"contributor-role"} =~ /author/) 0 0 0
376 0 $authcount++;
377 0 $r->_setauth($authcount,$au->{'credit-name'}->{'content'});
378 }
379 }
380 }
381 0 $r->_setauthcount($authcount);
382 0 ▶ if ($authcount>0) {return;} # found some authors, finish up
Branch: 1 branch
BranchTrueFalseTotal
if ($authcount > 0) 0 0 0
383 }
384 # no author info, lets see if bibtex has any author info
385 0 ▶ if (defined $entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry) 0 0 0
386 0 $self->_bibtex_setauth($r,$entry);
387 }
388 }
389  
390 ####################################################################################
391 sub add_orcid {
392 # get paper details from orcid using API
393
394 0 ▶ my $self = shift @_; my $orcid_id = shift @_;
Subroutine add_orcid: not called CC=18 SCAR=58.3
Pod documented
395
396 0 my $ua = LWP::UserAgent->new;
397 0 my $req = HTTP::Request->new(GET => "http://pub.orcid.org/$orcid_id/orcid-works/");
398 0 my $res = $ua->request($req);
399 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
400 0 my $xs = XML::Simple->new();
401 # the orcid response is utf8 xml
402 0 my $data = $xs->XMLin($res->decoded_content);
403 0 my @cites = $data->{'orcid-profile'}->{'orcid-activities'}->{'orcid-works'}->{'orcid-work'};
404 0 foreach my $cite (@{$cites[0]}) {
405 0 my $entry = undef;
406 0 ▶ if ($cite->{'work-citation'}->{'work-citation-type'} =~ m/bibtex/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cite->{"work-citation"}{"work-citation-type"} =~ /bibtex/) 0 0 0
407 # we have a bibtex reference, extract some extra info
408 0 my $bibtex = $cite->{'work-citation'}->{'citation'};
409 0 open my $fh, '<', \$bibtex;
410 0 my $parser = BibTeX::Parser->new($fh);
411 0 $entry = $parser->next;
412 0 ▶ if (!$entry->parse_ok) {$entry = undef;}
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry->parse_ok) 0 0 0
413 }
414 0 my $doi = _orcid_getdoi($cite);
415 0 ▶ if ((defined $doi) && (length($doi)>5)) { # we seem to have a DOI
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doi and length $doi > 5) 0 0 0
Condition0%0/3
defined $doi and length $doi > 5
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doi and length $doi > 5
defined $doi length $doi > 5
Truth table0%0/3
A and B
A= defined $doiB= length $doi > 5
ABresult
0X0
100
111
416 # use DOI to search.crossref.org
417 0 my $r = Bib::CrossRef->new;
418 0 $r->parse_text($doi);
419 0 ▶ if ($r->score>=1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->score >= 1) 0 0 0
420 0 ▶ if (!defined $r->authcount || $r->authcount==0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $r->authcount or $r->authcount == 0) 0 0 0
Condition0%0/3
not defined $r->authcount or $r->authcount == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $r->authcount or $r->authcount == 0
not defined $r->authcount $r->authcount == 0
Truth table0%0/3
not(A) or B
A= not defined $r->authcountB= $r->authcount == 0
ABresult
1X1
011
000
421 # shouldn't happen, but sometimes doi data lacks authors so use orcid data
422 0 $self->_orcid_setauth($r,$cite,$entry);
423 }
424 0 $self->append($r);
425 0 next; # move on
426 }
427 }
428 # use title etc to search.crossref.org
429 0 my $date; my $atitle; my $jtitle;
430 0 ▶ if (exists $cite->{'publication-date'}->{'year'}) {$date = $cite->{'publication-date'}->{'year'};}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cite->{"publication-date"}{"year"}) 0 0 0
431 0 ▶ if (exists $cite->{'work-title'}->{'title'}) {$atitle = $cite->{'work-title'}->{'title'};}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cite->{"work-title"}{"title"}) 0 0 0
432 0 ▶ if (exists $cite->{'journal-title'}) {$jtitle = $cite->{'journal-title'};}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cite->{"journal-title"}) 0 0 0
433 0 my $auth = _orcid_getauth($cite);
434 0 my $temp=$auth.' '.$date.' '.$atitle.' '.$jtitle;
435 0 ▶ if (length($temp)>10 && length($date)>0 && length($atitle)+length($auth)>0) { # we have a potentially useful search string
Branch: 1 branch
BranchTrueFalseTotal
if (length $temp > 10 and length $date > 0 and length($atitle) + length($auth) > 0) 0 0 0
Condition0%0/6
length $temp > 10 and length $date > 0 and length($atitle) + length($auth) > 0
!ll&&!rl&&r
000
length $temp > 10 and length $date > 0
!ll&&!rl&&r
000
MC/DC0%0/3
length $temp > 10 and length $date > 0 and length($atitle) + length($auth) > 0
length $temp > 10 length $date > 0 length($atitle) + length($auth) > 0
Truth table0%0/4
A and B and C
A= length $temp > 10B= length $date > 0C= length($atitle) + length($auth) > 0
ABCresult
0XX0
10X0
1100
1111
436 0 my $r = Bib::CrossRef->new;
437 0 $r->parse_text($temp);
438 0 ▶ if ($r->score >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->score >= 1) 0 0 0
439 # found an ok match, lets use it
440 0 $self->append($r);
441 0 next; # move on
442 }
443 }
444 # for a poor match, try to extract rest of info from orcid
445 0 my $r = Bib::CrossRef->new;
446 0 $r->_setdate($date); $r->_setatitle($atitle); $r->_setjtitle($jtitle);
447 0 ▶ if (exists $cite->{'work-type'}) {$r->_setgenre($cite->{'work-type'});}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cite->{"work-type"}) 0 0 0
448 0 $self->_orcid_setauth($r,$cite);
449 0 $self->_bibtex_parse($r,$entry);
450 0 $r->_setscore(1);
451 0 $r->_setquery($temp);
452 # add manually constructed record
453 0 $self->append($r);
454 }
455 0 $self->_split_duplicates();
456 } else {
457 0 $self->_err("Problem with orcid.org: ".$res->status_line);
458 }
459 }
460  
461 ####################################################################################
462 sub _find_pubmed {
463 0 ▶ my $c = shift @_;
Subroutine _find_pubmed: not called CC=3 SCAR=24.8
464 0 my $name = shift @_;
465 0 my $term = shift @_;
466 0 foreach my $item (@{$c}) {
467 0 ▶ if ($item->{'Name'} eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"Name"} eq $name) 0 0 0
468 0 return $item->{$term};
469 }
470 }
471 0 return undef;
472 }
473  
474 ####################################################################################
475 sub add_pubmed {
476 # add results from a pubmed query
477 0 ▶ my ($self,$q) = @_;
Subroutine add_pubmed: not called CC=10 SCAR=47.0
Pod documented
478  
479 0 my $ua = LWP::UserAgent->new;
480 0 $q =~ s/\s+/+/g;
481 0 my $req = HTTP::Request->new(GET => "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?usehistory=y&db=pubmed&term=".$q);
482 0 my $res = $ua->request($req);
483 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 0 0 0
484 0 ▶ my $web = $1 if ($res->decoded_content =~ /<WebEnv>(\S+)<\/WebEnv>/);
Branch: 1 branch
BranchTrueFalseTotal
if $res->decoded_content =~ m[<WebEnv>(\S+)</WebEnv>] 0 0 0
485 0 ▶ my $key = $1 if ($res->decoded_content =~ /<QueryKey>(\d+)<\/QueryKey>/);
Branch: 1 branch
BranchTrueFalseTotal
if $res->decoded_content =~ m[<QueryKey>(\d+)</QueryKey>] 0 0 0
486 0 $req = HTTP::Request->new(GET => "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&query_key=$key&WebEnv=$web");
487 0 $res = $ua->request($req);
488 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 0 0 0
489 0 my $xs = XML::Simple->new();
490 0 my $data = $xs->XMLin($res->decoded_content);
491 0 my @cites = $data->{'DocSum'};
492 0 foreach my $cite (@{$cites[0]}) {
493 0 my $c = $cite->{'Item'};
494 0 ▶ if (ref($c) ne "ARRAY") {next;}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c ne "ARRAY") 0 0 0
495 0 my $r = Bib::CrossRef->new;
496 0 my $doi = _find_pubmed($c,'DOI','content');
497 0 ▶ if (defined $doi) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doi) 0 0 0
498 # PubMed is reliable, no need to call crossref
499 # my $r = Bib::CrossRef->new;
500 # $r->parse_text($doi);
501 # $self->append($r);
502 # next; # move on
503 0 $r->_setdoi($doi);
504 0 $r->_seturl('http://dx.doi.org/'.$doi);
505 }
506 0 $r->_setjtitle(_find_pubmed($c,'FullJournalName','content'));
507 0 $r->_setatitle(_find_pubmed($c,'Title','content'));
508 0 my $date = _find_pubmed($c,'PubDate','content');
509 0 $date =~ m/^([0-9][0-9][0-9][0-9])/;
510 0 $r->_setdate($1); # extract the year
511 0 $r->_setvolume(_find_pubmed($c,'Volume','content'));
512 0 $r->_setissue(_find_pubmed($c,'Issue','content'));
513 0 my $p = _find_pubmed($c,'Pages','content');
514 0 my @bits = split('-',$p);
515 0 $r->_setspage($bits[0]); $r->_setepage($bits[1]);
516
517 0 my $aulist = _find_pubmed($c,'AuthorList','Item');
518 0 my $authcount=0;
519 0 ▶ if (ref($aulist) ne "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $aulist ne 'ARRAY') { } 0 0 0
520 0 $authcount = 1;
521 0 $r->_setauth($authcount,$aulist->{'content'});
522 } else {
523 0 foreach my $au (@{$aulist}) {
524 0 $authcount++;
525 0 $r->_setauth($authcount,$au->{'content'});
526 }
527 }
528 0 $r->_setauthcount($authcount);
529 0 my $g = _find_pubmed($c,'FullJournalName','Item');
530 0 $r->_setgenre($g->{'content'});
531 0 $r->_setscore(1);
532 #$r->_setquery($auth." ".$temp);
533 # add manually constructed record
534 0 $self->append($r);
535 }
536 }
537 0 $self->_split_duplicates();
538 0 return;
539 }
540 0 $self->_err("Problem with http://eutils.ncbi.nlm.nih.gov: ".$res->status_line);
541 }
542  
543 ####################################################################################
544 sub _rem_brackets {
545 # remove {} brackets from bibtex entry
546 6 ▶ my $self = shift @_; my $str = shift @_;
Subroutine _rem_brackets: called CC=1 SCAR=0.0
547
548 6 $str =~ s/[\{\}]//g;
549 6 $str =~ s/\\textquotesingle/\\'/g;
550 6 return $str
551 }
552  
553 ####################################################################################
554 sub _bibtex_setauth {
555 # use author list array to populate our author details
556 1 ▶ my $self = shift @_; my $r = shift @_; my $entry = shift @_;
Subroutine _bibtex_setauth: called CC=5 SCAR=17.6
557  
558 1 ▶ if ((!defined $r) || (!defined $entry) || (!$entry->parse_ok)) {return;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $r and defined $entry and $entry->parse_ok) 0 1 1
Condition33%2/6
defined $r and defined $entry and $entry->parse_ok
!ll&&!rl&&r
001
defined $r and defined $entry
!ll&&!rl&&r
001
MC/DC0%0/3
defined $r and defined $entry and $entry->parse_ok
defined $r defined $entry $entry->parse_ok
Truth table25%1/4
A and B and C
A= defined $rB= defined $entryC= $entry->parse_ok
ABCresult
0XX0
10X0
1100
1111
559  
560 1 my @authors = $entry->author;
561 1 my $count = 0;
562 1 foreach my $author (@authors) {
563 2 $count++;
564 2 $r->_setauth($count,$self->_rem_brackets($author->first).' '.$self->_rem_brackets($author->last));
565 };
566 1 $r->_setauthcount($count);
567 }
568  
569 ####################################################################################
570 sub _bibtex_parse {
571 # use data in a bibtext entry to populate citation
572 1 ▶ my $self = shift @_; my $r = shift @_; my $entry = shift @_;
Subroutine _bibtex_parse: called CC=18 SCAR=40.1
573
574 1 ▶ if ((!defined $r) || (!defined $entry) || (!$entry->parse_ok)) {return;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $r and defined $entry and $entry->parse_ok) 0 1 1
Condition33%2/6
defined $r and defined $entry and $entry->parse_ok
!ll&&!rl&&r
001
defined $r and defined $entry
!ll&&!rl&&r
001
MC/DC0%0/3
defined $r and defined $entry and $entry->parse_ok
defined $r defined $entry $entry->parse_ok
Truth table25%1/4
A and B and C
A= defined $rB= defined $entryC= $entry->parse_ok
ABCresult
0XX0
10X0
1100
1111
575
576 1 my $genre = lc($entry->type);
577 1 ▶ if (($genre eq "inproceedings") || ($genre eq "proceedings")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($genre eq 'inproceedings' or $genre eq 'proceedings') { } 0 1 1
Condition33%1/3
$genre eq "inproceedings" or $genre eq "proceedings"
l!l&&r!l&&!r
001
MC/DC0%0/2
$genre eq "inproceedings" or $genre eq "proceedings"
$genre eq "inproceedings" $genre eq "proceedings"
Truth table33%1/3
A or B
A= $genre eq "inproceedings"B= $genre eq "proceedings"
ABresult
1X1
011
000
578 0 $r->_setgenre("proceeding");
579 } else {
580 1 $r->_setgenre($genre);
581 }
582 1 ▶ if (defined $entry->field('title')) { $r->_setatitle($self->_rem_brackets($entry->field('title')));}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry->field("title")) 1 0 1
583 1 ▶ if (defined $entry->author) {$self->_bibtex_setauth($r,$entry);}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry->author) 1 0 1
584 1 ▶ if (defined $entry->field('url')) {$r->_seturl($entry->field('url'))};
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry->field("url")) 1 0 1
585 1 ▶ if (defined $entry->field('year')) {$r->_setdate($entry->field('year'))};
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry->field("year")) 1 0 1
586 1 ▶ if (defined $entry->field('volume')) {$r->_setvolume($entry->field('volume'))};
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry->field("volume")) 1 0 1
587 1 ▶ if (defined $entry->field('issue')) {$r->_setissue($entry->field('issue'))};
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry->field("issue")) 0 1 1
588 1 ▶ if (defined $entry->field('pages')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry->field("pages")) 0 1 1
589 0 my $pages = $entry->field('pages');
590 0 (my $s, my $e) = ($pages =~ /([0-9]+)-+([0-9]+)/ );
591 0 ▶ if (defined $s) { $r->_setspage($s); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s) 0 0 0
592 0 ▶ if (defined $e) { $r->_setepage($e); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $e) 0 0 0
593 }
594 1 ▶ if (defined $entry->field('journal')) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $entry->field('journal')) { } 1 0 1
elsif (defined $entry->field('booktitle')) { } 0 0 0
595 1 $r->_setjtitle($self->_rem_brackets($entry->field('journal')));
596 } elsif (defined $entry->field('booktitle')) {
597 0 $r->_setjtitle($self->_rem_brackets($entry->field('booktitle')));
598 }
599 1 ▶ if (defined $entry->field('doi')) {$r->_setdoi($entry->field('doi'));}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry->field("doi")) 0 1 1
600 }
601  
602 ####################################################################################
603 sub add_bibtex {
604 # read references from a bibtex file, takes file handle as input
605 1 ▶ my $self = shift @_; my $fh = shift @_;
Subroutine add_bibtex: called CC=10 SCAR=30.8
Pod documented
606 1 my $opt = shift @_; # options, =1 then use crossref to try to resolve DOIs
607 1 ▶ if (!defined $opt) {$opt=1;} # $opt defaults to 1
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opt) 1 0 1
608
609 1 my $parser = BibTeX::Parser->new($fh);
610 1 ▶ while (my $entry = $parser->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $parser->next) 1 1 2
611 1 ▶ if (!$entry->parse_ok) { next; } # problem, move on
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry->parse_ok) 0 1 1
612 1 my $doi = $entry->field('doi');
613 1 ▶ if ((defined $doi) && (length($doi)>5) && ($opt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doi and length $doi > 5 and $opt) 0 1 1
Condition33%2/6
defined $doi and length $doi > 5 and $opt
!ll&&!rl&&r
100
defined $doi and length $doi > 5
!ll&&!rl&&r
100
MC/DC0%0/3
defined $doi and length $doi > 5 and $opt
defined $doi length $doi > 5 $opt
Truth table25%1/4
A and B and C
A= defined $doiB= length $doi > 5C= $opt
ABCresult
0XX0
10X0
1100
1111
614 # we have a DOI and $opt != 0, use DOI to get rest of citation
615 0 my $r = Bib::CrossRef->new;
616 0 $r->parse_text($doi);
617 0 ▶ if ($r->score >=1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->score >= 1) 0 0 0
618 0 ▶ if (!defined $r->authcount || $r->authcount==0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $r->authcount or $r->authcount == 0) 0 0 0
Condition0%0/3
not defined $r->authcount or $r->authcount == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $r->authcount or $r->authcount == 0
not defined $r->authcount $r->authcount == 0
Truth table0%0/3
not(A) or B
A= not defined $r->authcountB= $r->authcount == 0
ABresult
1X1
011
000
619 # shouldn't happen, but sometimes doi data lacks authors. since we have bibtex data, use it
620 0 $self->_bibtex_setauth($r,$entry);
621 }
622 0 $self->append($r);
623 0 next; # move on
624 }
625 }
626 1 my $r = Bib::CrossRef->new;
627 # use bibtex to create citation
628 1 $self->_bibtex_parse($r,$entry);
629 1 $r->_setscore(1);
630 1 $r->_setquery($r->date." ".$r->atitle." ".$r->jtitle);
631 # add manually constructed record
632 1 $self->append($r);
633 }
634 1 $self->_split_duplicates();
635 }
636  
637 ####################################################################################
638 sub add_fromfile {
639 # read free text references from a file, one reference per line
640 # takes file handle as input
641 1 ▶ my $self = shift @_;
Subroutine add_fromfile: called CC=3 SCAR=11.1
Pod documented
642 1 my $fh = shift @_;
643 1 my @cites;
644 1 ▶ while (my $line=<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 2 1 3
645 2 chomp($line);
646 2 ▶ if (length($line)<5) {next;} # skip non-informative lines
Branch: 1 branch
BranchTrueFalseTotal
if (length $line < 5) 0 2 2
647 2 push @cites, $line;
648 }
649 1 $self->add_details(@cites);
650 }
651  
652 ####################################################################################
653 sub num {
654 # number of references with DOIs
655 18 ▶ my $self = shift @_;
Subroutine num: called CC=1 SCAR=0.0
Pod documented
656
657 18 my $len = @{$self->{refs}};
658 18 return $len;
659 }
660  
661 sub num_nodoi {
662 # number of references without DOIs
663 5 ▶ my $self = shift @_;
Subroutine num_nodoi: called CC=1 SCAR=0.0
Pod documented
664
665 5 my $len = @{$self->{nodoi_refs}};
666 5 return $len;
667 }
668  
669 ####################################################################################
670 sub getref {
671 # get i'th reference with a DOI
672 19 ▶ my ($self, $i) = @_;
Subroutine getref: called CC=1 SCAR=0.0
Pod documented
673 19 return ${$self->{refs}}[$i];
674 }
675  
676 ####################################################################################
677 sub getref_nodoi {
678 # get i'th reference without a DOI
679 2 ▶ my ($self, $i) = @_;
Subroutine getref_nodoi: called CC=1 SCAR=0.0
Pod documented
680
681 2 return ${$self->{nodoi_refs}}[$i];
682 }
683  
684 ####################################################################################
685 sub print {
686 # display a list of references
687 4 ▶ my $self = shift @_;
Subroutine print: called CC=6 SCAR=17.9
Pod documented
688 4 my $id = shift @_;
689
690 4 ▶ if ($self->num==0) {return ''};
Branch: 1 branch
BranchTrueFalseTotal
if ($self->num == 0) 0 4 4
691 4 my $out='';
692 4 ▶ if ($self->{html}) {$out.=$self->getref(0)->printheader($id);}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"html"}) 1 3 4
693 4 ▶ for (my $i=0; $i< $self->num; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->num) 8 4 12
694 8 ▶ if ($self->{html}) {$self->getref($i)->sethtml;} else {$self->getref($i)->clearhtml;}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'html'}) { } 1 7 8
695 8 $out .= $self->getref($i)->print($i+1);
696 8 $out .= "\n";
697 }
698 4 ▶ if ($self->{html}) {$out.=$self->getref(0)->printfooter;}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"html"}) 1 3 4
699 4 return $out;
700 }
701  
702 ####################################################################################
703 sub print_nodoi {
704 # display a list of references
705 1 ▶ my $self = shift @_;
Subroutine print_nodoi: called CC=6 SCAR=19.7
Pod documented
706 1 my $id = shift @_;
707
708 1 ▶ if ($self->num_nodoi==0) {return ''};
Branch: 1 branch
BranchTrueFalseTotal
if ($self->num_nodoi == 0) 0 1 1
709 1 my $out='';
710 1 ▶ if ($self->{html}) {$out.=$self->getref_nodoi(0)->printheader($id);}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"html"}) 0 1 1
711 1 ▶ for (my $i=0; $i< $self->num_nodoi; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->num_nodoi) 1 1 2
712 1 ▶ if ($self->{html}) {$self->getref_nodoi($i)->sethtml;} else {$self->getref_nodoi($i)->clearhtml;}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'html'}) { } 0 1 1
713 1 $out .= $self->getref_nodoi($i)->print($i+1);
714 }
715 1 ▶ if ($self->{html}) {$out.=$self->getref_nodoi(0)->printfooter;}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"html"}) 0 1 1
716 1 return $out;
717 }
718  
719 ####################################################################################
720 sub send_resp {
721 # generate simple web page with results ...
722
723 1 ▶ my $self = shift @_;
Subroutine send_resp: called CC=4 SCAR=14.8
Pod documented
724  
725 1 ▶ if ($self->num==0 && $self->num_nodoi==0) {return 'No Results'};
Branch: 1 branch
BranchTrueFalseTotal
if ($self->num == 0 and $self->num_nodoi == 0) 0 1 1
Condition33%1/3
$self->num == 0 and $self->num_nodoi == 0
!ll&&!rl&&r
100
MC/DC0%0/2
$self->num == 0 and $self->num_nodoi == 0
$self->num == 0 $self->num_nodoi == 0
Truth table33%1/3
A and B
A= $self->num == 0B= $self->num_nodoi == 0
ABresult
0X0
100
111
726 1 my $html = $self->{html};
727 1 $self->sethtml; # force use of html
728 1 my $out='';
729 #$out.="Content-Type: text/html;\n\n"; # html header
730 1 $out.=sprintf "%s", '<!DOCTYPE HTML>',"\n";
731 1 $out.=sprintf "%s", '<html><head><meta charset="utf-8"><meta http-equiv="Content-Type">';
732 1 $out.=sprintf "%s", '<script src="post.js"></script></head><body>',"\n";
733 1 $out.=sprintf "%s", $self->print('doi');
734 1 ▶ if ($self->num_nodoi>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->num_nodoi > 0) 0 1 1
735 0 $out.=sprintf "%s", '<h3>These have no DOIs:</h3>',"\n";
736 0 $out.=sprintf "%s", $self->print_nodoi('nodoi');
737 }
738 1 $out.=sprintf "%s", '<input id="Submit" type="button" value="Submit" onclick="GetCellValues(\'doi\');GetCellValues(\'nodoi\');" /><div id="out"></div>';
739 1 $out.=sprintf "%s", '</body></html>';
740 1 $self->{html} = $html; # restore previous setting
741 1 return $out;
742 }
743  
744 2 1;
745  
746 =pod
747
748 =head1 NAME
749
750 Bib::Tools - For managing collections of Bib::CrossRef references.
751
752 =head1 SYNOPSIS
753  
754 use strict;
755 use Bib::Tools;
756
757 # Create a new object
758  
759 my $refs = Bib::Tools->new();
760
761 # Add some bibliometric info e.g. as text, one reference per line
762  
763 $text=<<"END";
764 10.1109/lcomm.2011.040111.102111
765 10.1109/tnet.2010.2051038
766 END
767 open $fh, '<', \$text;
768 $refs->add_fromfile($fh);
769
770 or
771
772 $text=<<"END";
773 Dangerfield, I., Malone, D., Leith, D.J., 2011, Incentivising fairness and policing nodes in WiFi, IEEE Communications Letters, 15(5), pp500-502
774 D. Giustiniano, D. Malone, D.J. Leith and K. Papagiannaki, 2010. Measuring transmission opportunities in 802.11 links. IEEE/ACM Transactions on Networking, 18(5), pp1516-1529
775 END
776 open $fh, '<', \$text;
777 $refs->add_fromfile($fh);
778  
779 # or as text scraped from a google scholar personal home page
780  
781 $refs->add_google('http://scholar.google.com/citations?user=n8dX1fUAAAAJ');
782  
783 # or as text obtained from ORCID (www.orcid.org)
784  
785 $refs->add_orcid('0000-0003-4056-4014');
786  
787 # or as text from PubMed
788  
789 $refs->add_pubmed('mills kh[author]');
790
791 # or as text from DBLP
792  
793 $refs->add_dblp('http://www.informatik.uni-trier.de/~ley/pers/xx/l/Leith:Douglas_J=');
794  
795 # Bib:Tools will use Bib:CrossRef to try to resolve the supplied text into full citations. It will try to
796 detect duplicates using DOI information, so its fairly safe to import from multiple sources without creating
797 clashes. Full citations without DOI information are kept separately from those with a DOI for better quality
798 control.
799  
800 # The resulting list of full citations containing DOI's can be printed out in human readable form using
801  
802 print $refs->print;
803  
804 # and the list of full citations without DOI's
805  
806 print $refs->print_nodoi;
807  
808 # or the complete citation list can also be output as a simple web page using
809  
810 print $refs->send_resp;
811  
812 =head1 METHODS
813
814 =head2 new
815  
816 my $refs = Bib::Tools->new();
817  
818 Creates a new Bib::Tools object. Queries to crossref via Bib::CrossRef are rate limited. To change the ratelimit pass this as
819 an option to new e.g $refs = Bib::Tools->new(3) sets the rate limit to 3 queries per second.
820  
821 =head2 add_google
822  
823 $refs->add_google($url);
824
825 Scrapes citation information from a google scholar personal home page (*not* a search page, see below) and tries
826 resolve it into full citations using crossref.
827  
828 =head2 add_google_search
829  
830 $refs->add_google_search($url);
831
832 Scrapes citation information from a google scholar search page and tries to resolve into full citations. A different
833 method is needed for search and home pages due to the different html tags used.
834  
835 =head2 add_orcid
836  
837 $refs->add_orcid($orcid_id);
838
839 Uses the ORCID API to extract citations for the specified user identifier. If possible, the DOI is obtained and then resolved using crossref.
840  
841 =head2 add_dblp
842  
843 $refs->add_dblp($url);
844
845 Uses DBLP XML API to extract citations. If possible, the DOI is obtained and then resolved using crossref. E.g.
846  
847 $refs->add_dblp('http://www.informatik.uni-trier.de/~ley/pers/xx/l/Leith:Douglas_J=');
848
849 =head2 add_pubmed
850
851 $refs->add_dblp($query);
852
853 Uses PubMed API to extract citations listed in response to a query. E.g.
854  
855 $refs->add_pubmed('mills kh[author]');
856
857 =head2 add_details
858  
859 $refs->add_details(@lines);
860
861 Given a array of strings, one per citation, tries to resolve these into full citations.
862  
863 =head2 add_bibtex
864  
865 $refs->add_bibtex($fh, $opt);
866
867 Given a file handle to a file containing bibtex entries, imports these citations. If a citation has a DOI and $opt is non-zero (the default), this
868 will be used to try to obtain the full citation from crossref.org.
869  
870 =head2 add_fromfile
871  
872 $refs->add_fromfile($fh);
873
874 Given a file handle to a text file, with one citation per line, tries to resolve these into full citations.
875
876 =head2 print
877  
878 my $info = $refs->print;
879  
880 Display the list of full citations that have DOIs in human readable form.
881  
882 =head2 print_nodoi
883  
884 my $info = $refs->print_nodoi;
885  
886 Display the list of full citations without DOIs in human readable form.
887  
888 =head2 sethtml
889  
890 $refs->sethtml
891
892 Set the output format to be html
893  
894 =head2 clearhtml
895  
896 $refs->clearhtml
897
898 Set the output format to be plain text
899  
900 =head2 send_resp
901  
902 my $info = $refs->send_resp;
903  
904 =head2 num
905  
906 my $num = $refs->num;
907
908 Returns the number of full citations that have DOIs
909  
910 =head2 num_nodoi
911  
912 my $num = $refs->num_nodoi;
913
914 Returns the number of full citations without DOIs
915  
916 =head2 getref
917  
918 my $ref = $refs->getref($i);
919
920 Returns the $i citation from the list with DOIs. This can be used to walk the list of citations.
921  
922 =head2 getref_nodoi
923  
924 my $ref = $refs->getref_nodoi($i);
925  
926 Returns the $i citation from the list without DOIs
927  
928 =head2 append
929  
930 my $ref = Bib::CrossRef->new;
931 $refs->append($ref);
932  
933 Adds a Bib::CrossRef to end of a Bib::Tools list of objects
934  
935 =head1 EXPORTS
936
937 You can export the following functions if you do not want to use the object orientated interface:
938  
939 sethtml clearhtml add_details add_google add_google_search add_orcid add_fromfile add_dblp add_pubmed
940 send_resp print print_nodoi num num_nodoi getref getref_nodoi append add_bibtex
941  
942 The tag C<all> is available to easily export everything:
943
944 use Bib::Tools qw(:all);
945  
946 =head1 WEB INTERFACE
947  
948 A simple web interface to Bib::Tools is contained in the examples folder. This consists of three files: query.html, handle_query.pl and post.js.
949  
950 =head2 query.html
951  
952 <!DOCTYPE HTML>
953 <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"></head><body>
954 <div style="position:relative; height:600px; padding:10px; border:1px solid">
955 <h3>Import References</h3>
956 <form action="handle_query.pl" method="POST" id="in" enctype="multipart/form-data">
957 <table>
958 <tr><td>Use ORCID id:<br><small style="color:#C0C0C0">e.g. 0000-0003-4056-4014</small></td>
959 <td><INPUT type="text" name="orcid" size="128"></p></td></tr>
960 <tr><td colspan=2>(to import from Scopus, follow these <a href="http://orcid.scopusfeedback.com/">instructions</a>, and for Web of Science/ResearcherId follow <a href="http://wokinfo.com/researcherid/integration/">these</a>)</td></tr>
961  
962 <tr><td width="150px">Use DBLP XML page:<br>
963 <small style="color:#C0C0C0">e.g. http://www.informatik.uni-trier.de/~ley/pers/xx/l/Leith:Douglas_J=</small></td>
964 <td> <INPUT type="text" name="dblp" size="128"></td></tr>
965  
966 <tr><td>Use PubMed query:<br><small style="color:#C0C0C0">e.g. mills kh[author]</small></td>
967 <td> <INPUT type="text" name="query" size="128"></td></tr>
968  
969 <tr><td>BibTeX file:</td>
970 <td> <INPUT type="file" name="bibtex"></td></tr>
971  
972 <tr><td>Use Google Scholar personal page:<br>
973 <small style="color:#C0C0C0">e.g. http://scholar.google.com/citations?user=n8dX1fUAAAAJ</small></td>
974 <td><INPUT type="text" name="google" size="128"></p></td></tr>
975  
976 <tr><td>Use Google Scholar search page:<br>
977 <small style="color:#C0C0C0">e.g. http://scholar.google.com/scholar?q=andr%C3%A9s+garcia+saavedra</small></td>
978 <td><INPUT type="text" name="google2" size="128"></p></td></tr>
979  
980 </table>
981 <p>Enter references, one per line (free form text):</p>
982 <textarea name="refs" rows="10" cols="128" form="in"></textarea><br>
983 <INPUT type="submit" value="Submit">(can be slow, be patient)
984 </form></div>
985 <div style="position: absolute; bottom: 5px;"><small>Source: <a href="http://search.cpan.org/~dougleith/Bib-Tools/">Bib::Tools</a></div>
986 </body></html>
987
988 =head2 handle_query.pl
989  
990 #!/usr/bin/perl
991 use Bib::CrossRef;
992 use Bib::Tools;
993 use CGI;
994  
995 # send html header
996 print "Content-Type: text/html;\n\n";
997  
998 my $q = CGI->new;
999 my $refs = Bib::Tools->new;
1000 my $orcid = scalar $q->param('orcid');
1001 $orcid =~ /([0-9\-]+)$/; # extract id out of url
1002 $orcid = $1;
1003 if (length($orcid) > 5) {
1004 $refs->add_orcid($1);
1005 }
1006  
1007 my $google = scalar $q->param('google'); #NB: CGI has already carried out URL decoding
1008 if (length($google) > 5) {
1009 if (!($google =~ m/^http/)) { $google = "http://".$google;}
1010 $refs->add_google($google);
1011 }
1012  
1013 my $google2 = scalar $q->param('google2'); #NB: CGI has already carried out URL decoding
1014 if (length($google2) > 5) {
1015 if (!($google2 =~ m/^http/)) { $google2 = "http://".$google2;}
1016 $refs->add_google_search($google2);
1017 }
1018  
1019 my $dblp = scalar $q->param('dblp');
1020 if (length($dblp) > 5) {
1021 if (!($dblp =~ m/^http/)) { $dblp = "http://".$dblp;}
1022 if ($dblp =~ m/http:\/\/dblp.uni-trier.de\/pers\/xx\/l\/.+/) {
1023 # looks like a valid dblp url
1024 $refs->add_dblp($dblp);
1025 } else {
1026 print "<p style='color:red'>DBLP url looks invalid: ", $dblp,"</p>";
1027 }
1028 }
1029  
1030 my $pubmed = scalar $q->param('pubmed');
1031 if (length($pubmed) > 5) {
1032 $refs->add_pubmed($pubmed);
1033 }
1034  
1035 $filename = scalar $q->param('bibtex');
1036 $tmpfilename = $q->tmpFileName($filename);
1037 open my $fh, "<", $tmpfilename;
1038 $refs->add_bibtex($fh);
1039
1040 my @values = $q->multi_param('refs');
1041 foreach my $value (@values) {
1042 open my $fh, "<", \$value; #NB: CGI has already carried out URL decoding
1043 $refs->add_fromfile($fh);
1044 }
1045  
1046 $refs->sethtml;
1047 print $refs->send_resp;
1048  
1049 =head2 post.js
1050  
1051 function GetCellValues(dataTable) {
1052 var table = document.getElementById(dataTable);
1053 if (table == null) return;
1054 var i = 0; var Obj = [];
1055 var names = table.rows[0];
1056 for (var r = 1; r < table.rows.length; r++) {
1057 if (table.rows[r].id == 'cite') {
1058 var row = table.rows[r].cells;
1059 var check = table.rows[r].getElementsByTagName('Input');
1060 if (check.length>0){
1061 Obj[i] = {};
1062 for (var c = 3; c < row.length; c++){
1063 var tag = names.cells[c].textContent;
1064 Obj[i][tag] =row[c].textContent;
1065 }
1066 i = i+1;
1067 }
1068 }
1069 }
1070 var jsonString = JSON.stringify(Obj);
1071 document.getElementById('out').innerHTML = document.getElementById('out').innerHTML+jsonString;
1072 // or POST using ajax
1073 }
1074  
1075 =head1 VERSION
1076
1077 Ver 0.15
1078
1079 =head1 AUTHOR
1080
1081 Doug Leith
1082
1083 =head1 BUGS
1084
1085 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-Tools>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
1086
1087 =head1 COPYRIGHT
1088
1089 Copyright 2015 D.J.Leith.
1090
1091 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.
1092
1093 See http://dev.perl.org/licenses/ for more information.
1094
1095 =cut
1096  
1097  
1098 __END__
↑ Summary