bug fix : adding condition for create the right notify_id
[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 = time();
41
42 $|=1; # flushes output
43 my $starttime = gettimeofday;
44
45 #1st of all, find item MARC tag.
46 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.itemnumber",'');
47 # $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");
48 my $sth = $dbh->prepare("select bibid from marc_biblio");
49 $sth->execute;
50 # my ($bibidmax) =  $sth->fetchrow;
51 # warn "$bibidmax <<==";
52 while (my ($bibid)= $sth->fetchrow) {
53     #now, parse the record, extract the item fields, and store them in somewhere else.
54     my $record = GetMarcBiblio($bibid);
55     my @fields = $record->field($tagfield);
56     my @items;
57     my $nbitems=0;
58     print ".";
59     my $timeneeded = gettimeofday - $starttime;
60     print "$i in $timeneeded s\n" unless ($i % 50);
61     $i++;
62     foreach my $field (@fields) {
63         my $item = MARC::Record->new();
64         $item->append_fields($field);
65         push @items,$item;
66         $record->delete_field($field);
67         $nbitems++;
68     }
69 #     print "$bibid\n";
70     # now, create biblio and items with NEWnewXX call.
71     my $frameworkcode = GetFrameworkCode($bibid);
72     localNEWmodbiblio($dbh,$record,$bibid,$frameworkcode) unless $test_parameter;
73 #     warn 'B=>'.$record->as_formatted;
74 #     print "biblio done\n";
75     for (my $i=0;$i<=$#items;$i++) {
76         my $tmp = TransformMarcToKoha($dbh,$items[$i],$frameworkcode) unless $test_parameter; # finds the itemnumber
77 #         warn "    I=> ".$items[$i]->as_formatted;
78         localNEWmoditem($dbh,$items[$i],$bibid,$tmp->{itemnumber},0) unless $test_parameter;
79 #         print "1 item done\n";
80     }
81 }
82 # $dbh->do("unlock tables");
83 my $timeneeded = time() - $starttime;
84 print "$i MARC record done in $timeneeded seconds\n";
85
86 # modified NEWmodbiblio to jump the MARC part of the biblio modif
87 # highly faster
88 sub localNEWmodbiblio {
89     my ($dbh,$record,$bibid,$frameworkcode) =@_;
90     $frameworkcode="" unless $frameworkcode;
91     my $oldbiblio = TransformMarcToKoha($dbh,$record,$frameworkcode);
92
93     return 1;
94 }
95
96 sub localNEWmoditem {
97     my ( $dbh, $record, $bibid, $itemnumber, $delete ) = @_;
98 #     warn "NEWmoditem $bibid / $itemnumber / $delete ".$record->as_formatted;
99     my $frameworkcode=GetFrameworkCode($bibid);
100     my $olditem = TransformMarcToKoha( $dbh, $record,$frameworkcode );
101     C4::Biblio::_koha_modify_item( $dbh, $olditem );
102 }