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

(-)a/misc/batchRebuildBiblioTables.pl (-58 / +37 lines)
Lines 14-103 BEGIN { Link Here
14
14
15
# Koha modules used
15
# Koha modules used
16
use MARC::Record;
16
use MARC::Record;
17
use C4::Charset;
17
use C4::Context;
18
use C4::Context;
18
use C4::Biblio;
19
use C4::Biblio;
19
use Time::HiRes qw(gettimeofday);
20
use Time::HiRes qw(gettimeofday);
20
21
21
use Getopt::Long;
22
use Getopt::Long;
22
my ( $input_marc_file, $number) = ('', 0);
23
23
my ($version, $confirm, $test_parameter);
24
my ($version, $confirm);
24
GetOptions(
25
GetOptions(
25
    'c' => \$confirm,
26
    'c' => \$confirm,
26
    'h' => \$version,
27
    'h' => \$version
27
    't' => \$test_parameter,
28
);
28
);
29
29
30
if ($version || (!$confirm)) {
30
if ($version || (!$confirm)) {
31
    print <<EOF
31
    print <<EOF
32
This script rebuilds the non-MARC DB from the MARC values.
32
This script rebuilds the non-MARC fields from the MARC values.
33
You can/must use it when you change your mapping.
33
You can/must use it when you change your mapping.
34
34
35
Example: you decide to map biblio.title to 200\$a (it was previously mapped to 610\$a).
35
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 !
36
Run this script or you will have strange results in the UI!
37
37
38
Syntax:
38
Syntax:
39
\t./batchRebuildBiblioTables.pl -h (or without arguments => shows this screen)
39
\t./batchRebuildBiblioTables.pl -h (or without arguments => show this screen)
40
\t./batchRebuildBiblioTables.pl -c (c like confirm => rebuild non marc DB (may be long)
40
\t./batchRebuildBiblioTables.pl -c (c like confirm => rebuild non-MARC fields (may take long)
41
\t-t => test only, change nothing in DB
42
EOF
41
EOF
43
;
42
;
44
    exit;
43
    exit;
45
}
44
}
46
45
46
$|=1; # non-buffered output
47
47
my $dbh = C4::Context->dbh;
48
my $dbh = C4::Context->dbh;
48
my $i=0;
49
my $i=0;
49
my $starttime = time();
50
my $starttime = gettimeofday;
50
51
my $marcflavour = C4::Context->preference('marcflavour');
51
$|=1; # flushes output
52
my $sth = $dbh->prepare('SELECT biblionumber, frameworkcode FROM biblio');
52
$starttime = gettimeofday;
53
$sth->execute();
53
54
54
#1st of all, find item MARC tag.
55
my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.itemnumber",'');
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");
57
my $sth = $dbh->prepare("SELECT biblionumber FROM biblio");
58
$sth->execute;
59
# my ($biblionumbermax) =  $sth->fetchrow;
60
# warn "$biblionumbermax <<==";
61
my @errors;
55
my @errors;
62
while (my ($biblionumber)= $sth->fetchrow) {
56
while (my ($biblionumber, $frameworkcode) = $sth->fetchrow) {
63
    #now, parse the record, extract the item fields, and store them in somewhere else.
57
    my $marcxml = GetXmlBiblio($biblionumber);
64
    my $record = GetMarcBiblio({ biblionumber => $biblionumber });
58
    if (not defined $marcxml) {
65
    if (not defined $record) {
59
        push @errors, $biblionumber;
66
	push @errors, $biblionumber;
60
        next;
67
	next;
68
    }
61
    }
69
    my @fields = $record->field($tagfield);
62
70
    my @items;
63
    $marcxml = C4::Charset::StripNonXmlChars( $marcxml );
71
    my $nbitems=0;
64
    my $record = eval {
72
    print ".";
65
        MARC::Record::new_from_xml($marcxml, 'utf8', $marcflavour);
73
    my $timeneeded = gettimeofday - $starttime;
66
    };
74
    print "$i in $timeneeded s\n" unless ($i % 50);
67
    if ($@) {
75
    $i++;
68
        push @errors, $biblionumber;
76
    foreach my $field (@fields) {
69
        next;
77
        my $item = MARC::Record->new();
78
        $item->append_fields($field);
79
        push @items,$item;
80
        $record->delete_field($field);
81
        $nbitems++;
82
    }
70
    }
83
#     print "$biblionumber\n";
71
84
    my $frameworkcode = GetFrameworkCode($biblionumber);
72
    my $biblio = TransformMarcToKoha($record);
85
    localNEWmodbiblio($dbh,$record,$biblionumber,$frameworkcode) unless $test_parameter;
73
    C4::Biblio::_koha_modify_biblio($dbh, $biblio, $frameworkcode);
86
}
74
    C4::Biblio::_koha_modify_biblioitem_nonmarc($dbh, $biblio);
87
# $dbh->do("unlock tables");
75
88
my $timeneeded = time() - $starttime;
76
    $i++;
89
print "$i MARC record done in $timeneeded seconds\n";
77
    printf("%lu records processed in %.2f seconds\n", $i, gettimeofday() - $starttime) unless ($i % 100);
90
if (scalar(@errors) > 0) {
91
    print "Some biblionumber could not be processed though: ", join(" ", @errors);
92
}
78
}
93
79
94
# modified NEWmodbiblio to jump the MARC part of the biblio modif
80
printf("\n%lu records processed in %.2f seconds\n", $i, gettimeofday() - $starttime);
95
# highly faster
81
if (scalar(@errors) > 0) {
96
sub localNEWmodbiblio {
82
    print "Some records could not be processed though: ", join(' ', @errors);
97
    my ($dbh,$record,$biblionumber,$frameworkcode) =@_;
98
    $frameworkcode="" unless $frameworkcode;
99
    my $oldbiblio = TransformMarcToKoha($record,$frameworkcode);
100
    C4::Biblio::_koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
101
    C4::Biblio::_koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
102
    return 1;
103
}
83
}
104
- 

Return to bug 11529