]> git.donarmstrong.com Git - bin.git/blob - bibtex_to_paper
Abstract out GIT_HOST
[bin.git] / bibtex_to_paper
1 #!/usr/bin/perl
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
5 # more information.
6 # Copyright 2014 by Don Armstrong <don@donarmstrong.com>.
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 use File::Find;
16 use File::Basename qw(basename);
17 use File::Spec qw(rel2abs);
18 use Text::BibTeX;
19 use User;
20 use Data::Printer;
21 use POSIX;
22
23 use DBI;
24
25 =head1 NAME
26
27 bibtex_to_paper - opens the paper corresponding to a bibtex key
28
29 =head1 SYNOPSIS
30
31 bibtex_to_paper [options] bibtexkey
32
33  Options:
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
44
45 =head1 OPTIONS
46
47 =over
48
49 =item B<--bibtex, -b>
50
51 Bibtex file to look key up in
52
53 =item B<--bibtex-cache, -c>
54
55 Bibtex cache file; rebuilt if bibtex file changes
56
57 =item B<--pdfviewer, -p>
58
59 PDF viewer to use; defaults to evince unless a .xoj exists, in which
60 case xournal is used.
61
62 =item B<--only-print>
63
64 Only print the PDF file name, don't open it.
65
66 =item B<--debug, -d>
67
68 Debug verbosity. (Default 0)
69
70 =item B<--help, -h>
71
72 Display brief usage information.
73
74 =item B<--man, -m>
75
76 Display this manual.
77
78 =back
79
80 =head1 EXAMPLES
81
82 bibtex_to_paper
83
84 =cut
85
86
87 use vars qw($DEBUG);
88
89 my %options = (debug           => 0,
90                help            => 0,
91                man             => 0,
92                only_print      => 0,
93                search_by_pmid  => 0,
94                use_git         => 1,
95                'bibtex_cache'  => File::Spec->catfile(User->Home,'.bibtex_to_paper_cache'),
96               );
97
98 GetOptions(\%options,
99            'build_cache|build-cache!',
100            'bibtex|b=s@',
101            'bibtex_cache|bibtex-cache|c=s',
102            'pdfviewer|p=s',
103            'use_git|use-git!',
104            'only_print|only-print!',
105            'search_by_pmid|search-by-pmid!',
106            'clear_cache|clear-cache!',
107            'papers_directory|papers-directory=s@',
108            'debug|d+','help|h|?','man|m');
109
110 pod2usage() if $options{help};
111 pod2usage({verbose=>2}) if $options{man};
112
113 $DEBUG = $options{debug};
114
115 my @USAGE_ERRORS;
116 if (not exists $options{bibtex} and
117     not exists $options{bibtex_cache}) {
118     push @USAGE_ERRORS,
119         "You must give at least one of --bibtex".
120         "or --bibtex-cache";
121 }
122
123 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
124
125 main();
126
127 sub main{
128
129     my $dbh;
130     my $sth;
131     if (exists $options{bibtex_cache}) {
132         my $initialize = 0;
133         if (-e $options{bibtex_cache}) {
134             ($dbh,$sth) = open_cache($options{bibtex_cache});
135         } else {
136             ($dbh,$sth) = initialize_database($options{bibtex_cache});
137         }
138     }
139
140     if (exists $options{clear_cache}) {
141         clear_cache($dbh,$sth);
142     }
143     my %entries;
144     if (exists $options{build_cache}) {
145         $options{bibtex} //= [];
146         $options{bibtex} =
147             [@ARGV,
148              @{ref $options{bibtex}?$options{bibtex}:[$options{bibtex}]},
149             ];
150         @ARGV = ();
151     }
152     if (exists $options{bibtex}) {
153         for my $bibtex_file (@{ref $options{bibtex}?$options{bibtex}:[$options{bibtex}]}) {
154             parse_bibtex_file($bibtex_file,\%entries);
155         }
156     }
157
158     if (exists $options{papers_directory} and
159         defined $dbh
160        ) {
161         $dbh->begin_work;
162         load_papers_into_database($dbh,$sth,$options{papers_directory});
163         $dbh->commit;
164     }
165
166     p %entries if $DEBUG;
167     if (keys %entries and
168         defined $dbh) {
169         $dbh->begin_work;
170         load_bibtex_entries_into_database($dbh,$sth,\%entries);
171         $dbh->commit;
172     }
173
174     p @ARGV if $DEBUG;
175     for my $bibtex_key (@ARGV) {
176         open_bibtex_key(\%options,$dbh,$sth,\%entries,$bibtex_key);
177     }
178
179 }
180
181 sub clear_cache {
182     my ($dbh,$sth) = @_;
183     $sth->{clear_papers_cache}->execute();
184     $sth->{clear_bibtex_cache}->execute();
185 }
186
187 sub load_papers_into_database {
188     my ($dbh,$sth,$dir) = @_;
189
190     my @dirs = ref($dir)?@{$dir}:$dir;
191
192     if ($options{use_git}) {
193         my @files = grep /\.pdf\"?$/, split /\n/, qx(git ls-tree HEAD -r --full-name --name-only);
194         for my $file  (@files) {
195             $file =~ s/^\"(.+)\"$/"qq($1)"/gee;
196             insert_or_replace_papers($dbh,$sth,basename($file),File::Spec->rel2abs($file), -e "${file}.xoj");
197         }
198     } else {
199         my $actually_load_it = sub {
200             if (/\.git/) {
201                 $File::Find::prune = 1;
202                 return;
203             }
204             return unless /\.pdf$/;
205             my $xoj = 0;
206             if (-e "${_}.xoj") {
207                 $xoj = 1;
208             }
209             insert_or_replace_papers($dbh,$sth,basename($File::Find::name),File::Spec->rel2abs($_),$xoj);
210         };
211         find($actually_load_it,@dirs);
212     }
213 }
214
215 sub insert_or_replace_papers {
216     my ($dbh,$sth,$file_name,$file_loc,$has_xoj) = @_;
217     $sth->{insert_papers}->execute($file_name,$file_loc,$has_xoj);
218     $sth->{insert_papers}->finish();
219 }
220
221 sub load_bibtex_entries_into_database {
222     my ($dbh,$sth,$entries) = @_;
223     for my $entry (keys %{$entries}) {
224         next unless defined $entries->{$entry};
225         $sth->{insert_bibtex}->execute($entry,@{$entries->{$entry}}{qw(file_name pmid doi html)});
226         $sth->{insert_bibtex}->finish();
227         print STDERR "inserted $entry {".join(',',map {defined $_?"'$_'":"'undef'"} %{$entries->{$entry}})."}\n" if $DEBUG;
228     }
229 }
230
231 sub open_bibtex_key {
232     my ($options,$dbh,$sth,$entries,$bibtex_key) = @_;
233     if (not defined $dbh) {
234         open_entry($dbh,$sth,$entries->{$bibtex_key},$options);
235     } else {
236         my $entry;
237         if ($options->{search_by_pmid}) {
238             $entry = select_entry_from_pmid($dbh,$sth,$bibtex_key);
239         } else {
240             $entry = select_entry_from_bibtex_key($dbh,$sth,$bibtex_key);
241         }
242         p $entry if $DEBUG;
243         open_entry($dbh,$sth,$entry,$options);
244     }
245 }
246
247 sub fork_exec {
248     my (@cmd) = @_;
249     my $child = fork();
250     if (not defined $child) {
251         die "Unable to fork for some reason: $!";
252     }
253     if ($child == 0) {
254         foreach (0 .. (POSIX::sysconf (&POSIX::_SC_OPEN_MAX) || 1024))
255            { POSIX::close $_ }
256         open (STDIN, "</dev/null");
257         open (STDOUT, ">/dev/null");
258         open (STDERR, ">&STDOUT");
259         exec(@cmd);
260     } else {
261         return $child;
262     }
263
264 }
265
266 sub open_pdf {
267     my ($file_name,$options,$has_xoj) = @_;
268     print STDERR "opening $file_name\n" if $DEBUG;
269     my $pdf_viewer = 'xournal';
270     if (exists $options->{pdfviewer} and defined $options->{pdfviewer}) {
271         $pdf_viewer = $options->{pdfviewer};
272     }
273     fork_exec($pdf_viewer,$file_name);
274 }
275
276 sub open_browser{
277     my ($file) = @_;
278     fork_exec('sensible-browser',$file);
279 }
280
281 sub open_entry{
282     my ($dbh,$sth,$entry,$options) = @_;
283
284     return unless defined $entry and ref $entry and keys %{$entry};
285     if ($DEBUG) {
286         print STDERR "Entry: \n";
287         p $entry;
288     }
289     if (defined $entry->{file_name} and length $entry->{file_name}) {
290         my $paper = select_one($dbh,$sth->{select_papers_by_name},$entry->{file_name});
291         if (not defined $paper) {
292             my ($pmid) = $entry->{file_name} =~ /pmid_(\d+)/;
293             if (defined $pmid and length $pmid) {
294                 $paper = select_one($dbh,$sth->{select_papers_by_pmid},'%pmid_'.$pmid.'.%');
295             }
296         }
297         p $paper if $DEBUG;
298         print STDERR $entry->{file_name} if $DEBUG;
299         if (defined $paper) {
300             if ($options->{only_print}) {
301                 print $paper->{path};
302                 return;
303             }
304             open_pdf($paper->{path},$options,$paper->{has_xoj});
305             return;
306         } else {
307             print STDERR "Unable to find paper\n" if $DEBUG;
308         }
309     }
310     if (defined $entry->{doi}) {
311         if ($options->{only_print}) {
312             print $entry->{doi};
313             return;
314         }
315         my $url = $entry->{doi};
316         $url =~ s{^doi://}{http://dx.doi.org/};
317         open_browser($url,$options);
318         return;
319     }
320     if (defined $entry->{html}) {
321         if ($options->{only_print}) {
322             print $entry->{html};
323             return;
324         }
325         open_browser($entry->{html},$options);
326         return;
327     }
328 }
329
330 sub select_entry_from_pmid{
331     my ($dbh,$sth,$pmid) = @_;
332
333     return select_one($dbh,$sth->{select_bibtex_by_pmid},$pmid);
334 }
335
336
337 sub select_entry_from_bibtex_key{
338     my ($dbh,$sth,$bibtex_key) = @_;
339
340     my $entry = select_one($dbh,$sth->{select_bibtex_by_key},$bibtex_key);
341     if (not defined $entry) {
342         print STDERR "Unable to find entry by exact search\n" if $DEBUG;
343         $bibtex_key =~ s/:.*$//;
344         $entry = select_one($dbh,$sth->{select_bibtex_by_approximate_key},$bibtex_key.'%');
345     }
346     print STDERR "Found entry\n" if $DEBUG and defined $entry;
347     return $entry;
348 }
349
350 sub select_one{
351     my ($dbh,$sth,@bind_vals) = @_;
352     $sth->execute(@bind_vals) or
353         die "Unable to select one: ".$dbh->errstr();
354     my $results = $sth->fetchall_arrayref({});
355     $sth->finish();
356     return ref($results)?$results->[0]:undef;
357 }
358
359 sub parse_bibtex_file {
360     my ($file,$entries) = @_;
361
362     my $bibfile = Text::BibTeX::File->new($file)
363         or die "Unable to open $file for reading: $!";
364     my @entry_comments;
365     my $entry;
366     while ($entry = Text::BibTeX::Entry->new($bibfile)) {
367         print STDERR "In Entry ".$entry->metatype() if $DEBUG;
368         if ($entry->metatype() == BTE_COMMENT) {
369             push @entry_comments,$entry->value();
370         } elsif ($entry->metatype() == BTE_REGULAR) {
371             my $entry_key = $entry->key();
372             if (not defined $entry_key) {
373                 @entry_comments = ();
374                 next;
375             }
376             my %entry_data;
377             # if there is a file comment, use it as the file name
378             for my $comment (@entry_comments) {
379                 next unless $comment =~ /^\s*file(?:name)?:?\s*(.+?)\s*$/i;
380                 next unless length $1;
381                 $entry_data{file_name} = $1.'.pdf';
382                 last;
383             }
384             my %field_prefix = (doi => 'doi://',
385                                 html => 'http://',
386                                 file => '',
387                                 pmid => '',
388                                );
389             my %field_name = (doi => 'doi',
390                               html => 'html',
391                               pmid => 'pmid',
392                               file => 'file_name',);
393             for my $field (qw(file doi html pmid)) {
394                 my $field_value = $entry->get($field);
395                 if (defined $field_value and $field_value =~ /\S+/) {
396                     $entry_data{$field_name{$field}} =
397                         $field_prefix{$field}.$field_value if
398                         not defined $entry_data{$field_name{$field}};
399                 }
400             }
401             $entries->{$entry_key} = {} if not defined $entries->{$entry_key};
402             for my $field (keys %entry_data) {
403                 $entries->{$entry_key}{$field} = $entry_data{$field} if
404                     defined $entry_data{$field};
405             }
406             # reset the entry comments
407             @entry_comments = ();
408         } else {
409             # do nothing
410         }
411         print STDERR "\n" if $DEBUG;
412     }
413     return $entries;
414 }
415
416
417 sub initialize_database {
418     my ($cache) = @_;
419     return open_cache($cache,1);
420 }
421
422 sub open_cache {
423     my ($cache,$initialize) = @_;
424     my $dbh = DBI->connect("dbi:SQLite:dbname=$cache","","") or
425         die "Unable to open/create database $cache";
426     if ($initialize) {
427         $dbh->do("DROP TABLE IF EXISTS bibtex;");
428         $dbh->do("DROP TABLE IF EXISTS papers;");
429         $dbh->do(<<EOF);
430 CREATE TABLE bibtex (
431 bibtex_key TEXT PRIMARY KEY,
432 file_name TEXT,
433 pmid TEXT,
434 doi TEXT,
435 html TEXT
436 );
437 EOF
438         $dbh->do(<<EOF);
439 CREATE UNIQUE INDEX bibtex_file_name ON bibtex(file_name);
440 EOF
441         $dbh->do(<<EOF);
442 CREATE UNIQUE INDEX bibtex_bibtex_key ON bibtex(bibtex_key);
443 EOF
444         $dbh->do(<<EOF);
445 CREATE INDEX bibtex_pmid ON bibtex(pmid);
446 EOF
447         $dbh->do(<<EOF);
448 CREATE TABLE papers (
449 file_name TEXT PRIMARY KEY,
450 path TEXT,
451 has_xoj BOOLEAN
452 );
453 EOF
454         $dbh->do(<<EOF);
455 CREATE UNIQUE INDEX papers_path ON papers(path);
456 EOF
457         $dbh->do(<<EOF);
458 CREATE UNIQUE INDEX papers_file_name ON papers(file_name);
459 EOF
460     }
461     my %s =
462         (insert_papers => <<'EOF',
463 INSERT OR REPLACE INTO papers(file_name,path,has_xoj) VALUES (?,?,?);
464 EOF
465          insert_bibtex => <<'EOF',
466 INSERT OR REPLACE INTO bibtex (bibtex_key,file_name,pmid,doi,html) VALUES (?,?,?,?,?);
467 EOF
468          select_papers_by_name => <<'EOF',
469 SELECT * FROM papers WHERE file_name = ?;
470 EOF
471          select_papers_by_pmid => <<'EOF',
472 SELECT * FROM papers WHERE file_name LIKE ?;
473 EOF
474          select_papers_by_path => <<'EOF',
475 SELECT * FROM papers WHERE path = ?;
476 EOF
477          select_bibtex_by_key => <<'EOF',
478 SELECT * FROM bibtex WHERE bibtex_key = ?;
479 EOF
480          select_bibtex_by_approximate_key => <<'EOF',
481 SELECT * FROM bibtex WHERE bibtex_key LIKE ?;
482 EOF
483          select_bibtex_by_file_name => <<'EOF',
484 SELECT * FROM bibtex WHERE file_name = ?;
485 EOF
486          select_bibtex_by_pmid => <<'EOF',
487 SELECT * FROM bibtex WHERE pmid = ?;
488 EOF
489          clear_papers_cache => <<'EOF',
490 DELETE FROM papers;
491 EOF
492          clear_bibtex_cache => <<'EOF',
493 DELETE FROM bibtex;
494 EOF
495         );
496     my $st;
497     for my $key (keys %s) {
498         $st->{$key}=$dbh->prepare($s{$key}) //
499             die "Unable to prepare sql statement: ".$dbh->errstr;
500     }
501     return ($dbh,$st);
502 }
503
504
505 __END__