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

(-)a/C4/Koha.pm (-7 / +104 lines)
Lines 28-33 use C4::Branch qw(GetBranchesCount); Link Here
28
use Koha::DateUtils qw(dt_from_string);
28
use Koha::DateUtils qw(dt_from_string);
29
use Memoize;
29
use Memoize;
30
use DateTime::Format::MySQL;
30
use DateTime::Format::MySQL;
31
use Business::ISBN;
31
use autouse 'Data::Dumper' => qw(Dumper);
32
use autouse 'Data::Dumper' => qw(Dumper);
32
use DBI qw(:sql_types);
33
use DBI qw(:sql_types);
33
34
Lines 71-76 BEGIN { Link Here
71
		&GetNormalizedOCLCNumber
72
		&GetNormalizedOCLCNumber
72
        &xml_escape
73
        &xml_escape
73
74
75
        &GetVariationsOfISBN
76
        &GetVariationsOfISBNs
77
        &NormalizeISBN
78
74
		$DEBUG
79
		$DEBUG
75
	);
80
	);
76
	$DEBUG = 0;
81
	$DEBUG = 0;
Lines 1472-1486 sub _normalize_match_point { Link Here
1472
}
1477
}
1473
1478
1474
sub _isbn_cleanup {
1479
sub _isbn_cleanup {
1475
    require Business::ISBN;
1480
    my ($isbn) = @_;
1476
    my $isbn = Business::ISBN->new( $_[0] );
1481
    return NormalizeISBN(
1477
    if ( $isbn ) {
1482
        {
1478
        $isbn = $isbn->as_isbn10 if $isbn->type eq 'ISBN13';
1483
            isbn          => $isbn,
1479
        if (defined $isbn) {
1484
            format        => 'ISBN-10',
1480
            return $isbn->as_string([]);
1485
            strip_hyphens => 1,
1481
        }
1486
        }
1487
    );
1488
}
1489
1490
=head2 NormalizedISBN
1491
1492
  my $isbns = NormalizedISBN({
1493
    isbn => $isbn,
1494
    strip_hyphens => [0,1],
1495
    format => ['ISBN-10', 'ISBN-13']
1496
  });
1497
1498
  Returns an isbn validated by Business::ISBN.
1499
  Optionally strips hyphens and/or forces the isbn
1500
  to be of the specified format.
1501
1502
  If the string cannot be validated as an isbn,
1503
  it returns nothing.
1504
1505
=cut
1506
1507
sub NormalizeISBN {
1508
    my ($params) = @_;
1509
1510
    my $string        = $params->{isbn};
1511
    my $strip_hyphens = $params->{strip_hyphens};
1512
    my $format        = $params->{format};
1513
1514
    my $isbn = Business::ISBN->new($string);
1515
1516
    if ( $isbn && $isbn->error != Business::ISBN::BAD_ISBN ) {
1517
1518
        if ( $format eq 'ISBN-10' ) {
1519
            $isbn = $isbn->as_isbn10();
1520
        }
1521
        elsif ( $format eq 'ISBN-13' ) {
1522
            $isbn = $isbn->as_isbn13();
1523
        }
1524
1525
        if ($strip_hyphens) {
1526
            $string = $isbn->as_string( [] );
1527
        } else {
1528
            $string = $isbn->as_string();
1529
        }
1530
1531
        return $string;
1482
    }
1532
    }
1483
    return;
1533
}
1534
1535
=head2 GetVariationsOfISBN
1536
1537
  my @isbns = GetVariationsOfISBN( $isbn );
1538
1539
  Returns a list of varations of the given isbn in
1540
  both ISBN-10 and ISBN-13 formats, with and without
1541
  hyphens.
1542
1543
  In a scalar context, the isbns are returned as a
1544
  string delimited by ' | '.
1545
1546
=cut
1547
1548
sub GetVariationsOfISBN {
1549
    my ($isbn) = @_;
1550
1551
    my @isbns;
1552
1553
    push( @isbns, NormalizeISBN({ isbn => $isbn }) );
1554
    push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-10' }) );
1555
    push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-13' }) );
1556
    push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-10', strip_hyphens => 1 }) );
1557
    push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-13', strip_hyphens => 1 }) );
1558
1559
    return wantarray ? @isbns : join( " | ", @isbns );
1560
}
1561
1562
=head2 GetVariationsOfISBNs
1563
1564
  my @isbns = GetVariationsOfISBNs( @isbns );
1565
1566
  Returns a list of varations of the given isbns in
1567
  both ISBN-10 and ISBN-13 formats, with and without
1568
  hyphens.
1569
1570
  In a scalar context, the isbns are returned as a
1571
  string delimited by ' | '.
1572
1573
=cut
1574
1575
sub GetVariationsOfISBNs {
1576
    my (@isbns) = @_;
1577
1578
    @isbns = map { GetVariationsOfISBN( $_ ) } @isbns;
1579
1580
    return wantarray ? @isbns : join( " | ", @isbns );
1484
}
1581
}
1485
1582
1486
1;
1583
1;
(-)a/C4/Matcher.pm (-2 / +11 lines)
Lines 630-652 sub get_matches { Link Here
630
    $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
630
    $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
631
    foreach my $matchpoint ( @{ $self->{'matchpoints'} } ) {
631
    foreach my $matchpoint ( @{ $self->{'matchpoints'} } ) {
632
        my @source_keys = _get_match_keys( $source_record, $matchpoint );
632
        my @source_keys = _get_match_keys( $source_record, $matchpoint );
633
633
        next if scalar(@source_keys) == 0;
634
        next if scalar(@source_keys) == 0;
634
635
636
        @source_keys = C4::Koha::GetVariationsOfISBNs(@source_keys)
637
          if ( $matchpoint->{index} eq 'isbn'
638
            && C4::Context->preference('AggressiveMatchOnISBN') );
639
635
        # build query
640
        # build query
636
        my $query;
641
        my $query;
637
        my $error;
642
        my $error;
638
        my $searchresults;
643
        my $searchresults;
639
        my $total_hits;
644
        my $total_hits;
640
        if ( $self->{'record_type'} eq 'biblio' ) {
645
        if ( $self->{'record_type'} eq 'biblio' ) {
646
            my $phr = C4::Context->preference('AggressiveMatchOnISBN') ? ',phr' : q{};
647
641
            if ($QParser) {
648
            if ($QParser) {
642
                $query = join( " || ",
649
                $query = join( " || ",
643
                    map { "$matchpoint->{'index'}:$_" } @source_keys );
650
                    map { "$matchpoint->{'index'}$phr:$_" } @source_keys );
644
            }
651
            }
645
            else {
652
            else {
646
                $query = join( " or ",
653
                $query = join( " or ",
647
                    map { "$matchpoint->{'index'}=$_" } @source_keys );
654
                    map { "$matchpoint->{'index'}$phr=$_" } @source_keys );
648
            }
655
            }
656
649
            require C4::Search;
657
            require C4::Search;
658
650
            ( $error, $searchresults, $total_hits ) =
659
            ( $error, $searchresults, $total_hits ) =
651
              C4::Search::SimpleSearch( $query, 0, $max_matches );
660
              C4::Search::SimpleSearch( $query, 0, $max_matches );
652
        }
661
        }
(-)a/installer/data/mysql/sysprefs.sql (+1 lines)
Lines 9-14 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
9
('AdvancedSearchTypes','itemtypes','itemtypes|ccode','Select which set of fields comprise the Type limit in the advanced search','Choice'),
9
('AdvancedSearchTypes','itemtypes','itemtypes|ccode','Select which set of fields comprise the Type limit in the advanced search','Choice'),
10
('AgeRestrictionMarker','',NULL,'Markers for age restriction indication, e.g. FSK|PEGI|Age|','free'),
10
('AgeRestrictionMarker','',NULL,'Markers for age restriction indication, e.g. FSK|PEGI|Age|','free'),
11
('AgeRestrictionOverride','0',NULL,'Allow staff to check out an item with age restriction.','YesNo'),
11
('AgeRestrictionOverride','0',NULL,'Allow staff to check out an item with age restriction.','YesNo'),
12
('AggressiveMatchOnISBN','0','If enabled, attempt to match aggressively by trying all variations of the ISBNs in the imported record as a phrase in the ISBN fields of already cataloged records when matching on ISBN with the record import tool','YesNo'),
12
('AllFinesNeedOverride','1','0','If on, staff will be asked to override every fine, even if it is below noissuescharge.','YesNo'),
13
('AllFinesNeedOverride','1','0','If on, staff will be asked to override every fine, even if it is below noissuescharge.','YesNo'),
13
('AllowAllMessageDeletion','0','','Allow any Library to delete any message','YesNo'),
14
('AllowAllMessageDeletion','0','','Allow any Library to delete any message','YesNo'),
14
('AllowFineOverride','0','0','If on, staff will be able to issue books to patrons with fines greater than noissuescharge.','YesNo'),
15
('AllowFineOverride','0','0','If on, staff will be able to issue books to patrons with fines greater than noissuescharge.','YesNo'),
(-)a/installer/data/mysql/updatedatabase.pl (+20 lines)
Lines 8129-8134 if(CheckVersion($DBversion)) { Link Here
8129
    SetVersion($DBversion);
8129
    SetVersion($DBversion);
8130
}
8130
}
8131
8131
8132
$DBversion = "3.15.00.XXX";
8133
if ( CheckVersion($DBversion) ) {
8134
    $dbh->do("
8135
        INSERT INTO systempreferences (
8136
            variable,
8137
            value,
8138
            explanation,
8139
            type
8140
        ) VALUES (
8141
            'AggressiveMatchOnISBN',
8142
            '0',
8143
            'If enabled, attempt to match aggressively by trying all variations of the ISBNs in the imported record as a phrase in the ISBN fields of already cataloged records when matching on ISBN with the record import tool',
8144
            'YesNo'
8145
        )
8146
    ");
8147
8148
    print "Upgrade to $DBversion done (Bug 10500 - Improve isbn matching when importing records)\n";
8149
    SetVersion($DBversion);
8150
}
8151
8132
=head1 FUNCTIONS
8152
=head1 FUNCTIONS
8133
8153
8134
=head2 TableExists($table)
8154
=head2 TableExists($table)
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref (+8 lines)
Lines 182-184 Cataloging: Link Here
182
                  yes: Display
182
                  yes: Display
183
                  no: "Don't display"
183
                  no: "Don't display"
184
            - acquisition details on the biblio detail page.
184
            - acquisition details on the biblio detail page.
185
    Importing:
186
        -
187
            - When matching on ISBN with the record import tool,
188
            - pref: AggressiveMatchOnISBN
189
              choices:
190
                  yes: "do"
191
                  no: "don't"
192
            - attempt to match aggressively by trying all variations of the ISBNs in the imported record as a phrase in the ISBN fields of already cataloged records.
(-)a/t/Koha.t (-2 / +7 lines)
Lines 3-9 use strict; Link Here
3
use warnings;
3
use warnings;
4
4
5
use C4::Context;
5
use C4::Context;
6
use Test::More tests => 11;
6
use Test::More tests => 14;
7
use Test::MockModule;
7
use Test::MockModule;
8
use DBD::Mock;
8
use DBD::Mock;
9
9
Lines 61-63 is(C4::Koha::_isbn_cleanup('0-590-35340-3'), '0590353403', '_isbn_cleanup remove Link Here
61
is(C4::Koha::_isbn_cleanup('0590353403 (pbk.)'), '0590353403', '_isbn_cleanup removes parenthetical');
61
is(C4::Koha::_isbn_cleanup('0590353403 (pbk.)'), '0590353403', '_isbn_cleanup removes parenthetical');
62
is(C4::Koha::_isbn_cleanup('978-0-321-49694-2'), '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10');
62
is(C4::Koha::_isbn_cleanup('978-0-321-49694-2'), '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10');
63
63
64
- 
64
is(C4::Koha::NormalizeISBN({ isbn => '978-0-321-49694-2 (pbk.)', format => 'ISBN-10', strip_hyphens => 1 }), '0321496949', 'Test NormalizeISBN with all features enabled' );
65
66
my @isbns = qw/ 978-0-321-49694-2 0-321-49694-9 978-0-321-49694-2 0321496949 9780321496942/;
67
is( join('|', @isbns), join('|', GetVariationsOfISBN('978-0-321-49694-2 (pbk.)')), 'GetVariationsOfISBN returns all variations' );
68
69
is( join('|', @isbns), join('|', GetVariationsOfISBNs('978-0-321-49694-2 (pbk.)')), 'GetVariationsOfISBNs returns all variations' );

Return to bug 10500