fix for #380
[koha.git] / misc / rebuildnonmarc.pl
1 #!/usr/bin/perl
2 # small script that rebuilds the non-MARC DB
3
4 use strict;
5
6 # Koha modules used
7 # use MARC::File::USMARC;
8 use MARC::Record;
9 use MARC::Batch;
10 use C4::Context;
11 use C4::Biblio;
12
13 use Getopt::Long;
14 my ( $input_marc_file, $number) = ('',0);
15 my ($version, $confirm,$test_parameter);
16 GetOptions(
17         'c' => \$confirm,
18         'h' => \$version,
19         't' => \$test_parameter,
20 );
21
22 if ($version || (!$confirm)) {
23         print <<EOF
24 This script rebuilds the non-MARC DB from the MARC values.
25 You can/must use it when you change your mapping.
26 For example : you decide to map biblio.title to 200$a (it was previously mapped to 610$a) : run this script or you will have strange
27 results in OPAC !
28 syntax :
29 \t./rebuildnonmarc.pl -h (or without arguments => shows this screen)
30 \t./rebuildnonmarc.pl -c (c like confirm => rebuild non marc DB (may be long)
31 \t-t => test only, change nothing in DB
32 EOF
33 ;
34 die;
35 }
36
37 my $dbh = C4::Context->dbh;
38 my $i=0;
39 my $starttime = time();
40 #1st of all, find item MARC tag.
41 my ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber");
42 # $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");
43 my $sth = $dbh->prepare("select bibid from marc_biblio");
44 $sth->execute;
45 # my ($bibidmax) =  $sth->fetchrow;
46 # warn "$bibidmax <<==";
47 while (my ($bibid)= $sth->fetchrow) {
48         #now, parse the record, extract the item fields, and store them in somewhere else.
49         my $record = MARCgetbiblio($dbh,$bibid);
50         my @fields = $record->field($tagfield);
51         my @items;
52         my $nbitems=0;
53         $i++;
54         foreach my $field (@fields) {
55                 my $item = MARC::Record->new();
56                 $item->append_fields($field);
57                 push @items,$item;
58                 $record->delete_field($field);
59                 $nbitems++;
60         }
61         print "$bibid\n";
62         # now, create biblio and items with NEWnewXX call.
63         NEWmodbiblio($dbh,$record,$bibid) unless $test_parameter;
64 #       print "biblio done\n";
65         for (my $i=0;$i<=$#items;$i++) {
66                 my $tmp = MARCmarc2koha($dbh,$items[$i]) unless $test_parameter; # finds the itemnumber
67 #               warn "==> ".$items[$i]->as_formatted;
68                 NEWmoditem($dbh,$items[$i],$bibid,$tmp->{itemnumber}) unless $test_parameter;
69 #               print "1 item done\n";
70         }
71 }
72 # $dbh->do("unlock tables");
73 my $timeneeded = time() - $starttime;
74 print "$i MARC record done in $timeneeded seconds\n";