Bug 25650: Add location and itype descriptions in ILS-DI GetRecords
[koha.git] / misc / migration_tools / 22_to_30 / move_marc_to_biblioitems.pl
blob386831e8b9c022d3630237df52bfc73ea2452c6b
1 #!/usr/bin/perl
2 use Modern::Perl;
3 # script to shift marc to biblioitems
4 # scraped from updatedatabase for dev week by chris@katipo.co.nz
5 BEGIN {
6 # find Koha's Perl modules
7 # test carefully before changing this
8 use FindBin;
9 eval { require "$FindBin::Bin/../../kohalib.pl" };
11 use C4::Context;
12 use C4::Biblio;
13 use MARC::Record;
14 use MARC::File::XML ( BinaryEncoding => 'utf8' );
16 print "moving MARC record to biblioitems table\n";
18 my $dbh = C4::Context->dbh();
21 # moving MARC data from marc_subfield_table to biblioitems.marc
24 # changing marc field type
25 $dbh->do('ALTER TABLE `biblioitems` CHANGE `marc` `marc` LONGBLOB NULL DEFAULT NULL ');
26 # adding marc xml, just for convenience
27 $dbh->do('ALTER TABLE `biblioitems` ADD `marcxml` LONGTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ');
28 # moving data from marc_subfield_value to biblio
29 my $sth = $dbh->prepare('select bibid,biblionumber from marc_biblio');
30 $sth->execute;
31 my $sth_update = $dbh->prepare('update biblioitems set marc=?, marcxml=? where biblionumber=?');
32 my $totaldone=0;
34 $|=1;
36 while (my ($bibid,$biblionumber) = $sth->fetchrow) {
37 my $record = LocalMARCgetbiblio($dbh,$bibid);
38 #Force UTF-8 in record leader
39 $record->encoding('UTF-8');
40 my $marcflavour;
41 if (C4::Context->preference("marcflavour")=~/unimarc/i){
42 $marcflavour="UNIMARC";
43 } else {
44 $marcflavour="USMARC";
46 $sth_update->execute($record->as_usmarc(),$record->as_xml_record($marcflavour),$biblionumber);
47 $totaldone++;
48 print ".";
49 print "\r$totaldone" unless ($totaldone % 100);
51 print "\rdone\n";
55 # this sub is a copy of Biblio.pm, version 2.2.4
56 # It is useful only once, for moving from 2.2 to 3.0
57 # the MARCgetbiblio in Biblio.pm
58 # is still here, but uses other tables
59 # (the ones that are filled by updatedatabase !)
62 sub LocalMARCgetbiblio {
64 # Returns MARC::Record of the biblio passed in parameter.
65 my ( $dbh, $bibid ) = @_;
66 my $record = MARC::Record->new();
67 # warn "". $bidid;
69 my $sth =
70 $dbh->prepare(
71 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
72 from marc_subfield_table
73 where bibid=? order by tag,tagorder,subfieldorder
76 my $sth2 =
77 $dbh->prepare(
78 "select subfieldvalue from marc_blob_subfield where blobidlink=?");
79 $sth->execute($bibid);
80 my $prevtagorder = 1;
81 my $prevtag = 'XXX';
82 my $previndicator;
83 my $field; # for >=10 tags
84 my $prevvalue; # for <10 tags
85 while ( my $row = $sth->fetchrow_hashref ) {
87 if ( $row->{'valuebloblink'} ) { #---- search blob if there is one
88 $sth2->execute( $row->{'valuebloblink'} );
89 my $row2 = $sth2->fetchrow_hashref;
90 $sth2->finish;
91 $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
93 if ( $row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag ) {
94 $previndicator .= " ";
95 if ( $prevtag < 10 ) {
96 if ($prevtag ne '000') {
97 $record->add_fields( ( sprintf "%03s", $prevtag ), $prevvalue ) unless $prevtag eq "XXX"; # ignore the 1st loop
98 } else {
99 $record->leader(sprintf("%24s",$prevvalue));
102 else {
103 $record->add_fields($field) unless $prevtag eq "XXX";
105 undef $field;
106 $prevtagorder = $row->{tagorder};
107 $prevtag = $row->{tag};
108 $previndicator = $row->{tag_indicator};
109 if ( $row->{tag} < 10 ) {
110 $prevvalue = $row->{subfieldvalue};
112 else {
113 $field = MARC::Field->new(
114 ( sprintf "%03s", $prevtag ),
115 substr( $row->{tag_indicator} . ' ', 0, 1 ),
116 substr( $row->{tag_indicator} . ' ', 1, 1 ),
117 $row->{'subfieldcode'},
118 $row->{'subfieldvalue'}
122 else {
123 if ( $row->{tag} < 10 ) {
124 $record->add_fields( ( sprintf "%03s", $row->{tag} ),
125 $row->{'subfieldvalue'} );
127 else {
128 $field->add_subfields( $row->{'subfieldcode'},
129 $row->{'subfieldvalue'} );
131 $prevtag = $row->{tag};
132 $previndicator = $row->{tag_indicator};
136 # the last has not been included inside the loop... do it now !
137 if ( $prevtag ne "XXX" )
138 { # check that we have found something. Otherwise, prevtag is still XXX and we
139 # must return an empty record, not make MARC::Record fail because we try to
140 # create a record with XXX as field :-(
141 if ( $prevtag < 10 ) {
142 $record->add_fields( $prevtag, $prevvalue );
144 else {
146 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
147 $record->add_fields($field);
150 if (C4::Context->preference('marcflavour')=~/unimarc/i){
151 $record->leader(' nac 22 1u 4500');
152 my $string;
153 if ($record->field(100)) {
154 $string = substr($record->subfield(100,"a")." ",0,35);
155 my $f100 = $record->field(100);
156 $record->delete_field($f100);
157 } else {
158 $string = POSIX::strftime("%Y%m%d", localtime);
159 $string=~s/\-//g;
160 $string = sprintf("%-*s",35, $string);
162 substr($string,22,6,"frey50");
163 unless ($record->subfield(100,"a")){
164 $record->insert_fields_ordered(MARC::Field->new(100,"","","a"=>"$string"));
168 return $record;