From 310dd009028b32269916087f1ae53a0b001c1474 Mon Sep 17 00:00:00 2001 From: Marion Durand Date: Thu, 2 Dec 2021 10:05:05 +0100 Subject: [PATCH] Bug 18618: Add a better support for different id types Add a better support for ISMN, ISRN, ISRC and UPC Add normalization function for some of these ids. When sending pairs to mana and when getting suggestions these ids are now taken into account correctly. Documents with multiple identifier were causing various trouble. It was needed to add functions to normalize all ids This patch add the possibility to: - create pairs with documents with multiple ids (all the possible pairs will be created) - get suggestion for a document with multiple ids - display correctly the document when the suggested document has multiple ids --- Koha/Util/Normalize.pm | 379 ++++++++++++++++++++++++++++++- misc/cronjobs/mana_send_pairs.pl | 34 ++- opac/svc/mana/getSuggestion | 131 ++++++++--- 3 files changed, 494 insertions(+), 50 deletions(-) diff --git a/Koha/Util/Normalize.pm b/Koha/Util/Normalize.pm index f05999de07..f615520095 100644 --- a/Koha/Util/Normalize.pm +++ b/Koha/Util/Normalize.pm @@ -18,6 +18,7 @@ package Koha::Util::Normalize; # along with Koha; if not, see . use Modern::Perl; +use Scalar::Util; use parent qw( Exporter ); @@ -27,6 +28,14 @@ our @EXPORT = qw( upper_case lower_case ISBN + NormalizeISMN + NormalizeISRC + NormalizeISRN + NormalizeISBNs + NormalizeISSNs + NormalizeISMNs + NormalizeISRNs + NormalizeISRCs ); =head1 NAME @@ -121,11 +130,379 @@ sub ISBN { return $isbn; } +=head2 NormalizeISMN + +Normalization function converting ISMN strings to ISMN13 +my $ismn = NormalizeISMN({ + ismn => $ismn, + strip_hyphens => [0, 1], + format => ['ISMN-10', 'ISMN-13'] +}); + +=cut +sub NormalizeISMN{ + my ( $params ) = @_; + + my $string = $params->{ismn}; + my $strip_hyphens = $params->{strip_hyphens}; + my $format = $params->{format} || q{}; + my $return_invalid = $params->{return_invalid}; + + return unless $string; + + my $ismn; + #test if the string seems valid + if (is_ismn($string)){ + $ismn = $string; + } + #if the string seems valid + if ($ismn){ + if ( $format eq 'ISMN-10' ) { + #grab the first digit, if it's an "9" then the format is ISMN-13 + my $first = substr $ismn, 0, 1; + #if the ismn is in ISMN-13 format + if ($first eq "9"){ + #replace the prefix of ISMN-10 by the prefix of ISMN-13 + length($ismn)==17 ? $ismn =~ s/979-0-/M-/ : $ismn =~ s/9790/M/; + } + } + elsif ($format eq 'ISMN-13' ) { + #grab the first digit, if it's an "M" then the format is ISMN-10 + my $first = substr $ismn, 0, 1; + #if the ismn is in ISMN-10 format + if ($first eq "M"){ + #replace the prefix of ISMN-10 by the prefix of ISMN-13 + length($ismn)==13 ? $ismn =~ s/M-/979-0-/ : $ismn =~ s/M/9790/; + } + } + if ($strip_hyphens) { + $ismn =~ s/-//g; + } + return $ismn; + } + elsif($return_invalid){ + return $string; + } + else { + return; + } +} + +=head2 is_ismn + +Check if the string given looks like an ismn + +=cut + +sub is_ismn{ + my ( $string ) = @_; + + #check if length match the expected length of an ismn (with and withou hyphens) + return 0 unless ( (length($string)==10) or (length($string)==13) or (length($string)==14) or length($string)==17 ); + #check if the pattern look like an ismn + return 0 unless ( $string =~ m/^(((979-0-)|(M-))[0-9]{3,7}-[0-9]{1,5}-[0-9])|((M|(9790))[0-9]{9})$/ ) ; + + return 1 +} + +=head2 NormalizeISRC + +Normalization function removing hyphens +my $isrc = NormalizeISRC({ + isrc => $isrc, + strip_hyphens => [0, 1] +}); + +=cut +sub NormalizeISRC{ + my ( $params ) = @_; + + my $string = $params->{isrc}; + my $strip_hyphens = $params->{strip_hyphens}; + my $return_invalid = $params->{return_invalid}; + + return unless $string; + + my $isrc; + #test if the string seems valid + if (is_isrc($string)){ + $isrc = $string; + } + #if the string seems valid + if ($isrc){ + if ($strip_hyphens) { + $isrc =~ s/-//g; + } + return $isrc; + } + elsif($return_invalid){ + return $string; + } + else { + return; + } +} + +=head2 is_isrc + +Check if the string given looks like an isrc + +=cut + +sub is_isrc{ + my ( $string ) = @_; + + #check if length match the expected length of an isrc (with and withou hyphens) + return 0 unless ( (length($string)==15) or (length($string)==12) ); + #check if the pattern look like an isrc + return 0 unless ( $string =~ m/^([A-Z]{2}-\w{3}-\w{2}-\w{5})|(([A-Z]{2}\w{10}))$/ ) ; + + return 1 +} + +=head2 NormalizeISRN + +Normalization function removing hyphens +my $isrn = NormalizeISRN({ + isrn => $isrn, + convert_slash, => [0, 1], + drop_local_suffix => [0, 1] +}); + +=cut +sub NormalizeISRN{ + my ( $params ) = @_; + + my $string = $params->{isrn}; + my $convert_slash = $params->{convert_slash}; + my $drop_local_suffix = $params->{drop_local_suffix}; + my $return_invalid = $params->{return_invalid}; + + return unless $string; + + my $isrn; + #test if the string seems valid + if (is_isrn($string)){ + $isrn = $string; + } + #if the string seems valid + if ($isrn){ + if($convert_slash){ + $isrn =~ s/\//_/g; + } + if($drop_local_suffix){ + $isrn =~ s/\+.*$//; + } + return $isrn; + } + elsif($return_invalid){ + return $string; + } + else { + return; + } +} + +=head2 is_isrn + +Check if the string given looks like an isrn + +=cut + +sub is_isrn{ + my ( $string ) = @_; + + my $truncated_string = $string; + $truncated_string =~ s/\+.*$//; + #check if length match the expected length of an isrn + return 0 unless ( length($truncated_string)<=36 ); + #check if the pattern look like an isrn + return 0 unless ( $string =~ m/^[A-Z](\w[\/-]?)*--(\d\d[\/-])?\d+([\/-]\w+)?(--[A-Z][A-Z])?(\+[A-Z0-9a-z,\.\/]+)?$/ ); + #check if each part has a valid length + $string =~ m/^([\w\/-]+)--([\d\/-]+[\w\/-]*)(--[A-Z][A-Z])?(\+[A-Za-z0-9,\.\/]+)?$/; + return 0 unless ( length($1)<=16 ); + return 0 unless ( length($2)<=14 ); + + return 1 +} + 1; + + +=head2 NormalizeISBNs + +Normalization function normalizing a string with a list of ISBNs seperated by a given pattern +my $isbns = NormalizeISBNs({ + pattern => $pattern, + isbns => $isbns, + strip_hyphens => [0, 1] + format => ['ISBN-10', 'ISBN-13'] +}); + +=cut + +sub NormalizeISBNs{ + my ( $params ) = @_; + + my $string = $params->{isbns}; + my $pattern = $params->{pattern}; + my $strip_hyphens = $params->{strip_hyphens}; + my $format = $params->{format} || q{}; + my $return_invalid = $params->{return_invalid}; + + if ($string){ + my @isbn_list = split(/$pattern/, $string); + my $result = ""; + foreach my $isbn (@isbn_list){ + my $normalized_isbn = C4::Koha::NormalizeISBN({ isbn => $isbn, format => $format, strip_hyphens => $strip_hyphens, return_invalid => $return_invalid }); + $result .= $normalized_isbn." " if ($normalized_isbn); + } + $result =~ s/ $//; + return $result; + } else { + return; + } +} + +=head2 NormalizeISSNs + +Normalization function normalizing a string with a list of ISSNs seperated by a given pattern +my $issns = NormalizeISSNs({ + issns => $issns, + pattern => $pattern, + strip_hyphens => [0, 1] +}); + +=cut + +sub NormalizeISSNs{ + my ( $params ) = @_; + + my $string = $params->{issns}; + my $pattern = $params->{pattern}; + my $strip_hyphens = $params->{strip_hyphens}; + + if ($string){ + my @issn_list = split(/$pattern/, $string); + my $result = ""; + foreach my $issn (@issn_list){ + my $normalized_issn = C4::Koha::NormalizeISSN({ issn => $issn, strip_hyphen => $strip_hyphens }); + $result .= $normalized_issn." " if ($normalized_issn); + } + $result =~ s/ $//; + return $result; + } else { + return; + } +} + +=head2 NormalizeISMNs + +Normalization function normalizing a string with a list of ISMNs seperated by a given pattern +my $ismns = NormalizeISMNs({ + ismns => $ismns, + pattern => $pattern, + strip_hyphens => [0, 1] + format => ['ISMN-10', 'ISMN-13'] +}); + +=cut + +sub NormalizeISMNs{ + my ( $params ) = @_; + + my $string = $params->{ismns}; + my $pattern = $params->{pattern}; + my $strip_hyphens = $params->{strip_hyphens}; + my $format = $params->{format} || q{}; + my $return_invalid = $params->{return_invalid}; + + if ($string){ + my @ismn_list = split(/$pattern/, $string); + my $result = ""; + foreach my $ismn (@ismn_list){ + my $normalized_ismn = Koha::Util::Normalize::NormalizeISMN({ ismn => $ismn, format => $format, strip_hyphens => $strip_hyphens, return_invalid => $return_invalid }); + $result .= $normalized_ismn." " if ($normalized_ismn); + } + $result =~ s/ $//; + return $result; + } else { + return; + } +} + +=head2 NormalizeISRCs + +Normalization function normalizing a string with a list of ISRCs seperated by a given pattern +my $isrcs = NormalizeISRCs({ + isrcs => $isrcs, + pattern => $pattern, + strip_hyphens => [0, 1] +}); + +=cut + +sub NormalizeISRCs{ + my ( $params ) = @_; + + my $string = $params->{isrcs}; + my $pattern = $params->{pattern}; + my $strip_hyphens = $params->{strip_hyphens}; + my $return_invalid = $params->{return_invalid}; + + if ($string){ + my @isrc_list = split(/$pattern/, $string); + my $result = ""; + foreach my $isrc (@isrc_list){ + my $normalized_isrc = Koha::Util::Normalize::NormalizeISRC({ isrc => $isrc, strip_hyphens => $strip_hyphens, return_invalid => $return_invalid }); + $result .= $normalized_isrc." " if ($normalized_isrc); + } + $result =~ s/ $//; + return $result; + } else { + return; + } +} + +=head2 NormalizeISRNs + +Normalization function normalizing a string with a list of ISRNs seperated by a given pattern +my $isrns = NormalizeISRNs({ + isrns => $isrns, + pattern => $pattern, + convert_slash = [0, 1], + drop_local_suffix = [0, 1] +}); + +=cut + +sub NormalizeISRNs{ + my ( $params ) = @_; + + my $string = $params->{isrns}; + my $pattern = $params->{pattern}; + my $drop_local_suffix = $params->{drop_local_suffix}; + my $convert_slash = $params->{convert_slash}; + my $return_invalid = $params->{return_invalid}; + + if ($string){ + my @isrn_list = split(/$pattern/, $string); + my $result = ""; + foreach my $isrn (@isrn_list){ + my $normalized_isrn = Koha::Util::Normalize::NormalizeISRN({ isrn => $isrn, drop_local_suffix => $drop_local_suffix, convert_slash => $convert_slash, return_invalid => $return_invalid }); + $result .= $normalized_isrn." " if ($normalized_isrn); + } + $result =~ s/ $//; + return $result; + } else { + return; + } +} + + __END__ =head1 AUTHOR - Koha Development Team Tomas Cohen Arazi diff --git a/misc/cronjobs/mana_send_pairs.pl b/misc/cronjobs/mana_send_pairs.pl index e821e00c37..59845491ee 100644 --- a/misc/cronjobs/mana_send_pairs.pl +++ b/misc/cronjobs/mana_send_pairs.pl @@ -6,6 +6,7 @@ use Getopt::Long; use Koha; use C4::Koha; use Koha; +use Koha::Util::Normalize q/NormalizeISMN/; use JSON; use HTTP::Request; @@ -35,9 +36,6 @@ if (C4::Context->preference("Mana") == 1){ my $dbh = C4::Context->dbh; my $query_part1 = " SELECT - issues1.borrowernumber as borrowernumber, - biblio_metadata1.biblionumber as biblionumber1, - biblio_metadata2.biblionumber as biblionumber2, ExtractValue(biblio_metadata1.metadata, '//datafield[\@tag=\"010\"]/subfield[\@code=\"a\"]') as isbn1, ExtractValue(biblio_metadata2.metadata, '//datafield[\@tag=\"010\"]/subfield[\@code=\"a\"]') as isbn2, ExtractValue(biblio_metadata1.metadata, '//datafield[\@tag=\"011\"]/subfield[\@code=\"a\"]') as issn1, @@ -89,23 +87,35 @@ if (C4::Context->preference("Mana") == 1){ #for each reading pair, processes the id pairs while ( $row = $sth->fetchrow_hashref ){ #if necessary, normalize the ids - $row->{isbn1} = C4::Koha::NormalizeISBN({ isbns => $row->{isbn1}, format => 'ISBN-13', strip_hyphens => 1 }) if $row->{isbn1}; - $row->{isbn2} = C4::Koha::NormalizeISBN({ isbns => $row->{isbn2}, format => 'ISBN-13', strip_hyphens => 1 }) if $row->{isbn2}; + $row->{isbn1} = Koha::Util::Normalize::NormalizeISBNs({ isbns => $row->{isbn1}, pattern => ' ', format => 'ISBN-13', strip_hyphens => 1 }) if $row->{isbn1}; + $row->{isbn2} = Koha::Util::Normalize::NormalizeISBNs({ isbns => $row->{isbn2}, pattern => ' ', format => 'ISBN-13', strip_hyphens => 1 }) if $row->{isbn2}; + $row->{ismn1} = Koha::Util::Normalize::NormalizeISMNs({ ismn => $row->{ismn1}, pattern => ' ', format => 'ISMN-13', strip_hyphens => 1 }) if $row->{ismn1}; + $row->{ismn2} = Koha::Util::Normalize::NormalizeISMNs({ ismn => $row->{ismn2}, pattern => ' ', format => 'ISMN-13', strip_hyphens => 1 }) if $row->{ismn2}; + $row->{isrc1} = Koha::Util::Normalize::NormalizeISRCs({ isrc => $row->{isrc1}, pattern => ' ', strip_hyphens => 1 }) if $row->{isrc1}; + $row->{isrc2} = Koha::Util::Normalize::NormalizeISRCs({ isrc => $row->{isrc2}, pattern => ' ', strip_hyphens => 1 }) if $row->{isrc2}; + $row->{isrn1} = Koha::Util::Normalize::NormalizeISRNs({ isrn => $row->{isrn1}, pattern => ' ', drop_local_suffix => 1 }) if $row->{isrn1}; + $row->{isrn2} = Koha::Util::Normalize::NormalizeISRNs({ isrn => $row->{isrn2}, pattern => ' ', drop_local_suffix => 1 }) if $row->{isrn2}; # try to make pairs between every id possible foreach my $key1 ( qw ( isbn issn ismn isrn ean upc isrc ) ){ if ( $row->{ $key1."1" }){ + my @id1_list = split(/ /, $row->{ $key1."1" }); foreach my $key2 ( qw ( isbn issn ismn isrn ean upc isrc ) ){ # if both selected ids exists if ( $row->{ $key2."2" } ){ + my @id2_list = split(/ /, $row->{ $key1."2" }); # make pairs between every ids of the given type - my $currentpair; - $currentpair->{ idtype1 } = $key1; - $currentpair->{ documentid1 } = $row->{$key1."1"}; - $currentpair->{ idtype2 } = $key2; - $currentpair->{ documentid2 } = $row->{$key2."2"}; - push @reading_pairs, $currentpair; - } + foreach my $id1 (@id1_list){ + foreach my $id2 (@id2_list){ + my $currentpair; + $currentpair->{ idtype1 } = $key1; + $currentpair->{ documentid1 } = $id1; + $currentpair->{ idtype2 } = $key2; + $currentpair->{ documentid2 } = $id2; + push @reading_pairs, $durrentpair; + } + } + } } } } diff --git a/opac/svc/mana/getSuggestion b/opac/svc/mana/getSuggestion index d11bc5bd18..b6cea9cd4c 100755 --- a/opac/svc/mana/getSuggestion +++ b/opac/svc/mana/getSuggestion @@ -24,12 +24,15 @@ use Koha::Biblioitems; use Koha::Biblioitem; use Koha::Reading_suggestions; use Koha::Reading_suggestion; +use Koha::Util::Normalize q/NormalizeISMN/; use Koha; use C4::Koha; - +use C4::Context; use C4::Auth qw(check_cookie_auth), qw(get_template_and_user); +use List::MoreUtils qw(uniq); + use CGI; use JSON; @@ -70,6 +73,7 @@ eval { }; my @biblios; my @biblios_blessed; +my $bibliosnumbers; if ( $local_suggestions and DateTime::Duration->compare( $duration, DURATION_BEFORE_REFRESH ) == -1 ){ delete $local_suggestions->{timestamp}; delete $local_suggestions->{biblionumber}; @@ -83,22 +87,24 @@ else{ # get all informations to ask mana my $idtype; my $documentid; + my $documentids; my $notnormalizeddocumentid; my $found = 0; if ( $biblioitem && $biblioitem->isbn ){ - $idtype= "isbn"; - $documentid= C4::Koha::NormalizeISBN({ isbn => $biblioitem->isbn, format => 'ISBN-13', strip_hyphens => 1 }); + $idtype = "isbn"; + $documentids = Koha::Util::Normalize::NormalizeISBNs({ isbns => $biblioitem->isbn, pattern => ' \| ', format => 'ISBN-13', strip_hyphens => 1 }); if ($documentids) { $found = 1; }; } elsif ( $biblioitem && $biblioitem->issn ){ - $idtype= "issn"; - $documentid= $biblioitem->issn; + $idtype = "issn"; + $documentids = Koha::Util::Normalize::NormalizeISSNs({ issns => $biblioitem->issn, pattern => ' \| ', strip_hyphens => 0 }); if ($documentids) { $found = 1; }; } elsif ( $biblioitem && $biblioitem->ean ){ - $idtype= "ean"; - $documentid= $biblioitem->ean; + $idtype = "ean"; + $documentid = $biblioitem->ean; + $documentids =~ s/ \| / /; if ($documentids) { $found = 1; }; } if ($found == 0){ @@ -122,36 +128,38 @@ else{ if ($row->{isbn}){ $idtype = "isbn"; - $documentid = C4::Koha::NormalizeISBN({ isbn => $row->{isbn}, format => 'ISBN-13', strip_hyphens => 1 }); + $documentid = Koha::Util::Normalize::NormalizeISBNs({ isbn => $row->{isbn}, pattern => ' ', format => 'ISBN-13', strip_hyphens => 1 }); } elsif ($row->{issn}){ $idtype = "issn"; - $documentid = $row->{issn}; + $documentid = Koha::Util::Normalize::NormalizeISSNs({ ismn => $row->{issn}, pattern => ' ', strip_hyphens => 1 }); } elsif ($row->{ismn}){ $idtype = "ismn"; - $documentid = $row->{ismn}; + $documentids = Koha::Util::Normalize::NormalizeISMNs({ ismns => $row->{ismn}, pattern => ' ', format => 'ISMN-13', strip_hyphens => 1 }); } elsif ($row->{isrn}){ $idtype = "isrn"; - $documentid = $row->{isrn}; + $documentids = Koha::Util::Normalize::NormalizeISRNs({isrns => $row->{isrn}, pattern => ' ', convert_slash => 1, drop_local_suffix => 1 }); } elsif ($row->{isrc}){ $idtype = "isrc"; - $documentid = $row->{isrc}; + $documentids = Koha::Util::Normalize::NormalizeISRCs({ isrcs => $row->{isrc}, pattern => ' ', strip_hyphens => 1 }) } elsif ($row->{upc}){ $idtype = "upc"; - $documentid = $row->{upc}; + $documentids = $row->{upc}; } elsif ($row->{ean}){ $idtype = "ean"; - $documentid = $row->{ean}; + $documentids = $row->{ean}; } else{ die "error: no propper identifier"; } } + + $documentid=(split(/ /, $documentids, 2))[0]; $notnormalizeddocumentid = $documentid; my $offset = 1; my $length = 10; @@ -190,36 +198,85 @@ else{ #create the list of owned suggested resources my $ownedbiblioitem; foreach my $resource ( @{ $resources } ){ - #  isbn processsing - if ( $resource->{idtype} eq "isbn" ){ - $documentid= C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 1 }); - $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid; - - #if we don't have such a biblioitem, we try to format else the isbn - unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ - $documentid = C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 1 }); - $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid; - } + my $found = 0; - unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ - $documentid = C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 0 }); + #isbn, issn and ean can be search for with Koha::Biblioitems->search + if ($resource->{idtype} eq "isbn" || $resource->{idtype} eq "issn" || $resource->{idtype} eq "ean"){ + #isbn processsing + if ( $resource->{idtype} eq "isbn" ){ + $documentid= C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 1 }); $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid; + + #if we don't have such a biblioitem, we try to format else the isbn + unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ + $documentid = C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 1 }); + $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid; + } + + unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ + $documentid = C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 0 }); + $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid; + } + + unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ + $documentid = C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 0 }); + $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid; + } } - unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ - $documentid = C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 0 }); - $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid; + #issn and ean don't need special processing + elsif ($resource->{idtype} eq "issn" or $resource->{idtype} eq "ean"){ + $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $resource->{documentid} }); } + #construct the tables with biblionumber + if (scalar @{ $ownedbiblioitem->unblessed() } ){ + $found = 1; + my $ownedbiblio = Koha::Biblios->find( @{ $ownedbiblioitem->unblessed() }[0]->{biblionumber} ); + #add the biblio if not already present + if ( not(exists($bibliosnumbers->{@{ $ownedbiblioitem->unblessed() }[0]->{biblionumber}})) ){ + push @biblios_blessed, $ownedbiblio; + push @biblios, $ownedbiblio->unblessed(); + $bibliosnumbers->{@{ $ownedbiblioitem->unblessed() }[0]->{biblionumber}}=1; + } + } } - #others ids do not need any processing - else{ - $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $resource->{documentid} }); - } - if (scalar @{ $ownedbiblioitem->unblessed() } ){ - my $ownedbiblio = Koha::Biblios->find( @{ $ownedbiblioitem->unblessed() }[0]->{biblionumber} ); - push @biblios_blessed, $ownedbiblio; - push @biblios, $ownedbiblio->unblessed(); + # if we don't have such a biblioitem, we try to look directly in metadata + # because if the document has multiple isbn they are store in biblioitem table like this + # "isbn1 | isbn2" and can't be found by biblioitems->seach + # other id need to be search with sql + if ($found != 1) { + my @params; + my %field=("isbn" => "010", "issn" => "011", "ismn" => "013", "isrn" => "015", "isrc" => "016", "upc" => "072", "ean" => "073"); + + #pattern to be tolerent on the hyphens + my $pattern=""; + foreach my $p (split('', $resource->{documentid})){ + $pattern .= "$p-*"; + } + + my $dbh = C4::Context->dbh; + my $query = q{ + SELECT biblioitems.biblionumber as biblionumber + FROM biblioitems + LEFT JOIN biblio_metadata ON biblioitems.biblionumber=biblio_metadata.biblionumber + WHERE ExtractValue(biblio_metadata.metadata, '//datafield[@tag="}.$field{$resource->{idtype}}.q{"]/subfield[@code="a"]') REGEXP "}.$pattern.q{" + AND biblio_metadata.format="marcxml" + }; + my $sth = $dbh->prepare( $query ); + $ownedbiblioitem = $sth->execute; + + #construct the tables with biblionumber + my $row = $sth->fetchrow_hashref; + if ( $row ){ + my $ownedbiblio = Koha::Biblios->find( $row->{biblionumber} ); + #add the biblio if not already present + if ( not(exists($bibliosnumbers->{$row->{biblionumber}})) ){ + push @biblios_blessed, $ownedbiblio; + push @biblios, $ownedbiblio->unblessed(); + $bibliosnumbers->{$row->{biblionumber}}=1; + } + } } } #the number of requested resource is inversely proportionnal to the found_items/returned_items ratio, +1 is here just to avoid 0 -- 2.17.1