6 # This script checks for required updates to the database.
8 # Part of the Koha Library Software www.koha.org
9 # Licensed under the GPL.
12 # - Would also be a good idea to offer to do a backup at this time...
14 # NOTE: If you do something more than once in here, make it table driven.
26 # FIXME - The user might be installing a new database, so can't rely
27 # on /etc/koha.conf anyway.
34 %existingtables, # tables already in database
38 $type, $null, $key, $default, $extra,
39 $prefitem, # preference item in systempreferences table
46 my $dbh = C4::Context->dbh;
47 print "connected to your DB. Checking & modifying it\n" unless $silent;
48 $|=1; # flushes output
53 # Tables to add if they don't exist
55 categorytable => "(categorycode char(5) NOT NULL default '',
56 description text default '',
57 itemtypecodes text default '',
58 PRIMARY KEY (categorycode)
60 subcategorytable => "(subcategorycode char(5) NOT NULL default '',
61 description text default '',
62 itemtypecodes text default '',
63 PRIMARY KEY (subcategorycode)
65 mediatypetable => "(mediatypecode char(5) NOT NULL default '',
66 description text default '',
67 itemtypecodes text default '',
68 PRIMARY KEY (mediatypecode)
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` )
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 '',
85 PRIMARY KEY (module,code)
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)
100 subscription => { 'letter' => 'char(20) NULL'},
101 # tablename => { 'field' => 'fieldtype' },
104 my %dropable_table = (
105 # tablename => 'tablename',
108 my %uselessfields = (
109 # tablename => "field1,field2",
111 # the other hash contains other actions that can't be done elsewhere. they are done
112 # either BEFORE of AFTER everything else, depending on "when" entry (default => AFTER)
114 # The tabledata hash contains data that should be in the tables.
115 # The uniquefieldrequired hash entry is used to determine which (if any) fields
116 # must not exist in the table for this row to be inserted. If the
117 # uniquefieldrequired entry is already in the table, the existing data is not
118 # modified, unless the forceupdate hash entry is also set. Fields in the
119 # anonymous "forceupdate" hash will be forced to be updated to the default
120 # values given in the %tabledata hash.
124 # { uniquefielrequired => 'fieldname', # the primary key in the table
125 # fieldname => fieldvalue,
126 # fieldname2 => fieldvalue2,
129 systempreferences => [
131 uniquefieldrequired => 'variable',
132 variable => 'Activate_Log',
134 forceupdate => { 'explanation' => 1,
136 explanation => 'Turn Log Actions on DB On an Off',
140 uniquefieldrequired => 'variable',
141 variable => 'IndependantBranches',
143 forceupdate => { 'explanation' => 1,
145 explanation => 'Turn Branch independancy management On an Off',
149 uniquefieldrequired => 'variable',
150 variable => 'ReturnBeforeExpiry',
152 forceupdate => { 'explanation' => 1,
154 explanation => 'If Yes, Returndate on issuing can\'t be after borrower card expiry',
161 my %fielddefinitions = (
163 # { field => 'fieldname',
164 # type => 'fieldtype',
177 # Get version of MySQL database engine.
178 my $mysqlversion = `mysqld --version`;
179 $mysqlversion =~ /Ver (\S*) /;
181 if ( $mysqlversion ge '3.23' ) {
182 print "Could convert to MyISAM database tables...\n" unless $silent;
185 #---------------------------------
188 # Collect all tables into a list
189 $sth = $dbh->prepare("show tables");
191 while ( my ($table) = $sth->fetchrow ) {
192 $existingtables{$table} = 1;
196 # Now add any missing tables
197 foreach $table ( keys %requiretables ) {
198 unless ( $existingtables{$table} ) {
199 print "Adding $table table...\n" unless $silent;
200 my $sth = $dbh->prepare("create table $table $requiretables{$table}");
203 print "Error : $sth->errstr \n";
209 # now drop useless tables
210 foreach $table ( keys %dropable_table ) {
211 if ( $existingtables{$table} ) {
212 print "Dropping unused table $table\n" if $debug and not $silent;
213 $dbh->do("drop table $table");
215 print "Error : $dbh->errstr \n";
220 #---------------------------------
223 foreach $table ( keys %requirefields ) {
224 print "Check table $table\n" if $debug and not $silent;
225 $sth = $dbh->prepare("show columns from $table");
228 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
230 $types{$column} = $type;
232 foreach $column ( keys %{ $requirefields{$table} } ) {
233 print " Check column $column [$types{$column}]\n" if $debug and not $silent;
234 if ( !$types{$column} ) {
236 # column doesn't exist
237 print "Adding $column field to $table table...\n" unless $silent;
238 $query = "alter table $table
239 add column $column " . $requirefields{$table}->{$column};
240 print "Execute: $query\n" if $debug;
241 my $sti = $dbh->prepare($query);
244 print "**Error : $sti->errstr \n";
251 foreach $table ( keys %fielddefinitions ) {
252 print "Check table $table\n" if $debug;
253 $sth = $dbh->prepare("show columns from $table");
256 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
258 $definitions->{$column}->{type} = $type;
259 $definitions->{$column}->{null} = $null;
260 $definitions->{$column}->{key} = $key;
261 $definitions->{$column}->{default} = $default;
262 $definitions->{$column}->{extra} = $extra;
264 my $fieldrow = $fielddefinitions{$table};
265 foreach my $row (@$fieldrow) {
266 my $field = $row->{field};
267 my $type = $row->{type};
268 my $null = $row->{null};
269 my $key = $row->{key};
270 my $default = $row->{default};
271 $default="''" unless $default;
272 my $extra = $row->{extra};
273 my $def = $definitions->{$field};
274 unless ( $type eq $def->{type}
275 && $null eq $def->{null}
276 && $key eq $def->{key}
277 && $default eq $def->{default}
278 && $extra eq $def->{extra} )
284 if ( $key eq 'PRI' ) {
285 $key = 'PRIMARY KEY';
287 unless ( $extra eq 'auto_increment' ) {
290 # if it's a new column use "add", if it's an old one, use "change".
292 if ($definitions->{$field}->{type}) {
293 $action="change $field"
297 # if it's a primary key, drop the previous pk, before altering the table
299 if ($key ne 'PRIMARY KEY') {
300 $sth =$dbh->prepare("alter table $table $action $field $type $null $key $extra default ?");
302 $sth =$dbh->prepare("alter table $table drop primary key, $action $field $type $null $key $extra default ?");
304 $sth->execute($default);
305 print " Alter $field in $table\n" unless $silent;
311 # Populate tables with required data
312 foreach my $table ( keys %tabledata ) {
313 print "Checking for data required in table $table...\n" unless $silent;
314 my $tablerows = $tabledata{$table};
315 foreach my $row (@$tablerows) {
316 my $uniquefieldrequired = $row->{uniquefieldrequired};
317 my $uniquevalue = $row->{$uniquefieldrequired};
318 my $forceupdate = $row->{forceupdate};
321 "select $uniquefieldrequired from $table where $uniquefieldrequired=?"
323 $sth->execute($uniquevalue);
325 foreach my $field (keys %$forceupdate) {
326 if ($forceupdate->{$field}) {
327 my $sth=$dbh->prepare("update systempreferences set $field=? where $uniquefieldrequired=?");
328 $sth->execute($row->{$field}, $uniquevalue);
332 print "Adding row to $table: " unless $silent;
336 foreach my $field ( keys %$row ) {
337 next if $field eq 'uniquefieldrequired';
338 next if $field eq 'forceupdate';
339 my $value = $row->{$field};
340 push @values, $value;
341 print " $field => $value" unless $silent;
342 $fieldlist .= "$field,";
343 $placeholders .= "?,";
345 print "\n" unless $silent;
346 $fieldlist =~ s/,$//;
347 $placeholders =~ s/,$//;
350 "insert into $table ($fieldlist) values ($placeholders)");
351 $sth->execute(@values);
360 # create frameworkcode row in biblio table & fill it with marc_biblio.frameworkcode.
363 # 1st, get how many biblio we will have to do...
364 $sth = $dbh->prepare('select count(*) from marc_biblio');
366 my ($totaltodo) = $sth->fetchrow;
368 $sth = $dbh->prepare("show columns from biblio");
372 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow ){
373 $bibliofwexist=1 if $column eq 'frameworkcode';
375 unless ($bibliofwexist) {
376 print "moving biblioframework to biblio table\n";
377 $dbh->do('ALTER TABLE `biblio` ADD `frameworkcode` VARCHAR( 4 ) NOT NULL AFTER `biblionumber`');
378 $sth = $dbh->prepare('select biblionumber,frameworkcode from marc_biblio');
380 my $sth_update = $dbh->prepare('update biblio set frameworkcode=? where biblionumber=?');
382 while (my ($biblionumber,$frameworkcode) = $sth->fetchrow) {
383 $sth_update->execute($frameworkcode,$biblionumber);
385 print "\r$totaldone / $totaltodo" unless ($totaldone % 100);
391 # moving MARC data from marc_subfield_table to biblioitems.marc
393 $sth = $dbh->prepare("show columns from biblioitems");
397 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow ){
398 $marcdone=1 if ($type eq 'blob' && $column eq 'marc') ;
401 print "moving MARC record to biblioitems table\n";
402 # changing marc field type
403 $dbh->do('ALTER TABLE `biblioitems` CHANGE `marc` `marc` BLOB NULL DEFAULT NULL ');
404 # adding marc xml, just for convenience
405 $dbh->do('ALTER TABLE `biblioitems` ADD `marcxml` TEXT NOT NULL');
406 # moving data from marc_subfield_value to biblio
407 $sth = $dbh->prepare('select bibid,biblionumber from marc_biblio');
409 my $sth_update = $dbh->prepare('update biblioitems set marc=?, marcxml=? where biblionumber=?');
411 while (my ($bibid,$biblionumber) = $sth->fetchrow) {
412 my $record = MARCgetbiblio($dbh,$bibid);
413 $sth_update->execute($record->as_usmarc(),$record->as_xml(),$biblionumber);
415 print "\r$totaldone / $totaltodo" unless ($totaldone % 100);
420 # at last, remove useless fields
421 foreach $table ( keys %uselessfields ) {
422 my @fields = split /,/,$uselessfields{$table};
425 foreach my $fieldtodrop (@fields) {
426 $fieldtodrop =~ s/\t//g;
427 $fieldtodrop =~ s/\n//g;
429 $sth = $dbh->prepare("show columns from $table");
431 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
433 $exists =1 if ($column eq $fieldtodrop);
436 print "deleting $fieldtodrop field in $table...\n" unless $silent;
437 my $sth = $dbh->prepare("alter table $table drop $fieldtodrop");
447 # those 2 subs are a copy of Biblio.pm, version 2.2.4
448 # they are useful only once, for moving from 2.2 to 3.0
449 # the MARCgetbiblio & MARCgetitem subs in Biblio.pm
450 # are still here, but uses other tables
451 # (the ones that are filled by updatedatabase !)
455 # Returns MARC::Record of the biblio passed in parameter.
456 my ( $dbh, $bibid ) = @_;
457 my $record = MARC::Record->new();
462 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
463 from marc_subfield_table
464 where bibid=? order by tag,tagorder,subfieldorder
469 "select subfieldvalue from marc_blob_subfield where blobidlink=?");
470 $sth->execute($bibid);
471 my $prevtagorder = 1;
474 my $field; # for >=10 tags
475 my $prevvalue; # for <10 tags
476 while ( my $row = $sth->fetchrow_hashref ) {
478 if ( $row->{'valuebloblink'} ) { #---- search blob if there is one
479 $sth2->execute( $row->{'valuebloblink'} );
480 my $row2 = $sth2->fetchrow_hashref;
482 $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
484 if ( $row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag ) {
485 $previndicator .= " ";
486 if ( $prevtag < 10 ) {
487 if ($prevtag ne '000') {
488 $record->add_fields( ( sprintf "%03s", $prevtag ), $prevvalue ) unless $prevtag eq "XXX"; # ignore the 1st loop
490 $record->leader(sprintf("%24s",$prevvalue));
494 $record->add_fields($field) unless $prevtag eq "XXX";
497 $prevtagorder = $row->{tagorder};
498 $prevtag = $row->{tag};
499 $previndicator = $row->{tag_indicator};
500 if ( $row->{tag} < 10 ) {
501 $prevvalue = $row->{subfieldvalue};
504 $field = MARC::Field->new(
505 ( sprintf "%03s", $prevtag ),
506 substr( $row->{tag_indicator} . ' ', 0, 1 ),
507 substr( $row->{tag_indicator} . ' ', 1, 1 ),
508 $row->{'subfieldcode'},
509 $row->{'subfieldvalue'}
514 if ( $row->{tag} < 10 ) {
515 $record->add_fields( ( sprintf "%03s", $row->{tag} ),
516 $row->{'subfieldvalue'} );
519 $field->add_subfields( $row->{'subfieldcode'},
520 $row->{'subfieldvalue'} );
522 $prevtag = $row->{tag};
523 $previndicator = $row->{tag_indicator};
527 # the last has not been included inside the loop... do it now !
528 if ( $prevtag ne "XXX" )
529 { # check that we have found something. Otherwise, prevtag is still XXX and we
530 # must return an empty record, not make MARC::Record fail because we try to
531 # create a record with XXX as field :-(
532 if ( $prevtag < 10 ) {
533 $record->add_fields( $prevtag, $prevvalue );
537 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
538 $record->add_fields($field);
546 # Returns MARC::Record of the biblio passed in parameter.
547 my ( $dbh, $bibid, $itemnumber ) = @_;
548 my $record = MARC::Record->new();
550 # search MARC tagorder
553 "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=?"
555 $sth2->execute( $bibid, $itemnumber );
556 my ($tagorder) = $sth2->fetchrow_array();
558 #---- TODO : the leader is missing
561 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
562 from marc_subfield_table
563 where bibid=? and tagorder=? order by subfieldcode,subfieldorder
568 "select subfieldvalue from marc_blob_subfield where blobidlink=?");
569 $sth->execute( $bibid, $tagorder );
570 while ( my $row = $sth->fetchrow_hashref ) {
571 if ( $row->{'valuebloblink'} ) { #---- search blob if there is one
572 $sth2->execute( $row->{'valuebloblink'} );
573 my $row2 = $sth2->fetchrow_hashref;
575 $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
577 if ( $record->field( $row->{'tag'} ) ) {
580 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
581 #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
582 if ( length( $row->{'tag'} ) < 3 ) {
583 $row->{'tag'} = "0" . $row->{'tag'};
585 $field = $record->field( $row->{'tag'} );
588 $field->add_subfields( $row->{'subfieldcode'},
589 $row->{'subfieldvalue'} );
590 $record->delete_field($field);
591 $record->add_fields($field);
595 if ( length( $row->{'tag'} ) < 3 ) {
596 $row->{'tag'} = "0" . $row->{'tag'};
599 MARC::Field->new( $row->{'tag'}, " ", " ",
600 $row->{'subfieldcode'} => $row->{'subfieldvalue'} );
601 $record->add_fields($temp);
612 # Revision 1.120 2005/08/09 14:10:32 tipaul
613 # 1st commit to go to zebra.
614 # don't update your cvs if you want to have a working head...
616 # this commit contains :
617 # * 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...
618 # * 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.
619 # * other files : get rid of bibid and use biblionumber instead.
622 # * does not do anything on zebra yet.
623 # * if you rename marc_subfield_table, you can't search anymore.
624 # * you can view a biblio & bibliodetails, go to MARC editor, but NOT save any modif.
625 # * 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 ;-) )
627 # 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
628 # 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.
630 # Revision 1.119 2005/08/04 16:07:58 tipaul
631 # Synch really broke this script...
633 # Revision 1.118 2005/08/04 16:02:55 tipaul
634 # oops... error in synch between 2.2 and head
636 # Revision 1.117 2005/08/04 14:24:39 tipaul
637 # synch'ing 2.2 and head
639 # Revision 1.116 2005/08/04 08:55:54 tipaul
640 # Letters / alert system, continuing...
642 # * adding a package Letters.pm, that manages Letters & alerts.
643 # * 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)
644 # * 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)
645 # * 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.
647 # Note that the system should be generic enough to manage any type of alert.
648 # 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 ;-) )
650 # Revision 1.115 2005/08/02 16:15:34 tipaul
651 # adding 2 fields to letter system :
652 # * module (acquisition, catalogue...) : it will be usefull to show the librarian only letters he may be interested by.
653 # * title, that will be used as mail subject.
655 # Revision 1.114 2005/07/28 15:10:13 tipaul
656 # 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.
657 # the letter table contains 3 fields :
658 # * code => the code of the letter
659 # * name => the complete name of the letter
660 # * content => the complete text. It's a TEXT field type, so has no limits.
662 # My next goal now is to work on point 2-I "serial issue alert"
663 # 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.
664 # (see mail on koha-devel, 2005/04/07)
666 # The "serial issue alert" will be the 1st to use this letter system that probably needs some tweaking ;-)
668 # Once it will be stabilised default letters (in any languages) could be added during installer to help the library begin with this new feature.
670 # Revision 1.113 2005/07/28 08:38:41 tipaul
671 # 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 :
672 # * ReturnBeforeExpiry = yes => return date can't be after expiry date
673 # * ReturnBeforeExpiry = no => return date can be after expiry date
675 # Revision 1.112 2005/07/26 08:19:47 hdl
676 # Adding IndependantBranches System preference variable in order to manage Branch independancy.
678 # Revision 1.111 2005/07/25 15:35:38 tipaul
679 # we have decided that moving to Koha 3.0 requires being already in Koha 2.2.x
680 # So, the updatedatabase script can highly be cleaned (90% removed).
681 # Let's play with the new Koha DB structure now ;-)