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

(-)a/C4/Biblio.pm (+85 lines)
Lines 2251-2256 sub TransformKohaToMarc { Link Here
2251
    return $record;
2251
    return $record;
2252
}
2252
}
2253
2253
2254
=head UpdateKohaToMarc
2255
2256
    C4::Biblio::UpdateKohaToMarc($bibliodata, $record);
2257
    ||
2258
    C4::Biblio::UpdateKohaToMarc($biblionumberOrBibliodata);
2259
    ||
2260
    C4::Biblio::UpdateKohaToMarc($biblionumber);
2261
2262
Overwrites all MARC Fields mapped to Koha biblio or biblioitems -tables with
2263
their values from DB.
2264
items-table columns are not updated to the MARC Record, because they are no longer stored there.
2265
2266
@PARAM1  MANDATORY
2267
         Integer, biblionumber of the target Biblio. This updates all DB coluns to the MARC record which takes a long time.
2268
         OR
2269
         Hash, with keys matching column names of biblio- and biblioitems-tables. biblionumber must be present.
2270
               Eg. {biblio.biblionumber => 1213, biblioitems.agerestriction => 'K18', biblioitems.publishercode => 'PKA'}
2271
               This way only the given columns are updated to the MARC record, possibly skipping unnecessary work.
2272
@PARAM2  OPTIONAL
2273
               MARC::Record-object, to have it's Koha mapped fields updated.
2274
@RETURNS String, Error code:
2275
                        'NOBIBLIODATA' : Couldn't find the biblio columns from the given $biblionumberOrBibliodata, or the biblionumber is missing.
2276
                        'NORECORD'     : The given $record is not a MARC::Record-object
2277
=cut
2278
sub UpdateKohaToMarc {
2279
    my ($biblionumberOrBibliodata, $record) = @_;
2280
2281
    my $bibdata;
2282
2283
    unless (ref $biblionumberOrBibliodata) { #We have a SCALAR, eg. a biblionumber.
2284
        $bibdata = {};
2285
        my @biblioitems = C4::Biblio::GetBiblioItemByBiblioNumber($biblionumberOrBibliodata);
2286
        my $biblioitem = $biblioitems[0];
2287
        return 'NOBIBLIODATA' unless $biblioitem;
2288
        map {$bibdata->{"biblioitems.$_"} = $biblioitem->{$_}} keys($biblioitem);
2289
        my $biblio = C4::Biblio::GetBiblio($biblionumberOrBibliodata);
2290
        return 'NOBIBLIODATA' unless $biblio;
2291
        map {$bibdata->{"biblio.$_"} = $biblio->{$_}} keys(%$biblio);
2292
        $bibdata->{biblionumber} = $biblionumberOrBibliodata;
2293
    }
2294
    else {
2295
        $bibdata = $biblionumberOrBibliodata;
2296
    }
2297
    my $biblionumber = $bibdata->{'biblio.biblionumber'} || $bibdata->{'biblio.biblionumber'};
2298
    return 'NOBIBLIODATA' if (not($bibdata) || not($biblionumber));
2299
    unless (ref $record eq 'MARC::Record') {
2300
        $record = C4::Biblio::GetMarcBiblio(  $bibdata->{biblionumber}  );
2301
    }
2302
    return 'NORECORD' unless $record;
2303
    my $frameworkcode = ($bibdata->{frameworkcode}) ? $bibdata->{frameworkcode} : C4::Biblio::GetFrameworkCode($bibdata->{biblionumber});
2304
2305
    my $db_to_marc = C4::Context->marcfromkohafield; #Get the mapping rules.
2306
    my $db_columns_as_fields = {};
2307
    while ( my ($name, $value) = each %$bibdata ) {
2308
        next unless $value;
2309
        next unless my $dtm = $db_to_marc->{''}->{$name};
2310
        next unless ( scalar( @$dtm ) );
2311
        my ($tag, $letter) = @$dtm;
2312
        $tag .= '';
2313
        foreach my $value ( split(/\s?\|\s?/, $value, -1) ) {
2314
            next if $value eq '';
2315
            $db_columns_as_fields->{$tag} //= {};
2316
            $db_columns_as_fields->{$tag}->{$letter} = $value;
2317
        }
2318
    }
2319
    foreach my $fieldCode (sort keys %$db_columns_as_fields) {
2320
        my $subfields = $db_columns_as_fields->{ $fieldCode };
2321
2322
        my @existingFields = $record->field($fieldCode);
2323
        if ($existingFields[0]) {
2324
            $existingFields[0]->update(each $subfields);
2325
            for (my $i=1 ; $i<scalar(@existingFields) ; $i++) {
2326
                $record->delete_field($existingFields[$i]);
2327
            }
2328
        }
2329
        else {
2330
            my $newField = MARC::Field->new($fieldCode, '', '', each $subfields);
2331
            $record->insert_fields_ordered($newField);
2332
        }
2333
    }
2334
2335
    C4::Biblio::ModBiblio($record, $biblionumber, $frameworkcode);
2336
    return undef;
2337
}
2338
2254
=head2 PrepHostMarcField
2339
=head2 PrepHostMarcField
2255
2340
2256
    $hostfield = PrepHostMarcField ( $hostbiblionumber,$hostitemnumber,$marcflavour )
2341
    $hostfield = PrepHostMarcField ( $hostbiblionumber,$hostitemnumber,$marcflavour )
(-)a/misc/batchRebuildBiblioTables.pl (-14 / +65 lines)
Lines 20-49 use Time::HiRes qw(gettimeofday); Link Here
20
20
21
use Getopt::Long;
21
use Getopt::Long;
22
my ( $input_marc_file, $number) = ('', 0);
22
my ( $input_marc_file, $number) = ('', 0);
23
my ($version, $confirm, $test_parameter);
23
my ($help, $confirm, $test_parameter);
24
my ($updateUpstream, $updateDownstream, $limitedColumns);
24
GetOptions(
25
GetOptions(
25
    'c' => \$confirm,
26
    'c|confirm'    => \$confirm,
26
    'h' => \$version,
27
    'h|help'       => \$help,
27
    't' => \$test_parameter,
28
    't|test'       => \$test_parameter,
29
    'u|upstream'   => \$updateUpstream,
30
    'd|downstream' => \$updateDownstream,
31
    'columns:s'    => \$limitedColumns,
28
);
32
);
29
33
30
if ($version || (!$confirm)) {
34
my $helpText = <<EOF
31
    print <<EOF
35
This script rebuilds the non-MARC DB from the MARC values and vice versa.
32
This script rebuilds the non-MARC DB from the MARC values.
33
You can/must use it when you change your mapping.
36
You can/must use it when you change your mapping.
34
37
35
Example: you decide to map biblio.title to 200\$a (it was previously mapped to 610\$a).
38
Example: you decide to map biblio.title to 200\$a (it was previously mapped to 610\$a).
36
Run this script or you will have strange results in OPAC !
39
Run this script or you will have strange results in OPAC !
37
40
41
 -h | --help        This friendly helper!
42
 -u | --upstream    Update MARC Records from the database columns using "Koha to MARC mappings".
43
 -d | --downstream  Update database columns from the MARC Records  using "Koha to MARC mappings".
44
 -c | --confirm     Confirm, that you want to batch modify all of your Bibliographic records.
45
                    It is better to double check your mappings now since this operation cannot be undone.
46
                    This will take a long time...
47
 -t | --test        Test only, change nothing in DB
48
 --columns          NOT IMPLEMENTED YET! Limit the updates to this list of table
49
                    columns, instead of updating all "Koha to MARC mappings".
50
                    Example: --columns "biblioitems.publicationyear, biblioitems.datereceived, biblio.copyrightdate"
51
                    Only columns for tables biblio and biblioitems are supported.
52
38
Syntax:
53
Syntax:
39
\t./batchRebuildBiblioTables.pl -h (or without arguments => shows this screen)
54
./batchRebuildBiblioTables.pl --help
40
\t./batchRebuildBiblioTables.pl -c (c like confirm => rebuild non marc DB (may be long)
55
./batchRebuildBiblioTables.pl --test --upstream
41
\t-t => test only, change nothing in DB
56
./batchRebuildBiblioTables.pl --confirm --downstream
42
EOF
57
EOF
43
;
58
;
44
    exit;
59
60
if ($help) {
61
    print $helpText;
62
    exit();
63
}
64
if (not($updateUpstream) && not($updateDownstream)) {
65
    print $helpText."\n\nyou must choose either --upstream or --downstream !\n";
66
    exit();
67
}
68
if (!$confirm) {
69
    print $helpText."\n\nRead the help file!\n";
70
    exit();
71
}
72
my @limitedColumns = split(/\s*,\s*/,$limitedColumns);
73
if ($limitedColumns && scalar(@limitedColumns) == 0) {
74
    print $helpText."\n\nCouldn't parse --columns '$limitedColumns'!\n";
75
    exit();
76
}
77
elsif (scalar(@limitedColumns)) {
78
    @limitedColumns = undef;
79
    #TODO limited columns parsing.
45
}
80
}
46
81
82
47
my $dbh = C4::Context->dbh;
83
my $dbh = C4::Context->dbh;
48
my $i=0;
84
my $i=0;
49
my $starttime = time();
85
my $starttime = time();
Lines 56-61 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.itemnumber",''); Link Here
56
# $dbh->do("lock tables biblio write, biblioitems write, items write, marc_biblio write, marc_subfield_table write, marc_blob_subfield write, marc_word write, marc_subfield_structure write, stopwords write");
92
# $dbh->do("lock tables biblio write, biblioitems write, items write, marc_biblio write, marc_subfield_table write, marc_blob_subfield write, marc_word write, marc_subfield_structure write, stopwords write");
57
my $sth = $dbh->prepare("SELECT biblionumber FROM biblio");
93
my $sth = $dbh->prepare("SELECT biblionumber FROM biblio");
58
$sth->execute;
94
$sth->execute;
95
96
if ($updateUpstream) {
97
    while (my ($biblionumber) = $sth->fetchrow) {
98
        DB_ToRecord($biblionumber, \@limitedColumns);
99
    }
100
    exit();
101
}
102
103
59
# my ($biblionumbermax) =  $sth->fetchrow;
104
# my ($biblionumbermax) =  $sth->fetchrow;
60
# warn "$biblionumbermax <<==";
105
# warn "$biblionumbermax <<==";
61
my @errors;
106
my @errors;
Lines 63-70 while (my ($biblionumber)= $sth->fetchrow) { Link Here
63
    #now, parse the record, extract the item fields, and store them in somewhere else.
108
    #now, parse the record, extract the item fields, and store them in somewhere else.
64
    my $record = GetMarcBiblio($biblionumber);
109
    my $record = GetMarcBiblio($biblionumber);
65
    if (not defined $record) {
110
    if (not defined $record) {
66
	push @errors, $biblionumber;
111
    push @errors, $biblionumber;
67
	next;
112
    next;
68
    }
113
    }
69
    my @fields = $record->field($tagfield);
114
    my @fields = $record->field($tagfield);
70
    my @items;
115
    my @items;
Lines 101-103 sub localNEWmodbiblio { Link Here
101
    C4::Biblio::_koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
146
    C4::Biblio::_koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
102
    return 1;
147
    return 1;
103
}
148
}
104
- 
149
150
sub DB_ToRecord {
151
    my ($biblionumber) = @_;
152
153
    my $errors = C4::Biblio::UpdateKohaToMarc($biblionumber);
154
    print "bn: $biblionumber, $errors\n" if $errors;
155
}

Return to bug 13703