| 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::Library; | ||
| 25 | $App::PDFLibrarian::Library::VERSION = '6.3.4'; | ||
| 26 | use parent 'Exporter'; | ||
| 27 | |||
| 28 | use Carp; | ||
| 29 | use File::Copy; | ||
| 30 | use File::Find; | ||
| 31 | use File::Path; | ||
| 32 | use File::Spec; | ||
| 33 | use File::stat; | ||
| 34 | use FindBin qw($Script); | ||
| 35 | use Text::Unidecode; | ||
| 36 | |||
| 37 | use App::PDFLibrarian qw($pdflibrarydir); | ||
| 38 | use App::PDFLibrarian::BibTeX qw(bib_checksum format_bib_authors); | ||
| 39 | use App::PDFLibrarian::Util qw(is_in_dir remove_tex_markup remove_tex_markup_undef remove_short_words); | ||
| 40 | |||
| 41 | our @EXPORT_OK = qw(pdf_is_in_library update_pdf_lib make_pdf_links cleanup_links); | ||
| 42 | |||
| 43 | 1; | ||
| 44 | |||
| 45 | sub pdf_is_in_library { | ||
| 46 | my ($pdffile) = @_; | ||
| 47 | |||
| 48 | # return whether PDF file is already in library | ||
| 49 | return is_in_dir("$pdflibrarydir/Files", $pdffile); | ||
| 50 | |||
| 51 | } | ||
| 52 | |||
| 53 | sub update_pdf_lib { | ||
| 54 | my (@bibentries) = @_; | ||
| 55 | return unless @bibentries > 0; | ||
| 56 | |||
| 57 | # add PDF files in BibTeX entries to PDF library | ||
| 58 | my $newbibentries = 0; | ||
| 59 | foreach my $bibentry (@bibentries) { | ||
| 60 | |||
| 61 | # get name of PDF file | ||
| 62 | my $pdffile = $bibentry->get('file'); | ||
| 63 | |||
| 64 | # record which PDF files are new to the library | ||
| 65 | ++$newbibentries unless pdf_is_in_library($pdffile); | ||
| 66 | |||
| 67 | # create name of PDF file in library: | ||
| 68 | # - organise PDF files under '$pdflibrarydir/Files', then first uppercase character of key | ||
| 69 | # - name PDF files after key with '.pdf' extension | ||
| 70 | my $key = $bibentry->key; | ||
| 71 | my $pdflibdir = File::Spec->catdir($pdflibrarydir, "Files", uc(substr($key, 0, 1))); | ||
| 72 | my $pdflibfile = File::Spec->catfile($pdflibdir, "$key.pdf"); | ||
| 73 | |||
| 74 | # ensure directory for PDF file exists | ||
| 75 | File::Path::make_path($pdflibdir); | ||
| 76 | |||
| 77 | # move PDF file into library, including if library PDF filename has changed | ||
| 78 | if ($pdffile ne $pdflibfile) { | ||
| 79 | move($pdffile, $pdflibfile) or croak "$Script: could not move '$pdffile' to '$pdflibfile': $!"; | ||
| 80 | $pdffile = $pdflibfile; | ||
| 81 | } | ||
| 82 | |||
| 83 | # record new PDF filename | ||
| 84 | $bibentry->set('file', $pdflibfile); | ||
| 85 | |||
| 86 | } | ||
| 87 | printf STDERR "$Script: added %i PDF files to '$pdflibrarydir/Files'\n", $newbibentries if $newbibentries > 0; | ||
| 88 | |||
| 89 | } | ||
| 90 | |||
| 91 | sub make_pdf_links { | ||
| 92 | my (@bibentries) = @_; | ||
| 93 | return unless @bibentries > 0; | ||
| 94 | |||
| 95 | # find existing links | ||
| 96 | my %existinglinks; | ||
| 97 | { | ||
| 98 | my $wanted = sub { | ||
| 99 | if (-l $_) { | ||
| 100 | my $pdffile = readlink($_); | ||
| 101 | $existinglinks{$pdffile}->{$_} = 1 if defined($pdffile); | ||
| 102 | } | ||
| 103 | }; | ||
| 104 | find({wanted => \&$wanted, bydepth => 1, no_chdir => 1}, $pdflibrarydir); | ||
| 105 | } | ||
| 106 | |||
| 107 | # make symbolic links in PDF links directory to real PDF files | ||
| 108 | my $count = 0; | ||
| 109 | foreach my $bibentry (@bibentries) { | ||
| 110 | |||
| 111 | # get name of PDF file | ||
| 112 | my $pdffile = $bibentry->get('file'); | ||
| 113 | |||
| 114 | # get key from PDF filename | ||
| 115 | my $key; | ||
| 116 | { | ||
| 117 | my ($vol, $dir, $file) = File::Spec->splitpath($pdffile); | ||
| 118 | $key = $file; | ||
| 119 | $key =~ s/\.pdf$//; | ||
| 120 | } | ||
| 121 | |||
| 122 | # format and abbreviate title | ||
| 123 | my $title = remove_tex_markup_undef($bibentry->get("title")) // "!NO-TITLE!"; | ||
| 124 | $title = join(' ', map { ucfirst($_) } remove_short_words(split(/\s+/, $title))); | ||
| 125 | |||
| 126 | # make PDF link names; should be unique within library | ||
| 127 | my %pdflinkby; | ||
| 128 | { | ||
| 129 | |||
| 130 | # format authors, editors, and collaborations | ||
| 131 | my @authors = format_bib_authors("vl", 2, "et al", $bibentry->names("author")); | ||
| 132 | my @editors = format_bib_authors("vl", 2, "et al", $bibentry->names("editor")); | ||
| 133 | my @collaborations = format_bib_authors("vl", 2, "et al", $bibentry->names("collaboration")); | ||
| 134 | |||
| 135 | # first non-empty of authoring collaborations, individual authors, and/or editors | ||
| 136 | my $authorstr = "@collaborations"; | ||
| 137 | $authorstr = "@authors" unless length($authorstr) > 0; | ||
| 138 | $authorstr = "@editors ed" unless length($authorstr) > 0; | ||
| 139 | $authorstr = "!NO-AUTHOR!" unless length($authorstr) > 0; | ||
| 140 | |||
| 141 | # title, plus volume number (if any) for books and proceedings | ||
| 142 | my $titlestr = $title; | ||
| 143 | $titlestr .= " vol" . $bibentry->get("volume") if (grep { $bibentry->type eq $_ } qw(book inbook proceedings)) && $bibentry->exists("volume"); | ||
| 144 | |||
| 145 | # year | ||
| 146 | my $yearstr .= " " . $bibentry->get("year"); | ||
| 147 | |||
| 148 | # link name by author | ||
| 149 | $pdflinkby{author} = "$authorstr $yearstr $titlestr"; | ||
| 150 | |||
| 151 | # link name by title | ||
| 152 | $pdflinkby{title} = "$titlestr $authorstr $yearstr"; | ||
| 153 | |||
| 154 | # link name by year | ||
| 155 | $pdflinkby{year} = "$yearstr $authorstr $titlestr"; | ||
| 156 | |||
| 157 | } | ||
| 158 | |||
| 159 | # list of symbolic links to make | ||
| 160 | my @links; | ||
| 161 | |||
| 162 | # make links by DOI | ||
| 163 | if ($bibentry->exists('doi')) { | ||
| 164 | my @doidirs = grep(/./, File::Spec->splitdir($bibentry->get('doi'))); | ||
| 165 | push @links, ["DOIs", @doidirs]; | ||
| 166 | } | ||
| 167 | |||
| 168 | # make links by all listed collaborations, authors, and editors | ||
| 169 | { | ||
| 170 | |||
| 171 | # format authors, editors, and collaborations | ||
| 172 | my @authors = format_bib_authors("vl", undef, "", $bibentry->names("author")); | ||
| 173 | my @editors = format_bib_authors("vl", undef, "", $bibentry->names("editor")); | ||
| 174 | my @collaborations = format_bib_authors("vl", undef, "", $bibentry->names("collaboration")); | ||
| 175 | |||
| 176 | # make links | ||
| 177 | foreach my $by (qw(year title)) { | ||
| 178 | foreach my $author (@collaborations, @authors) { | ||
| 179 | next if $author eq ""; | ||
| 180 | push @links, ["Authors", $author, "by_${by}s", "$pdflinkby{$by}"]; | ||
| 181 | } | ||
| 182 | foreach my $editor (@editors) { | ||
| 183 | next if $editor eq ""; | ||
| 184 | push @links, ["Authors", "$editor ed", "by_${by}s", "$pdflinkby{$by}"]; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | } | ||
| 189 | |||
| 190 | # make links by first word of title | ||
| 191 | my $firstword = ucfirst($title); | ||
| 192 | $firstword =~ s/\s.*$//; | ||
| 193 | foreach my $by (qw(author year)) { | ||
| 194 | push @links, ["Titles", $firstword, "by_${by}s", "$pdflinkby{$by}"]; | ||
| 195 | } | ||
| 196 | |||
| 197 | # make links by year | ||
| 198 | foreach my $by (qw(author title)) { | ||
| 199 | my $year = $bibentry->get("year") // "!NO-YEAR!"; | ||
| 200 | push @links, ["Years", $year, "by_${by}s", "$pdflinkby{$by}"]; | ||
| 201 | } | ||
| 202 | |||
| 203 | # make links by keyword(s) | ||
| 204 | my %keywords; | ||
| 205 | foreach (split /[,;]/, $bibentry->get("keyword")) { | ||
| 206 | next if /^\s*$/; | ||
| 207 | $keywords{$_} = 1; | ||
| 208 | } | ||
| 209 | if (keys %keywords == 0) { | ||
| 210 | $keywords{"!NO-KEYWORDS!"} = 1; | ||
| 211 | } | ||
| 212 | foreach my $keyword (keys %keywords) { | ||
| 213 | my @subkeywords = split /:|(?: - )/, $keyword; | ||
| 214 | foreach my $subkeyword (@subkeywords) { | ||
| 215 | $subkeyword =~ s/\b(\w)/\U$1\E/g; | ||
| 216 | $subkeyword = "kw_" . $subkeyword; | ||
| 217 | } | ||
| 218 | foreach my $by (qw(author title year)) { | ||
| 219 | push @links, ["Keywords", @subkeywords, "by_${by}s", "$pdflinkby{$by}"]; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | # make links by pre-print server | ||
| 224 | if ($bibentry->exists('archiveprefix')) { | ||
| 225 | my $archiveprefix = $bibentry->get('archiveprefix'); | ||
| 226 | my $eprint = "!NO-EPRINT!"; | ||
| 227 | my $eprintprefix = $eprint; | ||
| 228 | if ($bibentry->exists('eprint')) { | ||
| 229 | $eprint = $bibentry->get('eprint'); | ||
| 230 | $eprintprefix = $eprint; | ||
| 231 | $eprintprefix =~ s/[^\d]//g; | ||
| 232 | $eprintprefix = substr($eprintprefix, 0, 2); | ||
| 233 | } | ||
| 234 | push @links, ["Pre Prints", "$archiveprefix", "$eprintprefix", "$eprint $pdflinkby{author}"]; | ||
| 235 | } | ||
| 236 | |||
| 237 | if (grep { $bibentry->type eq $_ } qw(article)) { | ||
| 238 | |||
| 239 | # make links to articles by journal | ||
| 240 | my $journal = remove_tex_markup_undef($bibentry->get("journal")) // "!NO-JOURNAL!"; | ||
| 241 | my $archiveprefix = $bibentry->get('archiveprefix') // ""; | ||
| 242 | my ($volume, $pages); | ||
| 243 | if ( $journal eq $archiveprefix ) { | ||
| 244 | |||
| 245 | # extract volume/pages from pre-print number | ||
| 246 | my $eprint = $bibentry->get('eprint') // "!NO-EPRINT!"; | ||
| 247 | if ($eprint =~ /^(\d+)[.](.*)$/ || $eprint =~ /^(.{2})(.*)$/) { | ||
| 248 | $volume = $1; | ||
| 249 | $pages = $2; | ||
| 250 | } else { | ||
| 251 | $volume = "!NO-VOLUME!"; | ||
| 252 | $pages = "!NO-PAGES!"; | ||
| 253 | } | ||
| 254 | |||
| 255 | } else { | ||
| 256 | $volume = $bibentry->get("volume") // "!NO-VOLUME!"; | ||
| 257 | $pages = $bibentry->get("pages") // "!NO-PAGES!"; | ||
| 258 | } | ||
| 259 | push @links, ["Journals", "$journal", "v$volume", "p$pages $pdflinkby{author}"]; | ||
| 260 | |||
| 261 | } elsif (grep { $bibentry->type eq $_ } qw(techreport)) { | ||
| 262 | |||
| 263 | # make links to technical reports by institution | ||
| 264 | my $institution = remove_tex_markup_undef($bibentry->get("institution")) // "!NO-INSTITUTION!"; | ||
| 265 | my $number = "!NO-NUMBER!"; | ||
| 266 | my $numberprefix = $number; | ||
| 267 | if ($bibentry->exists('number')) { | ||
| 268 | $number = $bibentry->get('number'); | ||
| 269 | $numberprefix = $number; | ||
| 270 | $numberprefix =~ s/[^\d]//g; | ||
| 271 | $numberprefix = substr($numberprefix, 0, 2); | ||
| 272 | } | ||
| 273 | push @links, ["Tech Reports", "$institution", "$numberprefix", "$number $pdflinkby{author}"]; | ||
| 274 | |||
| 275 | } elsif (grep { $bibentry->type eq $_ } qw(book inbook proceedings)) { | ||
| 276 | |||
| 277 | # make links to books and (whole) proceedings | ||
| 278 | push @links, ["Books", "$pdflinkby{author}"]; | ||
| 279 | |||
| 280 | } elsif (grep { $bibentry->type eq $_ } qw(conference incollection inproceedings)) { | ||
| 281 | |||
| 282 | # make links to articles in collections and proceedings | ||
| 283 | my $booktitle = remove_tex_markup_undef($bibentry->get("booktitle")) // "!NO-BOOKTITLE!"; | ||
| 284 | my $pages = $bibentry->get("pages") // "!NO-PAGES!"; | ||
| 285 | $booktitle = join(' ', map { ucfirst($_) } remove_short_words(split(/\s+/, $booktitle))); | ||
| 286 | push @links, ["In", $booktitle, "p$pages $pdflinkby{author}"]; | ||
| 287 | if ($bibentry->exists("series")) { | ||
| 288 | my $series = remove_tex_markup($bibentry->get("series")); | ||
| 289 | my $volume = $bibentry->get("volume") // "!NO-VOLUME!"; | ||
| 290 | push @links, ["In", "$series", "v$volume", "p$pages $pdflinkby{author}"]; | ||
| 291 | } | ||
| 292 | |||
| 293 | } elsif (grep { $bibentry->type eq $_ } qw(mastersthesis phdthesis)) { | ||
| 294 | |||
| 295 | # make links to theses | ||
| 296 | push @links, ["Theses", "$pdflinkby{author}"]; | ||
| 297 | |||
| 298 | } elsif ($bibentry->exists('howpublished')) { | ||
| 299 | |||
| 300 | # make links by publication method | ||
| 301 | my $howpublished = $bibentry->get("howpublished"); | ||
| 302 | $howpublished = join(' ', map { ucfirst($_) } remove_short_words(split(/\s+/, $howpublished))); | ||
| 303 | foreach my $by (qw(author title year)) { | ||
| 304 | push @links, ["How Published", $howpublished, "by_${by}s", "$pdflinkby{$by}"]; | ||
| 305 | } | ||
| 306 | |||
| 307 | } else { | ||
| 308 | |||
| 309 | # make links to everything else | ||
| 310 | my $type = ucfirst($bibentry->type); | ||
| 311 | foreach my $by (qw(author title year)) { | ||
| 312 | push @links, ["Other", "${type}s", "by_${by}s", "$pdflinkby{$by}"]; | ||
| 313 | } | ||
| 314 | |||
| 315 | } | ||
| 316 | |||
| 317 | # make links to entries with missing information | ||
| 318 | my @errorlinks; | ||
| 319 | foreach (@links) { | ||
| 320 | my @linkpath = @{$_}; | ||
| 321 | foreach (@linkpath) { | ||
| 322 | if (/!(NO-[A-Z]*)!/) { | ||
| 323 | my $errorcode = $1; | ||
| 324 | push @errorlinks, ["Errors", "$errorcode", "$key"]; | ||
| 325 | } | ||
| 326 | } | ||
| 327 | } | ||
| 328 | push @links, @errorlinks; | ||
| 329 | |||
| 330 | # mark old links for removal | ||
| 331 | foreach my $oldlink (keys %{$existinglinks{$pdffile}}) { | ||
| 332 | $existinglinks{$pdffile}->{$oldlink} = 0; | ||
| 333 | } | ||
| 334 | |||
| 335 | # make symbolic links | ||
| 336 | foreach (@links) { | ||
| 337 | |||
| 338 | # normalise elements of symbolic link path | ||
| 339 | my @linkpath = @{$_}; | ||
| 340 | foreach (@linkpath) { | ||
| 341 | die unless defined($_) && $_ ne ""; | ||
| 342 | $_ = unidecode($_); | ||
| 343 | s/[`'"!]//g; | ||
| 344 | s/[^-+.A-Za-z0-9]/_/g; | ||
| 345 | s/[-_][-_]+/_/g; | ||
| 346 | s/^_+//; | ||
| 347 | s/_+$//; | ||
| 348 | } | ||
| 349 | |||
| 350 | # make symbolic link path directories | ||
| 351 | my $linkfilebase = pop @linkpath; | ||
| 352 | my $linkdir = File::Spec->catdir($pdflibrarydir, @linkpath); | ||
| 353 | File::Path::make_path($linkdir); | ||
| 354 | |||
| 355 | # make symbolic link file | ||
| 356 | my $linkfile = File::Spec->catfile($linkdir, "$linkfilebase.pdf"); | ||
| 357 | if (-l $linkfile) { | ||
| 358 | unlink($linkfile) or croak "$Script: could not unlink '$linkfile': $!"; | ||
| 359 | } | ||
| 360 | symlink($pdffile, $linkfile) or croak "$Script: could not link '$linkfile' to '$pdffile': $!"; | ||
| 361 | $existinglinks{$pdffile}->{$linkfile} = 1; | ||
| 362 | |||
| 363 | } | ||
| 364 | |||
| 365 | # print progress | ||
| 366 | ++$count; | ||
| 367 | if ($count % 50 == 0 || $count == @bibentries) { | ||
| 368 | printf STDERR "$Script: making links to %i/%i PDF files in '$pdflibrarydir'\r", $count, scalar(@bibentries); | ||
| 369 | flush STDERR; | ||
| 370 | } | ||
| 371 | } | ||
| 372 | printf STDERR "\n"; | ||
| 373 | |||
| 374 | # remove old links | ||
| 375 | foreach my $pdffile (keys %existinglinks) { | ||
| 376 | foreach my $oldlink (keys %{$existinglinks{$pdffile}}) { | ||
| 377 | if (!$existinglinks{$pdffile}->{$oldlink}) { | ||
| 378 | unlink($oldlink) or croak "$Script: could not unlink '$oldlink': $!"; | ||
| 379 | } | ||
| 380 | } | ||
| 381 | } | ||
| 382 | |||
| 383 | } | ||
| 384 | |||
| 385 | sub cleanup_links { | ||
| 386 | |||
| 387 | # remove broken links and empty directories | ||
| 388 | my $wanted = sub { | ||
| 389 | if (-d $_) { | ||
| 390 | rmdir $_; | ||
| 391 | } | ||
| 392 | if (-l $_ && !stat($_)) { | ||
| 393 | unlink($_) or croak "$Script: could not unlink '$_': $!"; | ||
| 394 | } | ||
| 395 | }; | ||
| 396 | find({wanted => \&$wanted, bydepth => 1, no_chdir => 1}, $pdflibrarydir); | ||
| 397 | printf STDERR "$Script: cleaned up PDF file links in '$pdflibrarydir'\n"; | ||
| 398 | |||
| 399 | } |