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