View | Details | Raw Unified | Return to bug 6113
Collapse All | Expand All

(-)a/C4/Biblio.pm (-17 / +107 lines)
Lines 2944-2952 sub _koha_add_biblio { Link Here
2944
    	$biblio->{'serial'} = 0;
2944
    	$biblio->{'serial'} = 0;
2945
    	if ( $biblio->{'seriestitle'} ) { $biblio->{'serial'} = 1 }
2945
    	if ( $biblio->{'seriestitle'} ) { $biblio->{'serial'} = 1 }
2946
    }
2946
    }
2947
    
2948
    ### Validating biblionumber.
2949
    my $biblionumber;
2950
    my $isBiblioNumberOk = ''; #placeholder for the sql-clause which inserts biblionumber
2951
    if (exists $biblio->{'biblionumber'}) {
2952
        $biblionumber = $biblio->{'biblionumber'};
2953
        if (! ($biblionumber =~ /^\d+$/) ) {
2954
            $error .= "C4::Biblio::_koha_add_biblio():> Biblionumber $biblionumber is not a number! Using a new biblionumber\n";
2955
            warn $error;
2956
        }
2957
        elsif (! ($biblionumber > 0)) {
2958
            $error .= "C4::Biblio::_koha_add_biblio():> Biblionumber $biblionumber is not greater than 0! Using a new biblionumber\n";
2959
            warn $error;
2960
        }
2961
        #The real Perl LONG_MAX is 2147483646, but using 2147483640
2962
        #  so we can detect the imminent primary key exhaustion before it actually is exhausted.
2963
        elsif (! ($biblionumber < 2147483640)) { 
2964
            $error .= "C4::Biblio::_koha_add_biblio():> Biblionumber $biblionumber is too large for Perl! Using a new biblionumber\n";
2965
            warn $error;
2966
        }
2967
        else {
2968
            #We have validated the biblionumber and can prepare to insert it.
2969
            $isBiblioNumberOk = 'biblionumber = ?,';
2970
        }
2971
        
2972
    }
2947
2973
2948
    my $query = "INSERT INTO biblio
2974
    my $query = "INSERT INTO biblio
2949
        SET frameworkcode = ?,
2975
        SET frameworkcode = ?,
2976
            $isBiblioNumberOk
2950
            author = ?,
2977
            author = ?,
2951
            title = ?,
2978
            title = ?,
2952
            unititle =?,
2979
            unititle =?,
Lines 2958-2973 sub _koha_add_biblio { Link Here
2958
            abstract = ?
2985
            abstract = ?
2959
        ";
2986
        ";
2960
    my $sth = $dbh->prepare($query);
2987
    my $sth = $dbh->prepare($query);
2961
    $sth->execute(
2988
    
2962
        $frameworkcode, $biblio->{'author'},      $biblio->{'title'},         $biblio->{'unititle'}, $biblio->{'notes'},
2989
    if ($isBiblioNumberOk) {
2963
        $biblio->{'serial'},        $biblio->{'seriestitle'}, $biblio->{'copyrightdate'}, $biblio->{'abstract'}
2990
        $sth->execute(
2964
    );
2991
            $frameworkcode, $biblionumber, $biblio->{'author'},      $biblio->{'title'},         $biblio->{'unititle'}, $biblio->{'notes'},
2992
            $biblio->{'serial'},        $biblio->{'seriestitle'}, $biblio->{'copyrightdate'}, $biblio->{'abstract'}
2993
        );
2994
    }
2995
    else {
2996
        $sth->execute(
2997
            $frameworkcode, $biblio->{'author'},      $biblio->{'title'},         $biblio->{'unititle'}, $biblio->{'notes'},
2998
            $biblio->{'serial'},        $biblio->{'seriestitle'}, $biblio->{'copyrightdate'}, $biblio->{'abstract'}
2999
        );
3000
    }
3001
    
2965
3002
2966
    my $biblionumber = $dbh->{'mysql_insertid'};
3003
    $biblionumber = $isBiblioNumberOk ? $biblionumber : $dbh->{'mysql_insertid'};
2967
    if ( $dbh->errstr ) {
3004
    if ( $dbh->errstr ) {
2968
        $error .= "ERROR in _koha_add_biblio $query" . $dbh->errstr;
3005
        $error .= "ERROR in _koha_add_biblio $query" . $dbh->errstr;
3006
    }
3007
    
3008
    
3009
    if (defined $error) {
2969
        warn $error;
3010
        warn $error;
2970
    }
3011
    }
3012
    
2971
3013
2972
    $sth->finish();
3014
    $sth->finish();
2973
3015
Lines 3093-3101 Internal function to add a biblioitem Link Here
3093
sub _koha_add_biblioitem {
3135
sub _koha_add_biblioitem {
3094
    my ( $dbh, $biblioitem ) = @_;
3136
    my ( $dbh, $biblioitem ) = @_;
3095
    my $error;
3137
    my $error;
3138
    
3139
    ### Validating biblionumber.
3140
    my $biblioitemnumber;
3141
    my $isBiblioitemNumberOk = ''; #placeholder for the sql-clause which inserts biblionumber
3142
    if (exists $biblioitem->{'biblioitemnumber'}) {
3143
        $biblioitemnumber = $biblioitem->{'biblioitemnumber'};
3144
        if (! ($biblioitemnumber =~ /^\d+$/) ) {
3145
            $error .= "C4::Biblio::_koha_add_biblio():> biblioitemnumber $biblioitemnumber is not a number! Using a new biblioitemnumber\n";
3146
            warn $error;
3147
        }
3148
        elsif (! ($biblioitemnumber > 0)) {
3149
            $error .= "C4::Biblio::_koha_add_biblio():> biblioitemnumber $biblioitemnumber is not greater than 0! Using a new biblioitemnumber\n";
3150
            warn $error;
3151
        }
3152
        #The real Perl LONG_MAX is 2147483646, but using 2147483640
3153
        #  so we can detect the imminent primary key exhaustion before it actually is exhausted.
3154
        elsif (! ($biblioitemnumber < 2147483640)) { 
3155
            $error .= "C4::Biblio::_koha_add_biblio():> biblioitemnumber $biblioitemnumber is too large for Perl! Using a new biblioitemnumber\n";
3156
            warn $error;
3157
        }
3158
        else {
3159
            #We have validated the biblionumber and can prepare to insert it.
3160
            $isBiblioitemNumberOk = 'biblioitemnumber = ?,';
3161
        }
3162
        
3163
    }
3096
3164
3097
    my ($cn_sort) = GetClassSort( $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3165
    my ($cn_sort) = GetClassSort( $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3098
    my $query = "INSERT INTO biblioitems SET
3166
    my $query = "INSERT INTO biblioitems SET
3167
        $isBiblioitemNumberOk
3099
        biblionumber    = ?,
3168
        biblionumber    = ?,
3100
        volume          = ?,
3169
        volume          = ?,
3101
        number          = ?,
3170
        number          = ?,
Lines 3129-3152 sub _koha_add_biblioitem { Link Here
3129
        agerestriction  = ?
3198
        agerestriction  = ?
3130
        ";
3199
        ";
3131
    my $sth = $dbh->prepare($query);
3200
    my $sth = $dbh->prepare($query);
3132
    $sth->execute(
3201
    
3133
        $biblioitem->{'biblionumber'},     $biblioitem->{'volume'},           $biblioitem->{'number'},                $biblioitem->{'itemtype'},
3202
    if ($isBiblioitemNumberOk) {
3134
        $biblioitem->{'isbn'},             $biblioitem->{'issn'},             $biblioitem->{'publicationyear'},       $biblioitem->{'publishercode'},
3203
        $sth->execute(
3135
        $biblioitem->{'volumedate'},       $biblioitem->{'volumedesc'},       $biblioitem->{'collectiontitle'},       $biblioitem->{'collectionissn'},
3204
            $biblioitem->{'biblioitemnumber'},
3136
        $biblioitem->{'collectionvolume'}, $biblioitem->{'editionstatement'}, $biblioitem->{'editionresponsibility'}, $biblioitem->{'illus'},
3205
            $biblioitem->{'biblionumber'},     $biblioitem->{'volume'},           $biblioitem->{'number'},                $biblioitem->{'itemtype'},
3137
        $biblioitem->{'pages'},            $biblioitem->{'bnotes'},           $biblioitem->{'size'},                  $biblioitem->{'place'},
3206
            $biblioitem->{'isbn'},             $biblioitem->{'issn'},             $biblioitem->{'publicationyear'},       $biblioitem->{'publishercode'},
3138
        $biblioitem->{'lccn'},             $biblioitem->{'marc'},             $biblioitem->{'url'},                   $biblioitem->{'biblioitems.cn_source'},
3207
            $biblioitem->{'volumedate'},       $biblioitem->{'volumedesc'},       $biblioitem->{'collectiontitle'},       $biblioitem->{'collectionissn'},
3139
        $biblioitem->{'cn_class'},         $biblioitem->{'cn_item'},          $biblioitem->{'cn_suffix'},             $cn_sort,
3208
            $biblioitem->{'collectionvolume'}, $biblioitem->{'editionstatement'}, $biblioitem->{'editionresponsibility'}, $biblioitem->{'illus'},
3140
        $biblioitem->{'totalissues'},      $biblioitem->{'ean'},              $biblioitem->{'agerestriction'}
3209
            $biblioitem->{'pages'},            $biblioitem->{'bnotes'},           $biblioitem->{'size'},                  $biblioitem->{'place'},
3141
    );
3210
            $biblioitem->{'lccn'},             $biblioitem->{'marc'},             $biblioitem->{'url'},                   $biblioitem->{'biblioitems.cn_source'},
3142
    my $bibitemnum = $dbh->{'mysql_insertid'};
3211
            $biblioitem->{'cn_class'},         $biblioitem->{'cn_item'},          $biblioitem->{'cn_suffix'},             $cn_sort,
3212
            $biblioitem->{'totalissues'},      $biblioitem->{'ean'},              $biblioitem->{'agerestriction'}
3213
        );
3214
    }
3215
    else {
3216
        $sth->execute(
3217
            $biblioitem->{'biblionumber'},     $biblioitem->{'volume'},           $biblioitem->{'number'},                $biblioitem->{'itemtype'},
3218
            $biblioitem->{'isbn'},             $biblioitem->{'issn'},             $biblioitem->{'publicationyear'},       $biblioitem->{'publishercode'},
3219
            $biblioitem->{'volumedate'},       $biblioitem->{'volumedesc'},       $biblioitem->{'collectiontitle'},       $biblioitem->{'collectionissn'},
3220
            $biblioitem->{'collectionvolume'}, $biblioitem->{'editionstatement'}, $biblioitem->{'editionresponsibility'}, $biblioitem->{'illus'},
3221
            $biblioitem->{'pages'},            $biblioitem->{'bnotes'},           $biblioitem->{'size'},                  $biblioitem->{'place'},
3222
            $biblioitem->{'lccn'},             $biblioitem->{'marc'},             $biblioitem->{'url'},                   $biblioitem->{'biblioitems.cn_source'},
3223
            $biblioitem->{'cn_class'},         $biblioitem->{'cn_item'},          $biblioitem->{'cn_suffix'},             $cn_sort,
3224
            $biblioitem->{'totalissues'},      $biblioitem->{'ean'},              $biblioitem->{'agerestriction'}
3225
        );
3226
    }
3227
    
3228
    $biblioitemnumber = $isBiblioitemNumberOk ? $biblioitemnumber : $dbh->{'mysql_insertid'};
3143
3229
3144
    if ( $dbh->errstr ) {
3230
    if ( $dbh->errstr ) {
3145
        $error .= "ERROR in _koha_add_biblioitem $query" . $dbh->errstr;
3231
        $error .= "ERROR in _koha_add_biblioitem $query" . $dbh->errstr;
3232
    }
3233
    
3234
    if (defined $error) {
3146
        warn $error;
3235
        warn $error;
3147
    }
3236
    }
3237
    
3148
    $sth->finish();
3238
    $sth->finish();
3149
    return ( $bibitemnum, $error );
3239
    return ( $biblioitemnumber, $error );
3150
}
3240
}
3151
3241
3152
=head2 _koha_delete_biblio
3242
=head2 _koha_delete_biblio
(-)a/misc/migration_tools/bulkmarcimport.pl (-13 / +65 lines)
Lines 34-40 use Pod::Usage; Link Here
34
use open qw( :std :encoding(UTF-8) );
34
use open qw( :std :encoding(UTF-8) );
35
binmode( STDOUT, ":encoding(UTF-8)" );
35
binmode( STDOUT, ":encoding(UTF-8)" );
36
my ( $input_marc_file, $number, $offset) = ('',0,0);
36
my ( $input_marc_file, $number, $offset) = ('',0,0);
37
my ($version, $delete, $test_parameter, $skip_marc8_conversion, $char_encoding, $verbose, $commit, $fk_off,$format,$biblios,$authorities,$keepids,$match, $isbn_check, $logfile);
37
my ($version, $delete, $test_parameter, $skip_marc8_conversion, $char_encoding, $verbose, $commit, $fk_off,$format,$biblios,$authorities,$keepids,$match, $isbn_check, $logfile, $legacyIdAsBiblionumberFromHere);
38
my ( $insert, $filters, $update, $all, $yamlfile, $authtypes );
38
my ( $insert, $filters, $update, $all, $yamlfile, $authtypes );
39
my $cleanisbn = 1;
39
my $cleanisbn = 1;
40
my ($sourcetag,$sourcesubfield,$idmapfl, $dedup_barcode);
40
my ($sourcetag,$sourcesubfield,$idmapfl, $dedup_barcode);
Lines 73-78 GetOptions( Link Here
73
    'yaml:s'        => \$yamlfile,
73
    'yaml:s'        => \$yamlfile,
74
    'dedupbarcode' => \$dedup_barcode,
74
    'dedupbarcode' => \$dedup_barcode,
75
    'framework=s' => \$framework,
75
    'framework=s' => \$framework,
76
	'g|legacyIdAsBiblionumberFromHere:s' => \$legacyIdAsBiblionumberFromHere,
76
);
77
);
77
$biblios ||= !$authorities;
78
$biblios ||= !$authorities;
78
$insert  ||= !$update;
79
$insert  ||= !$update;
Lines 166-179 if ( $offset ) { Link Here
166
    $batch->next() while ($offset--);
167
    $batch->next() while ($offset--);
167
}
168
}
168
169
169
my ($tagid,$subfieldid);
170
my ($tagid, $subfieldid, $binum_tagid, $binum_subfieldid);
170
if ($authorities){
171
if ($authorities){
171
	  $tagid='001';
172
	  $tagid='001';
172
}
173
}
173
else {
174
else {
174
   ( $tagid, $subfieldid ) =
175
   ( $tagid, $subfieldid ) =
175
            GetMarcFromKohaField( "biblio.biblionumber", $framework );
176
            GetMarcFromKohaField( "biblio.biblionumber", $framework );
177
   ( $binum_tagid, $binum_subfieldid ) =
178
            GetMarcFromKohaField( "biblioitems.biblioitemnumber", $framework );		
176
	$tagid||="001";
179
	$tagid||="001";
180
	
181
	#This is important for the functionality $legacyIdAsBiblionumberFromHere
182
	if ($tagid ne $binum_tagid) {
183
		warn "Koha to MARC mapping values biblio.biblionumber and biblioitems.biblioitemnumber must share the same MARC field!";
184
		exit(1);
185
	}
186
	
177
}
187
}
178
188
179
# the SQL query to search on isbn
189
# the SQL query to search on isbn
Lines 207-222 RECORD: while ( ) { Link Here
207
	
217
	
208
	
218
	
209
	
219
	
210
	### Open library hack! yay ###
220
	### If you are like me and don't have the legacy id in $999c and $999d, you
211
	### Move field 001 to 999c and 999d ###
221
	###   can use this functionality to copy the id on the fly.
212
	my $f001 = $record->field('001');
222
	### By default, copy legacy id field 001 to 999c and 999d ###
213
	$f001 = $f001->data();
223
	if ($legacyIdAsBiblionumberFromHere) {
214
	my $f999 = MARC::Field->new( 999, '', '',
224
		my $legacyIdField = $record->field( $legacyIdAsBiblionumberFromHere );
215
       'c' => $f001,
225
		if ($legacyIdField) {
216
       'd' => $f001
226
			my $legacyId = $legacyIdField->data();
217
    );
227
			my $biblionumberField = MARC::Field->new( $tagid, '', '',
218
	$record->append_fields($f999);
228
			   $subfieldid => $legacyId,
219
229
			   $binum_subfieldid => $legacyId,
230
			);
231
			$record->append_fields($biblionumberField);	
232
		}
233
		else {
234
			warn "No $legacyIdAsBiblionumberFromHere found!";
235
		}	
236
	}
220
    
237
    
221
    # transcode the record to UTF8 if needed & applicable.
238
    # transcode the record to UTF8 if needed & applicable.
222
    if ($record->encoding() eq 'MARC-8' and not $skip_marc8_conversion) {
239
    if ($record->encoding() eq 'MARC-8' and not $skip_marc8_conversion) {
Lines 592-597 bulkmarcimport.pl - Import bibliographic/authority records into Koha Link Here
592
 $ export KOHA_CONF=/etc/koha.conf
609
 $ export KOHA_CONF=/etc/koha.conf
593
 $ perl misc/migration_tools/bulkmarcimport.pl -d -commit 1000 \\
610
 $ perl misc/migration_tools/bulkmarcimport.pl -d -commit 1000 \\
594
    -file /home/jmf/koha.mrc -n 3000
611
    -file /home/jmf/koha.mrc -n 3000
612
 
613
 Migrate your legacy bibliographic records to Koha, while having the legacy id
614
 in a field $999 according to your directives here
615
 "Administration" > "Koha to Marc mapping".
616
 You also have the legacy id in field $001 so your analytic and component
617
 records links wont be broken.
618
 $ perl misc/migration_tools/bulkmarcimport.pl -d -commit 1000 \\
619
    -file /home/jmf/koha.marcxml -m MARCXML -b
595
620
596
=head1 WARNING
621
=head1 WARNING
597
622
Lines 599-604 Don't use this script before you've entered and checked your MARC parameters Link Here
599
tables twice (or more!). Otherwise, the import won't work correctly and you
624
tables twice (or more!). Otherwise, the import won't work correctly and you
600
will get invalid data.
625
will get invalid data.
601
626
627
If you want to assign your legacy bibliographic id as the biblionumber and
628
biblioitemnumber in Koha biblio- and bibliotiems -tables, you must first
629
specify the mappings in "Administration" > "Koha to MARC mapping".
630
631
It is extremely recommended to use field $999 to migrate your legacy id's.
632
If you set it to for ex. 001, librarians might manually reuse an existing id.
633
Also make sure that the legacy id is a strict positive integer and less than
634
perl -MPOSIX -le 'print LONG_MAX'.
635
602
=head1 DESCRIPTION
636
=head1 DESCRIPTION
603
637
604
=over
638
=over
Lines 698-703 Field store ids in I<FIELD> (usefull for authorities, where 001 contains the Link Here
698
authid for Koha, that can contain a very valuable info for authorities coming
732
authid for Koha, that can contain a very valuable info for authorities coming
699
from LOC or BNF. useless for biblios probably)
733
from LOC or BNF. useless for biblios probably)
700
734
735
If your legacy id is already in 001, or the field you want it in. Dont use this
736
parameter as it will move the biblionumber from your definition at
737
"Administration" > "Koha to Marc mapping" as a duplicate of your original id.
738
Thus you lose your MARC-field mapped to biblionumber before the record is
739
processed.
740
701
=item B<-match>=<FIELD>
741
=item B<-match>=<FIELD>
702
742
703
I<FIELD> matchindex,fieldtomatch matchpoint to use to deduplicate fieldtomatch
743
I<FIELD> matchindex,fieldtomatch matchpoint to use to deduplicate fieldtomatch
Lines 745-750 This is the code for the framework that the requested records will have attached Link Here
745
to them when they are created. If not specified, then the default framework
785
to them when they are created. If not specified, then the default framework
746
will be used.
786
will be used.
747
787
788
=item B<-g, -legacyIdAsBiblionumberFromHere>=<FIELD>
789
790
If you already have your legacy database id in your marc record and want to
791
preserve it, but still want to get it set as the biblionumber and biblioitemnumber
792
in Kohas database, use this.
793
794
Example:
795
Field 001 has your legacy id.
796
Koha to MARC mapping needs legacy id in 999c and 999d as well for biblionumber
797
and biblioitemnumber, use
798
 -g 001
799
to copy the legacy id to field 999cd and thus get the id preserved in DB.
800
748
=back
801
=back
749
802
750
=cut
803
=cut
751
- 

Return to bug 6113