rebuild_unimarc_100 : better handling of unusual cases
[koha.git] / misc / migration_tools / 22_to_30 / rebuild_unimarc_100.pl
1 #!/usr/bin/perl
2 # This script finds and fixes missing 090 fields in Koha for MARC21
3 #  Written by TG on 01/10/2005
4 #  Revised by Joshua Ferraro on 03/31/2006
5 use strict;
6
7 # Koha modules used
8
9 use C4::Context;
10 use C4::Biblio;
11 use MARC::Record;
12 use MARC::File::USMARC;
13
14
15 my $dbh = C4::Context->dbh;
16
17 my $sth=$dbh->prepare("select biblionumber,timestamp from biblioitems");
18         $sth->execute();
19
20 $|=1; # flushes output
21 print "Creating/updating field 100 if needed\n";
22 while (my ($biblionumber,$time)=$sth->fetchrow ){
23 #   my $record;
24 # print "record : $biblionumber \n";
25     my $record = GetMarcBiblio($biblionumber);
26 # print "=> ".$record->as_formatted;
27     MARCmodrecord($biblionumber,$record,$time);
28 #
29 }
30
31 sub MARCmodrecord {
32     my ($biblionumber,$record,$time)=@_;
33 #     warn "AVANT : ".$record->as_formatted;
34         my $update=0;
35         $record->leader('     nac  22     1u 4500');
36         $update=1;
37         my $string;
38         if ($record->field(100)) {
39             $string = substr($record->subfield(100,"a")."                                   ",0,35);
40             my $f100 = $record->field(100);
41             $record->delete_field($f100);
42         } else {
43             $string = POSIX::strftime("%Y%m%d", localtime);
44             $string=~s/\-//g;
45             $string = sprintf("%-*s",35, $string);
46         }
47         substr($string,22,6,"frey50");
48         unless ($record->subfield(100,"a")){
49             $record->insert_fields_ordered(MARC::Field->new(100,"","","a"=>"$string"));
50         }
51     if ($update){
52         &ModBiblioMarc($record,$biblionumber,'');
53         print "\r$biblionumber" unless ( $biblionumber % 100 );
54     }
55
56 }
57 END;