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