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

(-)a/Koha/MetadataExtractor.pm (+109 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
use List::MoreUtils qw( any );
30
31
=head1 API
32
33
=head2 Class methods
34
35
=head3 new
36
37
    my $extractor = Koha::MetadataExtractor->new;
38
39
Constructor for the I<Koha::MetadataExtractor> class.
40
41
=cut
42
43
sub new {
44
    my ($class) = @_;
45
    my $self = { extractors => {} };
46
47
    return
48
        bless $self,
49
        $class;
50
}
51
52
=head2 get_normalized_upc
53
54
    my $normalized_upc = $extractor->get_normalized_upc( { record => $record, schema => $schema } );
55
56
Returns the normalized UPC for the passed I<$record>.
57
58
=cut
59
60
sub get_normalized_upc {
61
    my ( $self, $params ) = @_;
62
63
    Koha::Exceptions::MissingParameter->throw( parameter => 'record' )
64
        unless $params->{record};
65
66
    return $self->get_extractor( { schema => $params->{schema} } )->get_normalized_upc( $params->{record} );
67
}
68
69
=head2 Internal methods
70
71
=head3 get_extractor
72
73
    my $extractor = $self->get_extractor( { schema => $schema } );
74
75
Returns the cached extractor for the specified I<$schema>.
76
77
=cut
78
79
sub get_extractor {
80
    my ( $self, $params ) = @_;
81
82
    my $schema = $params->{schema};
83
84
    Koha::Exceptions::MissingParameter->throw( parameter => 'schema' )
85
        unless $schema;
86
87
    my $valid_schemas = { 'MARC21' => 1, 'UNIMARC' => 1 };
88
89
    Koha::Exceptions::WrongParameter->throw( name => 'schema', value => $schema )
90
        unless $valid_schemas->{$schema};
91
92
    unless ( $self->{extractors}->{$schema} ) {
93
        my $extractor_class = "Koha::MetadataExtractor::MARC::$schema";
94
        eval "require $extractor_class";
95
        $self->{extractors}->{$schema} = $extractor_class->new;
96
    }
97
98
    return $self->{extractors}->{$schema};
99
}
100
101
=head1 AUTHOR
102
103
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
104
105
=cut
106
107
1;
108
109
__END__
(-)a/Koha/MetadataExtractor/MARC/MARC21.pm (+87 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
use URI::Escape qw( uri_escape_utf8 );
31
32
=head1 API
33
34
=head2 Class methods
35
36
=head3 new
37
38
    my $extractor = Koha::MetadataExtractor::MARC::MARC21->new;
39
40
Constructor for the I<Koha::MetadataExtractor> class.
41
42
=cut
43
44
sub new {
45
    my ($class) = @_;
46
    my $self = {};
47
48
    return
49
        bless $self,
50
        $class;
51
}
52
53
=head2 get_normalized_upc
54
55
    my $normalized_upc = $extractor->get_normalized_upc( $record );
56
57
Returns a stringthe COinS (a span) which can be included in a biblio record
58
59
=cut
60
61
sub get_normalized_upc {
62
    my ( $self, $record ) = @_;
63
64
    my @fields = $record->field('024');
65
    foreach my $field (@fields) {
66
67
        my $indicator = $field->indicator(1);
68
        my $upc       = $field->subfield('a');
69
70
        ( my $normalized_upc ) = $upc =~ /([\d-]*[X]*)/;
71
        $normalized_upc =~ s/-//g;
72
73
        if ( $normalized_upc && $indicator == 1 ) {
74
            return $normalized_upc;
75
        }
76
    }
77
}
78
79
=head1 AUTHOR
80
81
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
82
83
=cut
84
85
1;
86
87
__END__
(-)a/Koha/MetadataExtractor/MARC/UNIMARC.pm (-1 / +86 lines)
Line 0 Link Here
0
- 
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
use URI::Escape qw( uri_escape_utf8 );
31
32
=head1 API
33
34
=head2 Class methods
35
36
=head3 new
37
38
    my $extractor = Koha::MetadataExtractor::MARC::UNIMARC->new;
39
40
Constructor for the I<Koha::MetadataExtractor::MARC::UNIMARC> class.
41
42
=cut
43
44
sub new {
45
    my ($class) = @_;
46
    my $self = {};
47
48
    return
49
        bless $self,
50
        $class;
51
}
52
53
=head2 get_normalized_upc
54
55
    my $normalized_upc = $extractor->get_normalized_upc( $record );
56
57
Returns the normalized UPC for the passed I<$record>.
58
59
=cut
60
61
sub get_normalized_upc {
62
    my ( $self, $record ) = @_;
63
64
    my @fields = $record->field('072');
65
    foreach my $field (@fields) {
66
67
        my $upc = $field->subfield('a');
68
69
        ( my $normalized_upc ) = $upc =~ /([\d-]*[X]*)/;
70
        $normalized_upc =~ s/-//g;
71
72
        if ($normalized_upc) {
73
            return $normalized_upc;
74
        }
75
    }
76
}
77
78
=head1 AUTHOR
79
80
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
81
82
=cut
83
84
1;
85
86
__END__

Return to bug 33955