fixing patronimages syspref
[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 biblionumber FROM biblio");
49 $sth->execute;
50 # my ($biblionumbermax) =  $sth->fetchrow;
51 # warn "$biblionumbermax <<==";
52 while (my ($biblionumber)= $sth->fetchrow) {
53     #now, parse the record, extract the item fields, and store them in somewhere else.
54     my $record = GetMarcBiblio($biblionumber);
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 "$biblionumber\n";
70     my $frameworkcode = GetFrameworkCode($biblionumber);
71     localNEWmodbiblio($dbh,$record,$biblionumber,$frameworkcode) unless $test_parameter;
72 }
73 # $dbh->do("unlock tables");
74 my $timeneeded = time() - $starttime;
75 print "$i MARC record done in $timeneeded seconds\n";
76
77 # modified NEWmodbiblio to jump the MARC part of the biblio modif
78 # highly faster
79 sub localNEWmodbiblio {
80     my ($dbh,$record,$biblionumber,$frameworkcode) =@_;
81     $frameworkcode="" unless $frameworkcode;
82     my $oldbiblio = TransformMarcToKoha($dbh,$record,$frameworkcode);
83     C4::Biblio::_koha_modify_biblio( $dbh, $oldbiblio );
84     C4::Biblio::_koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
85     return 1;
86 }
87