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