2 # bibtex_to_paper opens the paper corresponding to a bibtex key
3 # and is released under the terms of the GNU GPL version 3, or any
4 # later version, at your option. See the file README and COPYING for
6 # Copyright 2014 by Don Armstrong <don@donarmstrong.com>.
16 use File::Basename qw(basename);
17 use File::Spec qw(rel2abs);
27 bibtex_to_paper - opens the paper corresponding to a bibtex key
31 bibtex_to_paper [options] bibtexkey
34 --bibtex, -b bibtex file to look up key in
35 --bibtex-cache, -c bibtex cache file
36 --build-cache, -B build cache using bibtex files
37 --search-by-pmid Search term is a pmid instead of a bibtex key
38 --pdf-dir pdf directory
39 --pdfviewer, -p pdf viewer to use
40 --only-print Only print PDF file name
41 --debug, -d debugging level (Default 0)
42 --help, -h display this help
43 --man, -m display manual
51 Bibtex file to look key up in
53 =item B<--bibtex-cache, -c>
55 Bibtex cache file; rebuilt if bibtex file changes
57 =item B<--pdfviewer, -p>
59 PDF viewer to use; defaults to evince unless a .xoj exists, in which
64 Only print the PDF file name, don't open it.
68 Debug verbosity. (Default 0)
72 Display brief usage information.
89 my %options = (debug => 0,
94 'bibtex_cache' => File::Spec->catfile(User->Home,'.bibtex_to_paper_cache'),
98 'build_cache|build-cache!',
100 'bibtex_cache|bibtex-cache|c=s',
102 'only_print|only-print!',
103 'search_by_pmid|search-by-pmid!',
104 'clear_cache|clear-cache!',
105 'papers_directory|papers-directory=s@',
106 'debug|d+','help|h|?','man|m');
108 pod2usage() if $options{help};
109 pod2usage({verbose=>2}) if $options{man};
111 $DEBUG = $options{debug};
114 if (not exists $options{bibtex} and
115 not exists $options{bibtex_cache}) {
117 "You must give at least one of --bibtex".
121 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
129 if (exists $options{bibtex_cache}) {
131 if (-e $options{bibtex_cache}) {
132 ($dbh,$sth) = open_cache($options{bibtex_cache});
134 ($dbh,$sth) = initialize_database($options{bibtex_cache});
138 if (exists $options{clear_cache}) {
139 clear_cache($dbh,$sth);
142 if (exists $options{build_cache}) {
143 $options{bibtex} //= [];
146 @{ref $options{bibtex}?$options{bibtex}:[$options{bibtex}]},
150 if (exists $options{bibtex}) {
151 for my $bibtex_file (@{ref $options{bibtex}?$options{bibtex}:[$options{bibtex}]}) {
152 parse_bibtex_file($bibtex_file,\%entries);
156 if (exists $options{papers_directory} and
159 load_papers_into_database($dbh,$sth,$options{papers_directory});
162 p %entries if $DEBUG;
163 if (keys %entries and
165 load_bibtex_entries_into_database($dbh,$sth,\%entries);
169 for my $bibtex_key (@ARGV) {
170 open_bibtex_key(\%options,$dbh,$sth,\%entries,$bibtex_key);
177 $sth->{clear_papers_cache}->execute();
178 $sth->{clear_bibtex_cache}->execute();
181 sub load_papers_into_database {
182 my ($dbh,$sth,$dir) = @_;
184 my @dirs = ref($dir)?@{$dir}:$dir;
186 my $actually_load_it = sub {
188 $File::Find::prune = 1;
191 return unless /\.pdf$/;
196 insert_or_replace_papers($dbh,$sth,basename($File::Find::name),File::Spec->rel2abs($_),$xoj);
200 find($actually_load_it,@dirs);
203 sub insert_or_replace_papers {
204 my ($dbh,$sth,$file_name,$file_loc,$has_xoj) = @_;
205 $sth->{insert_papers}->execute($file_name,$file_loc,$has_xoj);
206 $sth->{insert_papers}->finish();
209 sub load_bibtex_entries_into_database {
210 my ($dbh,$sth,$entries) = @_;
211 for my $entry (keys %{$entries}) {
212 next unless defined $entries->{$entry};
213 $sth->{insert_bibtex}->execute($entry,@{$entries->{$entry}}{qw(file_name pmid doi html)});
214 $sth->{insert_bibtex}->finish();
215 print STDERR "inserted $entry {".join(',',map {defined $_?"'$_'":"'undef'"} %{$entries->{$entry}})."}\n" if $DEBUG;
219 sub open_bibtex_key {
220 my ($options,$dbh,$sth,$entries,$bibtex_key) = @_;
221 if (not defined $dbh) {
222 open_entry($dbh,$sth,$entries->{$bibtex_key},$options);
225 if ($options->{search_by_pmid}) {
226 $entry = select_entry_from_pmid($dbh,$sth,$bibtex_key);
228 $entry = select_entry_from_bibtex_key($dbh,$sth,$bibtex_key);
231 open_entry($dbh,$sth,$entry,$options);
238 if (not defined $child) {
239 die "Unable to fork for some reason: $!";
242 foreach (0 .. (POSIX::sysconf (&POSIX::_SC_OPEN_MAX) || 1024))
244 open (STDIN, "</dev/null");
245 open (STDOUT, ">/dev/null");
246 open (STDERR, ">&STDOUT");
255 my ($file_name,$options,$has_xoj) = @_;
256 print STDERR "opening $file_name\n" if $DEBUG;
258 fork_exec('xournal',$file_name);
260 fork_exec('xournal',$file_name)
266 fork_exec('sensible-browser',$file);
270 my ($dbh,$sth,$entry,$options) = @_;
272 return unless defined $entry and ref $entry and keys %{$entry};
274 print STDERR "Entry: \n";
277 if (defined $entry->{file_name} and length $entry->{file_name}) {
278 my $paper = select_one($dbh,$sth->{select_papers_by_name},$entry->{file_name});
279 if (not defined $paper) {
280 my ($pmid) = $entry->{file_name} =~ /pmid_(\d+)/;
281 if (defined $pmid and length $pmid) {
282 $paper = select_one($dbh,$sth->{select_papers_by_pmid},'%pmid_'.$pmid.'.%');
286 print STDERR $entry->{file_name} if $DEBUG;
287 if (defined $paper) {
288 if ($options->{only_print}) {
289 print $paper->{path};
292 open_pdf($paper->{path},$options,$paper->{has_xoj});
295 print STDERR "Unable to find paper\n" if $DEBUG;
298 if (defined $entry->{doi}) {
299 if ($options->{only_print}) {
303 my $url = $entry->{doi};
304 $url =~ s{^doi://}{http://dx.doi.org/};
305 open_browser($url,$options);
308 if (defined $entry->{html}) {
309 if ($options->{only_print}) {
310 print $entry->{html};
313 open_browser($entry->{html},$options);
318 sub select_entry_from_pmid{
319 my ($dbh,$sth,$pmid) = @_;
321 return select_one($dbh,$sth->{select_bibtex_by_pmid},$pmid);
325 sub select_entry_from_bibtex_key{
326 my ($dbh,$sth,$bibtex_key) = @_;
328 my $entry = select_one($dbh,$sth->{select_bibtex_by_key},$bibtex_key);
329 if (not defined $entry) {
330 print STDERR "Unable to find entry by exact search\n" if $DEBUG;
331 $bibtex_key =~ s/:.*$//;
332 $entry = select_one($dbh,$sth->{select_bibtex_by_approximate_key},$bibtex_key.'%');
334 print STDERR "Found entry\n" if $DEBUG and defined $entry;
339 my ($dbh,$sth,@bind_vals) = @_;
340 $sth->execute(@bind_vals) or
341 die "Unable to select one: ".$dbh->errstr();
342 my $results = $sth->fetchall_arrayref({});
344 return ref($results)?$results->[0]:undef;
347 sub parse_bibtex_file {
348 my ($file,$entries) = @_;
350 my $bibfile = Text::BibTeX::File->new($file)
351 or die "Unable to open $file for reading: $!";
354 while ($entry = Text::BibTeX::Entry->new($bibfile)) {
355 print STDERR "In Entry ".$entry->metatype() if $DEBUG;
356 if ($entry->metatype() == BTE_COMMENT) {
357 push @entry_comments,$entry->value();
358 } elsif ($entry->metatype() == BTE_REGULAR) {
359 my $entry_key = $entry->key();
360 if (not defined $entry_key) {
361 @entry_comments = ();
365 # if there is a file comment, use it as the file name
366 for my $comment (@entry_comments) {
367 next unless $comment =~ /^\s*file(?:name)?:?\s*(.+?)\s*$/i;
368 next unless length $1;
369 $entry_data{file_name} = $1.'.pdf';
372 my %field_prefix = (doi => 'doi://',
377 my %field_name = (doi => 'doi',
380 file => 'file_name',);
381 for my $field (qw(file doi html pmid)) {
382 my $field_value = $entry->get($field);
383 if (defined $field_value and $field_value =~ /\S+/) {
384 $entry_data{$field_name{$field}} =
385 $field_prefix{$field}.$field_value if
386 not defined $entry_data{$field_name{$field}};
389 $entries->{$entry_key} = {} if not defined $entries->{$entry_key};
390 for my $field (keys %entry_data) {
391 $entries->{$entry_key}{$field} = $entry_data{$field} if
392 defined $entry_data{$field};
394 # reset the entry comments
395 @entry_comments = ();
399 print STDERR "\n" if $DEBUG;
405 sub initialize_database {
407 return open_cache($cache,1);
411 my ($cache,$initialize) = @_;
412 my $dbh = DBI->connect("dbi:SQLite:dbname=$cache","","") or
413 die "Unable to open/create database $cache";
415 $dbh->do("DROP TABLE IF EXISTS bibtex;");
416 $dbh->do("DROP TABLE IF EXISTS papers;");
418 CREATE TABLE bibtex (
419 bibtex_key TEXT PRIMARY KEY,
427 CREATE UNIQUE INDEX bibtex_file_name ON bibtex(file_name);
430 CREATE UNIQUE INDEX bibtex_bibtex_key ON bibtex(bibtex_key);
433 CREATE INDEX bibtex_pmid ON bibtex(pmid);
436 CREATE TABLE papers (
437 file_name TEXT PRIMARY KEY,
443 CREATE UNIQUE INDEX papers_path ON papers(path);
446 CREATE UNIQUE INDEX papers_file_name ON papers(file_name);
450 (insert_papers => <<'EOF',
451 INSERT OR REPLACE INTO papers(file_name,path,has_xoj) VALUES (?,?,?);
453 insert_bibtex => <<'EOF',
454 INSERT OR REPLACE INTO bibtex (bibtex_key,file_name,pmid,doi,html) VALUES (?,?,?,?,?);
456 select_papers_by_name => <<'EOF',
457 SELECT * FROM papers WHERE file_name = ?;
459 select_papers_by_pmid => <<'EOF',
460 SELECT * FROM papers WHERE file_name LIKE ?;
462 select_papers_by_path => <<'EOF',
463 SELECT * FROM papers WHERE path = ?;
465 select_bibtex_by_key => <<'EOF',
466 SELECT * FROM bibtex WHERE bibtex_key = ?;
468 select_bibtex_by_approximate_key => <<'EOF',
469 SELECT * FROM bibtex WHERE bibtex_key LIKE ?;
471 select_bibtex_by_file_name => <<'EOF',
472 SELECT * FROM bibtex WHERE file_name = ?;
474 select_bibtex_by_pmid => <<'EOF',
475 SELECT * FROM bibtex WHERE pmid = ?;
477 clear_papers_cache => <<'EOF',
480 clear_bibtex_cache => <<'EOF',
485 for my $key (keys %s) {
486 $st->{$key}=$dbh->prepare($s{$key}) //
487 die "Unable to prepare sql statement: ".$dbh->errstr;