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

(-)a/misc/migration_tools/switch_marc21_series_info.pl (-42 / +88 lines)
Lines 17-28 use C4::Context; Link Here
17
use Getopt::Long;
17
use Getopt::Long;
18
18
19
my $commit;
19
my $commit;
20
my $add_links;
20
my $update_frameworks;
21
my $update_frameworks;
21
my $show_help;
22
my $show_help;
22
my $verbose;
23
my $verbose;
23
my $result = GetOptions(
24
my $result = GetOptions(
24
    'c'      => \$commit,
25
    'c'      => \$commit,
25
    'm'      => \$update_frameworks,
26
    'l'      => \$add_links,
27
    'f'      => \$update_frameworks,
26
    'h|help' => \$show_help,
28
    'h|help' => \$show_help,
27
    'v'      => \$verbose,
29
    'v'      => \$verbose,
28
    );
30
    );
Lines 53-135 unless ( $commit ) { Link Here
53
    else {
55
    else {
54
        print "There appears to be no series information to change\n";
56
        print "There appears to be no series information to change\n";
55
    }
57
    }
58
    print "Please run this again with the '-c' option to change the records\n";
56
    exit 0;
59
    exit 0;
57
}
60
}
58
61
59
print "Changing $num_records MARC records...\n";
62
print "Changing $num_records MARC records...\n";
60
63
64
#  MARC21 specific
65
my %fields = (
66
    '440' => {
67
        'a' => 'title',
68
        'n' => 'number',
69
        'p' => 'part',
70
        'v' => 'volume',
71
        'x' => 'issn',
72
        '6' => 'link',
73
        '8' => 'ln',
74
        'w' => 'control',
75
        '0' => 'auth',
76
    },
77
    '490' => {
78
        'a' => 'title',
79
        'v' => 'volume',
80
        'x' => 'issn',
81
        '6' => 'link',
82
        '8' => 'ln',
83
    },
84
    );
85
61
$bibs_sth->execute();
86
$bibs_sth->execute();
62
while ( my ( $biblionumber ) = $bibs_sth->fetchrow ) {
87
while ( my ( $biblionumber ) = $bibs_sth->fetchrow ) {
63
    my $framework = GetFrameworkCode( $biblionumber ) || '';
88
    my $framework = GetFrameworkCode( $biblionumber ) || '';
64
    my ( @newfields );
89
    my ( @newfields );
65
90
66
    #  MARC21 specific
67
    my ( $series1_t, $series1_f ) = ( '440', 'a' );
68
    my ( $volume1_t, $volume1_f ) = ( '440', 'v' );
69
    my ( $number1_t, $number1_f ) = ( '440', 'n' );
70
71
    my ( $series2_t, $series2_f ) = ( '490', 'a' );
72
    my ( $volume2_t, $volume2_f ) = ( '490', 'v' );
73
74
    # Get biblio marc
91
    # Get biblio marc
75
    my $biblio = GetMarcBiblio( $biblionumber );
92
    my $biblio = GetMarcBiblio( $biblionumber );
76
93
77
    foreach my $field ( $biblio->field( $series1_t ) ) {
94
    foreach my $field ( $biblio->field( '440' ) ) {
78
        my @newsubfields;
95
        my @newsubfields;
79
        my @series1 = $field->subfield( $series1_f );
96
        my @linksubfields;
80
        my @volume1 = $field->subfield( $volume1_f );
97
        my $has_links = '0';
81
        my @number1 = $field->subfield( $number1_f );
98
        foreach my $subfield ( sort keys %{ $fields{'440'} } ) {
82
        my $i = 0;
99
            my @values = $field->subfield( $subfield );
83
        foreach my $num ( @number1 ) {
100
84
            $volume1[$i] .= " " if ( $volume1[$i] );
101
            if ( $add_links && @values ) {
85
            $volume1[$i++] .= $num if ( $num );
102
                if ( $subfield eq 'w' || $subfield eq '0' ) {
86
        }
103
                    $has_links = '1';
104
                }
105
                foreach my $v ( @values ) {
106
                    push @linksubfields, ( $subfield, $v );
107
                }
108
            }
87
109
88
        while ( @series1 || @volume1 ) {
110
            if ( $subfield eq 'a' ) {
89
            if ( @series1 ) {
111
                my @numbers = $field->subfield( 'n' );
90
                push @newsubfields, ( $series2_f, shift @series1 );
112
                my @parts = $field->subfield( 'p' );
113
                my $i = 0;
114
                while ( $i < @numbers || $i < @parts ) {
115
                    my @strings = grep {$_} ( $values[$i], $numbers[$i], $parts[$i] );
116
                    $values[$i] = join ' ', @strings;
117
                    $i++;
118
                }
91
            }
119
            }
92
            if ( @volume1 ) {
120
93
                push @newsubfields, ( $volume2_f, shift @volume1 );
121
            if ( $fields{'490'}{$subfield} ) {
122
                foreach my $v ( @values ) {
123
                    push @newsubfields, ( $subfield, $v );
124
                }
94
            }
125
            }
95
        }
126
        }
96
127
97
        my $new_field = MARC::Field->new( $series2_t, '', '',
128
        if ( $has_links && @linksubfields ) {
98
                                          @newsubfields );
129
            my $link_field = MARC::Field->new(
130
                '830',
131
                $field->indicator(1), $field->indicator(2),
132
                @linksubfields
133
                );
134
            push @newfields, $link_field;
135
        }
136
137
        if ( @newsubfields ) {
138
            my $new_field = MARC::Field->new( '490', $has_links, '',
139
                                              @newsubfields );
140
            push @newfields, $new_field;
141
        }
99
142
100
        $biblio->delete_fields( $field );
143
        $biblio->delete_fields( $field );
101
        push @newfields, $new_field;
102
    }
144
    }
103
145
104
    foreach my $field ( $biblio->field( $series2_t ) ) {
146
    foreach my $field ( $biblio->field( '490' ) ) {
105
        my @newsubfields;
147
        my @newsubfields;
106
        my @series2 = $field->subfield( $series2_f );
148
        foreach my $subfield ( sort keys %{ $fields{'490'} } ) {
107
        my @volume2 = $field->subfield( $volume2_f );
149
            my @values = $field->subfield( $subfield );
108
150
109
        while ( @series2 || @volume2 ) {
151
            if ( $fields{'440'}{$subfield} ) {
110
            if ( @series2 ) {
152
                foreach my $v ( @values ) {
111
                push @newsubfields, ( $series1_f, shift @series2 );
153
                    push @newsubfields, ( $subfield, $v );
112
            }
154
                }
113
            if ( @volume2 ) {
114
                push @newsubfields, ( $volume1_f, shift @volume2 );
115
            }
155
            }
116
        }
156
        }
117
157
118
        my $new_field = MARC::Field->new( $series1_t, '', '',
158
        if ( @newsubfields ) {
119
                                          @newsubfields );
159
            my $new_field = MARC::Field->new( '440', '', '',
160
                                              @newsubfields );
161
            push @newfields, $new_field;
162
        }
120
163
121
        $biblio->delete_fields( $field );
164
        $biblio->delete_fields( $field );
122
        push @newfields, $new_field;
123
    }
165
    }
124
    $biblio->insert_fields_ordered( @newfields );
166
    $biblio->insert_fields_ordered( @newfields );
125
167
126
    ModBiblioMarc( $biblio, $biblionumber, $framework );
127
    if ( $verbose ) {
168
    if ( $verbose ) {
128
        print "Changing MARC for biblio number $biblionumber.\n";
169
        print "Changing MARC for biblio number $biblionumber.\n";
129
    }
170
    }
130
    else {
171
    else {
131
        print ".";
172
        print ".";
132
    }
173
    }
174
    ModBiblioMarc( $biblio, $biblionumber, $framework );
133
}
175
}
134
print "\n";
176
print "\n";
135
177
Lines 162-172 sub print_usage { Link Here
162
$0: switch MARC21 440 tag and 490 tag contents
204
$0: switch MARC21 440 tag and 490 tag contents
163
205
164
Parameters:
206
Parameters:
165
    -c            Commit the changes to the marc records
207
    -c            Commit the changes to the marc records.
166
208
167
    -m            Also update the Koha field to MARC framework mappings for the
209
    -l            Add 830 tags with authority information from 440.  Otherwise
210
                  this information will be ignored.
211
212
    -f            Also update the Koha field to MARC framework mappings for the
168
                  seriestitle and volume Koha fields.
213
                  seriestitle and volume Koha fields.
169
214
215
    -v            Show more information as the records are being changed.
216
170
    --help or -h  show this message.
217
    --help or -h  show this message.
171
218
172
_USAGE_
219
_USAGE_
173
- 

Return to bug 5608