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

(-)a/C4/Biblio.pm (-17 / +101 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
        }
2956
        elsif (! ($biblionumber > 0)) {
2957
            $error .= "C4::Biblio::_koha_add_biblio():> Biblionumber $biblionumber is not greater than 0! Using a new biblionumber\n";
2958
        }
2959
        #The real Perl LONG_MAX is 2147483646, but using 2147483640
2960
        #  so we can detect the imminent primary key exhaustion before it actually is exhausted.
2961
        elsif (! ($biblionumber < 2147483640)) { 
2962
            $error .= "C4::Biblio::_koha_add_biblio():> Biblionumber $biblionumber is too large for Perl! Using a new biblionumber\n";
2963
        }
2964
        else {
2965
            #We have validated the biblionumber and can prepare to insert it.
2966
            $isBiblioNumberOk = 'biblionumber = ?,';
2967
        }
2968
        
2969
    }
2947
2970
2948
    my $query = "INSERT INTO biblio
2971
    my $query = "INSERT INTO biblio
2949
        SET frameworkcode = ?,
2972
        SET frameworkcode = ?,
2973
            $isBiblioNumberOk
2950
            author = ?,
2974
            author = ?,
2951
            title = ?,
2975
            title = ?,
2952
            unititle =?,
2976
            unititle =?,
Lines 2958-2973 sub _koha_add_biblio { Link Here
2958
            abstract = ?
2982
            abstract = ?
2959
        ";
2983
        ";
2960
    my $sth = $dbh->prepare($query);
2984
    my $sth = $dbh->prepare($query);
2961
    $sth->execute(
2985
    
2962
        $frameworkcode, $biblio->{'author'},      $biblio->{'title'},         $biblio->{'unititle'}, $biblio->{'notes'},
2986
    if ($isBiblioNumberOk) {
2963
        $biblio->{'serial'},        $biblio->{'seriestitle'}, $biblio->{'copyrightdate'}, $biblio->{'abstract'}
2987
        $sth->execute(
2964
    );
2988
            $frameworkcode, $biblionumber, $biblio->{'author'},      $biblio->{'title'},         $biblio->{'unititle'}, $biblio->{'notes'},
2989
            $biblio->{'serial'},        $biblio->{'seriestitle'}, $biblio->{'copyrightdate'}, $biblio->{'abstract'}
2990
        );
2991
    }
2992
    else {
2993
        $sth->execute(
2994
            $frameworkcode, $biblio->{'author'},      $biblio->{'title'},         $biblio->{'unititle'}, $biblio->{'notes'},
2995
            $biblio->{'serial'},        $biblio->{'seriestitle'}, $biblio->{'copyrightdate'}, $biblio->{'abstract'}
2996
        );
2997
    }
2998
    
2965
2999
2966
    my $biblionumber = $dbh->{'mysql_insertid'};
3000
    $biblionumber = $isBiblioNumberOk ? $biblionumber : $dbh->{'mysql_insertid'};
2967
    if ( $dbh->errstr ) {
3001
    if ( $dbh->errstr ) {
2968
        $error .= "ERROR in _koha_add_biblio $query" . $dbh->errstr;
3002
        $error .= "ERROR in _koha_add_biblio $query" . $dbh->errstr;
3003
    }
3004
    
3005
    
3006
    if (defined $error) {
2969
        warn $error;
3007
        warn $error;
2970
    }
3008
    }
3009
    
2971
3010
2972
    $sth->finish();
3011
    $sth->finish();
2973
3012
Lines 3093-3101 Internal function to add a biblioitem Link Here
3093
sub _koha_add_biblioitem {
3132
sub _koha_add_biblioitem {
3094
    my ( $dbh, $biblioitem ) = @_;
3133
    my ( $dbh, $biblioitem ) = @_;
3095
    my $error;
3134
    my $error;
3135
    
3136
    ### Validating biblionumber.
3137
    my $biblioitemnumber;
3138
    my $isBiblioitemNumberOk = ''; #placeholder for the sql-clause which inserts biblionumber
3139
    if (exists $biblioitem->{'biblioitemnumber'}) {
3140
        $biblioitemnumber = $biblioitem->{'biblioitemnumber'};
3141
        if (! ($biblioitemnumber =~ /^\d+$/) ) {
3142
            $error .= "C4::Biblio::_koha_add_biblioitem():> biblioitemnumber $biblioitemnumber is not a number! Using a new biblioitemnumber\n";
3143
        }
3144
        elsif (! ($biblioitemnumber > 0)) {
3145
            $error .= "C4::Biblio::_koha_add_biblioitem():> biblioitemnumber $biblioitemnumber is not greater than 0! Using a new biblioitemnumber\n";
3146
        }
3147
        #The real Perl LONG_MAX is 2147483646, but using 2147483640
3148
        #  so we can detect the imminent primary key exhaustion before it actually is exhausted.
3149
        elsif (! ($biblioitemnumber < 2147483640)) { 
3150
            $error .= "C4::Biblio::_koha_add_biblioitem():> biblioitemnumber $biblioitemnumber is too large for Perl! Using a new biblioitemnumber\n";
3151
        }
3152
        else {
3153
            #We have validated the biblionumber and can prepare to insert it.
3154
            $isBiblioitemNumberOk = 'biblioitemnumber = ?,';
3155
        }
3156
        
3157
    }
3096
3158
3097
    my ($cn_sort) = GetClassSort( $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3159
    my ($cn_sort) = GetClassSort( $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3098
    my $query = "INSERT INTO biblioitems SET
3160
    my $query = "INSERT INTO biblioitems SET
3161
        $isBiblioitemNumberOk
3099
        biblionumber    = ?,
3162
        biblionumber    = ?,
3100
        volume          = ?,
3163
        volume          = ?,
3101
        number          = ?,
3164
        number          = ?,
Lines 3129-3152 sub _koha_add_biblioitem { Link Here
3129
        agerestriction  = ?
3192
        agerestriction  = ?
3130
        ";
3193
        ";
3131
    my $sth = $dbh->prepare($query);
3194
    my $sth = $dbh->prepare($query);
3132
    $sth->execute(
3195
    
3133
        $biblioitem->{'biblionumber'},     $biblioitem->{'volume'},           $biblioitem->{'number'},                $biblioitem->{'itemtype'},
3196
    if ($isBiblioitemNumberOk) {
3134
        $biblioitem->{'isbn'},             $biblioitem->{'issn'},             $biblioitem->{'publicationyear'},       $biblioitem->{'publishercode'},
3197
        $sth->execute(
3135
        $biblioitem->{'volumedate'},       $biblioitem->{'volumedesc'},       $biblioitem->{'collectiontitle'},       $biblioitem->{'collectionissn'},
3198
            $biblioitem->{'biblioitemnumber'},
3136
        $biblioitem->{'collectionvolume'}, $biblioitem->{'editionstatement'}, $biblioitem->{'editionresponsibility'}, $biblioitem->{'illus'},
3199
            $biblioitem->{'biblionumber'},     $biblioitem->{'volume'},           $biblioitem->{'number'},                $biblioitem->{'itemtype'},
3137
        $biblioitem->{'pages'},            $biblioitem->{'bnotes'},           $biblioitem->{'size'},                  $biblioitem->{'place'},
3200
            $biblioitem->{'isbn'},             $biblioitem->{'issn'},             $biblioitem->{'publicationyear'},       $biblioitem->{'publishercode'},
3138
        $biblioitem->{'lccn'},             $biblioitem->{'marc'},             $biblioitem->{'url'},                   $biblioitem->{'biblioitems.cn_source'},
3201
            $biblioitem->{'volumedate'},       $biblioitem->{'volumedesc'},       $biblioitem->{'collectiontitle'},       $biblioitem->{'collectionissn'},
3139
        $biblioitem->{'cn_class'},         $biblioitem->{'cn_item'},          $biblioitem->{'cn_suffix'},             $cn_sort,
3202
            $biblioitem->{'collectionvolume'}, $biblioitem->{'editionstatement'}, $biblioitem->{'editionresponsibility'}, $biblioitem->{'illus'},
3140
        $biblioitem->{'totalissues'},      $biblioitem->{'ean'},              $biblioitem->{'agerestriction'}
3203
            $biblioitem->{'pages'},            $biblioitem->{'bnotes'},           $biblioitem->{'size'},                  $biblioitem->{'place'},
3141
    );
3204
            $biblioitem->{'lccn'},             $biblioitem->{'marc'},             $biblioitem->{'url'},                   $biblioitem->{'biblioitems.cn_source'},
3142
    my $bibitemnum = $dbh->{'mysql_insertid'};
3205
            $biblioitem->{'cn_class'},         $biblioitem->{'cn_item'},          $biblioitem->{'cn_suffix'},             $cn_sort,
3206
            $biblioitem->{'totalissues'},      $biblioitem->{'ean'},              $biblioitem->{'agerestriction'}
3207
        );
3208
    }
3209
    else {
3210
        $sth->execute(
3211
            $biblioitem->{'biblionumber'},     $biblioitem->{'volume'},           $biblioitem->{'number'},                $biblioitem->{'itemtype'},
3212
            $biblioitem->{'isbn'},             $biblioitem->{'issn'},             $biblioitem->{'publicationyear'},       $biblioitem->{'publishercode'},
3213
            $biblioitem->{'volumedate'},       $biblioitem->{'volumedesc'},       $biblioitem->{'collectiontitle'},       $biblioitem->{'collectionissn'},
3214
            $biblioitem->{'collectionvolume'}, $biblioitem->{'editionstatement'}, $biblioitem->{'editionresponsibility'}, $biblioitem->{'illus'},
3215
            $biblioitem->{'pages'},            $biblioitem->{'bnotes'},           $biblioitem->{'size'},                  $biblioitem->{'place'},
3216
            $biblioitem->{'lccn'},             $biblioitem->{'marc'},             $biblioitem->{'url'},                   $biblioitem->{'biblioitems.cn_source'},
3217
            $biblioitem->{'cn_class'},         $biblioitem->{'cn_item'},          $biblioitem->{'cn_suffix'},             $cn_sort,
3218
            $biblioitem->{'totalissues'},      $biblioitem->{'ean'},              $biblioitem->{'agerestriction'}
3219
        );
3220
    }
3221
    
3222
    $biblioitemnumber = $isBiblioitemNumberOk ? $biblioitemnumber : $dbh->{'mysql_insertid'};
3143
3223
3144
    if ( $dbh->errstr ) {
3224
    if ( $dbh->errstr ) {
3145
        $error .= "ERROR in _koha_add_biblioitem $query" . $dbh->errstr;
3225
        $error .= "ERROR in _koha_add_biblioitem $query" . $dbh->errstr;
3226
    }
3227
    
3228
    if (defined $error) {
3146
        warn $error;
3229
        warn $error;
3147
    }
3230
    }
3231
    
3148
    $sth->finish();
3232
    $sth->finish();
3149
    return ( $bibitemnum, $error );
3233
    return ( $biblioitemnumber, $error );
3150
}
3234
}
3151
3235
3152
=head2 _koha_delete_biblio
3236
=head2 _koha_delete_biblio
(-)a/misc/migration_tools/bulkmarcimport.pl (-2 / +68 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 204-209 RECORD: while ( ) { Link Here
204
    $i++;
214
    $i++;
205
    print ".";
215
    print ".";
206
    print "\n$i" unless $i % 100;
216
    print "\n$i" unless $i % 100;
217
	
218
	
219
	
220
	### If you are like me and don't have the legacy id in $999c and $999d, you
221
	###   can use this functionality to copy the id on the fly.
222
	### By default, copy legacy id field 001 to 999c and 999d ###
223
	if ($legacyIdAsBiblionumberFromHere) {
224
		my $legacyIdField = $record->field( $legacyIdAsBiblionumberFromHere );
225
		if ($legacyIdField) {
226
			my $legacyId = $legacyIdField->data();
227
			my $biblionumberField = MARC::Field->new( $tagid, '', '',
228
			   $subfieldid => $legacyId,
229
			   $binum_subfieldid => $legacyId,
230
			);
231
			$record->append_fields($biblionumberField);	
232
		}
233
		else {
234
			warn "No $legacyIdAsBiblionumberFromHere found!";
235
		}	
236
	}
207
    
237
    
208
    # transcode the record to UTF8 if needed & applicable.
238
    # transcode the record to UTF8 if needed & applicable.
209
    if ($record->encoding() eq 'MARC-8' and not $skip_marc8_conversion) {
239
    if ($record->encoding() eq 'MARC-8' and not $skip_marc8_conversion) {
Lines 579-584 bulkmarcimport.pl - Import bibliographic/authority records into Koha Link Here
579
 $ export KOHA_CONF=/etc/koha.conf
609
 $ export KOHA_CONF=/etc/koha.conf
580
 $ perl misc/migration_tools/bulkmarcimport.pl -d -commit 1000 \\
610
 $ perl misc/migration_tools/bulkmarcimport.pl -d -commit 1000 \\
581
    -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
582
620
583
=head1 WARNING
621
=head1 WARNING
584
622
Lines 586-591 Don't use this script before you've entered and checked your MARC parameters Link Here
586
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
587
will get invalid data.
625
will get invalid data.
588
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
589
=head1 DESCRIPTION
636
=head1 DESCRIPTION
590
637
591
=over
638
=over
Lines 685-690 Field store ids in I<FIELD> (usefull for authorities, where 001 contains the Link Here
685
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
686
from LOC or BNF. useless for biblios probably)
733
from LOC or BNF. useless for biblios probably)
687
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
688
=item B<-match>=<FIELD>
741
=item B<-match>=<FIELD>
689
742
690
I<FIELD> matchindex,fieldtomatch matchpoint to use to deduplicate fieldtomatch
743
I<FIELD> matchindex,fieldtomatch matchpoint to use to deduplicate fieldtomatch
Lines 732-737 This is the code for the framework that the requested records will have attached Link Here
732
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
733
will be used.
786
will be used.
734
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
735
=back
801
=back
736
802
737
=cut
803
=cut
(-)a/t/db_dependent/PreserveLegacyId.t (-1 / +150 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
#
3
# This Koha test module is a stub!  
4
# Add more tests here!!!
5
6
use Modern::Perl;
7
use utf8;
8
9
use Test::More tests => 3;
10
use MARC::Record;
11
12
BEGIN {
13
        use_ok('C4::Record');
14
        use_ok('C4::Biblio');
15
}
16
17
### Preparing our testing data ###
18
my $bibFramework = ''; #Using the default bibliographic framework.
19
my $marcxml=qq(<?xml version="1.0" encoding="UTF-8"?>
20
<record format="MARC21" type="Bibliographic">
21
  <leader>00000cim a22000004a 4500</leader>
22
  <controlfield tag="001">1001</controlfield>
23
  <controlfield tag="005">2013-06-03 07:04:07+02</controlfield>
24
  <controlfield tag="007">ss||||j|||||||</controlfield>
25
  <controlfield tag="008">       uuuu    xxk|||||||||||||||||eng|c</controlfield>
26
  <datafield tag="020" ind1=" " ind2=" ">
27
    <subfield code="a">0-00-103147-3</subfield>
28
    <subfield code="c">14.46 EUR</subfield>
29
  </datafield>
30
  <datafield tag="041" ind1="0" ind2=" ">
31
    <subfield code="d">eng</subfield>
32
  </datafield>
33
  <datafield tag="084" ind1=" " ind2=" ">
34
    <subfield code="a">83.5</subfield>
35
    <subfield code="2">ykl</subfield>
36
  </datafield>
37
  <datafield tag="100" ind1="1" ind2=" ">
38
    <subfield code="a">SHAKESPEARE, WILLIAM.</subfield>
39
  </datafield>
40
  <datafield tag="245" ind1="1" ind2="4">
41
    <subfield code="a">THE TAMING OF THE SHREW /</subfield>
42
    <subfield code="c">WILLIAM SHAKESPEARE</subfield>
43
    <subfield code="h">[ÄÄNITE].</subfield>
44
  </datafield>
45
  <datafield tag="260" ind1=" " ind2=" ">
46
    <subfield code="a">LONDON :</subfield>
47
    <subfield code="b">COLLINS.</subfield>
48
  </datafield>
49
  <datafield tag="300" ind1=" " ind2=" ">
50
    <subfield code="a">2 ÄÄNIKASETTIA.</subfield>
51
  </datafield>
52
  <datafield tag="852" ind1=" " ind2=" ">
53
    <subfield code="a">FI-Jm</subfield>
54
    <subfield code="h">83.5</subfield>
55
  </datafield>
56
  <datafield tag="852" ind1=" " ind2=" ">
57
    <subfield code="a">FI-Konti</subfield>
58
    <subfield code="h">83.5</subfield>
59
  </datafield>
60
</record>
61
);
62
63
my $record=marcxml2marc($marcxml);
64
65
my ( $biblionumberTagid, $biblionumberSubfieldid ) =
66
            GetMarcFromKohaField( "biblio.biblionumber", $bibFramework );
67
my ( $biblioitemnumberTagid, $biblioitemnumberSubfieldid ) =
68
            GetMarcFromKohaField( "biblioitems.biblioitemnumber", $bibFramework );
69
70
# Making sure the Koha to MARC mappings stand correct
71
is ($biblionumberTagid, $biblioitemnumberTagid, 'Testing "Koha to MARC mapping" biblionumber and biblioitemnumber source MARC field sameness');
72
if ($biblionumberTagid ne $biblioitemnumberTagid) {
73
	warn "Koha to MARC mapping values biblio.biblionumber and biblioitems.biblioitemnumber must share the same MARC field!";
74
}
75
76
#Moving the 001 to the field configured for legacy id
77
my $legacyIdField = $record->field( '001' );
78
my $legacyId = $legacyIdField->data();
79
my $biblionumberField = MARC::Field->new( $biblionumberTagid, '', '',
80
   $biblionumberSubfieldid => $legacyId,
81
   $biblioitemnumberSubfieldid => $legacyId,
82
);
83
$record->append_fields($biblionumberField);
84
85
#Convenience method to easily change the legacy id.
86
# Is used to test out database id boundary values.
87
sub changeLegacyId {
88
    my ($record, $newId) = @_;
89
    
90
    # Because the author of Marc::Record decided it would be nice to replace all subfields with new ones whenever they are updated,
91
    #  old references are lost and need to be found again. 
92
    my $legacyIdField = $record->field( '001' );
93
    my $biblionumberField = $record->field( $biblionumberTagid );
94
    
95
    $legacyIdField->update( $newId );
96
    $biblionumberField->update( $biblionumberSubfieldid => $newId,
97
                                $biblioitemnumberSubfieldid => $newId);
98
    $legacyId = $newId;
99
100
    
101
}
102
103
##############################
104
### Running the main test! ###
105
##############################
106
107
## INSERT the record to the DB, SELECT the one we got,
108
## then find out if the biblionumber and biblioitemnumbers are the original ones.
109
110
my $dbh = C4::Context->dbh;
111
112
$dbh->{AutoCommit} = 0; #We don't want to save anything in DB as this is just a test run!
113
$dbh->{RaiseError} = 1;
114
115
##Testing just below the critical threshold in _koha_add_item() primary key validators.
116
##  Test should run OK. Also this biblionumber hardly is reserved :)
117
changeLegacyId($record, 2147483639);
118
119
my ( $newBiblionumber, $newBiblioitemnumber ) = AddBiblio( $record, $bibFramework, { defer_marc_save => 1 } );
120
my $selectedBiblio = GetBiblio($legacyId);
121
122
is ($legacyId, $newBiblionumber, 'Biblionumber returned by AddBiblio matches');
123
is ($legacyId, $selectedBiblio->{biblionumber}, 'Biblionumber returned by GetBiblio matches the legacy biblionumber');
124
125
126
##Testing primary key exhaustion situation. Validator should prevent biblionumber or biblioitemnumber INSERTion
127
##  if the value is over the safety threshold 2147483639 == LONG_MAX
128
##  So both tests should fail.
129
changeLegacyId($record, 2147483645);
130
131
( $newBiblionumber, $newBiblioitemnumber ) = AddBiblio( $record, $bibFramework, { defer_marc_save => 1 } );
132
$selectedBiblio = GetBiblio($legacyId);
133
134
isnt ($legacyId, $newBiblionumber, 'Testing primary key exhaustion prevention.');
135
isnt ($legacyId, $selectedBiblio->{biblionumber}, 'Primary key exhausted biblio matching');
136
137
##Testing bad primary key situation. Validator should prevent biblionumber or biblioitemnumber INSERTion
138
##  if the value is not a pure integer/long.
139
##  So both tests should fail.
140
changeLegacyId($record, '12134FAN');
141
142
( $newBiblionumber, $newBiblioitemnumber ) = AddBiblio( $record, $bibFramework, { defer_marc_save => 1 } );
143
$selectedBiblio = GetBiblio($legacyId);
144
145
isnt ($legacyId, $newBiblionumber, 'Testing primary key not-an-integer prevention.');
146
isnt ($legacyId, $selectedBiblio->{biblionumber}, 'Primary key not-an-integer biblio matching');
147
148
149
$dbh->rollback();
150
$dbh->disconnect();

Return to bug 6113