modify (initials,phone ) fields property in borrowers and deletedborrowers table
[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 use strict;
16
17 # CPAN modules
18 use DBI;
19 use Getopt::Long;
20 # Koha modules
21 use C4::Context;
22
23 use MARC::Record;
24 use MARC::File::XML ( BinaryEncoding => 'utf8' );
25  
26 # FIXME - The user might be installing a new database, so can't rely
27 # on /etc/koha.conf anyway.
28
29 my $debug = 0;
30
31 my (
32     $sth, $sti,
33     $query,
34     %existingtables,    # tables already in database
35     %types,
36     $table,
37     $column,
38     $type, $null, $key, $default, $extra,
39     $prefitem,          # preference item in systempreferences table
40 );
41
42 my $silent;
43 GetOptions(
44         's' =>\$silent
45         );
46 my $dbh = C4::Context->dbh;
47 print "connected to your DB. Checking & modifying it\n" unless $silent;
48 $|=1; # flushes output
49
50 #-------------------
51 # Defines
52
53 # Tables to add if they don't exist
54 my %requiretables = (
55     categorytable       => "(categorycode char(5) NOT NULL default '',
56                              description text default '',
57                              itemtypecodes text default '',
58                              PRIMARY KEY (categorycode)
59                             )",
60     subcategorytable       => "(subcategorycode char(5) NOT NULL default '',
61                              description text default '',
62                              itemtypecodes text default '',
63                              PRIMARY KEY (subcategorycode)
64                             )",
65     mediatypetable       => "(mediatypecode char(5) NOT NULL default '',
66                              description text default '',
67                              itemtypecodes text default '',
68                              PRIMARY KEY (mediatypecode)
69                             )",
70     action_logs         => "(
71                                     `timestamp` TIMESTAMP NOT NULL ,
72                                     `user` INT( 11 ) NOT NULL ,
73                                     `module` TEXT default '',
74                                     `action` TEXT default '' ,
75                                     `object` INT(11) default '' ,
76                                     `info` TEXT default '' ,
77                                     PRIMARY KEY ( `timestamp` , `user` )
78                             )",
79         letter          => "(
80                                         module varchar(20) NOT NULL default '',
81                                         code varchar(20) NOT NULL default '',
82                                         name varchar(100) NOT NULL default '',
83                                         title varchar(200) NOT NULL default '',
84                                         content text,
85                                         PRIMARY KEY  (module,code)
86                                 )",
87         alert           =>"(
88                                         alertid int(11) NOT NULL auto_increment,
89                                         borrowernumber int(11) NOT NULL default '0',
90                                         type varchar(10) NOT NULL default '',
91                                         externalid varchar(20) NOT NULL default '',
92                                         PRIMARY KEY  (alertid),
93                                         KEY borrowernumber (borrowernumber),
94                                         KEY type (type,externalid)
95                                 )",
96         opac_news => "(
97                                 `idnew` int(10) unsigned NOT NULL auto_increment,
98                                 `title` varchar(250) NOT NULL default '',
99                                 `new` text NOT NULL,
100                                 `lang` varchar(4) NOT NULL default '',
101                                 `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
102                                 PRIMARY KEY  (`idnew`)
103                                 )",
104         repeatable_holidays => "(
105                                 `id` int(11) NOT NULL auto_increment,
106                                 `branchcode` varchar(4) NOT NULL default '',
107                                 `weekday` smallint(6) default NULL,
108                                 `day` smallint(6) default NULL,
109                                 `month` smallint(6) default NULL,
110                                 `title` varchar(50) NOT NULL default '',
111                                 `description` text NOT NULL,
112                                 PRIMARY KEY  (`id`)
113                                 )",
114         special_holidays => "(
115                                 `id` int(11) NOT NULL auto_increment,
116                                 `branchcode` varchar(4) NOT NULL default '',
117                                 `day` smallint(6) NOT NULL default '0',
118                                 `month` smallint(6) NOT NULL default '0',
119                                 `year` smallint(6) NOT NULL default '0',
120                                 `isexception` smallint(1) NOT NULL default '1',
121                                 `title` varchar(50) NOT NULL default '',
122                                 `description` text NOT NULL,
123                                 PRIMARY KEY  (`id`)
124                                 )",
125         overduerules    =>"(`branchcode` varchar(255) NOT NULL default '',
126                                         `categorycode` char(2) NOT NULL default '',
127                                         `delay1` int(4) default '0',
128                                         `letter1` varchar(20) default NULL,
129                                         `debarred1` char(1) default '0',
130                                         `delay2` int(4) default '0',
131                                         `debarred2` char(1) default '0',
132                                         `letter2` varchar(20) default NULL,
133                                         `delay3` int(4) default '0',
134                                         `letter3` varchar(20) default NULL,
135                                         `debarred3` int(1) default '0',
136                                         PRIMARY KEY  (`branchcode`,`categorycode`)
137                                         )",
138         cities                  => "(`cityid` int auto_increment,
139                                                 `city_name` char(100) NOT NULL,
140                                                 `city_zipcode` char(20),
141                                                 PRIMARY KEY (`cityid`)
142                                         )",
143         roadtype                        => "(`roadtypeid` int auto_increment,
144                                                 `road_type` char(100) NOT NULL,
145                                                 PRIMARY KEY (`roadtypeid`)
146                                         )",
147
148         labels                     => "(
149                                 labelid int(11) NOT NULL auto_increment,
150                                 itemnumber varchar(100) NOT NULL default '',
151                                 timestamp timestamp(14) NOT NULL,
152                                 PRIMARY KEY  (labelid)
153                                 )",
154
155         labels_conf                => "(
156                                 id int(4) NOT NULL auto_increment,
157                                 barcodetype char(100) default '',
158                                 title tinyint(1) default '0',
159                                 isbn tinyint(1) default '0',
160                                 itemtype tinyint(1) default '0',
161                                 barcode tinyint(1) default '0',
162                                 dewey tinyint(1) default '0',
163                                 class tinyint(1) default '0',
164                                 author tinyint(1) default '0',
165                                 papertype char(100) default '',
166                                 startrow int(2) default NULL,
167                                 PRIMARY KEY  (id)
168                                 )",
169        reviews                  => "(
170                                 reviewid integer NOT NULL auto_increment,
171                                 borrowernumber integer,
172                                 biblionumber integer,
173                                 review text,
174                                 approved tinyint,
175                                 datereviewed datetime,
176                                 PRIMARY KEY (reviewid)
177                                 )",
178        borrowers_to_borrowers  => "(
179                                 borrower1 integer,
180                                 borrower2 integer
181                                 )",
182
183 );
184
185 my %requirefields = (
186         subscription => { 'letter' => 'char(20) NULL', 'distributedto' => 'text NULL'},
187         itemtypes => { 'imageurl' => 'char(200) NULL'},
188         aqbookfund => { 'branchcode' => 'varchar(4) NULL'},
189         aqbudget => { 'branchcode' => 'varchar(4) NULL'},
190         auth_header => { 'marc' => 'BLOB NOT NULL', 'linkid' => 'BIGINT(20) NULL'},
191         auth_subfield_structure =>{ 'hidden' => 'TINYINT(3) NOT NULL UNSIGNED ZEROFILL', 'kohafield' => 'VARCHAR(45) NOT NULL', 'linkid' =>  'TINYINT(1) NOT NULL UNSIGNED', 'isurl' => 'TINYINT(1) UNSIGNED'},
192         statistics => { 'associatedborrower' => 'integer'},
193 #    tablename        => { 'field' => 'fieldtype' },
194 );
195
196 my %dropable_table = (
197         sessionqueries  => 'sessionqueries',
198         marcrecorddone  => 'marcrecorddone',
199         users                   => 'users',
200         itemsprices             => 'itemsprices',
201         biblioanalysis  => 'biblioanalysis',
202         borexp                  => 'borexp',
203 # tablename => 'tablename',
204 );
205
206 my %uselessfields = (
207 # tablename => "field1,field2",
208         borrowers => "suburb,altstreetaddress,altsuburb,altcity,studentnumber,school,area,preferredcont,altcp",
209         deletedborrowers=> "suburb,altstreetaddress,altsuburb,altcity,studentnumber,school,area,preferredcont,altcp",
210         );
211 # the other hash contains other actions that can't be done elsewhere. they are done
212 # either BEFORE of AFTER everything else, depending on "when" entry (default => AFTER)
213
214 # The tabledata hash contains data that should be in the tables.
215 # The uniquefieldrequired hash entry is used to determine which (if any) fields
216 # must not exist in the table for this row to be inserted.  If the
217 # uniquefieldrequired entry is already in the table, the existing data is not
218 # modified, unless the forceupdate hash entry is also set.  Fields in the
219 # anonymous "forceupdate" hash will be forced to be updated to the default
220 # values given in the %tabledata hash.
221
222 my %tabledata = (
223 # tablename => [
224 #       {       uniquefielrequired => 'fieldname', # the primary key in the table
225 #               fieldname => fieldvalue,
226 #               fieldname2 => fieldvalue2,
227 #       },
228 # ],
229     systempreferences => [
230                 {
231             uniquefieldrequired => 'variable',
232             variable            => 'Activate_Log',
233             value               => 'On',
234             forceupdate         => { 'explanation' => 1,
235                                      'type' => 1},
236             explanation         => 'Turn Log Actions on DB On an Off',
237             type                => 'YesNo',
238         },
239         {
240             uniquefieldrequired => 'variable',
241             variable            => 'IndependantBranches',
242             value               => 0,
243             forceupdate         => { 'explanation' => 1,
244                                      'type' => 1},
245             explanation         => 'Turn Branch independancy management On an Off',
246             type                => 'YesNo',
247         },
248                 {
249             uniquefieldrequired => 'variable',
250             variable            => 'ReturnBeforeExpiry',
251             value               => 'Off',
252             forceupdate         => { 'explanation' => 1,
253                                      'type' => 1},
254             explanation         => 'If Yes, Returndate on issuing can\'t be after borrower card expiry',
255             type                => 'YesNo',
256         },
257         {
258             uniquefieldrequired => 'variable',
259             variable            => 'opacstylesheet',
260             value               => '',
261             forceupdate         => { 'explanation' => 1,
262                                      'type' => 1},
263             explanation         => 'Enter a complete URL to use an alternate stylesheet in OPAC',
264             type                => 'free',
265         },
266         {
267             uniquefieldrequired => 'variable',
268             variable            => 'opacsmallimage',
269             value               => '',
270             forceupdate         => { 'explanation' => 1,
271                                      'type' => 1},
272             explanation         => 'Enter a complete URL to an image, will be on top/left instead of the Koha logo',
273             type                => 'free',
274         },
275         {
276             uniquefieldrequired => 'variable',
277             variable            => 'opaclargeimage',
278             value               => '',
279             forceupdate         => { 'explanation' => 1,
280                                      'type' => 1},
281             explanation         => 'Enter a complete URL to an image, will be on the main page, instead of the Koha logo',
282             type                => 'free',
283         },
284         {
285             uniquefieldrequired => 'variable',
286             variable            => 'delimiter',
287             value               => ';',
288             forceupdate         => { 'explanation' => 1,
289                                      'type' => 1},
290             explanation         => 'separator for reports exported to spreadsheet',
291             type                => 'free',
292         },
293         {
294             uniquefieldrequired => 'variable',
295             variable            => 'MIME',
296             value               => 'OPENOFFICE.ORG',
297             forceupdate         => { 'explanation' => 1,
298                                      'type' => 1,
299                                      'options' => 1},
300             explanation         => 'Define the default application for report exportations into files',
301                 type            => 'Choice',
302                 options         => 'EXCEL|OPENOFFICE.ORG'
303         },
304         {
305             uniquefieldrequired => 'variable',
306             variable            => 'Delimiter',
307             value               => ';',
308                 forceupdate             => { 'explanation' => 1,
309                                      'type' => 1,
310                                      'options' => 1},
311             explanation         => 'Define the default separator character for report exportations into files',
312                 type            => 'Choice',
313                 options         => ';|tabulation|,|/|\|#'
314         },
315         {
316             uniquefieldrequired => 'variable',
317             variable            => 'SubscriptionHistory',
318             value               => ';',
319                 forceupdate             => { 'explanation' => 1,
320                                      'type' => 1,
321                                      'options' => 1},
322             explanation         => 'Define the information level for serials history in OPAC',
323                 type            => 'Choice',
324                 options         => 'simplified|full'
325         },
326         {
327             uniquefieldrequired => 'variable',
328             variable            => 'hidelostitems',
329             value               => 'No',
330             forceupdate         => { 'explanation' => 1,
331                                      'type' => 1},
332             explanation         => 'show or hide "lost" items in OPAC.',
333             type                => 'YesNo',
334         },
335                  {
336             uniquefieldrequired => 'variable',
337             variable            => 'IndependantBranches',
338             value               => '0',
339             forceupdate         => { 'explanation' => 1,
340                                      'type' => 1},
341             explanation         => 'Turn Branch independancy management On an Off',
342             type                => 'YesNo',
343         },
344                 {
345             uniquefieldrequired => 'variable',
346             variable            => 'ReturnBeforeExpiry',
347             value               => '0',
348             forceupdate         => { 'explanation' => 1,
349                                      'type' => 1},
350             explanation         => 'If Yes, Returndate on issuing can\'t be after borrower card expiry',
351             type                => 'YesNo',
352         },
353         {
354             uniquefieldrequired => 'variable',
355             variable            => 'Disable_Dictionary',
356             value               => '0',
357             forceupdate         => { 'explanation' => 1,
358                                      'type' => 1},
359             explanation         => 'Disables Dictionary buttons if set to yes',
360             type                => 'YesNo',
361         },
362         {
363             uniquefieldrequired => 'variable',
364             variable            => 'hide_marc',
365             value               => '0',
366             forceupdate         => { 'explanation' => 1,
367                                      'type' => 1},
368             explanation         => 'hide marc specific datas like subfield code & indicators to library',
369             type                => 'YesNo',
370         },
371         {
372             uniquefieldrequired => 'variable',
373             variable            => 'NotifyBorrowerDeparture',
374             value               => '0',
375             forceupdate         => { 'explanation' => 1,
376                                      'type' => 1},
377             explanation         => 'Delay before expiry where a notice is sent when issuing',
378             type                => 'Integer',
379         },
380         {
381             uniquefieldrequired => 'variable',
382             variable            => 'OpacPasswordChange',
383             value               => '1',
384             forceupdate         => { 'explanation' => 1,
385                                      'type' => 1},
386             explanation         => 'Enable/Disable password change in OPAC (disable it when using LDAP auth)',
387             type                => 'YesNo',
388         },
389         {
390             uniquefieldrequired => 'variable',
391             variable            => 'useDaysMode',
392             value               => 'Calendar',
393             forceupdate         => { 'explanation' => 1,
394                                      'type' => 1},
395             explanation                 => 'How to calculate return dates : Calendar means holidays will be controled, Days means the return date don\'t depend on holidays',
396                 type            => 'Choice',
397                 options         => 'Calendar|Days'
398         },
399         {
400             uniquefieldrequired => 'variable',
401             variable            => 'borrowerMandatoryField',
402             value               => 'zipcode|surname',
403             forceupdate         => { 'explanation' => 1,
404                                      'type' => 1},
405             explanation         => 'List all mandatory fields for borrowers',
406             type                => 'free',
407         },
408         {
409             uniquefieldrequired => 'variable',
410             variable            => 'borrowerRelationship',
411             value               => 'father|mother,grand-mother',
412             forceupdate         => { 'explanation' => 1,
413                                      'type' => 1},
414             explanation         => 'The relationships between a guarantor & a guarantee (separated by | or ,)',
415             type                => 'free',
416         },
417         {
418             uniquefieldrequired => 'variable',
419             variable            => 'ReservesMaxPickUpDelay',
420             value               => '10',
421             forceupdate         => { 'explanation' => 1,
422                                      'type' => 1},
423             explanation         => 'Maximum delay to pick up a reserved document',
424             type                => 'free',
425         },
426         {
427             uniquefieldrequired => 'variable',
428             variable            => 'TransfersMaxDaysWarning',
429             value               => '3',
430             forceupdate         => { 'explanation' => 1,
431                                      'type' => 1},
432             explanation         => 'Max delay before considering the transfer has potentialy a problem',
433             type                => 'free',
434         },
435         {
436             uniquefieldrequired => 'variable',
437             variable            => 'memberofinstitution',
438             value               => '0',
439             forceupdate         => { 'explanation' => 1,
440                                      'type' => 1},
441             explanation         => 'Are your patrons members of institutions',
442             type                => 'YesNo',
443         },
444         {
445             uniquefieldrequired => 'variable',
446             variable            => 'ReadingHistory',
447             value               => '0',
448             forceupdate         => { 'explanation' => 1,
449                                      'type' => 1},
450             explanation         => 'Allow reading record info retrievable from issues and oldissues tables',
451             type                => 'YesNo',
452         },
453         {
454             uniquefieldrequired => 'variable',
455             variable            => 'IssuingInProcess',
456             value               => '0',
457             forceupdate         => { 'explanation' => 1,
458                                      'type' => 1},
459             explanation         => 'Allow no debt alert if the patron is issuing item that accumulate debt',
460             type                => 'YesNo',
461         },
462         {
463             uniquefieldrequired => 'variable',
464             variable            => 'AutomaticItemReturn',
465             value               => '1',
466             forceupdate         => { 'explanation' => 1,
467                                      'type' => 1},
468             explanation         => 'This Variable allow or not to return automaticly to his homebranch',
469             type                => 'YesNo',
470         },
471         {
472             uniquefieldrequired => 'variable',
473             variable            => 'reviewson',
474             value               => '0',
475             forceupdate         => { 'explanation' => 1,
476                                      'type' => 1},
477             explanation         => 'Allows patrons to submit reviews from the opac',
478             type                => 'YesNo',
479         },
480     ],
481
482 );
483
484 my %fielddefinitions = (
485 # fieldname => [
486 #       {                 field => 'fieldname',
487 #             type    => 'fieldtype',
488 #             null    => '',
489 #             key     => '',
490 #             default => ''
491 #         },
492 #     ],
493         serial => [
494         {
495             field   => 'notes',
496             type    => 'TEXT',
497             null    => 'NULL',
498             key     => '',
499             default => '',
500             extra   => ''
501         },
502     ],
503         aqbasket =>  [
504                 {
505                         field   => 'booksellerid',
506                         type    => 'int(11)',
507                         null    => 'NOT NULL',
508                         key             => '',
509                         default => '1',
510                         extra   => '',
511                 },
512         ],
513         aqbooksellers =>  [
514                 {
515                         field   => 'listprice',
516                         type    => 'varchar(10)',
517                         null    => 'NULL',
518                         key             => '',
519                         default => '',
520                         extra   => '',
521                 },
522                 {
523                         field   => 'invoiceprice',
524                         type    => 'varchar(10)',
525                         null    => 'NULL',
526                         key             => '',
527                         default => '',
528                         extra   => '',
529                 },
530         ],
531         issues =>  [
532                 {
533                         field   => 'borrowernumber',
534                         type    => 'int(11)',
535                         null    => 'NULL', # can be null when a borrower is deleted and the foreign key rule executed
536                         key             => '',
537                         default => '',
538                         extra   => '',
539                 },
540                 {
541                         field   => 'itemnumber',
542                         type    => 'int(11)',
543                         null    => 'NULL', # can be null when a borrower is deleted and the foreign key rule executed
544                         key             => '',
545                         default => '',
546                         extra   => '',
547                 },
548         ],
549         borrowers => [
550                 {       field => 'firstname',
551                         type => 'text',
552                         null => 'NULL',
553                  },
554                 {       field => 'initials',
555                         type => 'text',
556                         null => 'NULL',
557                  },
558                 {       field => 'B_email',
559                         type => 'text',
560                         null => 'NULL',
561                         after => 'B_zipcode',
562                  },
563                  {
564                         field => 'streetnumber', # street number (hidden if streettable table is empty)
565                         type => 'char(10)',
566                         null => 'NULL',
567                         after => 'initials',
568                 },
569                 {
570                         field => 'streettype', # street table, list builded from a system table
571                         type => 'char(50)',
572                         null => 'NULL',
573                         after => 'streetnumber',
574                 },
575                 {       field => 'phone',
576                         type => 'text',
577                         null => 'NULL',
578                  },                             
579                 {
580                         field => 'B_streetnumber', # street number (hidden if streettable table is empty)
581                         type => 'char(10)',
582                         null => 'NULL',
583                         after => 'fax',
584                 },
585                 {
586                         field => 'B_streettype', # street table, list builded from a system table
587                         type => 'char(50)',
588                         null => 'NULL',
589                         after => 'B_streetnumber',
590                 },
591                 {
592                         field => 'phonepro',
593                         type => 'text',
594                         null => 'NULL',
595                         after => 'fax',
596                 },
597                 {
598                         field => 'address2', # complement address
599                         type => 'text',
600                         null => 'NULL',
601                         after => 'address',
602                 },
603                 {
604                         field => 'emailpro',
605                         type => 'text',
606                         null => 'NULL',
607                         after => 'fax',
608                 },
609                 {
610                         field => 'contactfirstname', # contact's firstname
611                         type => 'text',
612                         null => 'NULL',
613                         after => 'contactname',
614                 },
615                 {
616                         field => 'contacttitle', # contact's title
617                         type => 'text',
618                         null => 'NULL',
619                         after => 'contactfirstname',
620                 },
621         ],
622         
623         deletedborrowers => [
624                 {       field => 'firstname',
625                         type => 'text',
626                         null => 'NULL',
627                  },
628                 {       field => 'initials',
629                         type => 'text',
630                         null => 'NULL',
631                  },
632                 {       field => 'B_email',
633                         type => 'text',
634                         null => 'NULL',
635                         after => 'B_zipcode',
636                  },
637                  {
638                         field => 'streetnumber', # street number (hidden if streettable table is empty)
639                         type => 'char(10)',
640                         null => 'NULL',
641                         after => 'initials',
642                 },
643                 {
644                         field => 'streettype', # street table, list builded from a system table
645                         type => 'char(50)',
646                         null => 'NULL',
647                         after => 'streetnumber',
648                 },
649                 {       field => 'phone',
650                         type => 'text',
651                         null => 'NULL',
652                  },             
653                  {
654                         field => 'B_streetnumber', # street number (hidden if streettable table is empty)
655                         type => 'char(10)',
656                         null => 'NULL',
657                         after => 'fax',
658                 },
659                 {
660                         field => 'B_streettype', # street table, list builded from a system table
661                         type => 'char(50)',
662                         null => 'NULL',
663                         after => 'B_streetnumber',
664                 },
665                 {
666                         field => 'phonepro',
667                         type => 'text',
668                         null => 'NULL',
669                         after => 'fax',
670                 },
671                 {
672                         field => 'address2', # complement address
673                         type => 'text',
674                         null => 'NULL',
675                         after => 'address',
676                 },
677                 {
678                         field => 'emailpro',
679                         type => 'text',
680                         null => 'NULL',
681                         after => 'fax',
682                 },
683                 {
684                         field => 'contactfirstname', # contact's firstname
685                         type => 'text',
686                         null => 'NULL',
687                         after => 'contactname',
688                 },
689                 {
690                         field => 'contacttitle', # contact's title
691                         type => 'text',
692                         null => 'NULL',
693                         after => 'contactfirstname',
694                 },
695         ],
696         
697         branches =>  [
698                 {
699                         field   => 'branchip',
700                         type    => 'varchar(15)',
701                         null    => 'NULL',
702                         key             => '',
703                         default => '',
704                         extra   => '',
705                 },
706                 {
707                         field   => 'branchprinter',
708                         type    => 'varchar(100)',
709                         null    => 'NULL',
710                         key             => '',
711                         default => '',
712                         extra   => '',
713                 },
714         ],
715         categories =>  [
716                 {
717                         field   => 'category_type',
718                         type    => 'char(1)',
719                         null    => 'NOT NULL',
720                         key             => '',
721                         default => 'A',
722                         extra   => '',
723                 },
724         ],
725         reserves =>  [
726                 {
727                         field   => 'waitingdate',
728                         type    => 'date',
729                         null    => 'NULL',
730                         key             => '',
731                         default => '',
732                         extra   => '',
733                 },
734         ],
735 );
736
737 my %indexes = (
738 #       table => [
739 #               {       indexname => 'index detail'
740 #               }
741 #       ],
742         shelfcontents => [
743                 {       indexname => 'shelfnumber',
744                         content => 'shelfnumber',
745                 },
746                 {       indexname => 'itemnumber',
747                         content => 'itemnumber',
748                 }
749         ],
750         bibliosubject => [
751                 {       indexname => 'biblionumber',
752                         content => 'biblionumber',
753                 }
754         ],
755         items => [
756                 {       indexname => 'homebranch',
757                         content => 'homebranch',
758                 },
759                 {       indexname => 'holdingbranch',
760                         content => 'holdingbranch',
761                 }
762         ],
763         aqbooksellers => [
764                 {       indexname => 'PRIMARY',
765                         content => 'id',
766                         type => 'PRIMARY',
767                 }
768         ],
769         aqbasket => [
770                 {       indexname => 'booksellerid',
771                         content => 'booksellerid',
772                 },
773         ],
774         aqorders => [
775                 {       indexname => 'basketno',
776                         content => 'basketno',
777                 },
778         ],
779         aqorderbreakdown => [
780                 {       indexname => 'ordernumber',
781                         content => 'ordernumber',
782                 },
783                 {       indexname => 'bookfundid',
784                         content => 'bookfundid',
785                 },
786         ],
787         currency => [
788                 {       indexname => 'PRIMARY',
789                         content => 'currency',
790                         type => 'PRIMARY',
791                 }
792         ],
793 );
794
795 my %foreign_keys = (
796 #       table => [
797 #               {       key => 'the key in table' (must be indexed)
798 #                       foreigntable => 'the foreigntable name', # (the parent)
799 #                       foreignkey => 'the foreign key column(s)' # (in the parent)
800 #                       onUpdate => 'CASCADE|SET NULL|NO ACTION| RESTRICT',
801 #                       onDelete => 'CASCADE|SET NULL|NO ACTION| RESTRICT',
802 #               }
803 #       ],
804         shelfcontents => [
805                 {       key => 'shelfnumber',
806                         foreigntable => 'bookshelf',
807                         foreignkey => 'shelfnumber',
808                         onUpdate => 'CASCADE',
809                         onDelete => 'CASCADE',
810                 },
811                 {       key => 'itemnumber',
812                         foreigntable => 'items',
813                         foreignkey => 'itemnumber',
814                         onUpdate => 'CASCADE',
815                         onDelete => 'CASCADE',
816                 },
817         ],
818         # onDelete is RESTRICT on reference tables (branches, itemtype) as we don't want items to be 
819         # easily deleted, but branches/itemtype not too easy to empty...
820         biblioitems => [
821                 {       key => 'biblionumber',
822                         foreigntable => 'biblio',
823                         foreignkey => 'biblionumber',
824                         onUpdate => 'CASCADE',
825                         onDelete => 'CASCADE',
826                 },
827                 {       key => 'itemtype',
828                         foreigntable => 'itemtypes',
829                         foreignkey => 'itemtype',
830                         onUpdate => 'CASCADE',
831                         onDelete => 'RESTRICT',
832                 },
833         ],
834         items => [
835                 {       key => 'biblioitemnumber',
836                         foreigntable => 'biblioitems',
837                         foreignkey => 'biblioitemnumber',
838                         onUpdate => 'CASCADE',
839                         onDelete => 'CASCADE',
840                 },
841                 {       key => 'homebranch',
842                         foreigntable => 'branches',
843                         foreignkey => 'branchcode',
844                         onUpdate => 'CASCADE',
845                         onDelete => 'RESTRICT',
846                 },
847                 {       key => 'holdingbranch',
848                         foreigntable => 'branches',
849                         foreignkey => 'branchcode',
850                         onUpdate => 'CASCADE',
851                         onDelete => 'RESTRICT',
852                 },
853         ],
854         additionalauthors => [
855                 {       key => 'biblionumber',
856                         foreigntable => 'biblio',
857                         foreignkey => 'biblionumber',
858                         onUpdate => 'CASCADE',
859                         onDelete => 'CASCADE',
860                 },
861         ],
862         bibliosubject => [
863                 {       key => 'biblionumber',
864                         foreigntable => 'biblio',
865                         foreignkey => 'biblionumber',
866                         onUpdate => 'CASCADE',
867                         onDelete => 'CASCADE',
868                 },
869         ],
870         aqbasket => [
871                 {       key => 'booksellerid',
872                         foreigntable => 'aqbooksellers',
873                         foreignkey => 'id',
874                         onUpdate => 'CASCADE',
875                         onDelete => 'RESTRICT',
876                 },
877         ],
878         aqorders => [
879                 {       key => 'basketno',
880                         foreigntable => 'aqbasket',
881                         foreignkey => 'basketno',
882                         onUpdate => 'CASCADE',
883                         onDelete => 'CASCADE',
884                 },
885                 {       key => 'biblionumber',
886                         foreigntable => 'biblio',
887                         foreignkey => 'biblionumber',
888                         onUpdate => 'SET NULL',
889                         onDelete => 'SET NULL',
890                 },
891         ],
892         aqbooksellers => [
893                 {       key => 'listprice',
894                         foreigntable => 'currency',
895                         foreignkey => 'currency',
896                         onUpdate => 'CASCADE',
897                         onDelete => 'CASCADE',
898                 },
899                 {       key => 'invoiceprice',
900                         foreigntable => 'currency',
901                         foreignkey => 'currency',
902                         onUpdate => 'CASCADE',
903                         onDelete => 'CASCADE',
904                 },
905         ],
906         aqorderbreakdown => [
907                 {       key => 'ordernumber',
908                         foreigntable => 'aqorders',
909                         foreignkey => 'ordernumber',
910                         onUpdate => 'CASCADE',
911                         onDelete => 'CASCADE',
912                 },
913                 {       key => 'bookfundid',
914                         foreigntable => 'aqbookfund',
915                         foreignkey => 'bookfundid',
916                         onUpdate => 'CASCADE',
917                         onDelete => 'CASCADE',
918                 },
919         ],
920         branchtransfers => [
921                 {       key => 'frombranch',
922                         foreigntable => 'branches',
923                         foreignkey => 'branchcode',
924                         onUpdate => 'CASCADE',
925                         onDelete => 'CASCADE',
926                 },
927                 {       key => 'tobranch',
928                         foreigntable => 'branches',
929                         foreignkey => 'branchcode',
930                         onUpdate => 'CASCADE',
931                         onDelete => 'CASCADE',
932                 },
933                 {       key => 'itemnumber',
934                         foreigntable => 'items',
935                         foreignkey => 'itemnumber',
936                         onUpdate => 'CASCADE',
937                         onDelete => 'CASCADE',
938                 },
939         ],
940         issuingrules => [
941                 {       key => 'categorycode',
942                         foreigntable => 'categories',
943                         foreignkey => 'categorycode',
944                         onUpdate => 'CASCADE',
945                         onDelete => 'CASCADE',
946                 },
947                 {       key => 'itemtype',
948                         foreigntable => 'itemtypes',
949                         foreignkey => 'itemtype',
950                         onUpdate => 'CASCADE',
951                         onDelete => 'CASCADE',
952                 },
953         ],
954         issues => [     # constraint is SET NULL : when a borrower or an item is deleted, we keep the issuing record
955         # for stat purposes
956                 {       key => 'borrowernumber',
957                         foreigntable => 'borrowers',
958                         foreignkey => 'borrowernumber',
959                         onUpdate => 'SET NULL',
960                         onDelete => 'SET NULL',
961                 },
962                 {       key => 'itemnumber',
963                         foreigntable => 'items',
964                         foreignkey => 'itemnumber',
965                         onUpdate => 'SET NULL',
966                         onDelete => 'SET NULL',
967                 },
968         ],
969         reserves => [
970                 {       key => 'borrowernumber',
971                         foreigntable => 'borrowers',
972                         foreignkey => 'borrowernumber',
973                         onUpdate => 'CASCADE',
974                         onDelete => 'CASCADE',
975                 },
976                 {       key => 'biblionumber',
977                         foreigntable => 'biblio',
978                         foreignkey => 'biblionumber',
979                         onUpdate => 'CASCADE',
980                         onDelete => 'CASCADE',
981                 },
982                 {       key => 'itemnumber',
983                         foreigntable => 'items',
984                         foreignkey => 'itemnumber',
985                         onUpdate => 'CASCADE',
986                         onDelete => 'CASCADE',
987                 },
988                 {       key => 'branchcode',
989                         foreigntable => 'branches',
990                         foreignkey => 'branchcode',
991                         onUpdate => 'CASCADE',
992                         onDelete => 'CASCADE',
993                 },
994         ],
995         borrowers => [ # foreign keys are RESTRICT as we don't want to delete borrowers when a branch is deleted
996         # but prevent deleting a branch as soon as it has 1 borrower !
997                 {       key => 'categorycode',
998                         foreigntable => 'categories',
999                         foreignkey => 'categorycode',
1000                         onUpdate => 'RESTRICT',
1001                         onDelete => 'RESTRICT',
1002                 },
1003                 {       key => 'branchcode',
1004                         foreigntable => 'branches',
1005                         foreignkey => 'branchcode',
1006                         onUpdate => 'RESTRICT',
1007                         onDelete => 'RESTRICT',
1008                 },
1009         ],
1010         deletedborrowers => [ # foreign keys are RESTRICT as we don't want to delete borrowers when a branch is deleted
1011         # but prevent deleting a branch as soon as it has 1 borrower !
1012                 {       key => 'categorycode',
1013                         foreigntable => 'categories',
1014                         foreignkey => 'categorycode',
1015                         onUpdate => 'RESTRICT',
1016                         onDelete => 'RESTRICT',
1017                 },
1018                 {       key => 'branchcode',
1019                         foreigntable => 'branches',
1020                         foreignkey => 'branchcode',
1021                         onUpdate => 'RESTRICT',
1022                         onDelete => 'RESTRICT',
1023                 },
1024         ],
1025         accountlines => [
1026                 {       key => 'borrowernumber',
1027                         foreigntable => 'borrowers',
1028                         foreignkey => 'borrowernumber',
1029                         onUpdate => 'CASCADE',
1030                         onDelete => 'CASCADE',
1031                 },
1032                 {       key => 'itemnumber',
1033                         foreigntable => 'items',
1034                         foreignkey => 'itemnumber',
1035                         onUpdate => 'SET NULL',
1036                         onDelete => 'SET NULL',
1037                 },
1038         ],
1039         auth_tag_structure => [
1040                 {       key => 'authtypecode',
1041                         foreigntable => 'auth_types',
1042                         foreignkey => 'authtypecode',
1043                         onUpdate => 'CASCADE',
1044                         onDelete => 'CASCADE',
1045                 },
1046         ],
1047         # FIXME : don't constraint auth_*_table and auth_word, as they may be replaced by zebra
1048 );
1049
1050
1051 # column changes
1052 my %column_change = (
1053         # table
1054         borrowers => [
1055                                 {
1056                                         from => 'emailaddress',
1057                                         to => 'email',
1058                                         after => 'city',
1059                                 },
1060                                 {
1061                                         from => 'streetaddress',
1062                                         to => 'address',
1063                                         after => 'initials',
1064                                 },
1065                                 {
1066                                         from => 'faxnumber',
1067                                         to => 'fax',
1068                                         after => 'phone',
1069                                 },
1070                                 {
1071                                         from => 'textmessaging',
1072                                         to => 'opacnote',
1073                                         after => 'userid',
1074                                 },
1075                                 {
1076                                         from => 'altnotes',
1077                                         to => 'contactnote',
1078                                         after => 'opacnote',
1079                                 },
1080                                 {
1081                                         from => 'physstreet',
1082                                         to => 'B_address',
1083                                         after => 'fax',
1084                                 },
1085                                 {
1086                                         from => 'streetcity',
1087                                         to => 'B_city',
1088                                         after => 'B_address',
1089                                 },
1090                                 {
1091                                         from => 'phoneday',
1092                                         to => 'mobile',
1093                                         after => 'phone',
1094                                 },
1095                                 {
1096                                         from => 'zipcode',
1097                                         to => 'zipcode',
1098                                         after => 'city',
1099                                 },
1100                                 {
1101                                         from => 'homezipcode',
1102                                         to => 'B_zipcode',
1103                                         after => 'B_city',
1104                                 },
1105                                 {
1106                                         from => 'altphone',
1107                                         to => 'B_phone',
1108                                         after => 'B_zipcode',
1109                                 },
1110                                 {
1111                                         from => 'expiry',
1112                                         to => 'dateexpiry',
1113                                         after => 'dateenrolled',
1114                                 },
1115                                 {
1116                                         from => 'guarantor',
1117                                         to => 'guarantorid',
1118                                         after => 'contactname',
1119                                 },
1120                                 {
1121                                         from => 'textmessaging',
1122                                         to => 'opacnotes',
1123                                         after => 'flags',
1124                                 },
1125                                 {
1126                                         from => 'altnotes',
1127                                         to => 'contactnotes',
1128                                         after => 'opacnotes',
1129                                 },
1130                                 {
1131                                         from => 'altrelationship',
1132                                         to => 'relationship',
1133                                         after => 'borrowernotes',
1134                                 },
1135                         ],
1136
1137         deletedborrowers => [
1138                                 {
1139                                         from => 'emailaddress',
1140                                         to => 'email',
1141                                         after => 'city',
1142                                 },
1143                                 {
1144                                         from => 'streetaddress',
1145                                         to => 'address',
1146                                         after => 'initials',
1147                                 },
1148                                 {
1149                                         from => 'faxnumber',
1150                                         to => 'fax',
1151                                         after => 'phone',
1152                                 },
1153                                 {
1154                                         from => 'textmessaging',
1155                                         to => 'opacnote',
1156                                         after => 'userid',
1157                                 },
1158                                 {
1159                                         from => 'altnotes',
1160                                         to => 'contactnote',
1161                                         after => 'opacnote',
1162                                 },
1163                                 {
1164                                         from => 'physstreet',
1165                                         to => 'B_address',
1166                                         after => 'fax',
1167                                 },
1168                                 {
1169                                         from => 'streetcity',
1170                                         to => 'B_city',
1171                                         after => 'B_address',
1172                                 },
1173                                 {
1174                                         from => 'phoneday',
1175                                         to => 'mobile',
1176                                         after => 'phone',
1177                                 },
1178                                 {
1179                                         from => 'zipcode',
1180                                         to => 'zipcode',
1181                                         after => 'city',
1182                                 },
1183                                 {
1184                                         from => 'homezipcode',
1185                                         to => 'B_zipcode',
1186                                         after => 'B_city',
1187                                 },
1188                                 {
1189                                         from => 'altphone',
1190                                         to => 'B_phone',
1191                                         after => 'B_zipcode',
1192                                 },
1193                                 {
1194                                         from => 'expiry',
1195                                         to => 'dateexpiry',
1196                                         after => 'dateenrolled',
1197                                 },
1198                                 {
1199                                         from => 'guarantor',
1200                                         to => 'guarantorid',
1201                                         after => 'contactname',
1202                                 },
1203                                 {
1204                                         from => 'textmessaging',
1205                                         to => 'opacnotes',
1206                                         after => 'flags',
1207                                 },
1208                                 {
1209                                         from => 'altnotes',
1210                                         to => 'contactnotes',
1211                                         after => 'opacnotes',
1212                                 },
1213                                 {
1214                                         from => 'altrelationship',
1215                                         to => 'relationship',
1216                                         after => 'borrowernotes',
1217                                 },
1218                         ],
1219
1220                 );
1221                 
1222 foreach my $table (keys %column_change) {
1223         $sth = $dbh->prepare("show columns from $table");
1224         $sth->execute();
1225         undef %types;
1226         while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1227         {
1228                 $types{$column}->{type} ="$type";
1229                 $types{$column}->{null} = "$null";
1230                 $types{$column}->{key} = "$key";
1231                 $types{$column}->{default} = "$default";
1232                 $types{$column}->{extra} = "$extra";
1233         }    # while
1234         my $tablerows = $column_change{$table};
1235         foreach my $row ( @$tablerows ) {
1236                 if ($types{$row->{from}}->{type}) {
1237                         print "altering $table $row->{from} to $row->{to}\n";
1238                         # ALTER TABLE `borrowers` CHANGE `faxnumber` `fax` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL 
1239 #                       alter table `borrowers` change `faxnumber` `fax` type text  null after phone
1240                         my $sql = 
1241                                 "alter table `$table` change `$row->{from}` `$row->{to}` $types{$row->{from}}->{type} ".
1242                                 ($types{$row->{from}}->{null} eq 'YES'?" NULL":" NOT NULL").
1243                                 ($types{$row->{from}}->{default}?" default ".$types{$row->{from}}->{default}:"").
1244                                 "$types{$row->{from}}->{extra} after $row->{after} ";
1245 #                       print "$sql";
1246                         $dbh->do($sql);
1247                 }
1248         }
1249 }
1250
1251 #-------------------
1252 # Initialize
1253
1254 # Start checking
1255
1256 # Get version of MySQL database engine.
1257 my $mysqlversion = `mysqld --version`;
1258 $mysqlversion =~ /Ver (\S*) /;
1259 $mysqlversion = $1;
1260 if ( $mysqlversion ge '3.23' ) {
1261     print "Could convert to MyISAM database tables...\n" unless $silent;
1262 }
1263
1264 #---------------------------------
1265 # Tables
1266
1267 # Collect all tables into a list
1268 $sth = $dbh->prepare("show tables");
1269 $sth->execute;
1270 while ( my ($table) = $sth->fetchrow ) {
1271     $existingtables{$table} = 1;
1272 }
1273
1274
1275 # Now add any missing tables
1276 foreach $table ( keys %requiretables ) {
1277     unless ( $existingtables{$table} ) {
1278         print "Adding $table table...\n" unless $silent;
1279         my $sth = $dbh->prepare("create table $table $requiretables{$table}");
1280         $sth->execute;
1281         if ( $sth->err ) {
1282             print "Error : $sth->errstr \n";
1283             $sth->finish;
1284         }    # if error
1285     }    # unless exists
1286 }    # foreach
1287
1288 # now drop useless tables
1289 foreach $table ( keys %dropable_table ) {
1290         if ( $existingtables{$table} ) {
1291                 print "Dropping unused table $table\n" if $debug and not $silent;
1292                 $dbh->do("drop table $table");
1293                 if ( $dbh->err ) {
1294                         print "Error : $dbh->errstr \n";
1295                 }
1296         }
1297 }
1298
1299 #---------------------------------
1300 # Columns
1301
1302 foreach $table ( keys %requirefields ) {
1303     print "Check table $table\n" if $debug and not $silent;
1304     $sth = $dbh->prepare("show columns from $table");
1305     $sth->execute();
1306     undef %types;
1307     while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1308     {
1309         $types{$column} = $type;
1310     }    # while
1311     foreach $column ( keys %{ $requirefields{$table} } ) {
1312         print "  Check column $column  [$types{$column}]\n" if $debug and not $silent;
1313         if ( !$types{$column} ) {
1314
1315             # column doesn't exist
1316             print "Adding $column field to $table table...\n" unless $silent;
1317             $query = "alter table $table
1318                         add column $column " . $requirefields{$table}->{$column};
1319             print "Execute: $query\n" if $debug;
1320             my $sti = $dbh->prepare($query);
1321             $sti->execute;
1322             if ( $sti->err ) {
1323                 print "**Error : $sti->errstr \n";
1324                 $sti->finish;
1325             }    # if error
1326         }    # if column
1327     }    # foreach column
1328 }    # foreach table
1329
1330 foreach $table ( keys %fielddefinitions ) {
1331         print "Check table $table\n" if $debug;
1332         $sth = $dbh->prepare("show columns from $table");
1333         $sth->execute();
1334         my $definitions;
1335         while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1336         {
1337                 $definitions->{$column}->{type}    = $type;
1338                 $definitions->{$column}->{null}    = $null;
1339                 $definitions->{$column}->{null}    = 'NULL' if $null eq 'YES';
1340                 $definitions->{$column}->{key}     = $key;
1341                 $definitions->{$column}->{default} = $default;
1342                 $definitions->{$column}->{extra}   = $extra;
1343         }    # while
1344         my $fieldrow = $fielddefinitions{$table};
1345         foreach my $row (@$fieldrow) {
1346                 my $field   = $row->{field};
1347                 my $type    = $row->{type};
1348                 my $null    = $row->{null};
1349 #               $null    = 'YES' if $row->{null} eq 'NULL';
1350                 my $key     = $row->{key};
1351                 my $default = $row->{default};
1352                 my $null    = $row->{null};
1353 #               $default="''" unless $default;
1354                 my $extra   = $row->{extra};
1355                 my $def     = $definitions->{$field};
1356                 my $after       = ($row->{after}?" after ".$row->{after}:"");
1357
1358                 unless ( $type eq $def->{type}
1359                         && $null eq $def->{null}
1360                         && $key eq $def->{key}
1361                         && $extra eq $def->{extra} )
1362                 {
1363                         if ( $null eq '' ) {
1364                                 $null = 'NOT NULL';
1365                         }
1366                         if ( $key eq 'PRI' ) {
1367                                 $key = 'PRIMARY KEY';
1368                         }
1369                         unless ( $extra eq 'auto_increment' ) {
1370                                 $extra = '';
1371                         }
1372
1373                         # if it's a new column use "add", if it's an old one, use "change".
1374                         my $action;
1375                         if ($definitions->{$field}->{type}) {
1376                                 $action="change $field"
1377                         } else {
1378                                 $action="add";
1379                         }
1380 # if it's a primary key, drop the previous pk, before altering the table
1381                         my $sth;
1382                         if ($key ne 'PRIMARY KEY') {
1383                                 $sth =$dbh->prepare("alter table $table $action $field $type $null $key $extra default ? $after");
1384                         } else {
1385                                 $sth =$dbh->prepare("alter table $table drop primary key, $action $field $type $null $key $extra default ? $after");
1386                         }
1387                         $sth->execute($default);
1388                         print "  alter or create $field in $table\n" unless $silent;
1389                 }
1390         }
1391 }
1392
1393 # Populate tables with required data
1394
1395
1396 # synch table and deletedtable.
1397 foreach my $table (('borrowers','items','biblio','biblioitems')) {
1398         my %deletedborrowers;
1399         print "synch'ing $table\n";
1400         $sth = $dbh->prepare("show columns from deleted$table");
1401         $sth->execute;
1402         while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow ) {
1403                 $deletedborrowers{$column}=1;
1404         }
1405         $sth = $dbh->prepare("show columns from $table");
1406         $sth->execute;
1407         my $previous;
1408         while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow ) {
1409                 unless ($deletedborrowers{$column}) {
1410                         my $newcol="alter table deleted$table add $column $type";
1411                         if ($null eq 'YES') {
1412                                 $newcol .= " NULL ";
1413                         } else {
1414                                 $newcol .= " NOT NULL ";
1415                         }
1416                         $newcol .= "default $default" if $default;
1417                         $newcol .= " after $previous" if $previous;
1418                         $previous=$column;
1419                         print "creating column $column\n";
1420                         $dbh->do($newcol);
1421                 }
1422         }
1423 }
1424
1425 foreach my $table ( keys %tabledata ) {
1426     print "Checking for data required in table $table...\n" unless $silent;
1427     my $tablerows = $tabledata{$table};
1428     foreach my $row (@$tablerows) {
1429         my $uniquefieldrequired = $row->{uniquefieldrequired};
1430         my $uniquevalue         = $row->{$uniquefieldrequired};
1431         my $forceupdate         = $row->{forceupdate};
1432         my $sth                 =
1433           $dbh->prepare(
1434 "select $uniquefieldrequired from $table where $uniquefieldrequired=?"
1435         );
1436         $sth->execute($uniquevalue);
1437                 if ($sth->rows) {
1438                         foreach my $field (keys %$forceupdate) {
1439                                 if ($forceupdate->{$field}) {
1440                                         my $sth=$dbh->prepare("update systempreferences set $field=? where $uniquefieldrequired=?");
1441                                         $sth->execute($row->{$field}, $uniquevalue);
1442                                 }
1443                 }
1444                 } else {
1445                         print "Adding row to $table: " unless $silent;
1446                         my @values;
1447                         my $fieldlist;
1448                         my $placeholders;
1449                         foreach my $field ( keys %$row ) {
1450                                 next if $field eq 'uniquefieldrequired';
1451                                 next if $field eq 'forceupdate';
1452                                 my $value = $row->{$field};
1453                                 push @values, $value;
1454                                 print "  $field => $value" unless $silent;
1455                                 $fieldlist .= "$field,";
1456                                 $placeholders .= "?,";
1457                         }
1458                         print "\n" unless $silent;
1459                         $fieldlist    =~ s/,$//;
1460                         $placeholders =~ s/,$//;
1461                         my $sth =
1462                         $dbh->prepare(
1463                                 "insert into $table ($fieldlist) values ($placeholders)");
1464                         $sth->execute(@values);
1465                 }
1466         }
1467 }
1468
1469 #
1470 # check indexes and create them when needed
1471 #
1472 print "Checking for index required...\n" unless $silent;
1473 foreach my $table ( keys %indexes ) {
1474         #
1475         # read all indexes from $table
1476         #
1477         $sth = $dbh->prepare("show index from $table");
1478         $sth->execute;
1479         my %existingindexes;
1480         while ( my ( $table, $non_unique, $key_name, $Seq_in_index, $Column_name, $Collation, $cardinality, $sub_part, $Packed, $comment ) = $sth->fetchrow ) {
1481                 $existingindexes{$key_name} = 1;
1482         }
1483         # read indexes to check
1484         my $tablerows = $indexes{$table};
1485         foreach my $row (@$tablerows) {
1486                 my $key_name=$row->{indexname};
1487                 if ($existingindexes{$key_name} eq 1) {
1488 #                       print "$key_name existing";
1489                 } else {
1490                         print "\tCreating index $key_name in $table\n";
1491                         my $sql;
1492                         if ($row->{indexname} eq 'PRIMARY') {
1493                                 $sql = "alter table $table ADD PRIMARY KEY ($row->{content})";
1494                         } else {
1495                                 $sql = "alter table $table ADD INDEX $key_name ($row->{content}) $row->{type}";
1496                         }
1497                         $dbh->do($sql);
1498             print "Error $sql : $dbh->err \n" if $dbh->err;
1499                 }
1500         }
1501 }
1502
1503 #
1504 # check foreign keys and create them when needed
1505 #
1506 print "Checking for foreign keys required...\n" unless $silent;
1507 foreach my $table ( keys %foreign_keys ) {
1508         #
1509         # read all indexes from $table
1510         #
1511         $sth = $dbh->prepare("show table status like '$table'");
1512         $sth->execute;
1513         my $stat = $sth->fetchrow_hashref;
1514         # read indexes to check
1515         my $tablerows = $foreign_keys{$table};
1516         foreach my $row (@$tablerows) {
1517                 my $foreign_table=$row->{foreigntable};
1518                 if ($stat->{'Comment'} =~/$foreign_table/) {
1519 #                       print "$foreign_table existing\n";
1520                 } else {
1521                         print "\tCreating foreign key $foreign_table in $table\n";
1522                         # first, drop any orphan value in child table
1523                         if ($row->{onDelete} ne "RESTRICT") {
1524                                 my $sql = "delete from $table where $row->{key} not in (select $row->{foreignkey} from $row->{foreigntable})";
1525                                 $dbh->do($sql);
1526                                 print "SQL ERROR: $sql : $dbh->err \n" if $dbh->err;
1527                         }
1528                         my $sql="alter table $table ADD FOREIGN KEY $row->{key} ($row->{key}) REFERENCES $row->{foreigntable} ($row->{foreignkey})";
1529                         $sql .= " on update ".$row->{onUpdate} if $row->{onUpdate};
1530                         $sql .= " on delete ".$row->{onDelete} if $row->{onDelete};
1531                         $dbh->do($sql);
1532                         if ($dbh->err) {
1533                                 print "====================
1534 An error occured during :
1535 \t$sql
1536 It probably means there is something wrong in your DB : a row ($table.$row->{key}) refers to a value in $row->{foreigntable}.$row->{foreignkey} that does not exist. solve the problem and run updater again (or just the previous SQL statement).
1537 You can find those values with select
1538 \t$table.* from $table where $row->{key} not in (select $row->{foreignkey} from $row->{foreigntable})
1539 ====================\n
1540 ";
1541                         }
1542                 }
1543         }
1544 }
1545
1546 #
1547 # SPECIFIC STUFF
1548 #
1549 #
1550 # create frameworkcode row in biblio table & fill it with marc_biblio.frameworkcode.
1551 #
1552
1553 # 1st, get how many biblio we will have to do...
1554 $sth = $dbh->prepare('select count(*) from marc_biblio');
1555 $sth->execute;
1556 my ($totaltodo) = $sth->fetchrow;
1557
1558 $sth = $dbh->prepare("show columns from biblio");
1559 $sth->execute();
1560 my $definitions;
1561 my $bibliofwexist=0;
1562 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow ){
1563         $bibliofwexist=1 if $column eq 'frameworkcode';
1564 }
1565 unless ($bibliofwexist) {
1566         print "moving biblioframework to biblio table\n";
1567         $dbh->do('ALTER TABLE `biblio` ADD `frameworkcode` VARCHAR( 4 ) NOT NULL AFTER `biblionumber`');
1568         $sth = $dbh->prepare('select biblionumber,frameworkcode from marc_biblio');
1569         $sth->execute;
1570         my $sth_update = $dbh->prepare('update biblio set frameworkcode=? where biblionumber=?');
1571         my $totaldone=0;
1572         while (my ($biblionumber,$frameworkcode) = $sth->fetchrow) {
1573                 $sth_update->execute($frameworkcode,$biblionumber);
1574                 $totaldone++;
1575                 print "\r$totaldone / $totaltodo" unless ($totaldone % 100);
1576         }
1577         print "\rdone\n";
1578 }
1579
1580 #
1581 # moving MARC data from marc_subfield_table to biblioitems.marc
1582 #
1583 $sth = $dbh->prepare("show columns from biblioitems");
1584 $sth->execute();
1585 my $definitions;
1586 my $marcdone=0;
1587 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow ){
1588         $marcdone=1 if ($type eq 'blob' && $column eq 'marc') ;
1589 }
1590 unless ($marcdone) {
1591         print "moving MARC record to biblioitems table\n";
1592         # changing marc field type
1593         $dbh->do('ALTER TABLE `biblioitems` CHANGE `marc` `marc` BLOB NULL DEFAULT NULL ');
1594         # adding marc xml, just for convenience
1595         $dbh->do('ALTER TABLE `biblioitems` ADD `marcxml` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ');
1596         # moving data from marc_subfield_value to biblio
1597         $sth = $dbh->prepare('select bibid,biblionumber from marc_biblio');
1598         $sth->execute;
1599         my $sth_update = $dbh->prepare('update biblioitems set marc=?, marcxml=? where biblionumber=?');
1600         my $totaldone=0;
1601         while (my ($bibid,$biblionumber) = $sth->fetchrow) {
1602                 my $record = MARCgetbiblio($dbh,$bibid);
1603         #Force UTF-8 in record leader
1604                 $record->encoding('UTF-8');
1605                 print $record->as_formatted if ($biblionumber==3902);
1606                 $sth_update->execute($record->as_usmarc(),$record->as_xml_record(),$biblionumber);
1607                 $totaldone++;
1608                 print "\r$totaldone / $totaltodo" unless ($totaldone % 100);
1609         }
1610         print "\rdone\n";
1611 }
1612
1613
1614 # at last, remove useless fields
1615 foreach $table ( keys %uselessfields ) {
1616         my @fields = split /,/,$uselessfields{$table};
1617         my $fields;
1618         my $exists;
1619         foreach my $fieldtodrop (@fields) {
1620                 $fieldtodrop =~ s/\t//g;
1621                 $fieldtodrop =~ s/\n//g;
1622                 $exists =0;
1623                 $sth = $dbh->prepare("show columns from $table");
1624                 $sth->execute;
1625                 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1626                 {
1627                         $exists =1 if ($column eq $fieldtodrop);
1628                 }
1629                 if ($exists) {
1630                         print "deleting $fieldtodrop field in $table...\n" unless $silent;
1631                         my $sth = $dbh->prepare("alter table $table drop $fieldtodrop");
1632                         $sth->execute;
1633                 }
1634         }
1635 }    # foreach
1636
1637
1638 # MOVE all tables TO UTF-8 and innoDB
1639 $sth = $dbh->prepare("show table status");
1640 $sth->execute;
1641 while ( my $table = $sth->fetchrow_hashref ) {
1642 #       if ($table->{Engine} ne 'InnoDB') {
1643 #               $dbh->do("ALTER TABLE $table->{Name} TYPE = innodb");
1644 #               print "moving $table->{Name} to InnoDB\n";
1645 #       }
1646         unless ($table->{Collation} =~ /^utf8/) {
1647                 $dbh->do("ALTER TABLE $table->{Name} CONVERT TO CHARACTER SET utf8");
1648                 $dbh->do("ALTER TABLE $table->{Name} DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
1649                 # FIXME : maybe a ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8 would be better, def char set seems to work fine. If any problem encountered, let's try with convert !
1650                 print "moving $table->{Name} to utf8\n";
1651         } else {
1652         }
1653 }
1654
1655 $sth->finish;
1656
1657 #
1658 # those 2 subs are a copy of Biblio.pm, version 2.2.4
1659 # they are useful only once, for moving from 2.2 to 3.0
1660 # the MARCgetbiblio & MARCgetitem subs in Biblio.pm
1661 # are still here, but uses other tables
1662 # (the ones that are filled by updatedatabase !)
1663 #
1664
1665 sub MARCgetbiblio {
1666
1667     # Returns MARC::Record of the biblio passed in parameter.
1668     my ( $dbh, $bibid ) = @_;
1669     my $record = MARC::Record->new();
1670 #       warn "". $bidid;
1671
1672     my $sth =
1673       $dbh->prepare(
1674 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
1675                                  from marc_subfield_table
1676                                  where bibid=? order by tag,tagorder,subfieldorder
1677                          "
1678     );
1679     my $sth2 =
1680       $dbh->prepare(
1681         "select subfieldvalue from marc_blob_subfield where blobidlink=?");
1682     $sth->execute($bibid);
1683     my $prevtagorder = 1;
1684     my $prevtag      = 'XXX';
1685     my $previndicator;
1686     my $field;        # for >=10 tags
1687     my $prevvalue;    # for <10 tags
1688     while ( my $row = $sth->fetchrow_hashref ) {
1689
1690         if ( $row->{'valuebloblink'} ) {    #---- search blob if there is one
1691             $sth2->execute( $row->{'valuebloblink'} );
1692             my $row2 = $sth2->fetchrow_hashref;
1693             $sth2->finish;
1694             $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
1695         }
1696         if ( $row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag ) {
1697             $previndicator .= "  ";
1698             if ( $prevtag < 10 ) {
1699                                 if ($prevtag ne '000') {
1700                         $record->add_fields( ( sprintf "%03s", $prevtag ), $prevvalue ) unless $prevtag eq "XXX";    # ignore the 1st loop
1701                                 } else {
1702                                         $record->leader(sprintf("%24s",$prevvalue));
1703                                 }
1704             }
1705             else {
1706                 $record->add_fields($field) unless $prevtag eq "XXX";
1707             }
1708             undef $field;
1709             $prevtagorder  = $row->{tagorder};
1710             $prevtag       = $row->{tag};
1711             $previndicator = $row->{tag_indicator};
1712             if ( $row->{tag} < 10 ) {
1713                 $prevvalue = $row->{subfieldvalue};
1714             }
1715             else {
1716                 $field = MARC::Field->new(
1717                     ( sprintf "%03s", $prevtag ),
1718                     substr( $row->{tag_indicator} . '  ', 0, 1 ),
1719                     substr( $row->{tag_indicator} . '  ', 1, 1 ),
1720                     $row->{'subfieldcode'},
1721                     $row->{'subfieldvalue'}
1722                 );
1723             }
1724         }
1725         else {
1726             if ( $row->{tag} < 10 ) {
1727                 $record->add_fields( ( sprintf "%03s", $row->{tag} ),
1728                     $row->{'subfieldvalue'} );
1729             }
1730             else {
1731                 $field->add_subfields( $row->{'subfieldcode'},
1732                     $row->{'subfieldvalue'} );
1733             }
1734             $prevtag       = $row->{tag};
1735             $previndicator = $row->{tag_indicator};
1736         }
1737     }
1738
1739     # the last has not been included inside the loop... do it now !
1740     if ( $prevtag ne "XXX" )
1741     { # check that we have found something. Otherwise, prevtag is still XXX and we
1742          # must return an empty record, not make MARC::Record fail because we try to
1743          # create a record with XXX as field :-(
1744         if ( $prevtag < 10 ) {
1745             $record->add_fields( $prevtag, $prevvalue );
1746         }
1747         else {
1748
1749             #           my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
1750             $record->add_fields($field);
1751         }
1752     }
1753     return $record;
1754 }
1755
1756 sub MARCgetitem {
1757
1758     # Returns MARC::Record of the biblio passed in parameter.
1759     my ( $dbh, $bibid, $itemnumber ) = @_;
1760     my $record = MARC::Record->new();
1761
1762     # search MARC tagorder
1763     my $sth2 =
1764       $dbh->prepare(
1765 "select tagorder from marc_subfield_table,marc_subfield_structure where marc_subfield_table.tag=marc_subfield_structure.tagfield and marc_subfield_table.subfieldcode=marc_subfield_structure.tagsubfield and bibid=? and kohafield='items.itemnumber' and subfieldvalue=?"
1766     );
1767     $sth2->execute( $bibid, $itemnumber );
1768     my ($tagorder) = $sth2->fetchrow_array();
1769
1770     #---- TODO : the leader is missing
1771     my $sth =
1772       $dbh->prepare(
1773 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
1774                                  from marc_subfield_table
1775                                  where bibid=? and tagorder=? order by subfieldcode,subfieldorder
1776                          "
1777     );
1778     $sth2 =
1779       $dbh->prepare(
1780         "select subfieldvalue from marc_blob_subfield where blobidlink=?");
1781     $sth->execute( $bibid, $tagorder );
1782     while ( my $row = $sth->fetchrow_hashref ) {
1783         if ( $row->{'valuebloblink'} ) {    #---- search blob if there is one
1784             $sth2->execute( $row->{'valuebloblink'} );
1785             my $row2 = $sth2->fetchrow_hashref;
1786             $sth2->finish;
1787             $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
1788         }
1789         if ( $record->field( $row->{'tag'} ) ) {
1790             my $field;
1791
1792 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
1793             #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
1794             if ( length( $row->{'tag'} ) < 3 ) {
1795                 $row->{'tag'} = "0" . $row->{'tag'};
1796             }
1797             $field = $record->field( $row->{'tag'} );
1798             if ($field) {
1799                 my $x =
1800                   $field->add_subfields( $row->{'subfieldcode'},
1801                     $row->{'subfieldvalue'} );
1802                 $record->delete_field($field);
1803                 $record->add_fields($field);
1804             }
1805         }
1806         else {
1807             if ( length( $row->{'tag'} ) < 3 ) {
1808                 $row->{'tag'} = "0" . $row->{'tag'};
1809             }
1810             my $temp =
1811               MARC::Field->new( $row->{'tag'}, " ", " ",
1812                 $row->{'subfieldcode'} => $row->{'subfieldvalue'} );
1813             $record->add_fields($temp);
1814         }
1815
1816     }
1817     return $record;
1818 }
1819
1820
1821 exit;
1822
1823 # $Log$
1824 # Revision 1.152  2006/06/27 09:26:37  btoumi
1825 # modify (initials,phone ) fields property in borrowers and deletedborrowers table
1826 #
1827 # Revision 1.151  2006/06/22 10:33:14  btoumi
1828 # sorry i forget deletedborrowers table
1829 # modify firstname field from deletedborrowers table
1830 #
1831 # Revision 1.149  2006/06/20 22:35:47  rangi
1832 # Code to allow the associated borrowers to work
1833 #
1834 # Revision 1.148  2006/06/17 22:12:01  rangi
1835 # Adding id field to reviews table
1836 #
1837 # Revision 1.147  2006/06/17 03:36:41  rangi
1838 # Table definition for the reviews table
1839 #
1840 # Revision 1.146  2006/06/17 03:29:41  rangi
1841 # Variable to allow librarians to switch reviews on or off
1842 #
1843 # Revision 1.145  2006/06/16 09:45:02  btoumi
1844 # updatedatabase.pl: add change of borrowers table to deletedborrowers table
1845 # deletemem.pl: delete use of warn function
1846 #
1847 # Revision 1.144  2006/06/08 15:36:31  alaurin
1848 # Add a new system preference 'AutomaticItemReturn' :
1849 #
1850 # if this prefence is switched on: the document returned in another library than homebranch, the system automaticly transfer the document to his homebranch (with notification for librarian in returns.tmpl) .
1851 #
1852 # switch off : the document stay in the holdingbranch ...
1853 #
1854 # correcting bugs :
1855 # - comment C4::acquisition (not using in request.pl).
1856 # - correcting date in request.pl
1857 # -add the new call of function getbranches in request.pl
1858 #
1859 # Revision 1.143  2006/06/07 02:02:47  bob_lyon
1860 # merging katipo changes...
1861 #
1862 # adding new preference IssuingInProcess
1863 #
1864 # Revision 1.142  2006/06/06 23:42:46  bob_lyon
1865 # Merging Katipo changes...
1866 #
1867 # Adding new system pref where one can still retrieve a correct reading
1868 # record history if one has moved older data from issues to oldissues table
1869 # to speed up issues speed
1870 #
1871 # Revision 1.141  2006/06/01 03:18:11  rangi
1872 # Adding a new column to the statistics table
1873 #
1874 # Revision 1.140  2006/05/22 22:40:45  rangi
1875 # Adding new systempreference allowing for the library to add borrowers to institutions (rest homes, parishes, schools, classes etc).
1876 #
1877 # Revision 1.139  2006/05/19 19:31:29  tgarip1957
1878 # Added new fields to auth_header and auth_subfield_table to allow ZEBRA use of authorities and new MARC framework like structure.
1879 # Authority tables are modified to be compatible with new MARC frameworks. This change is part of Authority Linking & Zebra authorities. Requires change in Mysql database. It will break head unless all changes regarding this is implemented. This warning will take place on all commits regarding this
1880 #
1881 # Revision 1.138  2006/05/19 16:51:44  alaurin
1882 # update database for :
1883 # - new feature ip and printer management
1884 # adding two fields in branches table (branchip,branchprinter)
1885 #
1886 # - waiting date : adding one field in reserves table(waiting date) to calculate the Maximum delay to pick up a reserved document when it's available
1887 #
1888 # new system preference :
1889 # - ReservesMaxPickUpDelay : Maximum delay to pick up a reserved document
1890 # TransfersMaxDaysWarning : Max delay before considering the transfer as potentialy a problem
1891 #
1892 # Revision 1.137  2006/04/18 09:36:36  plg
1893 # bug fixed: typo fixed in labels and labels_conf tables creation query.
1894 #
1895 # Revision 1.136  2006/04/17 21:55:33  sushi
1896 # Added 'labels' and 'labels_conf' tables, for spine lable tool.
1897 #
1898 # Revision 1.135  2006/04/15 02:37:03  tgarip1957
1899 # Marc record should be set to UTF-8 in leader.Force it.
1900 # XML should be with<record> wrappers
1901 #
1902 # Revision 1.134  2006/04/14 09:37:29  tipaul
1903 # improvements from SAN Ouest Provence :
1904 # * introducing a category_type into categories. It can be A (adult), C (children), P (Professionnal), I (institution/organisation).
1905 # * each category_type has it's own forms to create members.
1906 # * the borrowers table has been heavily modified (many fields changed), to get something more logic & readable
1907 # * reintroducing guarantor/guanrantee system that is now independant from hardcoded C/A for categories
1908 # * updating templates to fit template rules
1909 #
1910 # (see mail feb, 17 on koha-devel "new features for borrowers" for more details)
1911 #
1912 # Revision 1.133  2006/04/13 08:36:42  plg
1913 # new: function C4::Date::get_date_format_string_for_DHTMLcalendar based on
1914 # the system preference prefered date format.
1915 #
1916 # improvement: book fund list and budget list screen redesigned. Filters on
1917 # each field. Columns are not sortable yet. Using DHTML Calendar to fill date
1918 # fields instead of manual filling. Pagination system. From the book fund
1919 # list, you can reach the budget list, filtered on a book fund, or not. A
1920 # budget can be added only from book fund list screen.
1921 #
1922 # bug fixed: branchcode was missing in table aqbudget.
1923 #
1924 # bug fixed: when setting a branchcode to a book fund, all associated budgets
1925 # move to this branchcode.
1926 #
1927 # modification: when adding/modifying budget/fund, MySQL specific "REPLACE..."
1928 # statements replaced by standard SQL compliant statement.
1929 #
1930 # bug fixed: when adding/modifying a budget, if the book fund is associated to
1931 # a branch, the branch selection is disabled and set to the book fund branch.
1932 #
1933 # Revision 1.132  2006/04/06 12:37:05  hdl
1934 # Bugfixing : aqbookfund needed a field.
1935 #
1936 # Revision 1.131  2006/03/03 17:02:22  tipaul
1937 # commit for holidays and news management.
1938 # (some forgotten files)
1939 #
1940 # Revision 1.130  2006/03/03 16:35:21  tipaul
1941 # commit for holidays and news management.
1942 #
1943 # Contrib from Tümer Garip (from Turkey) :
1944 # * holiday :
1945 # in /tools/ the holiday.pl script let you define holidays (days where the library is closed), branch by branch. You can define 3 types of holidays :
1946 # - single day : only this day is closed
1947 # - repet weekly (like "sunday") : the day is holiday every week
1948 # - repet yearly (like "July, 4") : this day is closed every year.
1949 #
1950 # You can also put exception :
1951 # - sunday is holiday, but "2006 March, 5th" the library will be open
1952 #
1953 # The holidays are used for return date calculation : the return date is set to the next date where the library is open. A systempreference (useDaysMode) set ON (Calendar) or OFF (Normal) the calendar calculation.
1954 #
1955 # Revision 1.129  2006/02/27 18:19:33  hdl
1956 # New table used in overduerules.pl tools page.
1957 #
1958 # Revision 1.128  2006/01/25 15:16:06  tipaul
1959 # updating DB :
1960 # * removing useless tables
1961 # * adding useful indexes
1962 # * altering some columns definitions
1963 # * The goal being to have updater working fine for foreign keys.
1964 #
1965 # For me it's done, let me know if it works for you. You can see an updated schema of the DB (with constraints) on the wiki
1966 #
1967 # Revision 1.127  2006/01/24 17:57:17  tipaul
1968 # DB improvements : adding foreign keys on some tables. partial stuff done.
1969 #
1970 # Revision 1.126  2006/01/06 16:39:42  tipaul
1971 # synch'ing head and rel_2_2 (from 2.2.5, including npl templates)
1972 # Seems not to break too many things, but i'm probably wrong here.
1973 # at least, new features/bugfixes from 2.2.5 are here (tested on some features on my head local copy)
1974 #
1975 # - removing useless directories (koha-html and koha-plucene)
1976 #
1977 # Revision 1.125  2006/01/04 15:54:55  tipaul
1978 # utf8 is a : go for beta test in HEAD.
1979 # some explanations :
1980 # - updater/updatedatabase => will transform all tables in innoDB (not related to utf8, just to warn you) AND collate them in utf8 / utf8_general_ci. The SQL command is : ALTER TABLE tablename DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci.
1981 # - *-top.inc will show the pages in utf8
1982 # - THE HARD THING : for me, mysql-client and mysql-server were set up to communicate in iso8859-1, whatever the mysql collation ! Thus, pages were improperly shown, as datas were transmitted in iso8859-1 format ! After a full day of investigation, someone on usenet pointed "set NAMES 'utf8'" to explain that I wanted utf8. I could put this in my.cnf, but if I do that, ALL databases will "speak" in utf8, that's not what we want. Thus, I added a line in Context.pm : everytime a DB handle is opened, the communication is set to utf8.
1983 # - using marcxml field and no more the iso2709 raw marc biblioitems.marc field.
1984 #
1985 # Revision 1.124  2005/10/27 12:09:05  tipaul
1986 # new features for serial module :
1987 # - the last 5 issues are now shown, and their status can be changed (but not reverted to "waited", as there can be only one "waited")
1988 # - the library can create a "distribution list". this paper contains a list of borrowers (selected from the borrower list, or manually entered), and print it for a given issue. once printed, the sheet can be put on the issue and distributed to every reader on the list (one by one).
1989 #
1990 # Revision 1.123  2005/10/26 09:13:37  tipaul
1991 # big commit, still breaking things...
1992 #
1993 # * synch with rel_2_2. Probably the last non manual synch, as rel_2_2 should not be modified deeply.
1994 # * code cleaning (cleaning warnings from perl -w) continued
1995 #
1996 # Revision 1.122  2005/09/02 14:18:38  tipaul
1997 # new feature : image for itemtypes.
1998 #
1999 # * run updater/updatedatabase to create imageurl field in itemtypes.
2000 # * go to Koha >> parameters >> itemtypes >> modify (or add) an itemtype. You will see around 20 nice images to choose between (thanks to owen). If you prefer your own image, you also can type a complete url (http://www.myserver.lib/path/to/my/image.gif)
2001 # * go to OPAC, and search something. In the result list, you now have the picture instead of the text itemtype.
2002 #
2003 # Revision 1.121  2005/08/24 08:49:03  hdl
2004 # Adding a note field in serial table.
2005 # This will allow librarian to mention a note on a peculiar waiting serial number.
2006 #
2007 # Revision 1.120  2005/08/09 14:10:32  tipaul
2008 # 1st commit to go to zebra.
2009 # don't update your cvs if you want to have a working head...
2010 #
2011 # this commit contains :
2012 # * updater/updatedatabase : get rid with marc_* tables, but DON'T remove them. As a lot of things uses them, it would not be a good idea for instance to drop them. If you really want to play, you can rename them to test head without them but being still able to reintroduce them...
2013 # * Biblio.pm : modify MARCgetbiblio to find the raw marc record in biblioitems.marc field, not from marc_subfield_table, modify MARCfindframeworkcode to find frameworkcode in biblio.frameworkcode, modify some other subs to use biblio.biblionumber & get rid of bibid.
2014 # * other files : get rid of bibid and use biblionumber instead.
2015 #
2016 # What is broken :
2017 # * does not do anything on zebra yet.
2018 # * if you rename marc_subfield_table, you can't search anymore.
2019 # * you can view a biblio & bibliodetails, go to MARC editor, but NOT save any modif.
2020 # * don't try to add a biblio, it would add data poorly... (don't try to delete either, it may work, but that would be a surprise ;-) )
2021 #
2022 # IMPORTANT NOTE : you need MARC::XML package (http://search.cpan.org/~esummers/MARC-XML-0.7/lib/MARC/File/XML.pm), that requires a recent version of MARC::Record
2023 # Updatedatabase stores the iso2709 data in biblioitems.marc field & an xml version in biblioitems.marcxml Not sure we will keep it when releasing the stable version, but I think it's a good idea to have something readable in sql, at least for development stage.
2024 #
2025 # Revision 1.119  2005/08/04 16:07:58  tipaul
2026 # Synch really broke this script...
2027 #
2028 # Revision 1.118  2005/08/04 16:02:55  tipaul
2029 # oops... error in synch between 2.2 and head
2030 #
2031 # Revision 1.117  2005/08/04 14:24:39  tipaul
2032 # synch'ing 2.2 and head
2033 #
2034 # Revision 1.116  2005/08/04 08:55:54  tipaul
2035 # Letters / alert system, continuing...
2036 #
2037 # * adding a package Letters.pm, that manages Letters & alerts.
2038 # * adding feature : it's now possible to define a "letter" for any subscription created. If a letter is defined, users in OPAC can put an alert on the subscription. When an issue is marked "arrived", all users in the alert will recieve a mail (as defined in the "letter"). This last part (= send the mail) is not yet developped. (Should be done this week)
2039 # * adding feature : it's now possible to "put to an alert" in OPAC, for any serial subscription. The alert is stored in a new table, called alert. An alert can be put only if the librarian has activated them in subscription (and they activate it just by choosing a "letter" to sent to borrowers on new issues)
2040 # * adding feature : librarian can see in borrower detail which alerts they have put, and a user can see in opac-detail which alert they have put too.
2041 #
2042 # Note that the system should be generic enough to manage any type of alert.
2043 # I plan to extend it soon to virtual shelves : a borrower will be able to put an alert on a virtual shelf, to be warned when something is changed in the virtual shelf (mail being sent once a day by cron, or manually by the shelf owner. Anyway, a mail won't be sent on every change, users would be spammed by Koha ;-) )
2044 #
2045 # Revision 1.115  2005/08/02 16:15:34  tipaul
2046 # adding 2 fields to letter system :
2047 # * module (acquisition, catalogue...) : it will be usefull to show the librarian only letters he may be interested by.
2048 # * title, that will be used as mail subject.
2049 #
2050 # Revision 1.114  2005/07/28 15:10:13  tipaul
2051 # Introducing new "Letters" system : Letters will be used everytime you want to sent something to someone (through mail or paper). For example, sending a mail for overdues use letter that you can put as parameters. Sending a mail to a borrower when a suggestion is validated uses a letter too.
2052 # the letter table contains 3 fields :
2053 # * code => the code of the letter
2054 # * name => the complete name of the letter
2055 # * content => the complete text. It's a TEXT field type, so has no limits.
2056 #
2057 # My next goal now is to work on point 2-I "serial issue alert"
2058 # With this feature, in serials, a user can subscribe the "issue alert". For every issue arrived/missing, a mail is sent to all subscribers of this list. The mail warns the user that the issue is arrive or missing. Will be in head.
2059 # (see mail on koha-devel, 2005/04/07)
2060 #
2061 # The "serial issue alert" will be the 1st to use this letter system that probably needs some tweaking ;-)
2062 #
2063 # Once it will be stabilised default letters (in any languages) could be added during installer to help the library begin with this new feature.
2064 #
2065 # Revision 1.113  2005/07/28 08:38:41  tipaul
2066 # For instance, the return date does not rely on the borrower expiration date. A systempref will be added in Koha, to modify return date calculation schema :
2067 # * ReturnBeforeExpiry = yes => return date can't be after expiry date
2068 # * ReturnBeforeExpiry = no  => return date can be after expiry date
2069 #
2070 # Revision 1.112  2005/07/26 08:19:47  hdl
2071 # Adding IndependantBranches System preference variable in order to manage Branch independancy.
2072 #
2073 # Revision 1.111  2005/07/25 15:35:38  tipaul
2074 # we have decided that moving to Koha 3.0 requires being already in Koha 2.2.x
2075 # So, the updatedatabase script can highly be cleaned (90% removed).
2076 # Let's play with the new Koha DB structure now ;-)
2077 #