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

(-)a/Koha/MetadataExtractor.pm (+108 lines)
Line 0 Link Here
1
package Koha::MetadataExtractor;
2
3
# Copyright ByWater Solutions 2023
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
=head1 NAME
21
22
Koha::MetadataExtractor - Extract specific metadata from MARC::Record objects
23
24
=cut
25
26
use Modern::Perl;
27
28
use Koha::Exceptions;
29
30
=head1 API
31
32
=head2 Class methods
33
34
=head3 new
35
36
    my $extractor = Koha::MetadataExtractor->new;
37
38
Constructor for the I<Koha::MetadataExtractor> class.
39
40
=cut
41
42
sub new {
43
    my ($class) = @_;
44
    my $self = { extractors => {} };
45
46
    return
47
        bless $self,
48
        $class;
49
}
50
51
=head2 get_normalized_upc
52
53
    my $normalized_upc = $extractor->get_normalized_upc( { record => $record, schema => $schema } );
54
55
Returns the normalized UPC for the passed I<$record>.
56
57
=cut
58
59
sub get_normalized_upc {
60
    my ( $self, $params ) = @_;
61
62
    Koha::Exceptions::MissingParameter->throw( parameter => 'record' )
63
        unless $params->{record};
64
65
    return $self->get_extractor( { schema => $params->{schema} } )->get_normalized_upc( $params->{record} );
66
}
67
68
=head2 Internal methods
69
70
=head3 get_extractor
71
72
    my $extractor = $self->get_extractor( { schema => $schema } );
73
74
Returns the cached extractor for the specified I<$schema>.
75
76
=cut
77
78
sub get_extractor {
79
    my ( $self, $params ) = @_;
80
81
    my $schema = $params->{schema};
82
83
    Koha::Exceptions::MissingParameter->throw( parameter => 'schema' )
84
        unless $schema;
85
86
    my $valid_schemas = { 'MARC21' => 1, 'UNIMARC' => 1 };
87
88
    Koha::Exceptions::WrongParameter->throw( name => 'schema', value => $schema )
89
        unless $valid_schemas->{$schema};
90
91
    unless ( $self->{extractors}->{$schema} ) {
92
        my $extractor_class = "Koha::MetadataExtractor::MARC::$schema";
93
        eval "require $extractor_class";
94
        $self->{extractors}->{$schema} = $extractor_class->new;
95
    }
96
97
    return $self->{extractors}->{$schema};
98
}
99
100
=head1 AUTHOR
101
102
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
103
104
=cut
105
106
1;
107
108
__END__
(-)a/Koha/MetadataExtractor/MARC/MARC21.pm (+93 lines)
Line 0 Link Here
1
package Koha::MetadataExtractor::MARC::MARC21;
2
3
# Copyright ByWater Solutions 2023
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
=head1 NAME
21
22
Koha::MetadataExtractor - Extract specific metadata from MARC::Record objects
23
24
=cut
25
26
use Modern::Perl;
27
28
use Koha::Exceptions;
29
30
=head1 API
31
32
=head2 Class methods
33
34
=head3 new
35
36
    my $extractor = Koha::MetadataExtractor::MARC::MARC21->new;
37
38
Constructor for the I<Koha::MetadataExtractor> class.
39
40
=cut
41
42
sub new {
43
    my ($class) = @_;
44
    my $self = {};
45
46
    return
47
        bless $self,
48
        $class;
49
}
50
51
=head2 get_normalized_upc
52
53
    my $normalized_upc = $extractor->get_normalized_upc( $record );
54
55
Returns a stringthe COinS (a span) which can be included in a biblio record
56
57
=cut
58
59
sub get_normalized_upc {
60
    my ( $self, $record ) = @_;
61
62
    Koha::Exceptions::MissingParameter->throw( parameter => 'record' )
63
        unless $record;
64
65
    Koha::Exceptions::WrongParameter->throw( name => 'record', type => ref($record) )
66
        unless ref($record) eq 'MARC::Record';
67
68
    my @fields = $record->field('024');
69
    foreach my $field (@fields) {
70
71
        my $indicator = $field->indicator(1);
72
        my $upc       = $field->subfield('a');
73
74
        ( my $normalized_upc ) = $upc =~ /([\d-]*[X]*)/;
75
        $normalized_upc =~ s/-//g;
76
77
        if ( $normalized_upc && $indicator eq "1" ) {
78
            return $normalized_upc;
79
        }
80
    }
81
82
    return;
83
}
84
85
=head1 AUTHOR
86
87
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
88
89
=cut
90
91
1;
92
93
__END__
(-)a/Koha/MetadataExtractor/MARC/UNIMARC.pm (+90 lines)
Line 0 Link Here
1
package Koha::MetadataExtractor::MARC::UNIMARC;
2
3
# Copyright ByWater Solutions 2023
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
=head1 NAME
21
22
Koha::MetadataExtractor - Extract specific metadata from MARC::Record objects
23
24
=cut
25
26
use Modern::Perl;
27
28
use Koha::Exceptions;
29
30
=head1 API
31
32
=head2 Class methods
33
34
=head3 new
35
36
    my $extractor = Koha::MetadataExtractor::MARC::UNIMARC->new;
37
38
Constructor for the I<Koha::MetadataExtractor::MARC::UNIMARC> class.
39
40
=cut
41
42
sub new {
43
    my ($class) = @_;
44
    my $self = {};
45
46
    return
47
        bless $self,
48
        $class;
49
}
50
51
=head2 get_normalized_upc
52
53
    my $normalized_upc = $extractor->get_normalized_upc( $record );
54
55
Returns the normalized UPC for the passed I<$record>.
56
57
=cut
58
59
sub get_normalized_upc {
60
    my ( $self, $record ) = @_;
61
62
    Koha::Exceptions::MissingParameter->throw( parameter => 'record' )
63
        unless $record;
64
65
    Koha::Exceptions::WrongParameter->throw( name => 'record', type => ref($record) )
66
        unless ref($record) eq 'MARC::Record';
67
68
    my @fields = $record->field('072');
69
    foreach my $field (@fields) {
70
71
        my $upc = $field->subfield('a');
72
73
        ( my $normalized_upc ) = $upc =~ /([\d-]*[X]*)/;
74
        $normalized_upc =~ s/-//g;
75
76
        if ($normalized_upc) {
77
            return $normalized_upc;
78
        }
79
    }
80
}
81
82
=head1 AUTHOR
83
84
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
85
86
=cut
87
88
1;
89
90
__END__
(-)a/t/Koha/MetadataExtractor.t (+102 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2023 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 3;
23
use Test::Exception;
24
use Test::MockModule;
25
26
use MARC::Record;
27
28
use Koha::MetadataExtractor;
29
30
subtest 'new() tests' => sub {
31
32
    plan tests => 1;
33
34
    my $extractor = Koha::MetadataExtractor->new;
35
    is( ref($extractor), 'Koha::MetadataExtractor' );
36
};
37
38
subtest 'get_extractor() tests' => sub {
39
40
    plan tests => 8;
41
42
    my $extractor = Koha::MetadataExtractor->new;
43
44
    foreach my $schema (qw{ MARC21 UNIMARC }) {
45
        my $specific_extractor = $extractor->get_extractor( { schema => $schema } );
46
        is(
47
            ref($specific_extractor), "Koha::MetadataExtractor::MARC::$schema",
48
            "Returns the right extractor library for schema ($schema)"
49
        );
50
        ok( exists $extractor->{extractors}->{$schema}, "Extractor for $schema cached" );
51
    }
52
53
    throws_ok { $extractor->get_extractor }
54
    'Koha::Exceptions::MissingParameter',
55
        'Exception if no schema parameter';
56
57
    like(
58
        "$@", qr{A required parameter is missing' with parameter => schema},
59
        'Exception correctly details missing parameter'
60
    );
61
62
    throws_ok { $extractor->get_extractor( { schema => 'POTATO' } ) }
63
    'Koha::Exceptions::WrongParameter',
64
        'Exception if the passed schema is not supported';
65
66
    like(
67
        "$@", qr{'Parameter has wrong value or type' with name => schema, value => POTATO},
68
        'Exception correctly details incorrect parameter value'
69
    );
70
};
71
72
subtest 'get_normalized_upc() tests' => sub {
73
74
    plan tests => 6;
75
76
    my $extractor = Koha::MetadataExtractor->new;
77
78
    my $record = MARC::Record->new();
79
80
    my $mock_marc21 = Test::MockModule->new('Koha::MetadataExtractor::MARC::MARC21');
81
    $mock_marc21->mock( 'get_normalized_upc', sub { return 'MARC21' } );
82
83
    my $mock_unimarc = Test::MockModule->new('Koha::MetadataExtractor::MARC::UNIMARC');
84
    $mock_unimarc->mock( 'get_normalized_upc', sub { return 'UNIMARC' } );
85
86
    foreach my $schema (qw{ MARC21 UNIMARC }) {
87
        is(
88
            $extractor->get_normalized_upc( { record => $record, schema => $schema } ), $schema,
89
            "Library for handling $schema called"
90
        );
91
        ok( exists $extractor->{extractors}->{$schema}, "Extractor for $schema cached" );
92
    }
93
94
    throws_ok { $extractor->get_normalized_upc() }
95
    'Koha::Exceptions::MissingParameter',
96
        'Exception if no record parameter';
97
98
    like(
99
        "$@", qr{A required parameter is missing' with parameter => record},
100
        'Exception correctly details missing parameter'
101
    );
102
};
(-)a/t/Koha/MetadataExtractor/MARC/MARC21.t (+64 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2023 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 2;
23
use Test::Exception;
24
25
use MARC::Record;
26
27
use Koha::MetadataExtractor::MARC::MARC21;
28
29
subtest 'new() tests' => sub {
30
31
    plan tests => 1;
32
33
    my $extractor = Koha::MetadataExtractor::MARC::MARC21->new;
34
    is( ref($extractor), 'Koha::MetadataExtractor::MARC::MARC21' );
35
};
36
37
subtest 'get_normalized_upc() tests' => sub {
38
39
    plan tests => 6;
40
41
    my $extractor = Koha::MetadataExtractor::MARC::MARC21->new;
42
43
    my $record = MARC::Record->new();
44
    $record->append_fields( MARC::Field->new( '024', '1', ' ', a => "9-123345345X" ) );
45
46
    is( $extractor->get_normalized_upc($record), '9123345345X' );
47
48
    $record = MARC::Record->new();
49
    $record->append_fields( MARC::Field->new( '024', ' ', ' ', a => "9-123345345X" ) );
50
51
    is( $extractor->get_normalized_upc($record), undef );
52
53
    throws_ok { $extractor->get_normalized_upc() }
54
    'Koha::Exceptions::MissingParameter',
55
        'Exception if no parameter';
56
57
    like( "$@", qr{A required parameter is missing' with parameter => record} );
58
59
    throws_ok { $extractor->get_normalized_upc("Some string") }
60
    'Koha::Exceptions::WrongParameter',
61
        'Exception if no parameter';
62
63
    like( "$@", qr{Parameter has wrong value or type} );
64
};
(-)a/t/Koha/MetadataExtractor/MARC/UNIMARC.t (-1 / +64 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2023 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 2;
23
use Test::Exception;
24
25
use MARC::Record;
26
27
use Koha::MetadataExtractor::MARC::UNIMARC;
28
29
subtest 'new() tests' => sub {
30
31
    plan tests => 1;
32
33
    my $extractor = Koha::MetadataExtractor::MARC::UNIMARC->new;
34
    is( ref($extractor), 'Koha::MetadataExtractor::MARC::UNIMARC' );
35
};
36
37
subtest 'get_normalized_upc() tests' => sub {
38
39
    plan tests => 6;
40
41
    my $extractor = Koha::MetadataExtractor::MARC::UNIMARC->new;
42
43
    my $record = MARC::Record->new();
44
    $record->append_fields( MARC::Field->new( '072', '1', ' ', a => "9-123345345X" ) );
45
46
    is( $extractor->get_normalized_upc($record), '9123345345X' );
47
48
    $record = MARC::Record->new();
49
    $record->append_fields( MARC::Field->new( '072', ' ', ' ', a => "9-123345345X" ) );
50
51
    is( $extractor->get_normalized_upc($record), '9123345345X', 'Indicator has no effect' );
52
53
    throws_ok { $extractor->get_normalized_upc() }
54
    'Koha::Exceptions::MissingParameter',
55
        'Exception if no parameter';
56
57
    like( "$@", qr{A required parameter is missing' with parameter => record} );
58
59
    throws_ok { $extractor->get_normalized_upc("Some string") }
60
    'Koha::Exceptions::WrongParameter',
61
        'Exception if no parameter';
62
63
    like( "$@", qr{Parameter has wrong value or type} );
64
};

Return to bug 34828