Bug fixes
[koha.git] / updater / updatedatabase
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Database Updater
6 # This script checks for required updates to the database.
7
8 # Part of the Koha Library Software www.koha.org
9 # Licensed under the GPL.
10
11 # Bugs/ToDo:
12 # - Would also be a good idea to offer to do a backup at this time...
13
14 # NOTE:  If you do something more than once in here, make it table driven.
15
16 use strict;
17
18 # CPAN modules
19 use DBI;
20
21 # Koha modules
22 use C4::Context("/etc/koha.conf.tmp");
23
24 # FIXME - /etc/koha.conf might not exist, so shouldn't use
25 # C4::Context.
26
27 # FIXME - The user might be installing a new database, so can't rely
28 # on /etc/koha.conf anyway.
29
30 my $debug = 0;
31
32 my (
33     $sth, $sti,
34     $query,
35     %existingtables,    # tables already in database
36     %types,
37     $table,
38     $column,
39     $type, $null, $key, $default, $extra,
40     $prefitem,          # preference item in systempreferences table
41 );
42
43 my $dbh = C4::Context->dbh;
44
45 #-------------------
46 # Defines
47
48 # Tables to add if they don't exist
49 my %requiretables = (
50     shelfcontents => "( shelfnumber int not null,
51                                                         itemnumber int not null,
52                                                         flags int)",
53     bookshelf => "( shelfnumber int auto_increment primary key,
54                                                 shelfname char(255))",
55     z3950queue => "( id int auto_increment primary key,
56                                                 term text,
57                                                 type char(10),
58                                                 startdate int,
59                                                 enddate int,
60                                                 done smallint,
61                                                 results longblob,
62                                                 numrecords int,
63                                                 servers text,
64                                                 identifier char(30))",
65     z3950results => "( id int auto_increment primary key,
66                                                 queryid int,
67                                                 server char(255),
68                                                 startdate int,
69                                                 enddate int,
70                                                 results longblob,
71                                                 numrecords int,
72                                                 numdownloaded int,
73                                                 highestseen int,
74                                                 active smallint)",
75     branchrelations => "( branchcode varchar(4),
76                                                         categorycode varchar(4))",
77     websites => "( websitenumber int(11) NOT NULL auto_increment,
78                                                 biblionumber int(11) NOT NULL default '0',
79                                                 title text,
80                                                 description text,
81                                                 url varchar(255),
82                                                 PRIMARY KEY (websitenumber) )",
83     marcrecorddone => "( isbn char(40),
84                                                                 issn char(40),
85                                                                 lccn char(40),
86                                                                 controlnumber char(40))",
87     uploadedmarc => "( id int(11) NOT NULL auto_increment PRIMARY KEY,
88                                                         marc longblob,
89                                                         hidden smallint(6) default NULL,
90                                                         name varchar(255) default NULL)",
91     ethnicity => "( code varchar(10) NOT NULL default '',
92                                         name varchar(255) default NULL,
93                                         PRIMARY KEY  (code)   )",
94     sessions => "( sessionID varchar(255) NOT NULL default '',
95                                                 userid varchar(255) default NULL,
96                                                 ip varchar(16) default NULL,
97                                                 lasttime int,
98                                                 PRIMARY KEY (sessionID)   )",
99     sessionqueries => "( sessionID varchar(255) NOT NULL default '',
100                                                                 userid char(100) NOT NULL default '',
101                                                                 ip char(18) NOT NULL default '',
102                                                                 url text NOT NULL default ''  )",
103     bibliothesaurus => "( id bigint(20) NOT NULL auto_increment,
104                                                         freelib char(255) NOT NULL default '',
105                                                         stdlib char(255) NOT NULL default '',
106                                                         category char(10) NOT NULL default '',
107                                                         level tinyint(4) NOT NULL default '1',
108                                                         hierarchy char(80) NOT NULL default '',
109                                                         father bigint(20) NOT NULL default '',
110                                                         PRIMARY KEY  (id),
111                                                         KEY freelib (freelib),
112                                                         KEY stdlib (stdlib),
113                                                         KEY category (category),
114                                                         KEY hierarchy (hierarchy)
115                                                         )",
116     marc_biblio => "(
117                                                 bibid bigint(20) unsigned NOT NULL auto_increment,
118                                                 biblionumber int(11) NOT NULL default '0',
119                                                 datecreated date NOT NULL default '0000-00-00',
120                                                 datemodified date default NULL,
121                                                 origincode char(20) default NULL,
122                                                 PRIMARY KEY  (bibid),
123                                                 KEY origincode (origincode),
124                                                 KEY biblionumber (biblionumber)
125                                                 ) ",
126     marc_blob_subfield => "(
127                                         blobidlink bigint(20) NOT NULL auto_increment,
128                                         subfieldvalue longtext NOT NULL,
129                                         PRIMARY KEY  (blobidlink)
130                                         ) ",
131     marc_subfield_structure => "(
132                                                 tagfield char(3) NOT NULL default '',
133                                                 tagsubfield char(1) NOT NULL default '',
134                                                 liblibrarian char(255) NOT NULL default '',
135                                                 libopac char(255) NOT NULL default '',
136                                                 repeatable tinyint(4) NOT NULL default '0',
137                                                 mandatory tinyint(4) NOT NULL default '0',
138                                                 kohafield char(40)  default NULL,
139                                                 tab tinyint(1) default NULL,
140                                                 authorised_value char(10) default NULL,
141                                                 thesaurus_category char(10) default NULL,
142                                                 value_builder char(80) default NULL,
143                                                 PRIMARY KEY  (tagfield,tagsubfield),
144                                                 KEY kohafield (kohafield),
145                                                 KEY tab (tab)
146                                                 )",
147     marc_subfield_table => "(
148                                                 subfieldid bigint(20) unsigned NOT NULL auto_increment,
149                                                 bibid bigint(20) unsigned NOT NULL default '0',
150                                                 tag char(3) NOT NULL default '',
151                                                 tagorder tinyint(4) NOT NULL default '1',
152                                                 tag_indicator char(2) NOT NULL default '',
153                                                 subfieldcode char(1) NOT NULL default '',
154                                                 subfieldorder tinyint(4) NOT NULL default '1',
155                                                 subfieldvalue varchar(255) default NULL,
156                                                 valuebloblink bigint(20) default NULL,
157                                                 PRIMARY KEY  (subfieldid),
158                                                 KEY bibid (bibid),
159                                                 KEY tag (tag),
160                                                 KEY tag_indicator (tag_indicator),
161                                                 KEY subfieldorder (subfieldorder),
162                                                 KEY subfieldcode (subfieldcode),
163                                                 KEY subfieldvalue (subfieldvalue),
164                                                 KEY tagorder (tagorder)
165                                         )",
166     marc_tag_structure => "(
167                                         tagfield char(3) NOT NULL default '',
168                                         liblibrarian char(255) NOT NULL default '',
169                                         libopac char(255) NOT NULL default '',
170                                         repeatable tinyint(4) NOT NULL default '0',
171                                         mandatory tinyint(4) NOT NULL default '0',
172                                         authorised_value char(10) default NULL,
173                                         PRIMARY KEY  (tagfield)
174                                         )",
175     marc_word => "(
176                                 bibid bigint(20) NOT NULL default '0',
177                                 tag char(3) NOT NULL default '',
178                                 tagorder tinyint(4) NOT NULL default '1',
179                                 subfieldid char(1) NOT NULL default '',
180                                 subfieldorder tinyint(4) NOT NULL default '1',
181                                 word varchar(255) NOT NULL default '',
182                                 sndx_word varchar(255) NOT NULL default '',
183                                 KEY bibid (bibid),
184                                 KEY tag (tag),
185                                 KEY tagorder (tagorder),
186                                 KEY subfieldid (subfieldid),
187                                 KEY subfieldorder (subfieldorder),
188                                 KEY word (word),
189                                 KEY sndx_word (sndx_word)
190                         )",
191     marc_breeding => "(  id bigint(20) NOT NULL auto_increment,
192                                 file varchar(80) NOT NULL default '',
193                                 isbn varchar(10) NOT NULL default '',
194                                 title varchar(128) default NULL,
195                                 author varchar(80) default NULL,
196                                 marc text NOT NULL,
197                                 encoding varchar(40) default NULL,
198                                 PRIMARY KEY  (id),
199                                 KEY title (title),
200                                 KEY isbn (isbn)
201                         )",
202     authorised_values => "(id int(11) NOT NULL auto_increment,
203                                 category char(10) NOT NULL default '',
204                                 authorised_value char(80) NOT NULL default '',
205                                 lib char(80) NULL,
206                                 PRIMARY KEY  (id),
207                                 KEY name (category)
208                         )",
209     userflags => "( bit int(11) NOT NULL default '0',
210                                 flag char(30), flagdesc char(255),
211                                 defaulton int(11)
212                         )",
213 );
214
215 my %requirefields = (
216     biblio        => { 'abstract' => 'text' },
217     deletedbiblio => { 'abstract' => 'text' },
218     biblioitems   => {
219         'lccn' => 'char(25)',
220         'url'  => 'varchar(255)',
221         'marc' => 'text'
222     },
223     deletedbiblioitems => {
224         'lccn' => 'char(25)',
225         'url'  => 'varchar(255)',
226         'marc' => 'text'
227     },
228     branchtransfers => { 'datearrived'    => 'datetime' },
229     statistics      => { 'borrowernumber' => 'int(11)' },
230     aqbooksellers   => {
231         'invoicedisc' => 'float(6,4)',
232         'nocalc'      => 'int(11)'
233     },
234     borrowers => {
235         'userid'        => 'char(30)',
236         'password'      => 'char(30)',
237         'flags'         => 'int(11)',
238         'textmessaging' => 'varchar(30)'
239     },
240     aqorders => { 'budgetdate' => 'date' },
241
242     #added so that reference items are not available for reserves...
243     itemtypes         => { 'notforloan'  => 'smallint(6)' },
244     systempreferences => { 'explanation' => 'char(80)' },
245     z3950servers      => { 'syntax'      => 'char(80)' },
246 );
247
248 my %dropable_table = (
249     classification => 'classification',
250     multipart      => 'multipart',
251     multivolume    => 'multivolume',
252     newitems       => 'newitems',
253     procedures     => 'procedures',
254     publisher      => 'publisher',
255     searchstats    => 'searchstats',
256     serialissues   => 'serialissues',
257 );
258
259 # The tabledata hash contains data that should be in the tables.
260 # The uniquefieldrequired hash entry is used to determine which (if any) fields
261 # must not exist in the table for this row to be inserted.  If the
262 # uniquefieldrequired entry is already in the table, the existing data is not
263 # modified.
264
265 my %tabledata = (
266     userflags => [
267         {
268             uniquefieldrequired => 'bit',
269             bit                 => 0,
270             flag                => 'superlibrarian',
271             flagdesc            => 'Access to all librarian functions',
272             defaulton           => 0
273         },
274         {
275             uniquefieldrequired => 'bit',
276             bit                 => 1,
277             flag                => 'circulate',
278             flagdesc            => 'Circulate books',
279             defaulton           => 0
280         },
281         {
282             uniquefieldrequired => 'bit',
283             bit                 => 2,
284             flag                => 'catalogue',
285             flagdesc            => 'View Catalogue (Librarian Interface)',
286             defaulton           => 0
287         },
288         {
289             uniquefieldrequired => 'bit',
290             bit                 => 3,
291             flag                => 'parameters',
292             flagdesc            => 'Set Koha system paramters',
293             defaulton           => 0
294         },
295         {
296             uniquefieldrequired => 'bit',
297             bit                 => 4,
298             flag                => 'borrowers',
299             flagdesc            => 'Add or modify borrowers',
300             defaulton           => 0
301         },
302         {
303             uniquefieldrequired => 'bit',
304             bit                 => 5,
305             flag                => 'permissions',
306             flagdesc            => 'Set user permissions',
307             defaulton           => 0
308         },
309         {
310             uniquefieldrequired => 'bit',
311             bit                 => 6,
312             flag                => 'reserveforothers',
313             flagdesc            => 'Reserve books for patrons',
314             defaulton           => 0
315         },
316         {
317             uniquefieldrequired => 'bit',
318             bit                 => 7,
319             flag                => 'borrow',
320             flagdesc            => 'Borrow books',
321             defaulton           => 1
322         },
323         {
324             uniquefieldrequired => 'bit',
325             bit                 => 8,
326             flag                => 'reserveforself',
327             flagdesc            => 'Reserve books for self',
328             defaulton           => 0
329         },
330         {
331             uniquefieldrequired => 'bit',
332             bit                 => 9,
333             flag                => 'editcatalogue',
334             flagdesc  => 'Edit Catalogue (Modify bibliographic/holdings data)',
335             defaulton => 0
336         },
337         {
338             uniquefieldrequired => 'bit',
339             bit                 => 10,
340             flag                => 'updatecharges',
341             flagdesc            => 'Update borrower charges',
342             defaulton           => 0
343         },
344     ],
345     systempreferences => [
346         {
347             uniquefieldrequired => 'variable',
348             variable            => 'autoMemberNum',
349             value               => '1',
350             explanation         => '1 or 0. If 1, Member number is auto-calculated'
351         },
352         {
353             uniquefieldrequired => 'variable',
354             variable            => 'acquisitions',
355             value               => 'simple',
356             explanation         =>
357 'normal or simple : whether to use "acqui" or "acqui.simple" acquisition system'
358         },
359         {
360             uniquefieldrequired => 'variable',
361             variable            => 'dateformat',
362             value               => 'metric',
363             explanation         => 'metric, us, or iso'
364         },
365         {
366             uniquefieldrequired => 'variable',
367             variable            => 'template',
368             value               => 'default',
369             explanation         => 'template default name'
370         },
371         {
372             uniquefieldrequired => 'variable',
373             variable            => 'autoBarcode',
374             value               => '1',
375             explanation         => '1 or 0. If 1, Barcode is auto-calculated'
376         },
377         {
378             uniquefieldrequired => 'variable',
379             variable            => 'insecure',
380             value               => 'NO',
381             explanation         =>
382 'if YES, no auth at all is needed. Be careful if you set this to yes !'
383         },
384         {
385             uniquefieldrequired => 'variable',
386             variable            => 'authoritysep',
387             value               => '--',
388             explanation         =>
389             'the separator used in authority/thesaurus. Usually --'
390         },
391         {
392             uniquefieldrequired => 'variable',
393             variable            => 'opaclanguages',
394             value               => 'en',
395             explanation         => 'languages'
396         },
397         {
398             uniquefieldrequired => 'variable',
399             variable            => 'opacthemes',
400             value               => 'default',
401             explanation         => 'theme'
402         },
403         {
404             uniquefieldrequired => 'variable',
405             variable            => 'timeout',
406             value               => '12000000',
407             explanation         => 'login timeout'
408         },
409         {
410             uniquefieldrequired => 'variable',
411             variable            => 'marc',
412             value               => 'ON',
413             explanation         => 'MARC support (ON or OFF)'
414         },
415         {
416             uniquefieldrequired => 'variable',
417             variable            => 'marcflavour',
418             value               => 'MARC21',
419             explanation         =>
420             'your MARC flavor (MARC21 or UNIMARC) used for character encoding'
421         },
422         {
423             uniquefieldrequired => 'variable',
424             variable            => 'checkdigit',
425             value               => 'katipo',
426             explanation         =>
427             'none= no check on member cardnumber. katipo= katipo check'
428         },
429         {
430             uniquefieldrequired => 'variable',
431             variable            => 'dateformat',
432             value               => 'ISO',
433             explanation         =>
434             'date format (US mm/dd/yyyy, metric dd/mm/yyy, ISO yyyy/mm/dd) '
435         },
436         {
437             uniquefieldrequired => 'variable',
438             variable            => 'maxoutstanding',
439             value               => '5',
440             explanation         =>
441             'maximum amount withstanding to be able make reserves '
442         },
443         {
444             uniquefieldrequired => 'variable',
445             variable            => 'maxreserves',
446             value               => '5',
447             explanation         =>
448             'maximum number of reserves a member can make '
449         },
450         {
451             uniquefieldrequired => 'variable',
452             variable            => 'KohaAdminEmailAddress',
453             value               => 'your.mail@here',
454             explanation => 'the email adress where borrowers modifs are sent'
455         },
456     ],
457
458 );
459
460 my %fielddefinitions = (
461     printers => [
462         {
463             field   => 'printername',
464             type    => 'char(40)',
465             null    => '',
466             key     => 'PRI',
467             default => ''
468         },
469     ],
470     aqbookfund => [
471         {
472             field   => 'bookfundid',
473             type    => 'char(5)',
474             null    => '',
475             key     => 'PRI',
476             default => ''
477         },
478     ],
479     z3950servers => [
480         {
481             field   => 'id',
482             type    => 'int',
483             null    => '',
484             key     => 'PRI',
485             default => '',
486             extra   => 'auto_increment'
487         },
488     ],
489 );
490
491 #-------------------
492 # Initialize
493
494 # Start checking
495
496 # Get version of MySQL database engine.
497 my $mysqlversion = `mysqld --version`;
498 $mysqlversion =~ /Ver (\S*) /;
499 $mysqlversion = $1;
500 if ( $mysqlversion ge '3.23' ) {
501     print "Could convert to MyISAM database tables...\n";
502 }
503
504 #---------------------------------
505 # Tables
506
507 # Collect all tables into a list
508 $sth = $dbh->prepare("show tables");
509 $sth->execute;
510 while ( my ($table) = $sth->fetchrow ) {
511     $existingtables{$table} = 1;
512 }
513
514 # Now add any missing tables
515 foreach $table ( keys %requiretables ) {
516     print "Checking $table table...\n" if $debug;
517     unless ( $existingtables{$table} ) {
518         print "Adding $table table...\n";
519         my $sth = $dbh->prepare("create table $table $requiretables{$table}");
520         $sth->execute;
521         if ( $sth->err ) {
522             print "Error : $sth->errstr \n";
523             $sth->finish;
524         }    # if error
525     }    # unless exists
526 }    # foreach
527
528 # now drop useless tables
529 foreach $table ( keys %dropable_table ) {
530     print "Dropping unused tables...\n" if $debug;
531     if ( $existingtables{$table} ) {
532         $dbh->do("drop table $table");
533         if ( $dbh->err ) {
534             print "Error : $dbh->errstr \n";
535         }
536     }
537 }
538 unless ( $existingtables{'z3950servers'} ) {
539     print "Adding z3950servers table...\n";
540     my $sti = $dbh->prepare( "create table z3950servers (
541                                                                                 host char(255),
542                                                                                 port int,
543                                                                                 db char(255),
544                                                                                 userid char(255),
545                                                                                 password char(255),
546                                                                                 name text,
547                                                                                 id int,
548                                                                                 checked smallint,
549                                                                                 rank int)"
550     );
551     $sti->execute;
552     $sti = $dbh->prepare( "insert into z3950servers
553                                                                 values ('z3950.loc.gov',
554                                                                 7090,
555                                                                 'voyager',
556                                                                 '', '',
557                                                                 'Library of Congress',
558                                                                 1, 1, 1)"
559     );
560     $sti->execute;
561 }
562
563 #---------------------------------
564 # Columns
565
566 foreach $table ( keys %requirefields ) {
567     print "Check table $table\n" if $debug;
568     $sth = $dbh->prepare("show columns from $table");
569     $sth->execute();
570     undef %types;
571     while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
572     {
573         $types{$column} = $type;
574     }    # while
575     foreach $column ( keys %{ $requirefields{$table} } ) {
576         print "  Check column $column\n" if $debug;
577         if ( !$types{$column} ) {
578
579             # column doesn't exist
580             print "Adding $column field to $table table...\n";
581             $query = "alter table $table
582                         add column $column " . $requirefields{$table}->{$column};
583             print "Execute: $query\n" if $debug;
584             my $sti = $dbh->prepare($query);
585             $sti->execute;
586             if ( $sti->err ) {
587                 print "**Error : $sti->errstr \n";
588                 $sti->finish;
589             }    # if error
590         }    # if column
591     }    # foreach column
592 }    # foreach table
593
594 foreach $table ( keys %fielddefinitions ) {
595     print "Check table $table\n" if $debug;
596     $sth = $dbh->prepare("show columns from $table");
597     $sth->execute();
598     my $definitions;
599     while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
600     {
601         $definitions->{$column}->{type}    = $type;
602         $definitions->{$column}->{null}    = $null;
603         $definitions->{$column}->{key}     = $key;
604         $definitions->{$column}->{default} = $default;
605         $definitions->{$column}->{extra}   = $extra;
606     }    # while
607     my $fieldrow = $fielddefinitions{$table};
608     foreach my $row (@$fieldrow) {
609         my $field   = $row->{field};
610         my $type    = $row->{type};
611         my $null    = $row->{null};
612         my $key     = $row->{key};
613         my $default = $row->{default};
614         my $extra   = $row->{extra};
615         my $def     = $definitions->{$field};
616         unless ( $type eq $def->{type}
617             && $null eq $def->{null}
618             && $key eq $def->{key}
619             && $default eq $def->{default}
620             && $extra eq $def->{extra} )
621         {
622
623             if ( $null eq '' ) {
624                 $null = 'NOT NULL';
625             }
626             if ( $key eq 'PRI' ) {
627                 $key = 'PRIMARY KEY';
628             }
629             unless ( $extra eq 'auto_increment' ) {
630                 $extra = '';
631             }
632             my $sth =
633               $dbh->prepare(
634 "alter table $table change $field $field $type $null $key $extra default ?"
635             );
636             $sth->execute($default);
637             print "  Alter $field in $table\n";
638         }
639     }
640 }
641
642 # Get list of columns from items table
643 my %itemtypes;
644
645 $sth = $dbh->prepare("show columns from items");
646 $sth->execute;
647 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
648 {
649     $itemtypes{$column} = $type;
650 }
651
652 unless ( $itemtypes{'barcode'} eq 'varchar(20)' ) {
653     $itemtypes{'barcode'} =~ /varchar\((\d+)\)/;
654     my $oldlength = $1;
655     if ( $oldlength < 20 ) {
656         print "Setting maximum barcode length to 20 (was $oldlength).\n";
657         my $sti =
658           $dbh->prepare(
659             "alter table items change barcode barcode varchar(20) not null");
660         $sti->execute;
661     }
662 }
663
664 # extending the timestamp in branchtransfers...
665 my %branchtransfers;
666
667 $sth = $dbh->prepare("show columns from branchtransfers");
668 $sth->execute;
669 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
670 {
671     $branchtransfers{$column} = $type;
672 }
673
674 unless ( $branchtransfers{'datesent'} eq 'datetime' ) {
675     print "Setting type of datesent in branchtransfers to datetime.\n";
676     my $sti =
677       $dbh->prepare(
678         "alter table branchtransfers change datesent datesent datetime");
679     $sti->execute;
680 }
681
682 unless ( $branchtransfers{'datearrived'} eq 'datetime' ) {
683     print "Setting type of datearrived in branchtransfers to datetime.\n";
684     my $sti =
685       $dbh->prepare(
686         "alter table branchtransfers change datearrived datearrived datetime");
687     $sti->execute;
688 }
689
690 # changing the branchcategories table around...
691 my %branchcategories;
692
693 $sth = $dbh->prepare("show columns from branchcategories");
694 $sth->execute;
695 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
696 {
697     $branchcategories{$column} = $type;
698 }
699
700 unless ( $branchcategories{'categorycode'} eq 'varchar(4)' ) {
701     print
702 "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
703     my $sti =
704       $dbh->prepare(
705 "alter table branchcategories change categorycode categorycode varchar(4) not null"
706     );
707     $sti->execute;
708     $sti =
709       $dbh->prepare(
710         "alter table branchcategories add primary key (categorycode)");
711     $sti->execute;
712 }
713
714 unless ( $branchcategories{'categoryname'} eq 'text' ) {
715     print "Changing branchcode in branchcategories to categoryname text.\n";
716     my $sth =
717       $dbh->prepare(
718         "alter table branchcategories change branchcode categoryname text");
719     $sth->execute;
720 }
721
722 unless ( $branchcategories{'codedescription'} eq 'text' ) {
723     print
724 "Replacing branchholding in branchcategories with codedescription text.\n";
725     my $sth =
726       $dbh->prepare(
727         "alter table branchcategories change branchholding codedescription text"
728     );
729     $sth->execute;
730 }
731
732 # Populate tables with required data
733
734 foreach my $table ( keys %tabledata ) {
735     print "Checking for data required in table $table...\n";
736     my $tablerows = $tabledata{$table};
737     foreach my $row (@$tablerows) {
738         my $uniquefieldrequired = $row->{uniquefieldrequired};
739         my $uniquevalue         = $row->{$uniquefieldrequired};
740         my $sth                 =
741           $dbh->prepare(
742 "select $uniquefieldrequired from $table where $uniquefieldrequired=?"
743         );
744         $sth->execute($uniquevalue);
745         unless ( $sth->rows ) {
746             print "Adding row to $table: ";
747             my @values;
748             my $fieldlist;
749             my $placeholders;
750             foreach my $field ( keys %$row ) {
751                 (next) if ( $field eq 'uniquefieldrequired' );
752                 my $value = $row->{$field};
753                 push @values, $value;
754                 print "  $field => $value";
755                 $fieldlist .= "$field,";
756                 $placeholders .= "?,";
757             }
758             print "\n";
759             $fieldlist    =~ s/,$//;
760             $placeholders =~ s/,$//;
761             my $sth =
762               $dbh->prepare(
763                 "insert into $table ($fieldlist) values ($placeholders)");
764             $sth->execute(@values);
765         }
766     }
767 }
768
769 $sth->finish;
770
771 exit;
772
773 # $Log$
774 # Revision 1.45  2003/05/08 12:26:16  wolfpac444
775 # Bug fixes
776 #
777 # Revision 1.44  2003/05/03 05:39:57  rangi
778 # Fixing bug 429
779 # (Wording changes in the explanation fields in system preferences)
780 #
781 # Revision 1.43  2003/05/02 23:01:09  rangi
782 # Adding the textmessaging column to the borrowers table.
783 # insertdata.pl is expecting this to exist, and hence modifying/adding
784 # borrowers was broken.
785 #
786 # Also ran they script thru perltidy
787 #
788 # Revision 1.42  2003/04/29 16:53:25  tipaul
789 # really proud of this commit :-)
790 # z3950 search and import seems to works fine.
791 # Let me explain how :
792 # * a "search z3950" button is added in the addbiblio template.
793 # * when clicked, a popup appears and z3950/search.pl is called
794 # * z3950/search.pl calls addz3950search in the DB
795 # * the z3950 daemon retrieve the records and stores them in z3950results AND in marc_breeding table.
796 # * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
797 # * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
798 #
799 # Note :
800 # * character encoding support : (It's a nightmare...) In the z3950servers table, a "encoding" column has been added. You can put "UNIMARC" or "USMARC" in this column. Depending on this, the char_decode in C4::Biblio.pm replaces marc-char-encode by an iso 8859-1 encoding. Note that in the breeding import this value has been added too, for a better support.
801 # * the marc_breeding and z3950* tables have been modified : they have an encoding column and the random z3950 number is stored too for convenience => it's the key I use to list only requested biblios in the popup.
802 #
803 # Revision 1.41  2003/04/29 08:09:44  tipaul
804 # z3950 support is coming...
805 # * adding a syntax column in z3950 table = this column will say wether the z3950 must be called with PerferedRecordsyntax => USMARC or PerferedRecordsyntax => UNIMARC. I tried some french UNIMARC z3950 servers, and some only send USMARC, some only UNIMARC, some can answer with both.
806 # Note this is a 1st draft. More to follow (today ? I hope).
807 #
808 # Revision 1.40  2003/04/22 10:48:27  wolfpac444
809 # Added "father" column to bibliothesaurus table
810 #
811 # Revision 1.39  2003/04/04 08:45:00  tipaul
812 # last commits before 1.9.1
813 #
814 # Revision 1.38  2003/03/18 10:58:19  tipaul
815 # adding checkdigit parameter that choose how to check the members cardnumber.
816 # At the moment :
817 # * none = no checking
818 # * katipo = checked as before
819 #
820 # Revision 1.37  2003/01/30 01:47:48  acli
821 # Corrected syntax error reported by Benedict
822 #
823 # Made the indentation somewhat easier to read; the messiness probably caused
824 # the original syntax error.
825 #
826 # Revision 1.36  2003/01/28 15:13:30  tipaul
827 # userflag table now created in upgrade script (bugfix #171)
828 #
829 # Revision 1.35  2003/01/27 03:12:49  acli
830 # Reworded the description for "acquisitions" to make it fit on the screen
831 #
832 # Added "iso" to dateformat, since dateformat is not yet being used anyway
833 #
834 # Revision 1.34  2003/01/23 12:30:02  tipaul
835 # introducint marcflavour in systempref file : used for character decoding
836 #
837 # Revision 1.33  2003/01/21 09:03:27  tipaul
838 # bugfix (NOTE : this bugs makes installation of the 1.3.3 a little fuzzy. Please fix your DB if you installed 1.3.3)
839 #
840 # Revision 1.32  2003/01/16 10:29:45  tipaul
841 # adding a MARC parameter in systempref ( which is ON or OFF)
842 # the search will be a marc search if MARC=ON
843 # and a standard (v1.2) search if MARC=OFF
844 #
845 # Revision 1.31  2003/01/06 13:32:43  tipaul
846 # *** empty log message ***
847 #
848 # Revision 1.29  2003/01/06 11:14:11  tipaul
849 # last bugfixes before 1.3.3 : systempref table correctly filled
850 #
851 # Revision 1.28  2002/12/10 13:27:47  tipaul
852 # bugfixes (davide mails in koha-dev)
853 #
854 # Revision 1.27  2002/11/26 15:04:54  tipaul
855 # road to 1.3.2. Updating db structure during installation
856 #
857 # Revision 1.26  2002/11/12 17:42:40  tonnesen
858 # Merged some features over from rel-1-2, including primary key checking.
859 #
860 # Revision 1.25  2002/11/12 16:44:38  tipaul
861 # road to 1.3.2 :
862 # * many bugfixes
863 # * adding value_builder : you can map a subfield in the marc_subfield_structure to a sub stored in "value_builder" directory. In this directory you can create screen used to build values with any method. In this commit is a 1st draft of the builder for 100$a unimarc french subfield, which is composed of 35 digits, with 12 differents values (only the 4th first are provided for instance)
864 #
865 # Revision 1.24  2002/10/30 14:00:23  arensb
866 # (bug fix): Fixed typo.
867 #
868 # Revision 1.23  2002/10/25 10:55:46  tipaul
869 # Road to 1.3.2
870 # * bugfixes and improvements
871 # * manage mandatory MARC subfields
872 # * new table : authorised_values. this table contains categories and authorised values for the category. On MARC management, you can map a subfield to a authorised_values category. If you do this, the subfield can only be filled with a authorised_value of the selected category.
873 # this submit contains everything needed :
874 # * updatedatabase
875 # * admin screens
876 # * "links" management
877 # * creation of a html-list if a subfield is mapped to an authorised value.
878 #
879 # Note this is different from authorities support, which will come soon.
880 # The authorised_values is supposed to contains a "small" number of authorised values for a category (less than 50-100). If you enter more authorised values than this, it should be hard to find what you want in a BIG list...
881 #
882 # Revision 1.22  2002/10/15 10:08:19  tipaul
883 # fixme corrected, re-indent and adding the marc_breeding table (see commit of marcimport.pl for more explanations about breeding)
884 #
885 # Revision 1.21  2002/10/14 11:48:59  tipaul
886 # bugfix
887 #
888 # Revision 1.20  2002/10/10 04:49:41  arensb
889 # Added some FIXME comments.
890 #
891 # Revision 1.19  2002/10/05 10:17:17  arensb
892 # Merged with arensb-context branch: use C4::Context->dbh instead of
893 # &C4Connect, and generally prefer C4::Context over C4::Database.
894 #
895 # Revision 1.18.2.2  2002/10/05 06:18:43  arensb
896 # Added a whole mess of FIXME comments.
897 #
898 # Revision 1.18.2.1  2002/10/04 02:46:00  arensb
899 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
900 # C4Connect.
901 #
902 # Revision 1.18  2002/09/24 13:50:55  tipaul
903 # long WAS the road to 1.3.0...
904 # coming VERY SOON NOW...
905 # modifying installer and buildrelease to update the DB
906 #
907 # Revision 1.17  2002/09/24 12:57:35  tipaul
908 # long WAS the road to 1.3.0...
909 # coming VERY SOON NOW...
910 # modifying installer and buildrelease to update the DB
911 #
912 # Revision 1.16  2002/07/31 02:34:27  finlayt
913 #
914 # added "notforloan" field to the itemtypes table.
915 #
916 # Revision 1.15  2002/07/20 22:30:06  rangi
917 # Making sure fix makes it into the main branch as well
918 # Fix for bug 69
919 #
920 # Revision 1.14  2002/07/08 16:20:26  tonnesen
921 # Added sessionqueries table and password/userid fields to borrowers table
922 #
923 # Revision 1.13  2002/07/04 18:05:36  tonnesen
924 # bug fix
925 #
926 # Revision 1.12  2002/07/04 16:41:06  tonnesen
927 # Merged changes from rel-1-2.  Abstracted table structure changes by alan.
928 #