| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
=pod |
| 4 |
|
| 5 |
=head1 NAME |
| 6 |
|
| 7 |
bibmradd.pl - add MR numbers to papers in a given bib file |
| 8 |
|
| 9 |
=head1 SYNOPSIS |
| 10 |
|
| 11 |
bibmradd [-d] [B<-f>] [B<-e> 1|0] [B<-o> I<output>] I<bib_file> |
| 12 |
|
| 13 |
=head1 OPTIONS |
| 14 |
|
| 15 |
=over 4 |
| 16 |
|
| 17 |
=item B<-d> |
| 18 |
|
| 19 |
Debug mode |
| 20 |
|
| 21 |
=item B<-e> |
| 22 |
|
| 23 |
If 1 (default), add an empty mrnumber if a mr cannot be found. This |
| 24 |
prevents repeated searches for the same entries if you add new entries |
| 25 |
to the file. Calling C<-e 0> suppresses this behavior. |
| 26 |
|
| 27 |
|
| 28 |
=item B<-f> |
| 29 |
|
| 30 |
Force searching for MR numbers even if the entry already has one. |
| 31 |
|
| 32 |
=item B<-o> I<output> |
| 33 |
|
| 34 |
Output file. If this option is not used, the name for the |
| 35 |
output file is formed by adding C<_mr> to the input file |
| 36 |
|
| 37 |
=back |
| 38 |
|
| 39 |
=head1 DESCRIPTION |
| 40 |
|
| 41 |
The script reads a BibTeX file. It checks whether the entries have |
| 42 |
mrnumbers. If not, it tries to find the numbers from Internet sites. The |
| 43 |
result is a BibTeX file with C<mrnumber=...> fields added. |
| 44 |
|
| 45 |
The name of the output file is either set by the B<-o> option or |
| 46 |
is derived by adding the suffix C<_mr> to the output file. |
| 47 |
|
| 48 |
See the C<bibdoiadd> script for more details on the processing. |
| 49 |
|
| 50 |
=head1 AUTHOR |
| 51 |
|
| 52 |
Boris Veytsman |
| 53 |
|
| 54 |
=head1 COPYRIGHT AND LICENSE |
| 55 |
|
| 56 |
Copyright (C) 2014-2024 Boris Veytsman |
| 57 |
|
| 58 |
This is free software. You may redistribute copies of it under the |
| 59 |
terms of the GNU General Public License |
| 60 |
L<http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the |
| 61 |
extent permitted by law. |
| 62 |
|
| 63 |
=cut |
| 64 |
|
| 65 |
use strict; use warnings; |
| 66 |
use Cwd; |
| 67 |
use File::Basename; |
| 68 |
use File::Spec; |
| 69 |
|
| 70 |
BEGIN { |
| 71 |
# find files relative to our installed location within TeX Live |
| 72 |
chomp(my $TLMaster = `kpsewhich -var-value=TEXMFROOT`); # TL root |
| 73 |
if (length($TLMaster)) { |
| 74 |
unshift @INC, "$TLMaster/texmf-dist/scripts/bibtexperllibs"; |
| 75 |
} |
| 76 |
# find development bibtexperllibs in sibling checkout to this script, |
| 77 |
# even if $0 is a symlink. All irrelevant when using from an installation. |
| 78 |
my $real0 = Cwd::abs_path($0); |
| 79 |
my $scriptdir = File::Basename::dirname($real0); |
| 80 |
my $dev_btxperllibs = Cwd::abs_path("$scriptdir/../bibtexperllibs"); |
| 81 |
# we need the lib/ subdirectories inside ... |
| 82 |
unshift (@INC, glob ("$dev_btxperllibs/*/lib")) if -d $dev_btxperllibs; |
| 83 |
} |
| 84 |
use IO::File; |
| 85 |
use BibTeX::Parser; |
| 86 |
use Getopt::Std; |
| 87 |
use URI::Escape; |
| 88 |
use LWP::UserAgent; |
| 89 |
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0; |
| 90 |
# Sometimes AMS forgets to update certificates |
| 91 |
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0; |
| 92 |
|
| 93 |
my $USAGE="USAGE: $0 [-d] [-e 1|0] [-f] [-o output] file\n"; |
| 94 |
my $VERSION = <<END; |
| 95 |
bibmradd v2.3 |
| 96 |
This is free software. You may redistribute copies of it under the |
| 97 |
terms of the GNU General Public License |
| 98 |
http://www.gnu.org/licenses/gpl.html. There is NO WARRANTY, to the |
| 99 |
extent permitted by law. |
| 100 |
$USAGE |
| 101 |
END |
| 102 |
my %opts; |
| 103 |
getopts('de:fo:hV',\%opts) or die $USAGE; |
| 104 |
|
| 105 |
if ($opts{h} || $opts{V}){ |
| 106 |
print $VERSION; |
| 107 |
exit 0; |
| 108 |
} |
| 109 |
|
| 110 |
################################################################ |
| 111 |
# Defaults and parameters |
| 112 |
################################################################ |
| 113 |
|
| 114 |
my $inputfile = shift; |
| 115 |
|
| 116 |
my $outputfile = $inputfile; |
| 117 |
|
| 118 |
$outputfile =~ s/\.([^\.]*)$/_mr.$1/; |
| 119 |
|
| 120 |
if ($opts{o}) { |
| 121 |
$outputfile = $opts{o}; |
| 122 |
} |
| 123 |
|
| 124 |
my $forceSearch=$opts{f}; |
| 125 |
|
| 126 |
my $forceEmpty = 1; |
| 127 |
if (exists $opts{e}) { |
| 128 |
$forceEmpty = $opts{e}; |
| 129 |
} |
| 130 |
|
| 131 |
my $debug = $opts{d}; |
| 132 |
|
| 133 |
my $input= IO::File->new($inputfile) or |
| 134 |
die "Cannot find BibTeX file $inputfile\n$USAGE\n"; |
| 135 |
my $output = IO::File->new("> $outputfile") or |
| 136 |
die "Cannot write to $outputfile\n$USAGE\n"; |
| 137 |
|
| 138 |
my $parser=new BibTeX::Parser($input); |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
# Creating the HTTP parameters |
| 143 |
my $mirror = |
| 144 |
"https://mathscinet.ams.org/mathscinet-mref"; |
| 145 |
my $userAgent = LWP::UserAgent->new; |
| 146 |
|
| 147 |
while (my $entry = $parser->next ) { |
| 148 |
if (!$entry->parse_ok()) { |
| 149 |
print STDERR "Cannot understand entry: "; |
| 150 |
$entry->print(*STDERR); |
| 151 |
print STDERR "Skipping this entry\n"; |
| 152 |
next; |
| 153 |
} |
| 154 |
if ($entry->has('mrnumber') && !$forceSearch) { |
| 155 |
print $output $entry->raw_bibtex(), "\n\n"; |
| 156 |
if ($debug) { |
| 157 |
print STDERR "DEBUG: entry ", $entry->key(), |
| 158 |
" has mrnumber ", $entry->field('mrnumber'), |
| 159 |
" and no forced search is requested\n"; |
| 160 |
} |
| 161 |
next; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
# Now we have an entry with no MR. Let us get to work. |
| 166 |
if ($debug) { |
| 167 |
print STDERR "DEBUG: Searching for mr number for entry ", |
| 168 |
$entry->key, "\n"; |
| 169 |
} |
| 170 |
my $mr = GetMr($entry, $userAgent, $mirror); |
| 171 |
if (length($mr) || $forceEmpty) { |
| 172 |
$entry->field('mrnumber', $mr); |
| 173 |
} |
| 174 |
print $output $entry->to_string(), "\n\n"; |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
$input->close(); |
| 179 |
$output->close(); |
| 180 |
exit 0; |
| 181 |
|
| 182 |
############################################################### |
| 183 |
# Getting one MR |
| 184 |
############################################################### |
| 185 |
|
| 186 |
sub GetMr { |
| 187 |
my $entry=shift; |
| 188 |
my $userAgent=shift; |
| 189 |
my $mirror=shift; |
| 190 |
|
| 191 |
my @query; |
| 192 |
|
| 193 |
my $string=uri_escape_utf8($entry->to_string()); |
| 194 |
|
| 195 |
if ($debug) { |
| 196 |
print STDERR "DEBUG: query: $mirror?ref=$string&dataType=bibtex\n" ; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
my $response = $userAgent->get("$mirror?ref=$string&dataType=bibtex"); |
| 201 |
if ($debug) { |
| 202 |
print STDERR "DEBUG: response: ", |
| 203 |
$response->decoded_content, "\n"; |
| 204 |
} |
| 205 |
|
| 206 |
if ($response->decoded_content =~ /MRNUMBER\s*=\s*{(.*)}/m) { |
| 207 |
my $mr=$1; |
| 208 |
# Somehow mref deletes leading zeros. They are needed! |
| 209 |
while (length($mr)<7) { |
| 210 |
$mr = "0$mr"; |
| 211 |
} |
| 212 |
if ($debug) { |
| 213 |
print STDERR "DEBUG: got MR: $mr\n", |
| 214 |
} |
| 215 |
return $mr; |
| 216 |
} else { |
| 217 |
if ($debug) { |
| 218 |
print STDERR "DEBUG: Did not get MR\n", |
| 219 |
} |
| 220 |
return (""); |
| 221 |
} |
| 222 |
|
| 223 |
} |