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

(-)a/t/Koha/Plugin/MarcFieldValues.pm (+86 lines)
Line 0 Link Here
1
package Koha::Plugin::MarcFieldValues;
2
3
use Modern::Perl;
4
use MARC::Field;
5
use MARC::Record;
6
7
use base qw(Koha::Plugins::Base);
8
9
our $VERSION = 1.00;
10
our $metadata = {
11
    name            => 'MarcFieldValues',
12
    author          => 'M. de Rooy',
13
    class           => 'Koha::Plugin::MarcFieldValues',
14
    description     => 'Convert MARC fields from plain text',
15
    date_authored   => '2017-08-08',
16
    date_updated    => '2017-08-08',
17
    minimum_version => '16.11',
18
    maximum_version => undef,
19
    version         => $VERSION,
20
    input_format    => 'MARC field/value pairs in plain text',
21
};
22
23
=head1 METHODS
24
25
=head2 new
26
27
    Create new object
28
29
=cut
30
31
sub new {
32
    my ( $class, $args ) = @_;
33
    $args->{'metadata'} = $metadata;
34
    my $self = $class->SUPER::new($args);
35
    return $self;
36
}
37
38
=head2 to_marc
39
40
    Create string of MARC blobs from plain text lines in the form:
41
        field [,ind1|,ind2|,subcode] = value
42
    Example:
43
        003 = OrgCode
44
        100,a = Author
45
        245,ind2 = 0
46
        245,a = Title
47
48
=cut
49
50
sub to_marc {
51
    my ( $self, $args ) = @_;
52
    # $args->{data} contains text to convert to MARC
53
    my $retval = '';
54
    my @records = split /\r?\n\r?\n/, $args->{data};
55
    foreach my $rec ( @records ) {
56
        my @lines = split /\r?\n/, $rec;
57
        my $marc = MARC::Record->new;
58
        my $inds = {};
59
        my $fldcount = 0;
60
        foreach my $line ( @lines ) {
61
            # each line is of the form field [,ind1|,ind2|,subcode] = value
62
            my @temp = split /\s*=\s*/, $line, 2;
63
            next if @temp < 2;
64
            $temp[0] =~ s/^\s*//;
65
            $temp[1] =~ s/\s*$//;
66
            my $value = $temp[1];
67
            @temp = split /\s*,\s*/, $temp[0];
68
            if( @temp > 1 && $temp[1] =~ /ind[12]/ ) {
69
                $inds->{$temp[0]}->{$temp[1]} = substr($value, 0, 1);
70
                next;
71
            }
72
            $fldcount++;
73
            $marc->append_fields( MARC::Field->new(
74
                $temp[0],
75
                $temp[0] < 10
76
                    ? ()
77
                    : ( ( $inds->{$temp[0]} ? $inds->{$temp[0]}->{ind1} // '' : '', $inds->{$temp[0]} ? $inds->{$temp[0]}->{ind2} // '' : ''), substr( $temp[1], 0, 1 ) ),
78
                $value,
79
            ));
80
        }
81
        $retval .= $marc->as_usmarc if $fldcount;
82
    }
83
    return $retval;
84
}
85
86
1;
(-)a/t/db_dependent/ImportBatch.t (-4 / +35 lines)
Lines 1-13 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use Test::More tests => 13;
4
use Test::More tests => 14;
5
use File::Basename;
6
use File::Temp qw/tempfile/;
5
7
6
use Koha::Database;
8
use t::lib::Mocks;
7
use t::lib::TestBuilder;
9
use t::lib::TestBuilder;
8
10
11
use Koha::Database;
12
use Koha::Plugins;
13
9
BEGIN {
14
BEGIN {
10
        use_ok('C4::ImportBatch');
15
    use_ok('C4::ImportBatch');
11
}
16
}
12
17
13
# Start transaction
18
# Start transaction
Lines 162-165 C4::ImportBatch::DeleteBatch( $id_import_batch3 ); Link Here
162
my $batch3_results = $dbh->do('SELECT * FROM import_batches WHERE import_batch_id = "$id_import_batch3"');
167
my $batch3_results = $dbh->do('SELECT * FROM import_batches WHERE import_batch_id = "$id_import_batch3"');
163
is( $batch3_results, "0E0", "Batch 3 has been deleted");
168
is( $batch3_results, "0E0", "Batch 3 has been deleted");
164
169
170
subtest "RecordsFromMarcPlugin" => sub {
171
    plan tests => 5;
172
173
    # Create a test file
174
    my ( $fh, $name ) = tempfile();
175
    print $fh q|
176
003 = NLAmRIJ
177
100,a = Author
178
245,ind2 = 0
179
245,a = Silence in the library
180
500 , a= Some note
181
182
100,a = Another
183
245,a = Noise in the library|;
184
    close $fh;
185
    t::lib::Mocks::mock_config( 'pluginsdir', dirname(__FILE__) . '/..' );
186
    my ( $plugin ) = Koha::Plugins->new->GetPlugins({ metadata => { name => 'MarcFieldValues' } });
187
    isnt( $plugin, undef, "Plugin found" );
188
    my $records = C4::ImportBatch::RecordsFromMarcPlugin( $name, ref $plugin, 'UTF-8' );
189
    is( @$records, 2, 'Two results returned' );
190
    is( ref $records->[0], 'MARC::Record', 'Returned MARC::Record object' );
191
    is( $records->[0]->subfield('245', 'a'), 'Silence in the library',
192
        'Checked one field in first record' );
193
    is( $records->[1]->subfield('100', 'a'), 'Another',
194
        'Checked one field in second record' );
195
};
196
165
$schema->storage->txn_rollback;
197
$schema->storage->txn_rollback;
166
- 

Return to bug 19049