PDFLibrarian/QueryDialog.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 106 CC 6 SCAR 20.0
CC
6
Cov
0%
do_query_dialog
20.0
extract_query_values_from_pdf
12.0
BEGIN
2.0
CRAP 7.4
1 # Copyright (C) 2016--2026 Karl Wette
2 #
3 # This file is part of App::PDFLibrarian.
4 #
5 # App::PDFLibrarian is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or (at
8 # your option) any later version.
9 #
10 # App::PDFLibrarian is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with App::PDFLibrarian. If not, see <http://www.gnu.org/licenses/>.
17  
18 use v5.18;
19 use strict;
20 use warnings;
21 use utf8;
22 use open qw(:std :encoding(UTF-8));
23  
24 package App::PDFLibrarian::QueryDialog;
25 $App::PDFLibrarian::QueryDialog::VERSION = '6.3.4';
26 use parent 'Exporter';
27  
28 use Carp;
29 use FindBin qw($Script);
30 use Wx qw(:id);
31  
32 use App::PDFLibrarian;
33 use App::PDFLibrarian::QueryDialog::impl;
34 use App::PDFLibrarian::Util qw(unique_list);
35  
36 our @EXPORT_OK = qw(extract_query_values_from_pdf do_query_dialog);
37  
38 sub extract_query_values_from_pdf {
39 my ($pdffile) = @_;
40  
41 # try to extract possible query values
42 my @query_values;
43  
44 {
45 # open PDF file
46 my $pdf = PDF::API2->open($pdffile);
47  
48 # try to extract a DOI from PDF info structure
49 my @pdfinfotags = $pdf->infoMetaAttributes();
50 push @pdfinfotags, qw(DOI doi);
51 $pdf->infoMetaAttributes(@pdfinfotags);
52 my %pdfinfo = $pdf->info();
53 while (my ($key, $value) = each %pdfinfo) {
54 if ($key =~ /^doi$/i) {
55 push @query_values, $value;
56 }
57 }
58  
59 # try to extract a DOI from PDF info structure
60 my $xmp = $pdf->xmpMetadata() // "";
61 $xmp =~ s/\s+//g;
62 while ($xmp =~ m|doi>([^<]+)<|ig) {
63 push @query_values, $1;
64 }
65 }
66  
67 if (@query_values == 0 ) {
68  
69 # try to use pdftotext to extract PDF text
70 my $cmd = "pdftotext '$pdffile' - 2>/dev/null";
71 printf STDERR "$Script: running $cmd ...\n";
72 flush STDERR;
73 open PDFTOTEXT, "$cmd |" or croak "$Script: could not run $cmd";
74 foreach my $text (<PDFTOTEXT>) {
75  
76 # try to extract a DOI from PDF text
77 $text =~ s/\s+/ /g;
78 while ($text =~ m{(?:doi[:]? *|https?[:]//[\w.]*doi\.org/)([^ ]+)}ig) {
79 push @query_values, $1;
80 }
81  
82 }
83 close PDFTOTEXT;
84  
85 }
86  
87 return unique_list(@query_values);
88 }
89  
90 sub do_query_dialog {
91 my ($pdffile, $query_db_name, $query_value, $query_values, $error_message) = @_;
92 my ($ui_query_db_name, $ui_query_value);
93  
94 # show dialog
95 my $dialog = App::PDFLibrarian::QueryDialog::impl->new($pdffile, $query_db_name, $query_value, $query_values, $error_message);
96 my $ui = $dialog->ShowModal();
97  
98 # cancel all imports and exit
99 return ('exit', undef, undef) if $ui == wxID_EXIT;
100  
101 # skip import of PDF
102 return ('cancel', undef, undef) if $ui == wxID_CANCEL;
103  
104 # manually enter BibTeX record
105 return ('manual', undef, undef) if $ui == wxID_EDIT;
106  
107 # run query of database with given query value
108 ($ui_query_db_name, $ui_query_value) = $dialog->get_data();
109 return ('query', $ui_query_db_name, $ui_query_value);
110  
111 }