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

(-)a/Koha/Authority.pm (-4 / +7 lines)
Lines 38-46 use MARC::Record; Link Here
38
use MARC::File::XML;
38
use MARC::File::XML;
39
use C4::Charset;
39
use C4::Charset;
40
40
41
use base qw(Koha::Record);
41
use base qw(Koha::MetadataRecord);
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/MetadataRecord.pm (+59 lines)
Line 0 Link Here
1
package Koha::MetadataRecord;
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
=head1 NAME
21
22
Koha::MetadataRecord - base class for metadata records
23
24
=head1 SYNOPSIS
25
26
    my $record = new Koha::MetadataRecord({ 'record' => $marcrecord });
27
28
=head1 DESCRIPTION
29
30
Object-oriented class that encapsulates all metadata (i.e. bibliographic
31
and authority) records in Koha.
32
33
=cut
34
35
use strict;
36
use warnings;
37
use C4::Context;
38
use Koha::Util::MARC;
39
40
use base qw(Class::Accessor);
41
42
__PACKAGE__->mk_accessors(qw( record schema ));
43
44
45
=head2 createMergeHash
46
47
Create a hash for use when merging records. At the moment the only
48
metadata schema supported is MARC.
49
50
=cut
51
52
sub createMergeHash {
53
    my ($self, $tagslib) = @_;
54
    if ($self->schema =~ m/marc/) {
55
        return Koha::Util::MARC::createMergeHash($self->record, $tagslib);
56
    }
57
}
58
59
1;
(-)a/Koha/Record.pm (-115 lines)
Lines 1-115 Link Here
1
package Koha::Record;
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
=head1 NAME
21
22
Koha::Record - base class for MARC records
23
24
=head1 SYNOPSIS
25
26
    my $record = new Koha::Record({ 'record' => $marcrecord });
27
28
=head1 DESCRIPTION
29
30
Object-oriented class that encapsulates all records in Koha.
31
32
=cut
33
34
use strict;
35
use warnings;
36
use C4::Context;
37
use MARC::Record;
38
39
use base qw(Class::Accessor);
40
41
__PACKAGE__->mk_accessors(qw( record marcflavour ));
42
43
44
=head2 createMarcHash
45
46
Create a MARC hash for use when merging records.
47
48
=cut
49
50
sub createMarcHash {
51
    my ($self, $tagslib) = @_;
52
    my $record = $self->record;
53
    my @array;
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
    }
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
}
114
115
1;
(-)a/Koha/Util/MARC.pm (+110 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
38
    return unless $record;
39
40
    my @array;
41
    my @fields = $record->fields();
42
43
    foreach my $field (@fields) {
44
        my $fieldtag = $field->tag();
45
        if ( $fieldtag < 10 ) {
46
            if ( !defined($tagslib)
47
                || $tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0 )
48
            {
49
                push @array,
50
                  {
51
                    field => [
52
                        {
53
                            tag   => $fieldtag,
54
                            key   => _createKey(),
55
                            value => $field->data(),
56
                        }
57
                    ]
58
                  };
59
            }
60
        }
61
        else {
62
            my @subfields = $field->subfields();
63
            my @subfield_array;
64
            foreach my $subfield (@subfields) {
65
                if ( !defined($tagslib)
66
                    || $tagslib->{$fieldtag}->{ @$subfield[0] }->{'tab'} >= 0 )
67
                {
68
                    push @subfield_array,
69
                      {
70
                        subtag => @$subfield[0],
71
                        subkey => _createKey(),
72
                        value  => @$subfield[1],
73
                      };
74
                }
75
76
            }
77
78
            if ( ( !defined($tagslib) || $tagslib->{$fieldtag}->{'tab'} >= 0 )
79
                && @subfield_array )
80
            {
81
                push @array,
82
                  {
83
                    field => [
84
                        {
85
                            tag        => $fieldtag,
86
                            key        => _createKey(),
87
                            indicator1 => $field->indicator(1),
88
                            indicator2 => $field->indicator(2),
89
                            subfield   => [@subfield_array],
90
                        }
91
                    ]
92
                  };
93
            }
94
95
        }
96
    }
97
    return [@array];
98
}
99
100
=head2 _createKey
101
102
Create a random value to set it into the input name
103
104
=cut
105
106
sub _createKey {
107
    return int(rand(1000000));
108
}
109
110
1;
(-)a/cataloguing/merge.pl (-5 / +5 lines)
Lines 30-36 use C4::Serials; Link Here
30
use C4::Koha;
30
use C4::Koha;
31
use C4::Reserves qw/MergeHolds/;
31
use C4::Reserves qw/MergeHolds/;
32
use C4::Acquisition qw/ModOrder GetOrdersByBiblionumber/;
32
use C4::Acquisition qw/ModOrder GetOrdersByBiblionumber/;
33
use Koha::Record;
33
use Koha::MetadataRecord;
34
34
35
my $input = new CGI;
35
my $input = new CGI;
36
my @biblionumber = $input->param('biblionumber');
36
my @biblionumber = $input->param('biblionumber');
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::MetadataRecord({ 'record' => GetMarcBiblio($mergereference), 'schema' => lc C4::Context->preference('marcflavour') });
174
            my $recordObj2 = new Koha::Record({ 'record' => GetMarcBiblio($notreference) });
174
            my $recordObj2 = new Koha::MetadataRecord({ '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_MetadataRecord.t (+98 lines)
Line 0 Link Here
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
use strict;
21
use warnings;
22
23
use Test::More tests => 4;
24
25
BEGIN {
26
        use_ok('Koha::MetadataRecord');
27
}
28
29
my $marcrecord = MARC::Record->new;
30
31
$marcrecord->add_fields(
32
        [ '001', '1234' ],
33
        [ '150', ' ', ' ', a => 'Cooking' ],
34
        [ '450', ' ', ' ', a => 'Cookery', z => 'Instructional manuals' ],
35
        );
36
my $record = Koha::MetadataRecord->new({ 'record' => $marcrecord, 'schema' => 'marc21' });
37
38
is(ref($record), 'Koha::MetadataRecord', 'Created valid Koha::MetadataRecord object');
39
40
my $samplehash = [
41
    {
42
        'field' => [
43
            {
44
                'value' => '1234',
45
                'tag'   => '001',
46
            }
47
        ]
48
    },
49
    {
50
        'field' => [
51
            {
52
                'subfield' => [
53
                    {
54
                        'value'  => 'Cooking',
55
                        'subtag' => 'a'
56
                    }
57
                ],
58
                'indicator2' => ' ',
59
                'tag'        => 150,
60
                'indicator1' => ' ',
61
            }
62
        ]
63
    },
64
    {
65
        'field' => [
66
            {
67
                'subfield' => [
68
                    {
69
                        'value'  => 'Cookery',
70
                        'subtag' => 'a'
71
                    },
72
                    {
73
                        'value' => 'Instructional manuals',
74
                        'subtag' => 'z'
75
                    }
76
                ],
77
                'indicator2' => ' ',
78
                'tag'        => 450,
79
                'indicator1' => ' ',
80
            }
81
        ]
82
    }
83
];
84
85
my $hash = $record->createMergeHash();
86
my %fieldkeys;
87
foreach my $field (@$hash) {
88
    $fieldkeys{delete $field->{'field'}->[0]->{'key'}}++;
89
    if (defined $field->{'field'}->[0]->{'subfield'}) {
90
        foreach my $subfield (@{$field->{'field'}->[0]->{'subfield'}}) {
91
            $fieldkeys{delete $subfield->{'subkey'}}++;
92
        }
93
    }
94
}
95
96
is_deeply($hash, $samplehash, 'Generated hash correctly');
97
my $dupkeys = grep { $_ > 1 } values %fieldkeys;
98
is($dupkeys, 0, 'No duplicate keys');
(-)a/t/Koha_Record.t (-9 / +6 lines)
Lines 17-29 Link Here
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
19
20
# Note that at present this test is almost identical to the one testing
21
# the encapsulating method in Koha::MetadataRecord.
22
20
use strict;
23
use strict;
21
use warnings;
24
use warnings;
22
25
23
use Test::More tests => 4;
26
use Test::More tests => 3;
24
27
25
BEGIN {
28
BEGIN {
26
        use_ok('Koha::Record');
29
        use_ok('Koha::Util::MARC');
27
}
30
}
28
31
29
my $marcrecord = MARC::Record->new;
32
my $marcrecord = MARC::Record->new;
Lines 33-42 $marcrecord->add_fields( Link Here
33
        [ '150', ' ', ' ', a => 'Cooking' ],
36
        [ '150', ' ', ' ', a => 'Cooking' ],
34
        [ '450', ' ', ' ', a => 'Cookery', z => 'Instructional manuals' ],
37
        [ '450', ' ', ' ', a => 'Cookery', z => 'Instructional manuals' ],
35
        );
38
        );
36
my $record = Koha::Record->new({ 'record' => $marcrecord });
37
38
is(ref($record), 'Koha::Record', 'Created valid Koha::Record object');
39
40
my $samplehash = [
39
my $samplehash = [
41
    {
40
    {
42
        'field' => [
41
        'field' => [
Lines 82-90 my $samplehash = [ Link Here
82
    }
81
    }
83
];
82
];
84
83
85
my $hash = $record->createMarcHash();
84
my $hash = Koha::Util::MARC::createMergeHash($marcrecord);
86
my %fieldkeys;
85
my %fieldkeys;
87
require Data::Dumper;
88
foreach my $field (@$hash) {
86
foreach my $field (@$hash) {
89
    $fieldkeys{delete $field->{'field'}->[0]->{'key'}}++;
87
    $fieldkeys{delete $field->{'field'}->[0]->{'key'}}++;
90
    if (defined $field->{'field'}->[0]->{'subfield'}) {
88
    if (defined $field->{'field'}->[0]->{'subfield'}) {
91
- 

Return to bug 9755