60b1b878fe7db732789445c8cedb8f1b96846757
[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 BEGIN {
7     # find Koha's Perl modules
8     # test carefully before changing this
9     use FindBin;
10     eval { require "$FindBin::Bin/../../kohalib.pl" };
11 }
12
13 # Koha modules used
14
15 use C4::Context;
16 use C4::Biblio;
17 use MARC::Record;
18 use MARC::File::USMARC;
19
20
21 my $dbh = C4::Context->dbh;
22
23 my $sth=$dbh->prepare("select biblionumber,timestamp from biblioitems");
24         $sth->execute();
25
26 $|=1; # flushes output
27 print "Creating/updating field 100 if needed\n";
28 while (my ($biblionumber,$time)=$sth->fetchrow ){
29 #   my $record;
30 # print "record : $biblionumber \n";
31     my $record = GetMarcBiblio($biblionumber);
32 # print "=> ".$record->as_formatted;
33     MARCmodrecord($biblionumber,$record,$time);
34 #
35 }
36
37 sub MARCmodrecord {
38     my ($biblionumber,$record,$time)=@_;
39 #     warn "AVANT : ".$record->as_formatted;
40         my $update=0;
41         $record->leader('     nac  22     1u 4500');
42         $update=1;
43         my $string;
44         if ($record->field(100)) {
45             $string = substr($record->subfield(100,"a")."                                   ",0,35);
46             my $f100 = $record->field(100);
47             $record->delete_field($f100);
48         } else {
49             $string = POSIX::strftime("%Y%m%d", localtime);
50             $string=~s/\-//g;
51             $string = sprintf("%-*s",35, $string);
52         }
53         substr($string,22,6,"frey50");
54         unless ($record->subfield(100,"a")){
55             $record->insert_fields_ordered(MARC::Field->new(100,"","","a"=>"$string"));
56         }
57     if ($update){
58         &ModBiblioMarc($record,$biblionumber,'');
59         print "\r$biblionumber" unless ( $biblionumber % 100 );
60     }
61
62 }
63 END;