Skip to content

Commit

Permalink
Merge pull request #2 from NicolasInlibro/pdf-to-cover-cam18603
Browse files Browse the repository at this point in the history
Pdf to cover cam18603
  • Loading branch information
philblou authored Jun 19, 2023
2 parents 6bfaa7e + ebfaf61 commit cc2b871
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 60 deletions.
217 changes: 158 additions & 59 deletions Koha/Plugin/PDFtoCover.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use C4::Auth;
use C4::Context;
use File::Spec;
use JSON qw( encode_json );
use URI::Escape;

BEGIN {
my $kohaversion = Koha::version;
Expand All @@ -45,13 +46,13 @@ BEGIN {
}

our $dbh = C4::Context->dbh();
our $VERSION = 1.8;
our $VERSION = 1.9;
our $metadata = {
name => 'PDFtoCover',
author => 'Mehdi Hamidi, Bouzid Fergani, Arthur Bousquet, The Minh Luong',
description => 'Creates cover images for documents missing one',
date_authored => '2016-06-08',
date_updated => '2022-11-18',
date_updated => '2023-06-15',
minimum_version => '17.05',
version => $VERSION,
};
Expand All @@ -61,7 +62,7 @@ sub new {
$args->{'metadata'} = $metadata;
$args->{'metadata'}->{'class'} = $class;
my $self = $class->SUPER::new($args);

$self->{cgi} = CGI->new();
return $self;
}

Expand All @@ -73,6 +74,7 @@ sub tool {
my $lock_path = File::Spec->catdir( File::Spec->rootdir(), "tmp", ".Koha.PDFtoCover.lock" );
my $lock = (-e $lock_path) ? 1 : 0;


my $poppler = "/usr/bin/pdftocairo";
unless (-e $poppler){
$self->missingModule();
Expand Down Expand Up @@ -135,82 +137,179 @@ sub getKohaVersion {

sub displayAffected {
my ( $self, $args ) = @_;
my $pdf = 0;
my $kohaversion = getKohaVersion();
my $query = "";
if($kohaversion < 21.0508000){
$query = "SELECT count(*) as count FROM biblio_metadata AS a WHERE EXTRACTVALUE(a.metadata,\"record/datafield[\@tag='856']/subfield[\@code='u']\") <> '' and a.biblionumber not in (select biblionumber from biblioimages);";
}else{
$query = "SELECT count(*) as count FROM biblio_metadata AS a WHERE EXTRACTVALUE(a.metadata,\"record/datafield[\@tag='856']/subfield[\@code='u']\") <> '' and a.biblionumber not in (select biblionumber from cover_images);";
}
my $table = getKohaVersion() < 21.0508000 ? "biblioimages" : "cover_images";
my $query = "SELECT count(*) as count FROM biblio_metadata AS a WHERE EXTRACTVALUE(a.metadata,\"record/datafield[\@tag='856']/subfield[\@code='u']\") <> '' and a.biblionumber not in (select biblionumber from $table);";
my $stmt = $dbh->prepare($query);
$stmt->execute();

if ( my $row = $stmt->fetchrow_hashref() ) {
$pdf = $row->{count};
return $row->{count};
}

return $pdf;
return 0;
}

sub genererVignette {
# methode appelée si on génère les vignettes pour toutes les notices
my ( $self, $args ) = @_;
my $dbh = C4::Context->dbh;
my $ua = LWP::UserAgent->new( timeout => "5" );
my $kohaversion = getKohaVersion();
my $query = "";
if($kohaversion < 21.0508000){
$query = "SELECT a.biblionumber, EXTRACTVALUE(a.metadata,\"record/datafield[\@tag='856']/subfield[\@code='u']\") AS url FROM biblio_metadata AS a WHERE EXTRACTVALUE(a.metadata,\"record/datafield[\@tag='856']/subfield[\@code='u']\") <> '' and a.biblionumber not in (select biblionumber from biblioimages);";
}else{
$query = "SELECT a.biblionumber, EXTRACTVALUE(a.metadata,\"record/datafield[\@tag='856']/subfield[\@code='u']\") AS url FROM biblio_metadata AS a WHERE EXTRACTVALUE(a.metadata,\"record/datafield[\@tag='856']/subfield[\@code='u']\") <> '' and a.biblionumber not in (select biblionumber from cover_images);";
}
my $table = getKohaVersion() < 21.0508000 ? "biblioimages" : "cover_images";
my $query = "SELECT a.biblionumber, EXTRACTVALUE(a.metadata,\"record/datafield[\@tag='856']/subfield[\@code='u']\") AS url FROM biblio_metadata AS a WHERE EXTRACTVALUE(a.metadata,\"record/datafield[\@tag='856']/subfield[\@code='u']\") <> '' and a.biblionumber not in (select biblionumber from $table);";

# Retourne 856$u, qui est le(s) URI(s) d'une ressource numérique
my $sthSelectPdfUri = $dbh->prepare($query);
$sthSelectPdfUri->execute();

while ( my ( $biblionumber, $urifield ) = $sthSelectPdfUri->fetchrow_array() ) {
my @uris = split / /, $urifield;
foreach my $url (@uris) {
my $response = $ua->get($url);
if ( $response->is_success && $response->header('content-type') =~ /application\/pdf/ ) {
my $lastmodified = $response->header('last-modified');

# On vérifie que le fichier à l'URL spécifié est bel et bien un pdf
my @filestodelete = ();
my $save = C4::Context->temporary_directory();
$save =~ s/\/*$/\//;
$save .= $biblionumber;
if ( is_success( getstore( $url, $save ) ) ) {
push @filestodelete, $save;
`pdftocairo "$save" -png "$save" -singlefile 2>&1`; # Conversion de pdf à png, seulement pour la première page
my $imageFile = $save . ".png";
push @filestodelete, $imageFile;

my $srcimage = GD::Image->new($imageFile);
my $replace = 1;
if($kohaversion < 21.0508000){
C4::Images::PutImage( $biblionumber, $srcimage, $replace );
}else{
my $input = CGI->new;
my $itemnumber = $input->param('itemnumber');
Koha::CoverImage->new(
{
biblionumber => $biblionumber,
itemnumber => $itemnumber,
src_image => $srcimage
}
)->store;
}
foreach my $file (@filestodelete) {
unlink $file or warn "Could not unlink $file: $!\nNo more images to import.Exiting.";
}
last;
$self->genererVignetteParUris($biblionumber, @uris);
$self->store_data({ to_process => $self->retrieve_data('to_process') - 1 });
}
return 0;
}

sub genererUneVignette {
# methode appelée si on génère la vignette pour une notice spécifique
my ( $self, $params) = @_;
my $biblionumber = $self->{cgi}->param('biblionumber');
if ($self->{cgi}->param('regenerer')) { # On supprime l'image si elle a été générée par le passé
my $query = "DELETE FROM cover_images WHERE biblionumber = ?";
my $stmt = $dbh->prepare($query);
$stmt->execute($biblionumber)
}
my @uris = $self->getUrisByBiblioNumber($biblionumber);
$self->genererVignetteParUris($biblionumber, @uris);
print $self->{cgi}->redirect(-url => '/cgi-bin/koha/catalogue/detail.pl?biblionumber=' . $biblionumber);
exit 0;
}

sub genererVignetteParUris {
my ( $self, $biblionumber, @uris) = @_;
foreach my $url (@uris) {
if ( $self->isPdfResource($url) ) {
my @filestodelete = ();
my $save = C4::Context->temporary_directory();
$save =~ s/\/*$/\//;
$save .= $biblionumber;
if ( is_success( getstore( $url, $save ) ) ) {
push @filestodelete, $save;
`pdftocairo "$save" -png "$save" -singlefile 2>&1`; # Conversion de pdf à png, seulement pour la première page
my $imageFile = $save . ".png";
push @filestodelete, $imageFile;

my $srcimage = GD::Image->new($imageFile);
my $replace = 1;
if(getKohaVersion() < 21.0508000){
C4::Images::PutImage( $biblionumber, $srcimage, $replace );
}else{
my $input = CGI->new;
my $itemnumber = $input->param('itemnumber');
Koha::CoverImage->new(
{
biblionumber => $biblionumber,
itemnumber => $itemnumber,
src_image => $srcimage
}
)->store;
}
foreach my $file (@filestodelete) {
unlink $file or warn "Could not unlink $file: $!\nNo more images to import.Exiting.";
}
}
last;
}
}
return 0;
}

$self->store_data({ to_process => $self->retrieve_data('to_process') - 1 });
sub getUrisByBiblioNumber {
# recupere toutes les uris correspondantes a une notice
my ( $self, $biblionumber ) = @_;

my $query = "SELECT EXTRACTVALUE(a.metadata,\"record/datafield[\@tag='856']/subfield[\@code='u']\") AS url FROM biblio_metadata AS a WHERE EXTRACTVALUE(a.metadata,\"record/datafield[\@tag='856']/subfield[\@code='u']\") <> '' and a.biblionumber = ? ;";

# Retourne 856$u, qui est le(s) URI(s) d'une ressource numérique
my $stmt = $dbh->prepare($query);
$stmt->execute($biblionumber);
my $urifield = $stmt->fetchrow_array();

my @uris = split / /, $urifield;
return @uris;
}

sub isPdfResource {
# vérifie si la ressource est un pdf
my ( $self, $url ) = @_;
my $ua = LWP::UserAgent->new( timeout => "5" );
my $response = $ua->get($url);
if ( $response->is_success ) {
if ($response->header('content-type') =~ /application\/pdf/) {
return 1;
} elsif ($response->header('content-disposition') && substr($response->header('content-disposition'), -5, 4) eq ".pdf") {
return 1;
}
}
return 0;
}

sub hasPdfResource {
# verifie si la notice a une ressource pdf
my ( $self, $biblionumber ) = @_;
my @uris = $self->getUrisByBiblioNumber($biblionumber);
foreach my $url (@uris) {
return $self->isPdfResource($url);
}
return 0;
}

sub hasAlreadyLocalImage {
my ( $self, $biblionumber ) = @_;
my $table = getKohaVersion() < 21.0508000 ? "biblioimages" : "cover_images";
my $query = "select count(*) as count from $table where biblionumber = ? ;";

my $stmt = $dbh->prepare($query);
$stmt->execute($biblionumber);

my $row = $stmt->fetchrow_hashref();
return $row->{count} > 0;
}

sub intranet_catalog_biblio_enhancements_toolbar_button { # hook koha
# Ajoute un bouton a la barre d'outils de la page detail.pl pour generer la vignette
my ( $self, $params ) = @_;
my $cgi = $self->{cgi};
my $biblionumber = $cgi->param('biblionumber');

# On affiche un bouton que s'il y a une ressource pdf
if ($self->hasPdfResource($biblionumber)) {
my $lang = $cgi->cookie('KohaOpacLanguage');
my $hasLocalImage = $self->hasAlreadyLocalImage($biblionumber);
my $stmt = $dbh->prepare("select value from systempreferences where variable='LocalCoverImages'");
$stmt->execute();

my $button = "<div class='btn-group'>";
my $textbutton = $lang eq "fr-CA" || $lang eq "fr" ? "G&eacute;n&eacute;rer l'image de couverture" : "Generate cover image";
if ($hasLocalImage) {
$textbutton = "Reg" . substr($textbutton, 1);
}


if ($stmt->fetchrow_array()) {
my $link = "/cgi-bin/koha/plugins/run.pl?class=" . uri_escape("Koha::Plugin::" . $metadata->{name}) .
"&method=genererUneVignette&biblionumber=" . $biblionumber . ($hasLocalImage ? "&regenerer=1" : "");

my $class = "<i class='fa fa-" . ($hasLocalImage ? "refresh" : "picture-o") . "'></i>&nbsp;";

$button .= "<a id='cover-$biblionumber' class='btn btn-default' href='$link'>$class $textbutton</a>";
} else {
my $title = "You must activate the LocalCoverImages system preference to generate the cover image";
if ($lang eq "fr-CA" || $lang eq "fr") {
$title = "Vous devez activer la pr&eacute;f&eacute;rence syst&egrave;me LocalCoverImages pour g&eacute;n&eacute;rer l'image de couverture";
}

$button .= "<button type='button' class='btn btn-default' title=\"$title\" disabled><i class='fa fa-exclamation-triangle'></i>&nbsp;$textbutton</button>";
}
return $button . "</div>";
}
return "";
}

sub progress {
Expand Down
6 changes: 5 additions & 1 deletion Koha/Plugin/PDFtoCover/step_1.tt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<label class="filterDescription"><p>Number of records to process: <span id="to_process">[% pdf %]</span></p></label>
<hr>

<form onbsubmit="launchGenerate()" method="get" id="formTraitement">
<form onsubmit="launchGenerate()" method="get" id="formTraitement">
<!-- Necessary for the plugin to run, do not remove -->
<input type="hidden" name="class" value="[% CLASS %]"/>
<input type="hidden" name="method" value="[% METHOD %]"/>
Expand All @@ -42,6 +42,10 @@
[% INCLUDE 'intranet-bottom.inc' %]

<style>
#doc3 {
margin-left: 20px;
}

#submit{
font-size: 100%;
font-weight: bold;
Expand Down
4 changes: 4 additions & 0 deletions Koha/Plugin/PDFtoCover/step_1_fr-CA.tt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
[% INCLUDE 'intranet-bottom.inc' %]

<style>
#doc3 {
margin-left: 20px;
}

#submit{
font-size: 100%;
font-weight: bold;
Expand Down
4 changes: 4 additions & 0 deletions Koha/Plugin/PDFtoCover/step_1_fr.tt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
[% INCLUDE 'intranet-bottom.inc' %]

<style>
#doc3 {
margin-left: 20px;
}

#submit{
font-size: 100%;
font-weight: bold;
Expand Down

0 comments on commit cc2b871

Please sign in to comment.