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

(-)a/Koha/Authority.pm (-3 / +6 lines)
Lines 40-46 use C4::Charset; Link Here
40
40
41
use base qw(Koha::Record);
41
use base qw(Koha::Record);
42
42
43
__PACKAGE__->mk_accessors(qw( authid authtype marcflavour ));
43
__PACKAGE__->mk_accessors(qw( authid authtype ));
44
44
45
=head2 new
45
=head2 new
46
46
Lines 65-76 sub new { Link Here
65
    my $auth = Koha::Authority->get_from_authid($authid);
65
    my $auth = Koha::Authority->get_from_authid($authid);
66
66
67
Create the Koha::Authority object associated with the provided authid.
67
Create the Koha::Authority object associated with the provided authid.
68
Note that this routine currently retrieves a MARC record because
69
authorities in Koha are MARC records by definition. This is an
70
unfortunate but unavoidable fact.
68
71
69
=cut
72
=cut
70
sub get_from_authid {
73
sub get_from_authid {
71
    my $class = shift;
74
    my $class = shift;
72
    my $authid = shift;
75
    my $authid = shift;
73
    my $marcflavour = C4::Context->preference("marcflavour");
76
    my $marcflavour = lc C4::Context->preference("marcflavour");
74
77
75
    my $dbh=C4::Context->dbh;
78
    my $dbh=C4::Context->dbh;
76
    my $sth=$dbh->prepare("select authtypecode, marcxml from auth_header where authid=?");
79
    my $sth=$dbh->prepare("select authtypecode, marcxml from auth_header where authid=?");
Lines 82-89 sub get_from_authid { Link Here
82
    $record->encoding('UTF-8');
85
    $record->encoding('UTF-8');
83
86
84
    my $self = $class->SUPER::new( { authid => $authid,
87
    my $self = $class->SUPER::new( { authid => $authid,
85
                                     marcflavour => $marcflavour,
86
                                     authtype => $authtypecode,
88
                                     authtype => $authtypecode,
89
                                     schema => $marcflavour,
87
                                     record => $record });
90
                                     record => $record });
88
91
89
    bless $self, $class;
92
    bless $self, $class;
(-)a/Koha/Record.pm (-66 / +9 lines)
Lines 19-25 package Koha::Record; Link Here
19
19
20
=head1 NAME
20
=head1 NAME
21
21
22
Koha::Record - base class for MARC records
22
Koha::Record - base class for records
23
23
24
=head1 SYNOPSIS
24
=head1 SYNOPSIS
25
25
Lines 34-115 Object-oriented class that encapsulates all records in Koha. Link Here
34
use strict;
34
use strict;
35
use warnings;
35
use warnings;
36
use C4::Context;
36
use C4::Context;
37
use MARC::Record;
37
use Koha::Util::MARC;
38
38
39
use base qw(Class::Accessor);
39
use base qw(Class::Accessor);
40
40
41
__PACKAGE__->mk_accessors(qw( record marcflavour ));
41
__PACKAGE__->mk_accessors(qw( record schema ));
42
42
43
43
44
=head2 createMarcHash
44
=head2 createMergeHash
45
45
46
Create a MARC hash for use when merging records.
46
Create a hash for use when merging records. At the moment the only
47
metadata schema supported is MARC.
47
48
48
=cut
49
=cut
49
50
50
sub createMarcHash {
51
sub createMergeHash {
51
    my ($self, $tagslib) = @_;
52
    my ($self, $tagslib) = @_;
52
    my $record = $self->record;
53
    if ($self->schema =~ m/marc/) {
53
    my @array;
54
        return Koha::Util::MARC::createMergeHash($self->record, $tagslib);
54
    my @fields = $record->fields();
55
56
57
    foreach my $field (@fields) {
58
    my $fieldtag = $field->tag();
59
    if ($fieldtag < 10) {
60
        if (!defined($tagslib) || $tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0) {
61
        push @array, {
62
            field => [
63
                    {
64
                    tag => $fieldtag,
65
                    key => _createKey(),
66
                    value => $field->data(),
67
                    }
68
                ]
69
                };
70
        }
71
    } else {
72
        my @subfields = $field->subfields();
73
        my @subfield_array;
74
        foreach my $subfield (@subfields) {
75
        if (!defined($tagslib) || $tagslib->{$fieldtag}->{@$subfield[0]}->{'tab'} >= 0) {
76
            push @subfield_array, {
77
                                    subtag => @$subfield[0],
78
                                    subkey => _createKey(),
79
                                    value => @$subfield[1],
80
                                  };
81
        }
82
83
        }
84
85
        if ((!defined($tagslib) || $tagslib->{$fieldtag}->{'tab'} >= 0) && $fieldtag ne '995' && $fieldtag ne '999') {
86
        push @array, {
87
            field => [
88
                {
89
                    tag => $fieldtag,
90
                    key => _createKey(),
91
                    indicator1 => $field->indicator(1),
92
                    indicator2 => $field->indicator(2),
93
                    subfield   => [@subfield_array],
94
                }
95
            ]
96
            };
97
        }
98
99
    }
100
    }
55
    }
101
    return [@array];
102
103
}
104
105
=head2 _createKey
106
107
Create a random value to set it into the input name
108
109
=cut
110
111
sub _createKey {
112
    return int(rand(1000000));
113
}
56
}
114
57
115
1;
58
1;
(-)a/Koha/Util/MARC.pm (+98 lines)
Line 0 Link Here
1
package Koha::Util::MARC;
2
3
# Copyright 2013 C & P Bibliography Services
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
use MARC::Record;
22
23
=head1 NAME
24
25
Koha::Util::MARC - utility class with routines for working with MARC records
26
27
=head1 METHODS
28
29
=head2 createMergeHash
30
31
Create a hash to use when merging MARC records
32
33
=cut
34
35
sub createMergeHash {
36
    my ($record, $tagslib) = @_;
37
    my @array;
38
    my @fields = $record->fields();
39
40
41
    foreach my $field (@fields) {
42
    my $fieldtag = $field->tag();
43
    if ($fieldtag < 10) {
44
        if (!defined($tagslib) || $tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0) {
45
        push @array, {
46
            field => [
47
                    {
48
                    tag => $fieldtag,
49
                    key => _createKey(),
50
                    value => $field->data(),
51
                    }
52
                ]
53
                };
54
        }
55
    } else {
56
        my @subfields = $field->subfields();
57
        my @subfield_array;
58
        foreach my $subfield (@subfields) {
59
        if (!defined($tagslib) || $tagslib->{$fieldtag}->{@$subfield[0]}->{'tab'} >= 0) {
60
            push @subfield_array, {
61
                                    subtag => @$subfield[0],
62
                                    subkey => _createKey(),
63
                                    value => @$subfield[1],
64
                                  };
65
        }
66
67
        }
68
69
        if ((!defined($tagslib) || $tagslib->{$fieldtag}->{'tab'} >= 0) && @subfield_array) {
70
        push @array, {
71
            field => [
72
                {
73
                    tag => $fieldtag,
74
                    key => _createKey(),
75
                    indicator1 => $field->indicator(1),
76
                    indicator2 => $field->indicator(2),
77
                    subfield   => [@subfield_array],
78
                }
79
            ]
80
            };
81
        }
82
83
    }
84
    }
85
    return [@array];
86
}
87
88
=head2 _createKey
89
90
Create a random value to set it into the input name
91
92
=cut
93
94
sub _createKey {
95
    return int(rand(1000000));
96
}
97
98
1;
(-)a/cataloguing/merge.pl (-4 / +4 lines)
Lines 170-180 if ($merge) { Link Here
170
170
171
            # Creating a loop for display
171
            # Creating a loop for display
172
172
173
            my $recordObj1 = new Koha::Record({ 'record' => GetMarcBiblio($mergereference) });
173
            my $recordObj1 = new Koha::Record({ 'record' => GetMarcBiblio($mergereference), 'schema' => lc C4::Context->preference('marcflavour') });
174
            my $recordObj2 = new Koha::Record({ 'record' => GetMarcBiblio($notreference) });
174
            my $recordObj2 = new Koha::Record({ 'record' => GetMarcBiblio($notreference), 'schema' => lc C4::Context->preference('marcflavour') });
175
175
176
            my @record1 = $recordObj1->createMarcHash($tagslib);
176
            my @record1 = $recordObj1->createMergeHash($tagslib);
177
            my @record2 = $recordObj2->createMarcHash($tagslib);
177
            my @record2 = $recordObj2->createMergeHash($tagslib);
178
178
179
            # Parameters
179
            # Parameters
180
            $template->param(
180
            $template->param(
(-)a/t/Koha_Record.t (-3 / +2 lines)
Lines 33-39 $marcrecord->add_fields( Link Here
33
        [ '150', ' ', ' ', a => 'Cooking' ],
33
        [ '150', ' ', ' ', a => 'Cooking' ],
34
        [ '450', ' ', ' ', a => 'Cookery', z => 'Instructional manuals' ],
34
        [ '450', ' ', ' ', a => 'Cookery', z => 'Instructional manuals' ],
35
        );
35
        );
36
my $record = Koha::Record->new({ 'record' => $marcrecord });
36
my $record = Koha::Record->new({ 'record' => $marcrecord, 'schema' => 'marc21' });
37
37
38
is(ref($record), 'Koha::Record', 'Created valid Koha::Record object');
38
is(ref($record), 'Koha::Record', 'Created valid Koha::Record object');
39
39
Lines 82-90 my $samplehash = [ Link Here
82
    }
82
    }
83
];
83
];
84
84
85
my $hash = $record->createMarcHash();
85
my $hash = $record->createMergeHash();
86
my %fieldkeys;
86
my %fieldkeys;
87
require Data::Dumper;
88
foreach my $field (@$hash) {
87
foreach my $field (@$hash) {
89
    $fieldkeys{delete $field->{'field'}->[0]->{'key'}}++;
88
    $fieldkeys{delete $field->{'field'}->[0]->{'key'}}++;
90
    if (defined $field->{'field'}->[0]->{'subfield'}) {
89
    if (defined $field->{'field'}->[0]->{'subfield'}) {
(-)a/t/Koha_Util_MARC.t (-1 / +98 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2013 C & P Bibliography Services
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 2 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
# Note that at present this test is almost identical to the one testing
21
# the encapsulating method in Koha::Record.
22
23
use strict;
24
use warnings;
25
26
use Test::More tests => 3;
27
28
BEGIN {
29
        use_ok('Koha::Util::MARC');
30
}
31
32
my $marcrecord = MARC::Record->new;
33
34
$marcrecord->add_fields(
35
        [ '001', '1234' ],
36
        [ '150', ' ', ' ', a => 'Cooking' ],
37
        [ '450', ' ', ' ', a => 'Cookery', z => 'Instructional manuals' ],
38
        );
39
my $samplehash = [
40
    {
41
        'field' => [
42
            {
43
                'value' => '1234',
44
                'tag'   => '001',
45
            }
46
        ]
47
    },
48
    {
49
        'field' => [
50
            {
51
                'subfield' => [
52
                    {
53
                        'value'  => 'Cooking',
54
                        'subtag' => 'a'
55
                    }
56
                ],
57
                'indicator2' => ' ',
58
                'tag'        => 150,
59
                'indicator1' => ' ',
60
            }
61
        ]
62
    },
63
    {
64
        'field' => [
65
            {
66
                'subfield' => [
67
                    {
68
                        'value'  => 'Cookery',
69
                        'subtag' => 'a'
70
                    },
71
                    {
72
                        'value' => 'Instructional manuals',
73
                        'subtag' => 'z'
74
                    }
75
                ],
76
                'indicator2' => ' ',
77
                'tag'        => 450,
78
                'indicator1' => ' ',
79
            }
80
        ]
81
    }
82
];
83
84
my $hash = Koha::Util::MARC::createMergeHash($marcrecord);
85
my %fieldkeys;
86
foreach my $field (@$hash) {
87
    $fieldkeys{delete $field->{'field'}->[0]->{'key'}}++;
88
    if (defined $field->{'field'}->[0]->{'subfield'}) {
89
        foreach my $subfield (@{$field->{'field'}->[0]->{'subfield'}}) {
90
            $fieldkeys{delete $subfield->{'subkey'}}++;
91
        }
92
    }
93
}
94
95
is_deeply($hash, $samplehash, 'Generated hash correctly');
96
my $dupkeys = grep { $_ > 1 } values %fieldkeys;
97
is($dupkeys, 0, 'No duplicate keys');
98

Return to bug 9755