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 Memoize;
28
use Memoize;
29
use DateTime;
29
use DateTime;
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
33
33
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $DEBUG);
34
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $DEBUG);
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 1492-1506 sub _normalize_match_point { Link Here
1492
}
1497
}
1493
1498
1494
sub _isbn_cleanup {
1499
sub _isbn_cleanup {
1495
    require Business::ISBN;
1500
    my ($isbn) = @_;
1496
    my $isbn = Business::ISBN->new( $_[0] );
1501
    return NormalizeISBN(
1497
    if ( $isbn ) {
1502
        {
1498
        $isbn = $isbn->as_isbn10 if $isbn->type eq 'ISBN13';
1503
            isbn          => $isbn,
1499
        if (defined $isbn) {
1504
            format        => 'ISBN-10',
1500
            return $isbn->as_string([]);
1505
            strip_hyphens => 1,
1501
        }
1506
        }
1507
    );
1508
}
1509
1510
=head2 NormalizedISBN
1511
1512
  my $isbns = NormalizedISBN({
1513
    isbn => $isbn,
1514
    strip_hyphens => [0,1],
1515
    format => ['ISBN-10', 'ISBN-13']
1516
  });
1517
1518
  Returns an isbn validated by Business::ISBN.
1519
  Optionally strips hyphens and/or forces the isbn
1520
  to be of the specified format.
1521
1522
  If the string cannot be validated as an isbn,
1523
  it returns nothing.
1524
1525
=cut
1526
1527
sub NormalizeISBN {
1528
    my ($params) = @_;
1529
1530
    my $string        = $params->{isbn};
1531
    my $strip_hyphens = $params->{strip_hyphens};
1532
    my $format        = $params->{format};
1533
1534
    my $isbn = Business::ISBN->new($string);
1535
1536
    if ( $isbn && $isbn->error != Business::ISBN::BAD_ISBN ) {
1537
1538
        if ( $format eq 'ISBN-10' ) {
1539
            $isbn = $isbn->as_isbn10();
1540
        }
1541
        elsif ( $format eq 'ISBN-13' ) {
1542
            $isbn = $isbn->as_isbn13();
1543
        }
1544
1545
        if ($strip_hyphens) {
1546
            $string = $isbn->as_string( [] );
1547
        } else {
1548
            $string = $isbn->as_string();
1549
        }
1550
1551
        return $string;
1502
    }
1552
    }
1503
    return;
1553
}
1554
1555
=head2 GetVariationsOfISBN
1556
1557
  my @isbns = GetVariationsOfISBN( $isbn );
1558
1559
  Returns a list of varations of the given isbn in
1560
  both ISBN-10 and ISBN-13 formats, with and without
1561
  hyphens.
1562
1563
  In a scalar context, the isbns are returned as a
1564
  string delimited by ' | '.
1565
1566
=cut
1567
1568
sub GetVariationsOfISBN {
1569
    my ($isbn) = @_;
1570
1571
    my @isbns;
1572
1573
    push( @isbns, NormalizeISBN({ isbn => $isbn }) );
1574
    push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-10' }) );
1575
    push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-13' }) );
1576
    push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-10', strip_hyphens => 1 }) );
1577
    push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-13', strip_hyphens => 1 }) );
1578
1579
    return wantarray ? @isbns : join( " | ", @isbns );
1580
}
1581
1582
=head2 GetVariationsOfISBNs
1583
1584
  my @isbns = GetVariationsOfISBNs( @isbns );
1585
1586
  Returns a list of varations of the given isbns in
1587
  both ISBN-10 and ISBN-13 formats, with and without
1588
  hyphens.
1589
1590
  In a scalar context, the isbns are returned as a
1591
  string delimited by ' | '.
1592
1593
=cut
1594
1595
sub GetVariationsOfISBNs {
1596
    my (@isbns) = @_;
1597
1598
    @isbns = map { GetVariationsOfISBN( $_ ) } @isbns;
1599
1600
    return wantarray ? @isbns : join( " | ", @isbns );
1504
}
1601
}
1505
1602
1506
1;
1603
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 427-429 INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES(' Link Here
427
INSERT IGNORE INTO systempreferences (variable,value,explanation,options,type) VALUES('UseCourseReserves', '0', 'Enable the course reserves feature.', NULL, 'YesNo');
427
INSERT IGNORE INTO systempreferences (variable,value,explanation,options,type) VALUES('UseCourseReserves', '0', 'Enable the course reserves feature.', NULL, 'YesNo');
428
INSERT IGNORE INTO systempreferences (variable,value,explanation,options,type) VALUES('OpacHoldNotes',0,'Show hold notes on OPAC','','YesNo');
428
INSERT IGNORE INTO systempreferences (variable,value,explanation,options,type) VALUES('OpacHoldNotes',0,'Show hold notes on OPAC','','YesNo');
429
INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('CalculateFinesOnReturn','1','Switch to control if overdue fines are calculated on return or not', '', 'YesNo');
429
INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('CalculateFinesOnReturn','1','Switch to control if overdue fines are calculated on return or not', '', 'YesNo');
430
INSERT INTO systempreferences (variable,value,explanation,type) VALUES ('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');
(-)a/installer/data/mysql/updatedatabase.pl (+20 lines)
Lines 7010-7015 CREATE TABLE IF NOT EXISTS borrower_files ( Link Here
7010
    SetVersion($DBversion);
7010
    SetVersion($DBversion);
7011
}
7011
}
7012
7012
7013
$DBversion = "3.13.00.XXX";
7014
if ( CheckVersion($DBversion) ) {
7015
    $dbh->do("
7016
        INSERT INTO systempreferences (
7017
            variable,
7018
            value,
7019
            explanation,
7020
            type
7021
        ) VALUES (
7022
            'AggressiveMatchOnISBN',
7023
            '0',
7024
            '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',
7025
            'YesNo'
7026
        )
7027
    ");
7028
7029
    print "Upgrade to $DBversion done (Bug 10500 - Improve isbn matching when importing records)\n";
7030
    SetVersion($DBversion);
7031
}
7032
7013
=head1 FUNCTIONS
7033
=head1 FUNCTIONS
7014
7034
7015
=head2 TableExists($table)
7035
=head2 TableExists($table)
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref (+8 lines)
Lines 176-178 Cataloging: Link Here
176
            - pref: NotesBlacklist
176
            - pref: NotesBlacklist
177
              class: multi
177
              class: multi
178
            - note fields in title notes separator (OPAC record details) and in the description separator (Staff client record details). The fields should appear separated with commas and according with the Koha MARC format (eg 3.. for UNIMARC, 5.. for MARC21)
178
            - note fields in title notes separator (OPAC record details) and in the description separator (Staff client record details). The fields should appear separated with commas and according with the Koha MARC format (eg 3.. for UNIMARC, 5.. for MARC21)
179
    Importing:
180
        -
181
            - When matching on ISBN with the record import tool,
182
            - pref: AggressiveMatchOnISBN
183
              choices:
184
                  yes: "do"
185
                  no: "don't"
186
            - 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