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