modify firstname field caracteristic from borrowers 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 => 'B_email',
555                         type => 'text',
556                         null => 'NULL',
557                         after => 'B_zipcode',
558                  },
559                  {
560                         field => 'streetnumber', # street number (hidden if streettable table is empty)
561                         type => 'char(10)',
562                         null => 'NULL',
563                         after => 'initials',
564                 },
565                 {
566                         field => 'streettype', # street table, list builded from a system table
567                         type => 'char(50)',
568                         null => 'NULL',
569                         after => 'streetnumber',
570                 },
571                  {
572                         field => 'B_streetnumber', # street number (hidden if streettable table is empty)
573                         type => 'char(10)',
574                         null => 'NULL',
575                         after => 'fax',
576                 },
577                 {
578                         field => 'B_streettype', # street table, list builded from a system table
579                         type => 'char(50)',
580                         null => 'NULL',
581                         after => 'B_streetnumber',
582                 },
583                 {
584                         field => 'phonepro',
585                         type => 'text',
586                         null => 'NULL',
587                         after => 'fax',
588                 },
589                 {
590                         field => 'address2', # complement address
591                         type => 'text',
592                         null => 'NULL',
593                         after => 'address',
594                 },
595                 {
596                         field => 'emailpro',
597                         type => 'text',
598                         null => 'NULL',
599                         after => 'fax',
600                 },
601                 {
602                         field => 'contactfirstname', # contact's firstname
603                         type => 'text',
604                         null => 'NULL',
605                         after => 'contactname',
606                 },
607                 {
608                         field => 'contacttitle', # contact's title
609                         type => 'text',
610                         null => 'NULL',
611                         after => 'contactfirstname',
612                 },
613         ],
614         
615         deletedborrowers => [
616                 {       field => 'B_email',
617                         type => 'text',
618                         null => 'NULL',
619                         after => 'B_zipcode',
620                  },
621                  {
622                         field => 'streetnumber', # street number (hidden if streettable table is empty)
623                         type => 'char(10)',
624                         null => 'NULL',
625                         after => 'initials',
626                 },
627                 {
628                         field => 'streettype', # street table, list builded from a system table
629                         type => 'char(50)',
630                         null => 'NULL',
631                         after => 'streetnumber',
632                 },
633                  {
634                         field => 'B_streetnumber', # street number (hidden if streettable table is empty)
635                         type => 'char(10)',
636                         null => 'NULL',
637                         after => 'fax',
638                 },
639                 {
640                         field => 'B_streettype', # street table, list builded from a system table
641                         type => 'char(50)',
642                         null => 'NULL',
643                         after => 'B_streetnumber',
644                 },
645                 {
646                         field => 'phonepro',
647                         type => 'text',
648                         null => 'NULL',
649                         after => 'fax',
650                 },
651                 {
652                         field => 'address2', # complement address
653                         type => 'text',
654                         null => 'NULL',
655                         after => 'address',
656                 },
657                 {
658                         field => 'emailpro',
659                         type => 'text',
660                         null => 'NULL',
661                         after => 'fax',
662                 },
663                 {
664                         field => 'contactfirstname', # contact's firstname
665                         type => 'text',
666                         null => 'NULL',
667                         after => 'contactname',
668                 },
669                 {
670                         field => 'contacttitle', # contact's title
671                         type => 'text',
672                         null => 'NULL',
673                         after => 'contactfirstname',
674                 },
675         ],
676         
677         branches =>  [
678                 {
679                         field   => 'branchip',
680                         type    => 'varchar(15)',
681                         null    => 'NULL',
682                         key             => '',
683                         default => '',
684                         extra   => '',
685                 },
686                 {
687                         field   => 'branchprinter',
688                         type    => 'varchar(100)',
689                         null    => 'NULL',
690                         key             => '',
691                         default => '',
692                         extra   => '',
693                 },
694         ],
695         categories =>  [
696                 {
697                         field   => 'category_type',
698                         type    => 'char(1)',
699                         null    => 'NOT NULL',
700                         key             => '',
701                         default => 'A',
702                         extra   => '',
703                 },
704         ],
705         reserves =>  [
706                 {
707                         field   => 'waitingdate',
708                         type    => 'date',
709                         null    => 'NULL',
710                         key             => '',
711                         default => '',
712                         extra   => '',
713                 },
714         ],
715 );
716
717 my %indexes = (
718 #       table => [
719 #               {       indexname => 'index detail'
720 #               }
721 #       ],
722         shelfcontents => [
723                 {       indexname => 'shelfnumber',
724                         content => 'shelfnumber',
725                 },
726                 {       indexname => 'itemnumber',
727                         content => 'itemnumber',
728                 }
729         ],
730         bibliosubject => [
731                 {       indexname => 'biblionumber',
732                         content => 'biblionumber',
733                 }
734         ],
735         items => [
736                 {       indexname => 'homebranch',
737                         content => 'homebranch',
738                 },
739                 {       indexname => 'holdingbranch',
740                         content => 'holdingbranch',
741                 }
742         ],
743         aqbooksellers => [
744                 {       indexname => 'PRIMARY',
745                         content => 'id',
746                         type => 'PRIMARY',
747                 }
748         ],
749         aqbasket => [
750                 {       indexname => 'booksellerid',
751                         content => 'booksellerid',
752                 },
753         ],
754         aqorders => [
755                 {       indexname => 'basketno',
756                         content => 'basketno',
757                 },
758         ],
759         aqorderbreakdown => [
760                 {       indexname => 'ordernumber',
761                         content => 'ordernumber',
762                 },
763                 {       indexname => 'bookfundid',
764                         content => 'bookfundid',
765                 },
766         ],
767         currency => [
768                 {       indexname => 'PRIMARY',
769                         content => 'currency',
770                         type => 'PRIMARY',
771                 }
772         ],
773 );
774
775 my %foreign_keys = (
776 #       table => [
777 #               {       key => 'the key in table' (must be indexed)
778 #                       foreigntable => 'the foreigntable name', # (the parent)
779 #                       foreignkey => 'the foreign key column(s)' # (in the parent)
780 #                       onUpdate => 'CASCADE|SET NULL|NO ACTION| RESTRICT',
781 #                       onDelete => 'CASCADE|SET NULL|NO ACTION| RESTRICT',
782 #               }
783 #       ],
784         shelfcontents => [
785                 {       key => 'shelfnumber',
786                         foreigntable => 'bookshelf',
787                         foreignkey => 'shelfnumber',
788                         onUpdate => 'CASCADE',
789                         onDelete => 'CASCADE',
790                 },
791                 {       key => 'itemnumber',
792                         foreigntable => 'items',
793                         foreignkey => 'itemnumber',
794                         onUpdate => 'CASCADE',
795                         onDelete => 'CASCADE',
796                 },
797         ],
798         # onDelete is RESTRICT on reference tables (branches, itemtype) as we don't want items to be 
799         # easily deleted, but branches/itemtype not too easy to empty...
800         biblioitems => [
801                 {       key => 'biblionumber',
802                         foreigntable => 'biblio',
803                         foreignkey => 'biblionumber',
804                         onUpdate => 'CASCADE',
805                         onDelete => 'CASCADE',
806                 },
807                 {       key => 'itemtype',
808                         foreigntable => 'itemtypes',
809                         foreignkey => 'itemtype',
810                         onUpdate => 'CASCADE',
811                         onDelete => 'RESTRICT',
812                 },
813         ],
814         items => [
815                 {       key => 'biblioitemnumber',
816                         foreigntable => 'biblioitems',
817                         foreignkey => 'biblioitemnumber',
818                         onUpdate => 'CASCADE',
819                         onDelete => 'CASCADE',
820                 },
821                 {       key => 'homebranch',
822                         foreigntable => 'branches',
823                         foreignkey => 'branchcode',
824                         onUpdate => 'CASCADE',
825                         onDelete => 'RESTRICT',
826                 },
827                 {       key => 'holdingbranch',
828                         foreigntable => 'branches',
829                         foreignkey => 'branchcode',
830                         onUpdate => 'CASCADE',
831                         onDelete => 'RESTRICT',
832                 },
833         ],
834         additionalauthors => [
835                 {       key => 'biblionumber',
836                         foreigntable => 'biblio',
837                         foreignkey => 'biblionumber',
838                         onUpdate => 'CASCADE',
839                         onDelete => 'CASCADE',
840                 },
841         ],
842         bibliosubject => [
843                 {       key => 'biblionumber',
844                         foreigntable => 'biblio',
845                         foreignkey => 'biblionumber',
846                         onUpdate => 'CASCADE',
847                         onDelete => 'CASCADE',
848                 },
849         ],
850         aqbasket => [
851                 {       key => 'booksellerid',
852                         foreigntable => 'aqbooksellers',
853                         foreignkey => 'id',
854                         onUpdate => 'CASCADE',
855                         onDelete => 'RESTRICT',
856                 },
857         ],
858         aqorders => [
859                 {       key => 'basketno',
860                         foreigntable => 'aqbasket',
861                         foreignkey => 'basketno',
862                         onUpdate => 'CASCADE',
863                         onDelete => 'CASCADE',
864                 },
865                 {       key => 'biblionumber',
866                         foreigntable => 'biblio',
867                         foreignkey => 'biblionumber',
868                         onUpdate => 'SET NULL',
869                         onDelete => 'SET NULL',
870                 },
871         ],
872         aqbooksellers => [
873                 {       key => 'listprice',
874                         foreigntable => 'currency',
875                         foreignkey => 'currency',
876                         onUpdate => 'CASCADE',
877                         onDelete => 'CASCADE',
878                 },
879                 {       key => 'invoiceprice',
880                         foreigntable => 'currency',
881                         foreignkey => 'currency',
882                         onUpdate => 'CASCADE',
883                         onDelete => 'CASCADE',
884                 },
885         ],
886         aqorderbreakdown => [
887                 {       key => 'ordernumber',
888                         foreigntable => 'aqorders',
889                         foreignkey => 'ordernumber',
890                         onUpdate => 'CASCADE',
891                         onDelete => 'CASCADE',
892                 },
893                 {       key => 'bookfundid',
894                         foreigntable => 'aqbookfund',
895                         foreignkey => 'bookfundid',
896                         onUpdate => 'CASCADE',
897                         onDelete => 'CASCADE',
898                 },
899         ],
900         branchtransfers => [
901                 {       key => 'frombranch',
902                         foreigntable => 'branches',
903                         foreignkey => 'branchcode',
904                         onUpdate => 'CASCADE',
905                         onDelete => 'CASCADE',
906                 },
907                 {       key => 'tobranch',
908                         foreigntable => 'branches',
909                         foreignkey => 'branchcode',
910                         onUpdate => 'CASCADE',
911                         onDelete => 'CASCADE',
912                 },
913                 {       key => 'itemnumber',
914                         foreigntable => 'items',
915                         foreignkey => 'itemnumber',
916                         onUpdate => 'CASCADE',
917                         onDelete => 'CASCADE',
918                 },
919         ],
920         issuingrules => [
921                 {       key => 'categorycode',
922                         foreigntable => 'categories',
923                         foreignkey => 'categorycode',
924                         onUpdate => 'CASCADE',
925                         onDelete => 'CASCADE',
926                 },
927                 {       key => 'itemtype',
928                         foreigntable => 'itemtypes',
929                         foreignkey => 'itemtype',
930                         onUpdate => 'CASCADE',
931                         onDelete => 'CASCADE',
932                 },
933         ],
934         issues => [     # constraint is SET NULL : when a borrower or an item is deleted, we keep the issuing record
935         # for stat purposes
936                 {       key => 'borrowernumber',
937                         foreigntable => 'borrowers',
938                         foreignkey => 'borrowernumber',
939                         onUpdate => 'SET NULL',
940                         onDelete => 'SET NULL',
941                 },
942                 {       key => 'itemnumber',
943                         foreigntable => 'items',
944                         foreignkey => 'itemnumber',
945                         onUpdate => 'SET NULL',
946                         onDelete => 'SET NULL',
947                 },
948         ],
949         reserves => [
950                 {       key => 'borrowernumber',
951                         foreigntable => 'borrowers',
952                         foreignkey => 'borrowernumber',
953                         onUpdate => 'CASCADE',
954                         onDelete => 'CASCADE',
955                 },
956                 {       key => 'biblionumber',
957                         foreigntable => 'biblio',
958                         foreignkey => 'biblionumber',
959                         onUpdate => 'CASCADE',
960                         onDelete => 'CASCADE',
961                 },
962                 {       key => 'itemnumber',
963                         foreigntable => 'items',
964                         foreignkey => 'itemnumber',
965                         onUpdate => 'CASCADE',
966                         onDelete => 'CASCADE',
967                 },
968                 {       key => 'branchcode',
969                         foreigntable => 'branches',
970                         foreignkey => 'branchcode',
971                         onUpdate => 'CASCADE',
972                         onDelete => 'CASCADE',
973                 },
974         ],
975         borrowers => [ # foreign keys are RESTRICT as we don't want to delete borrowers when a branch is deleted
976         # but prevent deleting a branch as soon as it has 1 borrower !
977                 {       key => 'categorycode',
978                         foreigntable => 'categories',
979                         foreignkey => 'categorycode',
980                         onUpdate => 'RESTRICT',
981                         onDelete => 'RESTRICT',
982                 },
983                 {       key => 'branchcode',
984                         foreigntable => 'branches',
985                         foreignkey => 'branchcode',
986                         onUpdate => 'RESTRICT',
987                         onDelete => 'RESTRICT',
988                 },
989         ],
990         deletedborrowers => [ # foreign keys are RESTRICT as we don't want to delete borrowers when a branch is deleted
991         # but prevent deleting a branch as soon as it has 1 borrower !
992                 {       key => 'categorycode',
993                         foreigntable => 'categories',
994                         foreignkey => 'categorycode',
995                         onUpdate => 'RESTRICT',
996                         onDelete => 'RESTRICT',
997                 },
998                 {       key => 'branchcode',
999                         foreigntable => 'branches',
1000                         foreignkey => 'branchcode',
1001                         onUpdate => 'RESTRICT',
1002                         onDelete => 'RESTRICT',
1003                 },
1004         ],
1005         accountlines => [
1006                 {       key => 'borrowernumber',
1007                         foreigntable => 'borrowers',
1008                         foreignkey => 'borrowernumber',
1009                         onUpdate => 'CASCADE',
1010                         onDelete => 'CASCADE',
1011                 },
1012                 {       key => 'itemnumber',
1013                         foreigntable => 'items',
1014                         foreignkey => 'itemnumber',
1015                         onUpdate => 'SET NULL',
1016                         onDelete => 'SET NULL',
1017                 },
1018         ],
1019         auth_tag_structure => [
1020                 {       key => 'authtypecode',
1021                         foreigntable => 'auth_types',
1022                         foreignkey => 'authtypecode',
1023                         onUpdate => 'CASCADE',
1024                         onDelete => 'CASCADE',
1025                 },
1026         ],
1027         # FIXME : don't constraint auth_*_table and auth_word, as they may be replaced by zebra
1028 );
1029
1030
1031 # column changes
1032 my %column_change = (
1033         # table
1034         borrowers => [
1035                                 {
1036                                         from => 'emailaddress',
1037                                         to => 'email',
1038                                         after => 'city',
1039                                 },
1040                                 {
1041                                         from => 'streetaddress',
1042                                         to => 'address',
1043                                         after => 'initials',
1044                                 },
1045                                 {
1046                                         from => 'faxnumber',
1047                                         to => 'fax',
1048                                         after => 'phone',
1049                                 },
1050                                 {
1051                                         from => 'textmessaging',
1052                                         to => 'opacnote',
1053                                         after => 'userid',
1054                                 },
1055                                 {
1056                                         from => 'altnotes',
1057                                         to => 'contactnote',
1058                                         after => 'opacnote',
1059                                 },
1060                                 {
1061                                         from => 'physstreet',
1062                                         to => 'B_address',
1063                                         after => 'fax',
1064                                 },
1065                                 {
1066                                         from => 'streetcity',
1067                                         to => 'B_city',
1068                                         after => 'B_address',
1069                                 },
1070                                 {
1071                                         from => 'phoneday',
1072                                         to => 'mobile',
1073                                         after => 'phone',
1074                                 },
1075                                 {
1076                                         from => 'zipcode',
1077                                         to => 'zipcode',
1078                                         after => 'city',
1079                                 },
1080                                 {
1081                                         from => 'homezipcode',
1082                                         to => 'B_zipcode',
1083                                         after => 'B_city',
1084                                 },
1085                                 {
1086                                         from => 'altphone',
1087                                         to => 'B_phone',
1088                                         after => 'B_zipcode',
1089                                 },
1090                                 {
1091                                         from => 'expiry',
1092                                         to => 'dateexpiry',
1093                                         after => 'dateenrolled',
1094                                 },
1095                                 {
1096                                         from => 'guarantor',
1097                                         to => 'guarantorid',
1098                                         after => 'contactname',
1099                                 },
1100                                 {
1101                                         from => 'textmessaging',
1102                                         to => 'opacnotes',
1103                                         after => 'flags',
1104                                 },
1105                                 {
1106                                         from => 'altnotes',
1107                                         to => 'contactnotes',
1108                                         after => 'opacnotes',
1109                                 },
1110                                 {
1111                                         from => 'altrelationship',
1112                                         to => 'relationship',
1113                                         after => 'borrowernotes',
1114                                 },
1115                         ],
1116
1117         deletedborrowers => [
1118                                 {
1119                                         from => 'emailaddress',
1120                                         to => 'email',
1121                                         after => 'city',
1122                                 },
1123                                 {
1124                                         from => 'streetaddress',
1125                                         to => 'address',
1126                                         after => 'initials',
1127                                 },
1128                                 {
1129                                         from => 'faxnumber',
1130                                         to => 'fax',
1131                                         after => 'phone',
1132                                 },
1133                                 {
1134                                         from => 'textmessaging',
1135                                         to => 'opacnote',
1136                                         after => 'userid',
1137                                 },
1138                                 {
1139                                         from => 'altnotes',
1140                                         to => 'contactnote',
1141                                         after => 'opacnote',
1142                                 },
1143                                 {
1144                                         from => 'physstreet',
1145                                         to => 'B_address',
1146                                         after => 'fax',
1147                                 },
1148                                 {
1149                                         from => 'streetcity',
1150                                         to => 'B_city',
1151                                         after => 'B_address',
1152                                 },
1153                                 {
1154                                         from => 'phoneday',
1155                                         to => 'mobile',
1156                                         after => 'phone',
1157                                 },
1158                                 {
1159                                         from => 'zipcode',
1160                                         to => 'zipcode',
1161                                         after => 'city',
1162                                 },
1163                                 {
1164                                         from => 'homezipcode',
1165                                         to => 'B_zipcode',
1166                                         after => 'B_city',
1167                                 },
1168                                 {
1169                                         from => 'altphone',
1170                                         to => 'B_phone',
1171                                         after => 'B_zipcode',
1172                                 },
1173                                 {
1174                                         from => 'expiry',
1175                                         to => 'dateexpiry',
1176                                         after => 'dateenrolled',
1177                                 },
1178                                 {
1179                                         from => 'guarantor',
1180                                         to => 'guarantorid',
1181                                         after => 'contactname',
1182                                 },
1183                                 {
1184                                         from => 'textmessaging',
1185                                         to => 'opacnotes',
1186                                         after => 'flags',
1187                                 },
1188                                 {
1189                                         from => 'altnotes',
1190                                         to => 'contactnotes',
1191                                         after => 'opacnotes',
1192                                 },
1193                                 {
1194                                         from => 'altrelationship',
1195                                         to => 'relationship',
1196                                         after => 'borrowernotes',
1197                                 },
1198                         ],
1199
1200                 );
1201                 
1202 foreach my $table (keys %column_change) {
1203         $sth = $dbh->prepare("show columns from $table");
1204         $sth->execute();
1205         undef %types;
1206         while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1207         {
1208                 $types{$column}->{type} ="$type";
1209                 $types{$column}->{null} = "$null";
1210                 $types{$column}->{key} = "$key";
1211                 $types{$column}->{default} = "$default";
1212                 $types{$column}->{extra} = "$extra";
1213         }    # while
1214         my $tablerows = $column_change{$table};
1215         foreach my $row ( @$tablerows ) {
1216                 if ($types{$row->{from}}->{type}) {
1217                         print "altering $table $row->{from} to $row->{to}\n";
1218                         # ALTER TABLE `borrowers` CHANGE `faxnumber` `fax` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL 
1219 #                       alter table `borrowers` change `faxnumber` `fax` type text  null after phone
1220                         my $sql = 
1221                                 "alter table `$table` change `$row->{from}` `$row->{to}` $types{$row->{from}}->{type} ".
1222                                 ($types{$row->{from}}->{null} eq 'YES'?" NULL":" NOT NULL").
1223                                 ($types{$row->{from}}->{default}?" default ".$types{$row->{from}}->{default}:"").
1224                                 "$types{$row->{from}}->{extra} after $row->{after} ";
1225 #                       print "$sql";
1226                         $dbh->do($sql);
1227                 }
1228         }
1229 }
1230
1231 #-------------------
1232 # Initialize
1233
1234 # Start checking
1235
1236 # Get version of MySQL database engine.
1237 my $mysqlversion = `mysqld --version`;
1238 $mysqlversion =~ /Ver (\S*) /;
1239 $mysqlversion = $1;
1240 if ( $mysqlversion ge '3.23' ) {
1241     print "Could convert to MyISAM database tables...\n" unless $silent;
1242 }
1243
1244 #---------------------------------
1245 # Tables
1246
1247 # Collect all tables into a list
1248 $sth = $dbh->prepare("show tables");
1249 $sth->execute;
1250 while ( my ($table) = $sth->fetchrow ) {
1251     $existingtables{$table} = 1;
1252 }
1253
1254
1255 # Now add any missing tables
1256 foreach $table ( keys %requiretables ) {
1257     unless ( $existingtables{$table} ) {
1258         print "Adding $table table...\n" unless $silent;
1259         my $sth = $dbh->prepare("create table $table $requiretables{$table}");
1260         $sth->execute;
1261         if ( $sth->err ) {
1262             print "Error : $sth->errstr \n";
1263             $sth->finish;
1264         }    # if error
1265     }    # unless exists
1266 }    # foreach
1267
1268 # now drop useless tables
1269 foreach $table ( keys %dropable_table ) {
1270         if ( $existingtables{$table} ) {
1271                 print "Dropping unused table $table\n" if $debug and not $silent;
1272                 $dbh->do("drop table $table");
1273                 if ( $dbh->err ) {
1274                         print "Error : $dbh->errstr \n";
1275                 }
1276         }
1277 }
1278
1279 #---------------------------------
1280 # Columns
1281
1282 foreach $table ( keys %requirefields ) {
1283     print "Check table $table\n" if $debug and not $silent;
1284     $sth = $dbh->prepare("show columns from $table");
1285     $sth->execute();
1286     undef %types;
1287     while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1288     {
1289         $types{$column} = $type;
1290     }    # while
1291     foreach $column ( keys %{ $requirefields{$table} } ) {
1292         print "  Check column $column  [$types{$column}]\n" if $debug and not $silent;
1293         if ( !$types{$column} ) {
1294
1295             # column doesn't exist
1296             print "Adding $column field to $table table...\n" unless $silent;
1297             $query = "alter table $table
1298                         add column $column " . $requirefields{$table}->{$column};
1299             print "Execute: $query\n" if $debug;
1300             my $sti = $dbh->prepare($query);
1301             $sti->execute;
1302             if ( $sti->err ) {
1303                 print "**Error : $sti->errstr \n";
1304                 $sti->finish;
1305             }    # if error
1306         }    # if column
1307     }    # foreach column
1308 }    # foreach table
1309
1310 foreach $table ( keys %fielddefinitions ) {
1311         print "Check table $table\n" if $debug;
1312         $sth = $dbh->prepare("show columns from $table");
1313         $sth->execute();
1314         my $definitions;
1315         while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1316         {
1317                 $definitions->{$column}->{type}    = $type;
1318                 $definitions->{$column}->{null}    = $null;
1319                 $definitions->{$column}->{null}    = 'NULL' if $null eq 'YES';
1320                 $definitions->{$column}->{key}     = $key;
1321                 $definitions->{$column}->{default} = $default;
1322                 $definitions->{$column}->{extra}   = $extra;
1323         }    # while
1324         my $fieldrow = $fielddefinitions{$table};
1325         foreach my $row (@$fieldrow) {
1326                 my $field   = $row->{field};
1327                 my $type    = $row->{type};
1328                 my $null    = $row->{null};
1329 #               $null    = 'YES' if $row->{null} eq 'NULL';
1330                 my $key     = $row->{key};
1331                 my $default = $row->{default};
1332                 my $null    = $row->{null};
1333 #               $default="''" unless $default;
1334                 my $extra   = $row->{extra};
1335                 my $def     = $definitions->{$field};
1336                 my $after       = ($row->{after}?" after ".$row->{after}:"");
1337
1338                 unless ( $type eq $def->{type}
1339                         && $null eq $def->{null}
1340                         && $key eq $def->{key}
1341                         && $extra eq $def->{extra} )
1342                 {
1343                         if ( $null eq '' ) {
1344                                 $null = 'NOT NULL';
1345                         }
1346                         if ( $key eq 'PRI' ) {
1347                                 $key = 'PRIMARY KEY';
1348                         }
1349                         unless ( $extra eq 'auto_increment' ) {
1350                                 $extra = '';
1351                         }
1352
1353                         # if it's a new column use "add", if it's an old one, use "change".
1354                         my $action;
1355                         if ($definitions->{$field}->{type}) {
1356                                 $action="change $field"
1357                         } else {
1358                                 $action="add";
1359                         }
1360 # if it's a primary key, drop the previous pk, before altering the table
1361                         my $sth;
1362                         if ($key ne 'PRIMARY KEY') {
1363                                 $sth =$dbh->prepare("alter table $table $action $field $type $null $key $extra default ? $after");
1364                         } else {
1365                                 $sth =$dbh->prepare("alter table $table drop primary key, $action $field $type $null $key $extra default ? $after");
1366                         }
1367                         $sth->execute($default);
1368                         print "  alter or create $field in $table\n" unless $silent;
1369                 }
1370         }
1371 }
1372
1373 # Populate tables with required data
1374
1375
1376 # synch table and deletedtable.
1377 foreach my $table (('borrowers','items','biblio','biblioitems')) {
1378         my %deletedborrowers;
1379         print "synch'ing $table\n";
1380         $sth = $dbh->prepare("show columns from deleted$table");
1381         $sth->execute;
1382         while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow ) {
1383                 $deletedborrowers{$column}=1;
1384         }
1385         $sth = $dbh->prepare("show columns from $table");
1386         $sth->execute;
1387         my $previous;
1388         while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow ) {
1389                 unless ($deletedborrowers{$column}) {
1390                         my $newcol="alter table deleted$table add $column $type";
1391                         if ($null eq 'YES') {
1392                                 $newcol .= " NULL ";
1393                         } else {
1394                                 $newcol .= " NOT NULL ";
1395                         }
1396                         $newcol .= "default $default" if $default;
1397                         $newcol .= " after $previous" if $previous;
1398                         $previous=$column;
1399                         print "creating column $column\n";
1400                         $dbh->do($newcol);
1401                 }
1402         }
1403 }
1404
1405 foreach my $table ( keys %tabledata ) {
1406     print "Checking for data required in table $table...\n" unless $silent;
1407     my $tablerows = $tabledata{$table};
1408     foreach my $row (@$tablerows) {
1409         my $uniquefieldrequired = $row->{uniquefieldrequired};
1410         my $uniquevalue         = $row->{$uniquefieldrequired};
1411         my $forceupdate         = $row->{forceupdate};
1412         my $sth                 =
1413           $dbh->prepare(
1414 "select $uniquefieldrequired from $table where $uniquefieldrequired=?"
1415         );
1416         $sth->execute($uniquevalue);
1417                 if ($sth->rows) {
1418                         foreach my $field (keys %$forceupdate) {
1419                                 if ($forceupdate->{$field}) {
1420                                         my $sth=$dbh->prepare("update systempreferences set $field=? where $uniquefieldrequired=?");
1421                                         $sth->execute($row->{$field}, $uniquevalue);
1422                                 }
1423                 }
1424                 } else {
1425                         print "Adding row to $table: " unless $silent;
1426                         my @values;
1427                         my $fieldlist;
1428                         my $placeholders;
1429                         foreach my $field ( keys %$row ) {
1430                                 next if $field eq 'uniquefieldrequired';
1431                                 next if $field eq 'forceupdate';
1432                                 my $value = $row->{$field};
1433                                 push @values, $value;
1434                                 print "  $field => $value" unless $silent;
1435                                 $fieldlist .= "$field,";
1436                                 $placeholders .= "?,";
1437                         }
1438                         print "\n" unless $silent;
1439                         $fieldlist    =~ s/,$//;
1440                         $placeholders =~ s/,$//;
1441                         my $sth =
1442                         $dbh->prepare(
1443                                 "insert into $table ($fieldlist) values ($placeholders)");
1444                         $sth->execute(@values);
1445                 }
1446         }
1447 }
1448
1449 #
1450 # check indexes and create them when needed
1451 #
1452 print "Checking for index required...\n" unless $silent;
1453 foreach my $table ( keys %indexes ) {
1454         #
1455         # read all indexes from $table
1456         #
1457         $sth = $dbh->prepare("show index from $table");
1458         $sth->execute;
1459         my %existingindexes;
1460         while ( my ( $table, $non_unique, $key_name, $Seq_in_index, $Column_name, $Collation, $cardinality, $sub_part, $Packed, $comment ) = $sth->fetchrow ) {
1461                 $existingindexes{$key_name} = 1;
1462         }
1463         # read indexes to check
1464         my $tablerows = $indexes{$table};
1465         foreach my $row (@$tablerows) {
1466                 my $key_name=$row->{indexname};
1467                 if ($existingindexes{$key_name} eq 1) {
1468 #                       print "$key_name existing";
1469                 } else {
1470                         print "\tCreating index $key_name in $table\n";
1471                         my $sql;
1472                         if ($row->{indexname} eq 'PRIMARY') {
1473                                 $sql = "alter table $table ADD PRIMARY KEY ($row->{content})";
1474                         } else {
1475                                 $sql = "alter table $table ADD INDEX $key_name ($row->{content}) $row->{type}";
1476                         }
1477                         $dbh->do($sql);
1478             print "Error $sql : $dbh->err \n" if $dbh->err;
1479                 }
1480         }
1481 }
1482
1483 #
1484 # check foreign keys and create them when needed
1485 #
1486 print "Checking for foreign keys required...\n" unless $silent;
1487 foreach my $table ( keys %foreign_keys ) {
1488         #
1489         # read all indexes from $table
1490         #
1491         $sth = $dbh->prepare("show table status like '$table'");
1492         $sth->execute;
1493         my $stat = $sth->fetchrow_hashref;
1494         # read indexes to check
1495         my $tablerows = $foreign_keys{$table};
1496         foreach my $row (@$tablerows) {
1497                 my $foreign_table=$row->{foreigntable};
1498                 if ($stat->{'Comment'} =~/$foreign_table/) {
1499 #                       print "$foreign_table existing\n";
1500                 } else {
1501                         print "\tCreating foreign key $foreign_table in $table\n";
1502                         # first, drop any orphan value in child table
1503                         if ($row->{onDelete} ne "RESTRICT") {
1504                                 my $sql = "delete from $table where $row->{key} not in (select $row->{foreignkey} from $row->{foreigntable})";
1505                                 $dbh->do($sql);
1506                                 print "SQL ERROR: $sql : $dbh->err \n" if $dbh->err;
1507                         }
1508                         my $sql="alter table $table ADD FOREIGN KEY $row->{key} ($row->{key}) REFERENCES $row->{foreigntable} ($row->{foreignkey})";
1509                         $sql .= " on update ".$row->{onUpdate} if $row->{onUpdate};
1510                         $sql .= " on delete ".$row->{onDelete} if $row->{onDelete};
1511                         $dbh->do($sql);
1512                         if ($dbh->err) {
1513                                 print "====================
1514 An error occured during :
1515 \t$sql
1516 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).
1517 You can find those values with select
1518 \t$table.* from $table where $row->{key} not in (select $row->{foreignkey} from $row->{foreigntable})
1519 ====================\n
1520 ";
1521                         }
1522                 }
1523         }
1524 }
1525
1526 #
1527 # SPECIFIC STUFF
1528 #
1529 #
1530 # create frameworkcode row in biblio table & fill it with marc_biblio.frameworkcode.
1531 #
1532
1533 # 1st, get how many biblio we will have to do...
1534 $sth = $dbh->prepare('select count(*) from marc_biblio');
1535 $sth->execute;
1536 my ($totaltodo) = $sth->fetchrow;
1537
1538 $sth = $dbh->prepare("show columns from biblio");
1539 $sth->execute();
1540 my $definitions;
1541 my $bibliofwexist=0;
1542 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow ){
1543         $bibliofwexist=1 if $column eq 'frameworkcode';
1544 }
1545 unless ($bibliofwexist) {
1546         print "moving biblioframework to biblio table\n";
1547         $dbh->do('ALTER TABLE `biblio` ADD `frameworkcode` VARCHAR( 4 ) NOT NULL AFTER `biblionumber`');
1548         $sth = $dbh->prepare('select biblionumber,frameworkcode from marc_biblio');
1549         $sth->execute;
1550         my $sth_update = $dbh->prepare('update biblio set frameworkcode=? where biblionumber=?');
1551         my $totaldone=0;
1552         while (my ($biblionumber,$frameworkcode) = $sth->fetchrow) {
1553                 $sth_update->execute($frameworkcode,$biblionumber);
1554                 $totaldone++;
1555                 print "\r$totaldone / $totaltodo" unless ($totaldone % 100);
1556         }
1557         print "\rdone\n";
1558 }
1559
1560 #
1561 # moving MARC data from marc_subfield_table to biblioitems.marc
1562 #
1563 $sth = $dbh->prepare("show columns from biblioitems");
1564 $sth->execute();
1565 my $definitions;
1566 my $marcdone=0;
1567 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow ){
1568         $marcdone=1 if ($type eq 'blob' && $column eq 'marc') ;
1569 }
1570 unless ($marcdone) {
1571         print "moving MARC record to biblioitems table\n";
1572         # changing marc field type
1573         $dbh->do('ALTER TABLE `biblioitems` CHANGE `marc` `marc` BLOB NULL DEFAULT NULL ');
1574         # adding marc xml, just for convenience
1575         $dbh->do('ALTER TABLE `biblioitems` ADD `marcxml` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ');
1576         # moving data from marc_subfield_value to biblio
1577         $sth = $dbh->prepare('select bibid,biblionumber from marc_biblio');
1578         $sth->execute;
1579         my $sth_update = $dbh->prepare('update biblioitems set marc=?, marcxml=? where biblionumber=?');
1580         my $totaldone=0;
1581         while (my ($bibid,$biblionumber) = $sth->fetchrow) {
1582                 my $record = MARCgetbiblio($dbh,$bibid);
1583         #Force UTF-8 in record leader
1584                 $record->encoding('UTF-8');
1585                 print $record->as_formatted if ($biblionumber==3902);
1586                 $sth_update->execute($record->as_usmarc(),$record->as_xml_record(),$biblionumber);
1587                 $totaldone++;
1588                 print "\r$totaldone / $totaltodo" unless ($totaldone % 100);
1589         }
1590         print "\rdone\n";
1591 }
1592
1593
1594 # at last, remove useless fields
1595 foreach $table ( keys %uselessfields ) {
1596         my @fields = split /,/,$uselessfields{$table};
1597         my $fields;
1598         my $exists;
1599         foreach my $fieldtodrop (@fields) {
1600                 $fieldtodrop =~ s/\t//g;
1601                 $fieldtodrop =~ s/\n//g;
1602                 $exists =0;
1603                 $sth = $dbh->prepare("show columns from $table");
1604                 $sth->execute;
1605                 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1606                 {
1607                         $exists =1 if ($column eq $fieldtodrop);
1608                 }
1609                 if ($exists) {
1610                         print "deleting $fieldtodrop field in $table...\n" unless $silent;
1611                         my $sth = $dbh->prepare("alter table $table drop $fieldtodrop");
1612                         $sth->execute;
1613                 }
1614         }
1615 }    # foreach
1616
1617
1618 # MOVE all tables TO UTF-8 and innoDB
1619 $sth = $dbh->prepare("show table status");
1620 $sth->execute;
1621 while ( my $table = $sth->fetchrow_hashref ) {
1622 #       if ($table->{Engine} ne 'InnoDB') {
1623 #               $dbh->do("ALTER TABLE $table->{Name} TYPE = innodb");
1624 #               print "moving $table->{Name} to InnoDB\n";
1625 #       }
1626         unless ($table->{Collation} =~ /^utf8/) {
1627                 $dbh->do("ALTER TABLE $table->{Name} CONVERT TO CHARACTER SET utf8");
1628                 $dbh->do("ALTER TABLE $table->{Name} DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
1629                 # 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 !
1630                 print "moving $table->{Name} to utf8\n";
1631         } else {
1632         }
1633 }
1634
1635 $sth->finish;
1636
1637 #
1638 # those 2 subs are a copy of Biblio.pm, version 2.2.4
1639 # they are useful only once, for moving from 2.2 to 3.0
1640 # the MARCgetbiblio & MARCgetitem subs in Biblio.pm
1641 # are still here, but uses other tables
1642 # (the ones that are filled by updatedatabase !)
1643 #
1644
1645 sub MARCgetbiblio {
1646
1647     # Returns MARC::Record of the biblio passed in parameter.
1648     my ( $dbh, $bibid ) = @_;
1649     my $record = MARC::Record->new();
1650 #       warn "". $bidid;
1651
1652     my $sth =
1653       $dbh->prepare(
1654 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
1655                                  from marc_subfield_table
1656                                  where bibid=? order by tag,tagorder,subfieldorder
1657                          "
1658     );
1659     my $sth2 =
1660       $dbh->prepare(
1661         "select subfieldvalue from marc_blob_subfield where blobidlink=?");
1662     $sth->execute($bibid);
1663     my $prevtagorder = 1;
1664     my $prevtag      = 'XXX';
1665     my $previndicator;
1666     my $field;        # for >=10 tags
1667     my $prevvalue;    # for <10 tags
1668     while ( my $row = $sth->fetchrow_hashref ) {
1669
1670         if ( $row->{'valuebloblink'} ) {    #---- search blob if there is one
1671             $sth2->execute( $row->{'valuebloblink'} );
1672             my $row2 = $sth2->fetchrow_hashref;
1673             $sth2->finish;
1674             $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
1675         }
1676         if ( $row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag ) {
1677             $previndicator .= "  ";
1678             if ( $prevtag < 10 ) {
1679                                 if ($prevtag ne '000') {
1680                         $record->add_fields( ( sprintf "%03s", $prevtag ), $prevvalue ) unless $prevtag eq "XXX";    # ignore the 1st loop
1681                                 } else {
1682                                         $record->leader(sprintf("%24s",$prevvalue));
1683                                 }
1684             }
1685             else {
1686                 $record->add_fields($field) unless $prevtag eq "XXX";
1687             }
1688             undef $field;
1689             $prevtagorder  = $row->{tagorder};
1690             $prevtag       = $row->{tag};
1691             $previndicator = $row->{tag_indicator};
1692             if ( $row->{tag} < 10 ) {
1693                 $prevvalue = $row->{subfieldvalue};
1694             }
1695             else {
1696                 $field = MARC::Field->new(
1697                     ( sprintf "%03s", $prevtag ),
1698                     substr( $row->{tag_indicator} . '  ', 0, 1 ),
1699                     substr( $row->{tag_indicator} . '  ', 1, 1 ),
1700                     $row->{'subfieldcode'},
1701                     $row->{'subfieldvalue'}
1702                 );
1703             }
1704         }
1705         else {
1706             if ( $row->{tag} < 10 ) {
1707                 $record->add_fields( ( sprintf "%03s", $row->{tag} ),
1708                     $row->{'subfieldvalue'} );
1709             }
1710             else {
1711                 $field->add_subfields( $row->{'subfieldcode'},
1712                     $row->{'subfieldvalue'} );
1713             }
1714             $prevtag       = $row->{tag};
1715             $previndicator = $row->{tag_indicator};
1716         }
1717     }
1718
1719     # the last has not been included inside the loop... do it now !
1720     if ( $prevtag ne "XXX" )
1721     { # check that we have found something. Otherwise, prevtag is still XXX and we
1722          # must return an empty record, not make MARC::Record fail because we try to
1723          # create a record with XXX as field :-(
1724         if ( $prevtag < 10 ) {
1725             $record->add_fields( $prevtag, $prevvalue );
1726         }
1727         else {
1728
1729             #           my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
1730             $record->add_fields($field);
1731         }
1732     }
1733     return $record;
1734 }
1735
1736 sub MARCgetitem {
1737
1738     # Returns MARC::Record of the biblio passed in parameter.
1739     my ( $dbh, $bibid, $itemnumber ) = @_;
1740     my $record = MARC::Record->new();
1741
1742     # search MARC tagorder
1743     my $sth2 =
1744       $dbh->prepare(
1745 "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=?"
1746     );
1747     $sth2->execute( $bibid, $itemnumber );
1748     my ($tagorder) = $sth2->fetchrow_array();
1749
1750     #---- TODO : the leader is missing
1751     my $sth =
1752       $dbh->prepare(
1753 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
1754                                  from marc_subfield_table
1755                                  where bibid=? and tagorder=? order by subfieldcode,subfieldorder
1756                          "
1757     );
1758     $sth2 =
1759       $dbh->prepare(
1760         "select subfieldvalue from marc_blob_subfield where blobidlink=?");
1761     $sth->execute( $bibid, $tagorder );
1762     while ( my $row = $sth->fetchrow_hashref ) {
1763         if ( $row->{'valuebloblink'} ) {    #---- search blob if there is one
1764             $sth2->execute( $row->{'valuebloblink'} );
1765             my $row2 = $sth2->fetchrow_hashref;
1766             $sth2->finish;
1767             $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
1768         }
1769         if ( $record->field( $row->{'tag'} ) ) {
1770             my $field;
1771
1772 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
1773             #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
1774             if ( length( $row->{'tag'} ) < 3 ) {
1775                 $row->{'tag'} = "0" . $row->{'tag'};
1776             }
1777             $field = $record->field( $row->{'tag'} );
1778             if ($field) {
1779                 my $x =
1780                   $field->add_subfields( $row->{'subfieldcode'},
1781                     $row->{'subfieldvalue'} );
1782                 $record->delete_field($field);
1783                 $record->add_fields($field);
1784             }
1785         }
1786         else {
1787             if ( length( $row->{'tag'} ) < 3 ) {
1788                 $row->{'tag'} = "0" . $row->{'tag'};
1789             }
1790             my $temp =
1791               MARC::Field->new( $row->{'tag'}, " ", " ",
1792                 $row->{'subfieldcode'} => $row->{'subfieldvalue'} );
1793             $record->add_fields($temp);
1794         }
1795
1796     }
1797     return $record;
1798 }
1799
1800
1801 exit;
1802
1803 # $Log$
1804 # Revision 1.150  2006/06/22 10:30:57  btoumi
1805 # modify firstname field caracteristic from borrowers table
1806 #
1807 # Revision 1.149  2006/06/20 22:35:47  rangi
1808 # Code to allow the associated borrowers to work
1809 #
1810 # Revision 1.148  2006/06/17 22:12:01  rangi
1811 # Adding id field to reviews table
1812 #
1813 # Revision 1.147  2006/06/17 03:36:41  rangi
1814 # Table definition for the reviews table
1815 #
1816 # Revision 1.146  2006/06/17 03:29:41  rangi
1817 # Variable to allow librarians to switch reviews on or off
1818 #
1819 # Revision 1.145  2006/06/16 09:45:02  btoumi
1820 # updatedatabase.pl: add change of borrowers table to deletedborrowers table
1821 # deletemem.pl: delete use of warn function
1822 #
1823 # Revision 1.144  2006/06/08 15:36:31  alaurin
1824 # Add a new system preference 'AutomaticItemReturn' :
1825 #
1826 # 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) .
1827 #
1828 # switch off : the document stay in the holdingbranch ...
1829 #
1830 # correcting bugs :
1831 # - comment C4::acquisition (not using in request.pl).
1832 # - correcting date in request.pl
1833 # -add the new call of function getbranches in request.pl
1834 #
1835 # Revision 1.143  2006/06/07 02:02:47  bob_lyon
1836 # merging katipo changes...
1837 #
1838 # adding new preference IssuingInProcess
1839 #
1840 # Revision 1.142  2006/06/06 23:42:46  bob_lyon
1841 # Merging Katipo changes...
1842 #
1843 # Adding new system pref where one can still retrieve a correct reading
1844 # record history if one has moved older data from issues to oldissues table
1845 # to speed up issues speed
1846 #
1847 # Revision 1.141  2006/06/01 03:18:11  rangi
1848 # Adding a new column to the statistics table
1849 #
1850 # Revision 1.140  2006/05/22 22:40:45  rangi
1851 # Adding new systempreference allowing for the library to add borrowers to institutions (rest homes, parishes, schools, classes etc).
1852 #
1853 # Revision 1.139  2006/05/19 19:31:29  tgarip1957
1854 # Added new fields to auth_header and auth_subfield_table to allow ZEBRA use of authorities and new MARC framework like structure.
1855 # 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
1856 #
1857 # Revision 1.138  2006/05/19 16:51:44  alaurin
1858 # update database for :
1859 # - new feature ip and printer management
1860 # adding two fields in branches table (branchip,branchprinter)
1861 #
1862 # - 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
1863 #
1864 # new system preference :
1865 # - ReservesMaxPickUpDelay : Maximum delay to pick up a reserved document
1866 # TransfersMaxDaysWarning : Max delay before considering the transfer as potentialy a problem
1867 #
1868 # Revision 1.137  2006/04/18 09:36:36  plg
1869 # bug fixed: typo fixed in labels and labels_conf tables creation query.
1870 #
1871 # Revision 1.136  2006/04/17 21:55:33  sushi
1872 # Added 'labels' and 'labels_conf' tables, for spine lable tool.
1873 #
1874 # Revision 1.135  2006/04/15 02:37:03  tgarip1957
1875 # Marc record should be set to UTF-8 in leader.Force it.
1876 # XML should be with<record> wrappers
1877 #
1878 # Revision 1.134  2006/04/14 09:37:29  tipaul
1879 # improvements from SAN Ouest Provence :
1880 # * introducing a category_type into categories. It can be A (adult), C (children), P (Professionnal), I (institution/organisation).
1881 # * each category_type has it's own forms to create members.
1882 # * the borrowers table has been heavily modified (many fields changed), to get something more logic & readable
1883 # * reintroducing guarantor/guanrantee system that is now independant from hardcoded C/A for categories
1884 # * updating templates to fit template rules
1885 #
1886 # (see mail feb, 17 on koha-devel "new features for borrowers" for more details)
1887 #
1888 # Revision 1.133  2006/04/13 08:36:42  plg
1889 # new: function C4::Date::get_date_format_string_for_DHTMLcalendar based on
1890 # the system preference prefered date format.
1891 #
1892 # improvement: book fund list and budget list screen redesigned. Filters on
1893 # each field. Columns are not sortable yet. Using DHTML Calendar to fill date
1894 # fields instead of manual filling. Pagination system. From the book fund
1895 # list, you can reach the budget list, filtered on a book fund, or not. A
1896 # budget can be added only from book fund list screen.
1897 #
1898 # bug fixed: branchcode was missing in table aqbudget.
1899 #
1900 # bug fixed: when setting a branchcode to a book fund, all associated budgets
1901 # move to this branchcode.
1902 #
1903 # modification: when adding/modifying budget/fund, MySQL specific "REPLACE..."
1904 # statements replaced by standard SQL compliant statement.
1905 #
1906 # bug fixed: when adding/modifying a budget, if the book fund is associated to
1907 # a branch, the branch selection is disabled and set to the book fund branch.
1908 #
1909 # Revision 1.132  2006/04/06 12:37:05  hdl
1910 # Bugfixing : aqbookfund needed a field.
1911 #
1912 # Revision 1.131  2006/03/03 17:02:22  tipaul
1913 # commit for holidays and news management.
1914 # (some forgotten files)
1915 #
1916 # Revision 1.130  2006/03/03 16:35:21  tipaul
1917 # commit for holidays and news management.
1918 #
1919 # Contrib from Tümer Garip (from Turkey) :
1920 # * holiday :
1921 # 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 :
1922 # - single day : only this day is closed
1923 # - repet weekly (like "sunday") : the day is holiday every week
1924 # - repet yearly (like "July, 4") : this day is closed every year.
1925 #
1926 # You can also put exception :
1927 # - sunday is holiday, but "2006 March, 5th" the library will be open
1928 #
1929 # 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.
1930 #
1931 # Revision 1.129  2006/02/27 18:19:33  hdl
1932 # New table used in overduerules.pl tools page.
1933 #
1934 # Revision 1.128  2006/01/25 15:16:06  tipaul
1935 # updating DB :
1936 # * removing useless tables
1937 # * adding useful indexes
1938 # * altering some columns definitions
1939 # * The goal being to have updater working fine for foreign keys.
1940 #
1941 # 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
1942 #
1943 # Revision 1.127  2006/01/24 17:57:17  tipaul
1944 # DB improvements : adding foreign keys on some tables. partial stuff done.
1945 #
1946 # Revision 1.126  2006/01/06 16:39:42  tipaul
1947 # synch'ing head and rel_2_2 (from 2.2.5, including npl templates)
1948 # Seems not to break too many things, but i'm probably wrong here.
1949 # at least, new features/bugfixes from 2.2.5 are here (tested on some features on my head local copy)
1950 #
1951 # - removing useless directories (koha-html and koha-plucene)
1952 #
1953 # Revision 1.125  2006/01/04 15:54:55  tipaul
1954 # utf8 is a : go for beta test in HEAD.
1955 # some explanations :
1956 # - 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.
1957 # - *-top.inc will show the pages in utf8
1958 # - 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.
1959 # - using marcxml field and no more the iso2709 raw marc biblioitems.marc field.
1960 #
1961 # Revision 1.124  2005/10/27 12:09:05  tipaul
1962 # new features for serial module :
1963 # - 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")
1964 # - 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).
1965 #
1966 # Revision 1.123  2005/10/26 09:13:37  tipaul
1967 # big commit, still breaking things...
1968 #
1969 # * synch with rel_2_2. Probably the last non manual synch, as rel_2_2 should not be modified deeply.
1970 # * code cleaning (cleaning warnings from perl -w) continued
1971 #
1972 # Revision 1.122  2005/09/02 14:18:38  tipaul
1973 # new feature : image for itemtypes.
1974 #
1975 # * run updater/updatedatabase to create imageurl field in itemtypes.
1976 # * 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)
1977 # * go to OPAC, and search something. In the result list, you now have the picture instead of the text itemtype.
1978 #
1979 # Revision 1.121  2005/08/24 08:49:03  hdl
1980 # Adding a note field in serial table.
1981 # This will allow librarian to mention a note on a peculiar waiting serial number.
1982 #
1983 # Revision 1.120  2005/08/09 14:10:32  tipaul
1984 # 1st commit to go to zebra.
1985 # don't update your cvs if you want to have a working head...
1986 #
1987 # this commit contains :
1988 # * 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...
1989 # * 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.
1990 # * other files : get rid of bibid and use biblionumber instead.
1991 #
1992 # What is broken :
1993 # * does not do anything on zebra yet.
1994 # * if you rename marc_subfield_table, you can't search anymore.
1995 # * you can view a biblio & bibliodetails, go to MARC editor, but NOT save any modif.
1996 # * 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 ;-) )
1997 #
1998 # 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
1999 # 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.
2000 #
2001 # Revision 1.119  2005/08/04 16:07:58  tipaul
2002 # Synch really broke this script...
2003 #
2004 # Revision 1.118  2005/08/04 16:02:55  tipaul
2005 # oops... error in synch between 2.2 and head
2006 #
2007 # Revision 1.117  2005/08/04 14:24:39  tipaul
2008 # synch'ing 2.2 and head
2009 #
2010 # Revision 1.116  2005/08/04 08:55:54  tipaul
2011 # Letters / alert system, continuing...
2012 #
2013 # * adding a package Letters.pm, that manages Letters & alerts.
2014 # * 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)
2015 # * 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)
2016 # * 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.
2017 #
2018 # Note that the system should be generic enough to manage any type of alert.
2019 # 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 ;-) )
2020 #
2021 # Revision 1.115  2005/08/02 16:15:34  tipaul
2022 # adding 2 fields to letter system :
2023 # * module (acquisition, catalogue...) : it will be usefull to show the librarian only letters he may be interested by.
2024 # * title, that will be used as mail subject.
2025 #
2026 # Revision 1.114  2005/07/28 15:10:13  tipaul
2027 # 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.
2028 # the letter table contains 3 fields :
2029 # * code => the code of the letter
2030 # * name => the complete name of the letter
2031 # * content => the complete text. It's a TEXT field type, so has no limits.
2032 #
2033 # My next goal now is to work on point 2-I "serial issue alert"
2034 # 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.
2035 # (see mail on koha-devel, 2005/04/07)
2036 #
2037 # The "serial issue alert" will be the 1st to use this letter system that probably needs some tweaking ;-)
2038 #
2039 # Once it will be stabilised default letters (in any languages) could be added during installer to help the library begin with this new feature.
2040 #
2041 # Revision 1.113  2005/07/28 08:38:41  tipaul
2042 # 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 :
2043 # * ReturnBeforeExpiry = yes => return date can't be after expiry date
2044 # * ReturnBeforeExpiry = no  => return date can be after expiry date
2045 #
2046 # Revision 1.112  2005/07/26 08:19:47  hdl
2047 # Adding IndependantBranches System preference variable in order to manage Branch independancy.
2048 #
2049 # Revision 1.111  2005/07/25 15:35:38  tipaul
2050 # we have decided that moving to Koha 3.0 requires being already in Koha 2.2.x
2051 # So, the updatedatabase script can highly be cleaned (90% removed).
2052 # Let's play with the new Koha DB structure now ;-)
2053 #