Koha-clms/installer/data/mysql/updatedatabase30.pl

408 lines
19 KiB
Perl

#!/usr/bin/perl
# Database Updater
# This script checks for required updates to the database.
# Part of the Koha Library Software www.koha.org
# Licensed under the GPL.
# Bugs/ToDo:
# - Would also be a good idea to offer to do a backup at this time...
# NOTE: If you do something more than once in here, make it table driven.
# NOTE: Please keep the version in kohaversion.pl up-to-date!
use strict;
# use warnings;
# CPAN modules
use DBI;
use Getopt::Long;
# Koha modules
use C4::Context;
use C4::Installer;
use MARC::Record;
use MARC::File::XML ( BinaryEncoding => 'utf8' );
# FIXME - The user might be installing a new database, so can't rely
# on /etc/koha.conf anyway.
my $debug = 0;
my (
$sth, $sti,
$query,
%existingtables, # tables already in database
%types,
$table,
$column,
$type, $null, $key, $default, $extra,
$prefitem, # preference item in systempreferences table
);
my $silent;
GetOptions(
's' =>\$silent
);
my $dbh = C4::Context->dbh;
$|=1; # flushes output
=item
Deal with virtualshelves
=cut
my $DBversion = '3.00.01.000';
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
print "Upgrade to $DBversion done (start of 3.0.1)\n";
SetVersion ($DBversion);
}
$DBversion = '3.00.01.001';
if ( C4::Context->preference('Version') < TransformToNum($DBversion) ) {
# use statistics where available
$dbh->do("
ALTER TABLE statistics ADD KEY tmp_stats (type, itemnumber, borrowernumber)
");
$dbh->do("
UPDATE issues iss
SET issuedate = (
SELECT max(datetime)
FROM statistics
WHERE type = 'issue'
AND itemnumber = iss.itemnumber
AND borrowernumber = iss.borrowernumber
)
WHERE issuedate IS NULL;
");
$dbh->do("ALTER TABLE statistics DROP KEY tmp_stats");
# default to last renewal date
$dbh->do("
UPDATE issues
SET issuedate = lastreneweddate
WHERE issuedate IS NULL
and lastreneweddate IS NOT NULL
");
my $num_bad_issuedates = $dbh->selectrow_array("SELECT COUNT(*) FROM issues WHERE issuedate IS NULL");
if ($num_bad_issuedates > 0) {
print STDERR "After the upgrade to $DBversion, there are still $num_bad_issuedates loan(s) with a NULL (blank) loan date. ",
"Please check the issues table in your database.";
}
print "Upgrade to $DBversion done (bug 2582: set null issues.issuedate to lastreneweddate)";
SetVersion($DBversion);
}
$DBversion = "3.00.01.002";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('AllowRenewalLimitOverride', '0', 'if ON, allows renewal limits to be overridden on the circulation screen',NULL,'YesNo')");
print "Upgrade to $DBversion done (add new syspref)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.01.003";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
my $search=$dbh->selectall_arrayref("select * from systempreferences where variable='dontmerge'");
if (@$search){
my $search=$dbh->selectall_arrayref("select * from systempreferences where variable='MergeAuthoritiesOnUpdate'");
if (@$search){
$dbh->do("DELETE FROM systempreferences set variable='dontmerge'");
}
else {
$dbh->do("UPDATE systempreferences set variable='MergeAuthoritiesOnUpdate' ,value=1-value*1 WHERE variable='dontmerge'");
}
}
else {
$dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('MergeAuthoritiesOnUpdate', '1', 'if ON, Updating authorities will automatically updates biblios',NULL,'YesNo')");
}
print "Upgrade to $DBversion done (add new syspref MergeAuthoritiesOnUpdate)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.01.004";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
if (lc(C4::Context->preference('marcflavour')) eq "unimarc"){
$dbh->do("INSERT IGNORE INTO `marc_tag_structure` (`tagfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `authorised_value`, `frameworkcode`) VALUES ('099', 'Informations locales', '', 0, 0, '', '');");
$dbh->do("INSERT IGNORE INTO `marc_tag_structure` (`frameworkcode`,`tagfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `authorised_value`) SELECT DISTINCT(frameworkcode),'099', 'Informations locales', '', 0, 0, '' from biblio_framework");
$dbh->do(<<ENDOFSQL);
INSERT IGNORE INTO marc_subfield_structure (`tagfield`, `tagsubfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `kohafield`, `tab`, `authorised_value`, `authtypecode`, `value_builder`, `isurl`, `hidden`, `seealso`, `link`, `defaultvalue`,frameworkcode )
VALUES ('099', 'c', 'date creation notice (koha)', '', 0, 0, 'biblio.datecreated', -1, '', '', '', NULL, 0, '', '', NULL, ''),
('099', 'd', 'date modification notice (koha)', '', 0, 0, 'biblio.timestamp', -1, '', '', '', NULL, 0, '', '', NULL, ''),
('995', '2', 'Perdu', '', 0, 0, 'items.itemlost', 10, '', '', '', NULL, 1, '', NULL, NULL, '');
ENDOFSQL
$dbh->do(<<ENDOFSQL1);
INSERT IGNORE INTO marc_subfield_structure (`frameworkcode`,`tagfield`, `tagsubfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `kohafield`, `tab`, `authorised_value`, `authtypecode`, `value_builder`, `isurl`, `hidden`, seealso, link, defaultvalue )
SELECT DISTINCT(frameworkcode), '099', 'c', 'date creation notice (koha)', '', 0, 0, 'biblio.datecreated', -1, '', '', '', NULL, 0, '', '', NULL from biblio_framework;
ENDOFSQL1
$dbh->do(<<ENDOFSQL2);
INSERT IGNORE INTO marc_subfield_structure (`frameworkcode`,`tagfield`, `tagsubfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `kohafield`, `tab`, `authorised_value`, `authtypecode`, `value_builder`, `isurl`, `hidden`, seealso, link, defaultvalue )
SELECT DISTINCT(frameworkcode), '099', 'd', 'date modification notice (koha)', '', 0, 0, 'biblio.timestamp', -1, '', '', '', NULL, 0, '', '', NULL from biblio_framework;
ENDOFSQL2
$dbh->do(<<ENDOFSQL3);
INSERT IGNORE INTO marc_subfield_structure (`frameworkcode`,`tagfield`, `tagsubfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `kohafield`, `tab`, `authorised_value`, `authtypecode`, `value_builder`, `isurl`, `hidden`, seealso, link, defaultvalue )
SELECT DISTINCT(frameworkcode), '995', '2', 'Perdu', '', 0, 0, 'items.itemlost', 10, '', '', '', NULL, 1, '', NULL, NULL from biblio_framework;
ENDOFSQL3
print "Upgrade to $DBversion done (updates MARC framework structure)\n";
}
SetVersion ($DBversion);
}
$DBversion = "3.00.01.005";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do(<<ENDOFNOTFORLOANOVERRIDE);
INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('AllowNotForLoanOverride', '0', 'if ON, enables the librarian to choose when they want to check out a notForLoan regular item',NULL,'YesNo')
ENDOFNOTFORLOANOVERRIDE
print "Upgrade to $DBversion done (Adding AllowNotForLoanOverride System preference)\n";
}
$DBversion = "3.00.01.005";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do("ALTER TABLE issuingrules ADD COLUMN `finedays` int(11) default NULL AFTER `fine`");
print "Upgrade to $DBversion done (Adding a field in issuingrules table)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.01.006";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do("ALTER TABLE `aqbudget` CHANGE `aqbudgetid` `aqbudgetid` INT( 11 ) NOT NULL AUTO_INCREMENT");
print "Upgrade to $DBversion done (Change the field)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.01.007";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do(<<ENDOFRENEWAL);
INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('RenewalPeriodBase', 'date_due', 'Set whether the renewal date should be counted from the date_due or from the moment the Patron asks for renewal ','date_due|now','Choice');
ENDOFRENEWAL
print "Upgrade to $DBversion done (Change the field)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.02.001";
#01.00.005';
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do("
INSERT INTO `letter` (module, code, name, title, content)
VALUES('reserves', 'HOLD', 'Hold Available for Pickup', 'Hold Available for Pickup at <<branches.branchname>>', 'Dear <<borrowers.firstname>> <<borrowers.surname>>,\r\n\r\nYou have a hold available for pickup as of <<reserves.waitingdate>>:\r\n\r\nTitle: <<biblio.title>>\r\nAuthor: <<biblio.author>>\r\nCopy: <<items.copynumber>>\r\nLocation: <<branches.branchname>>\r\n<<branches.branchaddress1>>\r\n<<branches.branchaddress2>>\r\n<<branches.branchaddress3>>')
");
$dbh->do("INSERT INTO `message_attributes` (message_attribute_id, message_name, takes_days) values(4, 'Hold Filled', 0)");
$dbh->do("INSERT INTO `message_transports` (message_attribute_id, message_transport_type, is_digest, letter_module, letter_code) values(4, 'sms', 0, 'reserves', 'HOLD')");
$dbh->do("INSERT INTO `message_transports` (message_attribute_id, message_transport_type, is_digest, letter_module, letter_code) values(4, 'email', 0, 'reserves', 'HOLD')");
print "Upgrade to $DBversion done (Add letter for holds notifications)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.02.002";
#$DBversion = '3.01.00.006';
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do("ALTER TABLE `biblioitems` ADD KEY issn (issn)");
print "Upgrade to $DBversion done (add index on biblioitems.issn)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.02.003";
#$DBversion = "3.01.00.007";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='intranetmainUserblock'");
$dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='intranetuserjs'");
$dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='opacheader'");
$dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='OpacMainUserBlock'");
$dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='OpacNav'");
$dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='opacuserjs'");
$dbh->do("UPDATE `systempreferences` SET options='30|10', type='Textarea' WHERE variable='OAI-PMH:Set'");
$dbh->do("UPDATE `systempreferences` SET options='50' WHERE variable='intranetstylesheet'");
$dbh->do("UPDATE `systempreferences` SET options='50' WHERE variable='intranetcolorstylesheet'");
$dbh->do("UPDATE `systempreferences` SET options='10' WHERE variable='globalDueDate'");
$dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='numSearchResults'");
$dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='OPACnumSearchResults'");
$dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='ReservesMaxPickupDelay'");
$dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='TransfersMaxDaysWarning'");
$dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='StaticHoldsQueueWeight'");
$dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='holdCancelLength'");
$dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='XISBNDailyLimit'");
$dbh->do("UPDATE `systempreferences` SET type='Float' WHERE variable='gist'");
$dbh->do("UPDATE `systempreferences` SET type='Free' WHERE variable='BakerTaylorUsername'");
$dbh->do("UPDATE `systempreferences` SET type='Free' WHERE variable='BakerTaylorPassword'");
$dbh->do("UPDATE `systempreferences` SET type='Textarea', options='70|10' WHERE variable='ISBD'");
$dbh->do("UPDATE `systempreferences` SET type='Textarea', options='70|10', explanation='Enter a specific hash for NoZebra indexes. Enter : \\\'indexname\\\' => \\\'100a,245a,500*\\\',\\\'index2\\\' => \\\'...\\\'' WHERE variable='NoZebraIndexes'");
print "Upgrade to $DBversion done (fix display of many sysprefs)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.02.004";
#$DBversion = "3.01.00.009";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do("INSERT INTO permissions (module_bit, code, description) VALUES ( 1, 'circulate_remaining_permissions', 'Remaining circulation permissions')");
$dbh->do("INSERT INTO permissions (module_bit, code, description) VALUES ( 1, 'override_renewals', 'Override blocked renewals')");
print "Upgrade to $DBversion done (added subpermissions for circulate permission)\n";
}
$DBversion = "3.00.02.005";
#$DBversion = '3.01.00.010';
if ( C4::Context->preference('Version') < TransformToNum($DBversion) ) {
$dbh->do("ALTER TABLE `borrower_attributes` MODIFY COLUMN `attribute` VARCHAR(64) DEFAULT NULL");
$dbh->do("ALTER TABLE `borrower_attributes` MODIFY COLUMN `password` VARCHAR(64) DEFAULT NULL");
print "Upgrade to $DBversion done (bug 2687: increase length of borrower attribute fields)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.02.006";
#$DBversion = '3.01.00.011';
if ( C4::Context->preference('Version') < TransformToNum($DBversion) ) {
# Yes, the old value was ^M terminated.
my $bad_value = "function prepareEmailPopup(){\r\n if (!document.getElementById) return false;\r\n if (!document.getElementById('reserveemail')) return false;\r\n rsvlink = document.getElementById('reserveemail');\r\n rsvlink.onclick = function() {\r\n doReservePopup();\r\n return false;\r\n }\r\n}\r\n\r\nfunction doReservePopup(){\r\n}\r\n\r\nfunction prepareReserveList(){\r\n}\r\n\r\naddLoadEvent(prepareEmailPopup);\r\naddLoadEvent(prepareReserveList);";
my $intranetuserjs = C4::Context->preference('intranetuserjs');
if ($intranetuserjs and $intranetuserjs eq $bad_value) {
my $sql = <<'END_SQL';
UPDATE systempreferences
SET value = ''
WHERE variable = 'intranetuserjs'
END_SQL
$dbh->do($sql);
}
print "Upgrade to $DBversion done (removed bogus intranetuserjs syspref)\n";
SetVersion($DBversion);
}
$DBversion = "3.00.02.007";
#$DBversion = '3.01.00.015';
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('OPACAmazonCoverImages', '0', 'Display cover images on OPAC from Amazon Web Services','','YesNo')");
$dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('AmazonCoverImages', '0', 'Display Cover Images in Staff Client from Amazon Web Services','','YesNo')");
$dbh->do("UPDATE systempreferences SET variable='AmazonEnabled' WHERE variable = 'AmazonContent'");
$dbh->do("UPDATE systempreferences SET variable='OPACAmazonEnabled' WHERE variable = 'OPACAmazonContent'");
print "Upgrade to $DBversion done (added Syndetics Enhanced Content system preferences)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.02.008";
#$DBversion = "3.01.00.018";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do("ALTER TABLE deletedborrowers ADD `smsalertnumber` varchar(50) default NULL");
print "Upgrade to $DBversion done (added deletedborrowers.smsalertnumber, missed in 3.00.00.091)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.02.009";
#$DBversion = '3.01.00.023';
if ( C4::Context->preference('Version') < TransformToNum($DBversion) ) {
$dbh->do("ALTER TABLE biblioitems MODIFY COLUMN isbn VARCHAR(30) DEFAULT NULL");
$dbh->do("ALTER TABLE deletedbiblioitems MODIFY COLUMN isbn VARCHAR(30) DEFAULT NULL");
$dbh->do("ALTER TABLE import_biblios MODIFY COLUMN isbn VARCHAR(30) DEFAULT NULL");
$dbh->do("ALTER TABLE suggestions MODIFY COLUMN isbn VARCHAR(30) DEFAULT NULL");
print "Upgrade to $DBversion done (bug 2765: increase width of isbn column in several tables)\n";
SetVersion ($DBversion);
}
$DBversion = '3.00.02.010';
#$DBversion = '3.01.00.027';
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do("ALTER TABLE zebraqueue CHANGE `biblio_auth_number` `biblio_auth_number` bigint(20) unsigned NOT NULL default 0");
print "Upgrade to $DBversion done (Increased size of zebraqueue biblio_auth_number to address bug 3148.)\n";
SetVersion ($DBversion);
}
#$DBversion = '3.01.00.028';
$DBversion = '3.00.02.011';
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
my $enable_reviews = C4::Context->preference('AmazonEnabled') ? '1' : '0';
$dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('AmazonReviews', '$enable_reviews', 'Display Amazon reviews on staff interface','','YesNo')");
print "Upgrade to $DBversion done (added AmazonReviews)\n";
SetVersion ($DBversion);
}
#$DBversion = '3.01.00.029';
$DBversion = '3.00.02.012';
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do(q( UPDATE language_rfc4646_to_iso639
SET iso639_2_code = 'spa'
WHERE rfc4646_subtag = 'es'
AND iso639_2_code = 'rus' )
);
print "Upgrade to $DBversion done (fixed bug 2599: using Spanish search limit retrieves Russian results)\n";
SetVersion ($DBversion);
}
$DBversion = '3.00.03.001';
if (C4::Context->preference('Version') < TransformToNum($DBversion)){
$dbh->do("
INSERT INTO `letter` (module, code, name, title, content) VALUES('reserves', 'HOLDPLACED', 'Hold Placed on Item', 'Hold Placed on Item','An hold has been placed on the following item : <<title>> (<<biblionumber>>) by the user <<firstname>> <<surname>> (<<cardnumber>>).');
");
}
=item DropAllForeignKeys($table)
Drop all foreign keys of the table $table
=cut
sub DropAllForeignKeys {
my ($table) = @_;
# get the table description
my $sth = $dbh->prepare("SHOW CREATE TABLE $table");
$sth->execute;
my $vsc_structure = $sth->fetchrow;
# split on CONSTRAINT keyword
my @fks = split /CONSTRAINT /,$vsc_structure;
# parse each entry
foreach (@fks) {
# isolate what is before FOREIGN KEY, if there is something, it's a foreign key to drop
$_ = /(.*) FOREIGN KEY.*/;
my $id = $1;
if ($id) {
# we have found 1 foreign, drop it
$dbh->do("ALTER TABLE $table DROP FOREIGN KEY $id");
$id="";
}
}
}
=item TransformToNum
Transform the Koha version from a 4 parts string
to a number, with just 1 .
=cut
sub TransformToNum {
my $version = shift;
# remove the 3 last . to have a Perl number
$version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
return $version;
}
=item SetVersion
set the DBversion in the systempreferences
=cut
sub SetVersion {
my $kohaversion = TransformToNum(shift);
if (C4::Context->preference('Version')) {
my $finish=$dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='Version'");
$finish->execute($kohaversion);
} else {
my $finish=$dbh->prepare("INSERT into systempreferences (variable,value,explanation) values ('Version',?,'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller')");
$finish->execute($kohaversion);
}
}
exit;