View | Details | Raw Unified | Return to bug 18618
Collapse All | Expand All

(-)a/Koha/Util/Normalize.pm (-1 / +378 lines)
Lines 18-23 package Koha::Util::Normalize; Link Here
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use Scalar::Util;
21
22
22
use parent qw( Exporter );
23
use parent qw( Exporter );
23
24
Lines 27-32 our @EXPORT = qw( Link Here
27
  upper_case
28
  upper_case
28
  lower_case
29
  lower_case
29
  ISBN
30
  ISBN
31
  NormalizeISMN
32
  NormalizeISRC
33
  NormalizeISRN
34
  NormalizeISBNs
35
  NormalizeISSNs
36
  NormalizeISMNs
37
  NormalizeISRNs
38
  NormalizeISRCs
30
);
39
);
31
40
32
=head1 NAME
41
=head1 NAME
Lines 121-131 sub ISBN { Link Here
121
    return $isbn;
130
    return $isbn;
122
}
131
}
123
132
133
=head2 NormalizeISMN
134
135
Normalization function converting ISMN strings to ISMN13
136
my $ismn = NormalizeISMN({
137
  ismn => $ismn,
138
  strip_hyphens => [0, 1],
139
  format => ['ISMN-10', 'ISMN-13']
140
});
141
142
=cut
143
sub NormalizeISMN{
144
    my ( $params ) = @_;
145
146
    my $string = $params->{ismn};
147
    my $strip_hyphens = $params->{strip_hyphens};
148
    my $format = $params->{format} || q{};
149
    my $return_invalid = $params->{return_invalid};
150
151
    return unless $string;
152
153
    my $ismn;
154
    #test if the string seems valid
155
    if (is_ismn($string)){
156
        $ismn = $string;
157
    }
158
    #if the string seems valid
159
    if ($ismn){
160
        if ( $format eq 'ISMN-10' ) {
161
            #grab the first digit, if it's an "9" then the format is ISMN-13
162
            my $first = substr $ismn, 0, 1;
163
            #if the ismn is in ISMN-13 format
164
            if ($first eq "9"){
165
                #replace the prefix of ISMN-10 by the prefix of ISMN-13
166
                length($ismn)==17 ? $ismn =~ s/979-0-/M-/ : $ismn =~ s/9790/M/;
167
            }
168
        }
169
        elsif ($format eq 'ISMN-13' ) {
170
            #grab the first digit, if it's an "M" then the format is ISMN-10
171
            my $first = substr $ismn, 0, 1;
172
            #if the ismn is in ISMN-10 format
173
            if ($first eq "M"){
174
                #replace the prefix of ISMN-10 by the prefix of ISMN-13
175
                length($ismn)==13 ? $ismn =~ s/M-/979-0-/ : $ismn =~ s/M/9790/;
176
            }
177
        }
178
        if ($strip_hyphens) {
179
            $ismn =~ s/-//g;
180
        }
181
        return $ismn;
182
    }
183
    elsif($return_invalid){
184
        return $string;
185
    }
186
    else {
187
        return;
188
    }
189
}
190
191
=head2 is_ismn
192
193
Check if the string given looks like an ismn
194
195
=cut
196
197
sub is_ismn{
198
    my ( $string ) = @_;
199
200
    #check if length match the expected length of an ismn (with and withou hyphens)
201
    return 0 unless ( (length($string)==10) or (length($string)==13) or (length($string)==14) or length($string)==17 );
202
    #check if the pattern look like an ismn
203
    return 0 unless ( $string =~ m/^(((979-0-)|(M-))[0-9]{3,7}-[0-9]{1,5}-[0-9])|((M|(9790))[0-9]{9})$/ ) ;
204
205
    return 1
206
}
207
208
=head2 NormalizeISRC
209
210
Normalization function removing hyphens
211
my $isrc = NormalizeISRC({
212
  isrc => $isrc,
213
  strip_hyphens => [0, 1]
214
});
215
216
=cut
217
sub NormalizeISRC{
218
    my ( $params ) = @_;
219
220
    my $string = $params->{isrc};
221
    my $strip_hyphens = $params->{strip_hyphens};
222
    my $return_invalid = $params->{return_invalid};
223
224
    return unless $string;
225
226
    my $isrc;
227
    #test if the string seems valid
228
    if (is_isrc($string)){
229
        $isrc = $string;
230
    }
231
    #if the string seems valid
232
    if ($isrc){
233
        if ($strip_hyphens) {
234
            $isrc =~ s/-//g;
235
        }
236
        return $isrc;
237
    }
238
    elsif($return_invalid){
239
        return $string;
240
    }
241
    else {
242
        return;
243
    }
244
}
245
246
=head2 is_isrc
247
248
Check if the string given looks like an isrc
249
250
=cut
251
252
sub is_isrc{
253
    my ( $string ) = @_;
254
255
    #check if length match the expected length of an isrc (with and withou hyphens)
256
    return 0 unless ( (length($string)==15) or (length($string)==12) );
257
    #check if the pattern look like an isrc
258
    return 0 unless ( $string =~ m/^([A-Z]{2}-\w{3}-\w{2}-\w{5})|(([A-Z]{2}\w{10}))$/ ) ;
259
260
    return 1
261
}
262
263
=head2 NormalizeISRN
264
265
Normalization function removing hyphens
266
my $isrn = NormalizeISRN({
267
  isrn => $isrn,
268
  convert_slash, => [0, 1],
269
  drop_local_suffix => [0, 1]
270
});
271
272
=cut
273
sub NormalizeISRN{
274
    my ( $params ) = @_;
275
276
    my $string = $params->{isrn};
277
    my $convert_slash = $params->{convert_slash};
278
    my $drop_local_suffix = $params->{drop_local_suffix};
279
    my $return_invalid = $params->{return_invalid};
280
281
    return unless $string;
282
283
    my $isrn;
284
    #test if the string seems valid
285
    if (is_isrn($string)){
286
        $isrn = $string;
287
    }
288
    #if the string seems valid
289
    if ($isrn){
290
        if($convert_slash){
291
            $isrn =~ s/\//_/g;
292
        }
293
        if($drop_local_suffix){
294
            $isrn =~ s/\+.*$//;
295
        }
296
        return $isrn;
297
    }
298
    elsif($return_invalid){
299
        return $string;
300
    }
301
    else {
302
        return;
303
    }
304
}
305
306
=head2 is_isrn
307
308
Check if the string given looks like an isrn
309
310
=cut
311
312
sub is_isrn{
313
    my ( $string ) = @_;
314
315
    my $truncated_string = $string;
316
    $truncated_string =~ s/\+.*$//;
317
    #check if length match the expected length of an isrn
318
    return 0 unless ( length($truncated_string)<=36 );
319
    #check if the pattern look like an isrn
320
    return 0 unless ( $string =~ m/^[A-Z](\w[\/-]?)*--(\d\d[\/-])?\d+([\/-]\w+)?(--[A-Z][A-Z])?(\+[A-Z0-9a-z,\.\/]+)?$/ );
321
    #check if each part has a valid length
322
    $string =~ m/^([\w\/-]+)--([\d\/-]+[\w\/-]*)(--[A-Z][A-Z])?(\+[A-Za-z0-9,\.\/]+)?$/;
323
    return 0 unless ( length($1)<=16 );
324
    return 0 unless ( length($2)<=14 );
325
326
    return 1
327
}
328
124
1;
329
1;
330
331
332
=head2 NormalizeISBNs
333
334
Normalization function normalizing a string with a list of ISBNs seperated by a given pattern
335
my $isbns = NormalizeISBNs({
336
  pattern => $pattern,
337
  isbns => $isbns,
338
  strip_hyphens => [0, 1]
339
  format => ['ISBN-10', 'ISBN-13']
340
});
341
342
=cut
343
344
sub NormalizeISBNs{
345
    my ( $params ) = @_;
346
347
    my $string = $params->{isbns};
348
    my $pattern = $params->{pattern};
349
    my $strip_hyphens = $params->{strip_hyphens};
350
    my $format = $params->{format} || q{};
351
    my $return_invalid = $params->{return_invalid};
352
353
    if ($string){
354
        my @isbn_list = split(/$pattern/, $string);
355
        my $result = "";
356
        foreach my $isbn (@isbn_list){
357
            my $normalized_isbn = C4::Koha::NormalizeISBN({ isbn => $isbn, format => $format, strip_hyphens => $strip_hyphens, return_invalid => $return_invalid });
358
            $result .= $normalized_isbn." " if ($normalized_isbn);
359
        }
360
        $result =~ s/ $//;
361
        return $result;
362
    } else {
363
        return;
364
    }
365
}
366
367
=head2 NormalizeISSNs
368
369
Normalization function normalizing a string with a list of ISSNs seperated by a given pattern
370
my $issns = NormalizeISSNs({
371
  issns => $issns,
372
  pattern => $pattern,
373
  strip_hyphens => [0, 1]
374
});
375
376
=cut
377
378
sub NormalizeISSNs{
379
    my ( $params ) = @_;
380
381
    my $string = $params->{issns};
382
    my $pattern = $params->{pattern};
383
    my $strip_hyphens = $params->{strip_hyphens};
384
385
    if ($string){
386
        my @issn_list = split(/$pattern/, $string);
387
        my $result = "";
388
        foreach my $issn (@issn_list){
389
            my $normalized_issn = C4::Koha::NormalizeISSN({ issn => $issn, strip_hyphen => $strip_hyphens });
390
            $result .= $normalized_issn." " if ($normalized_issn);
391
        }
392
        $result =~ s/ $//;
393
        return $result;
394
    } else {
395
        return;
396
    }
397
}
398
399
=head2 NormalizeISMNs
400
401
Normalization function normalizing a string with a list of ISMNs seperated by a given pattern
402
my $ismns = NormalizeISMNs({
403
  ismns => $ismns,
404
  pattern => $pattern,
405
  strip_hyphens => [0, 1]
406
  format => ['ISMN-10', 'ISMN-13']
407
});
408
409
=cut
410
411
sub NormalizeISMNs{
412
    my ( $params ) = @_;
413
414
    my $string = $params->{ismns};
415
    my $pattern = $params->{pattern};
416
    my $strip_hyphens = $params->{strip_hyphens};
417
    my $format = $params->{format} || q{};
418
    my $return_invalid = $params->{return_invalid};
419
420
    if ($string){
421
        my @ismn_list = split(/$pattern/, $string);
422
        my $result = "";
423
        foreach my $ismn (@ismn_list){
424
            my $normalized_ismn = Koha::Util::Normalize::NormalizeISMN({ ismn => $ismn, format => $format, strip_hyphens => $strip_hyphens, return_invalid => $return_invalid });
425
            $result .= $normalized_ismn." " if ($normalized_ismn);
426
        }
427
        $result =~ s/ $//;
428
        return $result;
429
    } else {
430
        return;
431
    }
432
}
433
434
=head2 NormalizeISRCs
435
436
Normalization function normalizing a string with a list of ISRCs seperated by a given pattern
437
my $isrcs = NormalizeISRCs({
438
  isrcs => $isrcs,
439
  pattern => $pattern,
440
  strip_hyphens => [0, 1]
441
});
442
443
=cut
444
445
sub NormalizeISRCs{
446
    my ( $params ) = @_;
447
448
    my $string = $params->{isrcs};
449
    my $pattern = $params->{pattern};
450
    my $strip_hyphens = $params->{strip_hyphens};
451
    my $return_invalid = $params->{return_invalid};
452
453
    if ($string){
454
        my @isrc_list = split(/$pattern/, $string);
455
        my $result = "";
456
        foreach my $isrc (@isrc_list){
457
            my $normalized_isrc = Koha::Util::Normalize::NormalizeISRC({ isrc => $isrc, strip_hyphens => $strip_hyphens, return_invalid => $return_invalid });
458
            $result .= $normalized_isrc." " if ($normalized_isrc);
459
        }
460
        $result =~ s/ $//;
461
        return $result;
462
    } else {
463
        return;
464
    }
465
}
466
467
=head2 NormalizeISRNs
468
469
Normalization function normalizing a string with a list of ISRNs seperated by a given pattern
470
my $isrns = NormalizeISRNs({
471
  isrns => $isrns,
472
  pattern => $pattern,
473
  convert_slash = [0, 1],
474
  drop_local_suffix = [0, 1]
475
});
476
477
=cut
478
479
sub NormalizeISRNs{
480
    my ( $params ) = @_;
481
482
    my $string = $params->{isrns};
483
    my $pattern = $params->{pattern};
484
    my $drop_local_suffix = $params->{drop_local_suffix};
485
    my $convert_slash = $params->{convert_slash};
486
    my $return_invalid = $params->{return_invalid};
487
488
    if ($string){
489
        my @isrn_list = split(/$pattern/, $string);
490
        my $result = "";
491
        foreach my $isrn (@isrn_list){
492
            my $normalized_isrn = Koha::Util::Normalize::NormalizeISRN({ isrn => $isrn, drop_local_suffix => $drop_local_suffix, convert_slash => $convert_slash, return_invalid => $return_invalid });
493
            $result .= $normalized_isrn." " if ($normalized_isrn);
494
        }
495
        $result =~ s/ $//;
496
        return $result;
497
    } else {
498
        return;
499
    }
500
}
501
502
125
__END__
503
__END__
126
504
127
=head1 AUTHOR
505
=head1 AUTHOR
128
129
Koha Development Team <http://koha-community.org/>
506
Koha Development Team <http://koha-community.org/>
130
507
131
Tomas Cohen Arazi <tomascohen@theke.io>
508
Tomas Cohen Arazi <tomascohen@theke.io>
(-)a/misc/cronjobs/mana_send_pairs.pl (-12 / +22 lines)
Lines 6-11 use Getopt::Long; Link Here
6
use Koha;
6
use Koha;
7
use C4::Koha;
7
use C4::Koha;
8
use Koha;
8
use Koha;
9
use Koha::Util::Normalize q/NormalizeISMN/;
9
10
10
use JSON;
11
use JSON;
11
use HTTP::Request;
12
use HTTP::Request;
Lines 35-43 if (C4::Context->preference("Mana") == 1){ Link Here
35
    my $dbh = C4::Context->dbh;
36
    my $dbh = C4::Context->dbh;
36
    my $query_part1 = "
37
    my $query_part1 = "
37
    SELECT
38
    SELECT
38
        issues1.borrowernumber as borrowernumber,
39
        biblio_metadata1.biblionumber as biblionumber1,
40
        biblio_metadata2.biblionumber as biblionumber2,
41
        ExtractValue(biblio_metadata1.metadata, '//datafield[\@tag=\"010\"]/subfield[\@code=\"a\"]') as isbn1,
39
        ExtractValue(biblio_metadata1.metadata, '//datafield[\@tag=\"010\"]/subfield[\@code=\"a\"]') as isbn1,
42
        ExtractValue(biblio_metadata2.metadata, '//datafield[\@tag=\"010\"]/subfield[\@code=\"a\"]') as isbn2,
40
        ExtractValue(biblio_metadata2.metadata, '//datafield[\@tag=\"010\"]/subfield[\@code=\"a\"]') as isbn2,
43
        ExtractValue(biblio_metadata1.metadata, '//datafield[\@tag=\"011\"]/subfield[\@code=\"a\"]') as issn1,
41
        ExtractValue(biblio_metadata1.metadata, '//datafield[\@tag=\"011\"]/subfield[\@code=\"a\"]') as issn1,
Lines 89-111 if (C4::Context->preference("Mana") == 1){ Link Here
89
    #for each reading pair, processes the id pairs
87
    #for each reading pair, processes the id pairs
90
    while ( $row = $sth->fetchrow_hashref ){
88
    while ( $row = $sth->fetchrow_hashref ){
91
        #if necessary, normalize the ids
89
        #if necessary, normalize the ids
92
        $row->{isbn1} = C4::Koha::NormalizeISBN({ isbns => $row->{isbn1}, format => 'ISBN-13', strip_hyphens => 1 }) if $row->{isbn1};
90
        $row->{isbn1} = Koha::Util::Normalize::NormalizeISBNs({ isbns => $row->{isbn1}, pattern => ' ', format => 'ISBN-13', strip_hyphens => 1 }) if $row->{isbn1};
93
        $row->{isbn2} = C4::Koha::NormalizeISBN({ isbns => $row->{isbn2}, format => 'ISBN-13', strip_hyphens => 1 }) if $row->{isbn2};
91
        $row->{isbn2} = Koha::Util::Normalize::NormalizeISBNs({ isbns => $row->{isbn2}, pattern => ' ', format => 'ISBN-13', strip_hyphens => 1 }) if $row->{isbn2};
92
        $row->{ismn1} = Koha::Util::Normalize::NormalizeISMNs({ ismn => $row->{ismn1}, pattern => ' ', format => 'ISMN-13', strip_hyphens => 1 }) if $row->{ismn1};
93
        $row->{ismn2} = Koha::Util::Normalize::NormalizeISMNs({ ismn => $row->{ismn2}, pattern => ' ', format => 'ISMN-13', strip_hyphens => 1 }) if $row->{ismn2};
94
        $row->{isrc1} = Koha::Util::Normalize::NormalizeISRCs({ isrc => $row->{isrc1}, pattern => ' ', strip_hyphens => 1 }) if $row->{isrc1};
95
        $row->{isrc2} = Koha::Util::Normalize::NormalizeISRCs({ isrc => $row->{isrc2}, pattern => ' ', strip_hyphens => 1 }) if $row->{isrc2};
96
        $row->{isrn1} = Koha::Util::Normalize::NormalizeISRNs({ isrn => $row->{isrn1}, pattern => ' ', drop_local_suffix => 1 }) if $row->{isrn1};
97
        $row->{isrn2} = Koha::Util::Normalize::NormalizeISRNs({ isrn => $row->{isrn2}, pattern => ' ', drop_local_suffix => 1 }) if $row->{isrn2};
94
98
95
        # try to make pairs between every id possible
99
        # try to make pairs between every id possible
96
        foreach my $key1 ( qw ( isbn issn ismn isrn ean upc isrc ) ){
100
        foreach my $key1 ( qw ( isbn issn ismn isrn ean upc isrc ) ){
97
            if ( $row->{ $key1."1" }){
101
            if ( $row->{ $key1."1" }){
102
                my @id1_list = split(/ /, $row->{ $key1."1" });
98
                foreach my $key2 ( qw ( isbn issn ismn isrn ean upc isrc ) ){
103
                foreach my $key2 ( qw ( isbn issn ismn isrn ean upc isrc ) ){
99
                    # if both selected ids exists
104
                    # if both selected ids exists
100
                    if ( $row->{ $key2."2" } ){
105
                    if ( $row->{ $key2."2" } ){
106
                        my @id2_list = split(/ /, $row->{ $key1."2" });
101
                        # make pairs between every ids of the given type
107
                        # make pairs between every ids of the given type
102
                        my $currentpair;
108
                        foreach my $id1 (@id1_list){
103
                        $currentpair->{ idtype1 } = $key1;
109
                            foreach my $id2 (@id2_list){
104
                        $currentpair->{ documentid1 } = $row->{$key1."1"};
110
                                my $currentpair;
105
                        $currentpair->{ idtype2 } = $key2;
111
                                $currentpair->{ idtype1 } = $key1;
106
                        $currentpair->{ documentid2 } = $row->{$key2."2"};
112
                                $currentpair->{ documentid1 } = $id1;
107
                        push @reading_pairs, $currentpair;
113
                                $currentpair->{ idtype2 } = $key2;
108
                    }
114
                                $currentpair->{ documentid2 } = $id2;
115
                                push @reading_pairs, $durrentpair;
116
                            }
117
                        }
118
                   }
109
                }
119
                }
110
            }
120
            }
111
        }
121
        }
(-)a/opac/svc/mana/getSuggestion (-38 / +94 lines)
Lines 24-35 use Koha::Biblioitems; Link Here
24
use Koha::Biblioitem;
24
use Koha::Biblioitem;
25
use Koha::Reading_suggestions;
25
use Koha::Reading_suggestions;
26
use Koha::Reading_suggestion;
26
use Koha::Reading_suggestion;
27
use Koha::Util::Normalize q/NormalizeISMN/;
27
28
28
use Koha;
29
use Koha;
29
use C4::Koha;
30
use C4::Koha;
30
31
use C4::Context;
31
use C4::Auth qw(check_cookie_auth), qw(get_template_and_user);
32
use C4::Auth qw(check_cookie_auth), qw(get_template_and_user);
32
33
34
use List::MoreUtils qw(uniq);
35
33
use CGI;
36
use CGI;
34
use JSON;
37
use JSON;
35
38
Lines 70-75 eval { Link Here
70
};
73
};
71
my @biblios;
74
my @biblios;
72
my @biblios_blessed;
75
my @biblios_blessed;
76
my $bibliosnumbers;
73
if ( $local_suggestions and DateTime::Duration->compare( $duration, DURATION_BEFORE_REFRESH ) == -1 ){
77
if ( $local_suggestions and DateTime::Duration->compare( $duration, DURATION_BEFORE_REFRESH ) == -1 ){
74
        delete $local_suggestions->{timestamp};
78
        delete $local_suggestions->{timestamp};
75
        delete $local_suggestions->{biblionumber};
79
        delete $local_suggestions->{biblionumber};
Lines 83-104 else{ Link Here
83
    # get all informations to ask mana
87
    # get all informations to ask mana
84
    my $idtype;
88
    my $idtype;
85
    my $documentid;
89
    my $documentid;
90
    my $documentids;
86
    my $notnormalizeddocumentid;
91
    my $notnormalizeddocumentid;
87
    my $found = 0;
92
    my $found = 0;
88
93
89
    if ( $biblioitem && $biblioitem->isbn ){
94
    if ( $biblioitem && $biblioitem->isbn ){
90
        $idtype= "isbn";
95
        $idtype = "isbn";
91
        $documentid= C4::Koha::NormalizeISBN({ isbn => $biblioitem->isbn, format => 'ISBN-13', strip_hyphens => 1 });
96
        $documentids = Koha::Util::Normalize::NormalizeISBNs({ isbns => $biblioitem->isbn, pattern => ' \| ', format => 'ISBN-13', strip_hyphens => 1 });
92
        if ($documentids) { $found = 1; };
97
        if ($documentids) { $found = 1; };
93
    }
98
    }
94
    elsif ( $biblioitem && $biblioitem->issn ){
99
    elsif ( $biblioitem && $biblioitem->issn ){
95
        $idtype= "issn";
100
        $idtype = "issn";
96
        $documentid= $biblioitem->issn;
101
        $documentids =  Koha::Util::Normalize::NormalizeISSNs({ issns => $biblioitem->issn, pattern => ' \| ', strip_hyphens => 0 });
97
        if ($documentids) { $found = 1; };
102
        if ($documentids) { $found = 1; };
98
    }
103
    }
99
    elsif ( $biblioitem && $biblioitem->ean ){
104
    elsif ( $biblioitem && $biblioitem->ean ){
100
        $idtype= "ean";
105
        $idtype = "ean";
101
        $documentid= $biblioitem->ean;
106
        $documentid = $biblioitem->ean;
107
        $documentids =~ s/ \| / /;
102
        if ($documentids) { $found = 1; };
108
        if ($documentids) { $found = 1; };
103
    }
109
    }
104
    if ($found == 0){
110
    if ($found == 0){
Lines 122-157 else{ Link Here
122
128
123
        if ($row->{isbn}){
129
        if ($row->{isbn}){
124
            $idtype = "isbn";
130
            $idtype = "isbn";
125
            $documentid = C4::Koha::NormalizeISBN({ isbn => $row->{isbn}, format => 'ISBN-13', strip_hyphens => 1 });
131
            $documentid = Koha::Util::Normalize::NormalizeISBNs({ isbn => $row->{isbn}, pattern => ' ', format => 'ISBN-13', strip_hyphens => 1 });
126
        }
132
        }
127
        elsif ($row->{issn}){
133
        elsif ($row->{issn}){
128
            $idtype = "issn";
134
            $idtype = "issn";
129
            $documentid = $row->{issn};
135
            $documentid = Koha::Util::Normalize::NormalizeISSNs({ ismn => $row->{issn}, pattern => ' ', strip_hyphens => 1 });
130
        }
136
        }
131
        elsif ($row->{ismn}){
137
        elsif ($row->{ismn}){
132
            $idtype = "ismn";
138
            $idtype = "ismn";
133
            $documentid = $row->{ismn};
139
            $documentids = Koha::Util::Normalize::NormalizeISMNs({ ismns => $row->{ismn}, pattern => ' ', format => 'ISMN-13', strip_hyphens => 1 });
134
        }
140
        }
135
        elsif ($row->{isrn}){
141
        elsif ($row->{isrn}){
136
            $idtype = "isrn";
142
            $idtype = "isrn";
137
            $documentid = $row->{isrn};
143
            $documentids = Koha::Util::Normalize::NormalizeISRNs({isrns => $row->{isrn}, pattern => ' ', convert_slash => 1, drop_local_suffix => 1 });
138
        }
144
        }
139
        elsif ($row->{isrc}){
145
        elsif ($row->{isrc}){
140
            $idtype = "isrc";
146
            $idtype = "isrc";
141
            $documentid = $row->{isrc};
147
            $documentids = Koha::Util::Normalize::NormalizeISRCs({ isrcs => $row->{isrc}, pattern => ' ', strip_hyphens => 1 })
142
        }
148
        }
143
        elsif ($row->{upc}){
149
        elsif ($row->{upc}){
144
            $idtype = "upc";
150
            $idtype = "upc";
145
            $documentid = $row->{upc};
151
            $documentids = $row->{upc};
146
        }
152
        }
147
        elsif ($row->{ean}){
153
        elsif ($row->{ean}){
148
            $idtype = "ean";
154
            $idtype = "ean";
149
            $documentid = $row->{ean};
155
            $documentids = $row->{ean};
150
        }
156
        }
151
        else{
157
        else{
152
            die "error: no propper identifier";
158
            die "error: no propper identifier";
153
        }
159
        }
154
    }
160
    }
161
162
    $documentid=(split(/ /, $documentids, 2))[0];
155
    $notnormalizeddocumentid = $documentid;
163
    $notnormalizeddocumentid = $documentid;
156
    my $offset = 1;
164
    my $offset = 1;
157
    my $length = 10;
165
    my $length = 10;
Lines 190-225 else{ Link Here
190
        #create the list of owned suggested resources
198
        #create the list of owned suggested resources
191
        my $ownedbiblioitem;
199
        my $ownedbiblioitem;
192
        foreach my $resource ( @{ $resources } ){
200
        foreach my $resource ( @{ $resources } ){
193
        #   isbn processsing
201
            my $found = 0;
194
            if ( $resource->{idtype} eq "isbn" ){
195
                $documentid= C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 1 });
196
                $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid;
197
198
            #if we don't have such a biblioitem, we try to format else the isbn
199
                unless ( scalar @{ $ownedbiblioitem->unblessed() } ){
200
                    $documentid = C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 1 });
201
                    $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid;
202
                }
203
202
204
                unless ( scalar @{ $ownedbiblioitem->unblessed() } ){
203
            #isbn, issn and ean can be search for with Koha::Biblioitems->search
205
                    $documentid = C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 0 });
204
            if ($resource->{idtype} eq "isbn" || $resource->{idtype} eq "issn" || $resource->{idtype} eq "ean"){
205
                #isbn processsing
206
                if ( $resource->{idtype} eq "isbn" ){
207
                    $documentid= C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 1 });
206
                    $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid;
208
                    $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid;
209
210
                    #if we don't have such a biblioitem, we try to format else the isbn
211
                    unless ( scalar @{ $ownedbiblioitem->unblessed() } ){
212
                        $documentid = C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 1 });
213
                        $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid;
214
                    }
215
216
                    unless ( scalar @{ $ownedbiblioitem->unblessed() } ){
217
                        $documentid = C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 0 });
218
                        $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid;
219
                    }
220
221
                    unless ( scalar @{ $ownedbiblioitem->unblessed() } ){
222
                        $documentid = C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 0 });
223
                        $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid;
224
                    }
207
                }
225
                }
208
226
209
                unless ( scalar @{ $ownedbiblioitem->unblessed() } ){
227
                #issn and ean don't need special processing
210
                    $documentid = C4::Koha::NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 0 });
228
                elsif ($resource->{idtype} eq "issn" or $resource->{idtype} eq "ean"){
211
                    $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid;
229
                    $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $resource->{documentid} });
212
                }
230
                }
213
231
232
                #construct the tables with biblionumber
233
                if (scalar @{ $ownedbiblioitem->unblessed() } ){
234
                    $found = 1;
235
                    my $ownedbiblio = Koha::Biblios->find( @{ $ownedbiblioitem->unblessed() }[0]->{biblionumber} );
236
                    #add the biblio if not already present
237
                    if ( not(exists($bibliosnumbers->{@{ $ownedbiblioitem->unblessed() }[0]->{biblionumber}})) ){
238
                        push @biblios_blessed, $ownedbiblio;
239
                        push @biblios, $ownedbiblio->unblessed();
240
                        $bibliosnumbers->{@{ $ownedbiblioitem->unblessed() }[0]->{biblionumber}}=1;
241
                    }
242
                }
214
            }
243
            }
215
        #others ids do not need any processing
244
            # if we don't have such a biblioitem, we try to look directly in metadata
216
            else{
245
            # because if the document has multiple isbn they are store in biblioitem table like this
217
                $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $resource->{documentid} });
246
            # "isbn1 | isbn2" and can't be found by biblioitems->seach
218
            }
247
            # other id need to be search with sql
219
            if (scalar @{ $ownedbiblioitem->unblessed() } ){
248
            if ($found != 1) {
220
                my $ownedbiblio = Koha::Biblios->find(  @{ $ownedbiblioitem->unblessed() }[0]->{biblionumber} );
249
                my @params;
221
                push @biblios_blessed, $ownedbiblio;
250
                my %field=("isbn" => "010", "issn" => "011", "ismn" => "013", "isrn" => "015", "isrc" => "016", "upc" => "072", "ean" => "073");
222
                push @biblios, $ownedbiblio->unblessed();
251
252
                #pattern to be tolerent on the hyphens
253
                my $pattern="";
254
                foreach my $p (split('', $resource->{documentid})){
255
                    $pattern .= "$p-*";
256
                }
257
258
                my $dbh = C4::Context->dbh;
259
                my $query = q{
260
                    SELECT biblioitems.biblionumber as biblionumber
261
                    FROM biblioitems
262
                    LEFT JOIN biblio_metadata ON biblioitems.biblionumber=biblio_metadata.biblionumber
263
                    WHERE ExtractValue(biblio_metadata.metadata, '//datafield[@tag="}.$field{$resource->{idtype}}.q{"]/subfield[@code="a"]') REGEXP "}.$pattern.q{"
264
                      AND biblio_metadata.format="marcxml"
265
                };
266
                my $sth = $dbh->prepare( $query );
267
                $ownedbiblioitem = $sth->execute;
268
269
                #construct the tables with biblionumber
270
                my $row = $sth->fetchrow_hashref;
271
                if ( $row ){
272
                    my $ownedbiblio = Koha::Biblios->find( $row->{biblionumber} );
273
                    #add the biblio if not already present
274
                    if ( not(exists($bibliosnumbers->{$row->{biblionumber}})) ){
275
                        push @biblios_blessed, $ownedbiblio;
276
                        push @biblios, $ownedbiblio->unblessed();
277
                        $bibliosnumbers->{$row->{biblionumber}}=1;
278
                    }
279
                }
223
            }
280
            }
224
        }
281
        }
225
            #the number of requested resource is inversely proportionnal to the found_items/returned_items ratio, +1 is here just to avoid 0
282
            #the number of requested resource is inversely proportionnal to the found_items/returned_items ratio, +1 is here just to avoid 0
226
- 

Return to bug 18618