Added flags to borrowers table and userflags table. Also, OPAC scripts now
[koha.git] / koha.upgrade
1 #!/usr/bin/perl -w
2
3 #use diagnostics;
4 use strict; # please develop with the strict pragma
5
6
7 if ($<) {
8     print "\n\nYou must run koha.upgrade as root.\n\n";
9     exit;
10 }
11
12 my $input;
13
14
15 loadconfigfile();
16
17
18
19 ($kohaversion) || ($kohaversion='unknown version');
20 my $newversion=`cat koha.version`;
21 chomp $newversion;
22 if ($::kohaversion =~ /RC/) {
23     releasecandidatewarning();
24 }
25
26 print qq|
27
28 ================
29 = Koha Upgrade =
30 ================
31
32 You are attempting to upgrade from Koha $kohaversion to $newversion.
33
34 We recommend that you do a complete backup of all your files before upgrading.
35 This upgrade script will make a backup copy of your files for you.
36
37 Would you like to proceed?  ([Y]/N):  
38 |;
39
40 my $answer = <STDIN>;
41 chomp $answer;
42
43 if ($answer eq "Y" || $answer eq "y") {
44         print "Great! continuing upgrade... \n";
45     } else {
46     print qq|
47
48 Aborting.  Please re-run koha.upgrade when you are ready to upgrade Koha.
49 |;
50     exit;
51 };
52
53
54
55 checkperlmodules();
56
57
58
59 my $backupdir='/usr/local/koha/backups';
60 print "Please specify a backup directory [$backupdir]: ";  
61
62 $answer = <STDIN>;
63 chomp $answer;
64
65 if ($answer) {
66     $backupdir=$answer;
67 }
68
69 if (! -e $backupdir) {
70    my $result=mkdir ($backupdir, oct(770));
71    if ($result==0) {
72        my @dirs = split(m#/#, $backupdir);
73         my $checkdir='';
74         foreach (@dirs) {
75             $checkdir.="$_/";
76             unless (-e "$checkdir") {
77                 mkdir($checkdir, 0775);
78             }
79         }
80    }
81 }
82
83 chmod 0770, $backupdir;
84
85 # Backup MySql database
86 #
87 #
88 my $mysql;
89 my $mysqldir;
90
91 foreach my $mysql (qw(/usr/local/mysql
92                       /opt/mysql
93                     )) {
94     if ( -d $mysql ) {
95             $mysqldir=$mysql;
96     }
97 }
98 if (!$mysqldir){
99     $mysqldir='/usr';
100 }
101
102
103
104 my ($sec, $min, $hr, $day, $month, $year) = (localtime(time))[0,1,2,3,4,5];
105 $month++;
106 $year+=1900;
107 my $date= sprintf "%4d-%02d-%02d_%02d:%02d:%02d", $year, $month, $day,$hr,$min,$sec;
108
109 open (MD, "$mysqldir/bin/mysqldump --user=$user --password=$pass --host=$hostname $database|");
110
111 (open BF, ">$backupdir/Koha.backup_$date") || (die "Error opening up backup file $backupdir/Koha.backup_$date: $!\n");
112
113 my $itemcounter=0;
114 my $bibliocounter=0;
115 my $biblioitemcounter=0;
116 my $membercounter=0;
117
118 while (<MD>) {
119     (/insert into items /i) && ($itemcounter++);
120     (/insert into biblioitems /i) && ($biblioitemcounter++);
121     (/insert into biblio /i) && ($bibliocounter++);
122     (/insert into borrowers /i) && ($membercounter++);
123     print BF $_;
124 }
125
126 close BF;
127 close MD;
128
129
130 my $filels=`ls -hl $backupdir/Koha.backup_$date`;
131 chomp $filels;
132 printf qq|
133
134 Backed up:
135
136 %6d biblio entries
137 %6d biblioitems entries
138 %6d items entries
139 %6d borrowers
140
141 $filels
142
143 Does this look right? ([Y]/N):
144 |, $bibliocounter, $biblioitemcounter, $itemcounter, $membercounter;
145
146 $answer = <STDIN>;
147 chomp $answer;
148
149 if ($answer=~/^n/i) {
150     print qq|
151
152 Aborting.  The database dump is located in:
153
154         $backupdir/Koha.backup_$date
155
156 |;
157     exit;
158 } else {
159         print "Great! continuing upgrade... \n";
160 };
161
162
163
164 if ($opacdir && $intranetdir) {
165     print qq|
166
167 I believe that your old files are located in:
168
169   OPAC:      $opacdir
170   INTRANET:  $intranetdir
171
172
173 Does this look right?  ([Y]/N):
174 |;
175     $answer = <STDIN>;
176     chomp $answer;
177
178     if ($answer =~/n/i) {
179         $intranetdir='';
180         $opacdir='';
181     } else {
182         print "Great! continuing upgrade... \n";
183     }
184 }
185
186
187 if (!$opacdir || !$intranetdir) {
188     $intranetdir='';
189     $opacdir='';
190     while (!$intranetdir) {
191         print "Please specify the location of your INTRANET files: ";  
192
193         $answer = <STDIN>;
194         chomp $answer;
195
196         if ($answer) {
197             $intranetdir=$answer;
198         }
199         if (! -e "$intranetdir/htdocs") {
200             print "\nCouldn't find the htdocs directory here.  That doesn't look right.\nPlease enter another location.\n\n";
201             $intranetdir='';
202         }
203     }
204     while (!$opacdir) {
205         print "Please specify the location of your OPAC files: ";  
206
207         $answer = <STDIN>;
208         chomp $answer;
209
210         if ($answer) {
211             $opacdir=$answer;
212         }
213         if (! -e "$opacdir/htdocs") {
214             print "\nCouldn't find the htdocs directory here.  That doesn't look right.\nPlease enter another location.\n\n";
215             $opacdir='';
216         }
217     }
218 }
219
220
221
222 print "\n\nBacking up old Koha scripts...\n";
223 print     "===============================\n\n";
224
225 mkdir "$backupdir/kohafiles-$date", 0770;
226 mkdir "$backupdir/kohafiles-$date/intranet", 0770;
227 mkdir "$backupdir/kohafiles-$date/opac", 0770;
228
229 my $result=system("cp -R $intranetdir/* $backupdir/kohafiles-$date/intranet/");
230 if ($result) {
231     print "Error encounted when copying $intranetdir to $backupdir/kohafiles-$date/intranet/\n";
232     exit;
233 } else {
234     system("rm -rf $intranetdir/*");
235 }
236 $result=system("cp -R $opacdir/* $backupdir/kohafiles-$date/opac/");
237 if ($result) {
238     print "Error encounted when copying $opacdir to $backupdir/kohafiles-$date/opac/\n";
239     exit;
240 } else {
241     system("rm -rf $opacdir/*");
242 }
243
244    print "Creating $intranetdir/htdocs...\n";
245    mkdir ("$intranetdir/htdocs", oct(750));
246    print "Creating $intranetdir/cgi-bin...\n";
247    mkdir ("$intranetdir/cgi-bin", oct(750));
248    print "Creating $intranetdir/modules...\n";
249    mkdir ("$intranetdir/modules", oct(750));
250    print "Creating $intranetdir/scripts...\n";
251    mkdir ("$intranetdir/scripts", oct(750));
252    chmod (oct(770), "$opacdir");
253    print "Creating $opacdir/htdocs...\n";
254    mkdir ("$opacdir/htdocs", oct(750));
255    print "Creating $opacdir/cgi-bin...\n";
256    mkdir ("$opacdir/cgi-bin", oct(750));
257
258 my $httpduser;
259 my $realhttpdconf;
260
261 foreach my $httpdconf (qw(/usr/local/apache/conf/httpd.conf
262                       /usr/local/etc/apache/httpd.conf
263                       /usr/local/etc/apache/apache.conf
264                       /var/www/conf/httpd.conf
265                       /etc/apache/conf/httpd.conf
266                       /etc/apache/conf/apache.conf
267                       /etc/apache-ssl/conf/apache.conf
268                       /etc/httpd/conf/httpd.conf
269                       /etc/httpd/httpd.conf)) {
270    if ( -f $httpdconf ) {
271             $realhttpdconf=$httpdconf;
272             open (HTTPDCONF, $httpdconf) or warn "Insufficient privileges to open $httpdconf for reading.\n";
273       while (<HTTPDCONF>) {
274          if (/^\s*User\s+"?([-\w]+)"?\s*$/) {
275             $httpduser = $1;
276          }
277       }
278       close(HTTPDCONF);
279    }
280 }
281
282 unless ($httpduser) {
283     print qq|
284 I was not able to determine the user that Apache is running as.  This
285 information is necessary in order to set the access privileges correctly on
286 /etc/koha.conf.  This user should be set in one of the Apache configuration
287 files using the "User" directive.
288 |;
289     print "What is your Apache user? ";
290     chomp($input = <STDIN>);
291
292     if ($input) {
293         $httpduser = $input;
294     } else {
295         $httpduser='Undetermined';
296     }
297 }
298
299 print "\n\nINSTALLING KOHA...\n";
300 print "\n\n==================\n";
301 print "Copying internet-html files to $intranetdir/htdocs...\n";
302 system("cp -R intranet-html/* $intranetdir/htdocs/");
303 print "Copying intranet-cgi files to $intranetdir/cgi-bin...\n";
304 system("cp -R intranet-cgi/* $intranetdir/cgi-bin/");
305 print "Copying script files to $intranetdir/scripts...\n";
306 system("cp -R scripts/* $intranetdir/scripts/");
307 print "Copying module files to $intranetdir/modules...\n";
308 system("cp -R modules/* $intranetdir/modules/");
309 print "Copying opac-html files to $opacdir/htdocs...\n";
310 system("cp -R opac-html/* $opacdir/htdocs/");
311 print "Copying opac-cgi files to $opacdir/cgi-bin...\n";
312 system("cp -R opac-cgi/* $opacdir/cgi-bin/");
313 system("touch $::opacdir/cgi-bin/opac");
314
315 system("chown -R root.$httpduser $opacdir");
316 system("chown -R root.$httpduser $intranetdir");
317
318
319 unless ($kohalogdir && -e $kohalogdir) {
320     $kohalogdir='/var/log/koha';
321     print "\n\nDirectory for logging by Z39.50 daemon [$kohalogdir]: ";
322     chomp($input = <STDIN>);
323     if ($input) {
324         $kohalogdir=$input;
325     }
326 }
327
328 unless (-e "$kohalogdir") {
329     my $result = mkdir 0770, "$kohalogdir"; 
330     if ($result==0) {
331         my @dirs = split(m#/#, $kohalogdir);
332         my $checkdir='';
333         foreach (@dirs) {
334             $checkdir.="$_/";
335             unless (-e "$checkdir") {
336                 mkdir($checkdir, 0775);
337             }
338         }
339     }
340 }
341
342 chmod 0750, "$intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh";
343 chmod 0750, "$intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh";
344 chmod 0750, "$intranetdir/scripts/z3950daemon/processz3950queue";
345 chown(0, (getpwnam($httpduser)) [3], "$intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh") or warn "can't chown $intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh: $!";
346 chown(0, (getpwnam($httpduser)) [3], "$intranetdir/scripts/z3950daemon/processz3950queue") or warn "can't chown $intranetdir/scripts/z3950daemon/processz3950queue: $!";
347
348
349 open (KC, "/etc/koha.conf");
350 my $kccontents='';
351 my $kc;
352 while (<KC>) {
353     if (/^\s*includes\s*=/) {
354         $kccontents.="includes=$intranetdir/htdocs/includes\n";
355         $kc->{'includes'}=1;
356     } elsif (/^\s*httpduser\s*=/) {
357         $kccontents.="httpduser=$httpduser\n";
358         $kc->{'httpduser'}=1;
359     } elsif (/^\s*kohaversion\s*=/) {
360         $kccontents.="kohaversion=$newversion\n";
361         $kc->{'kohaversion'}=1;
362     } elsif (/^\s*kohalogdir\s*=/) {
363         $kccontents.="kohalogdir=$kohalogdir\n";
364         $kc->{'kohalogdir'}=1;
365     } elsif (/^\s*intranetdir\s*=/) {
366         $kccontents.="intranetdir=$intranetdir\n";
367         $kc->{'intranetdir'}=1;
368     } elsif (/^\s*opacdir\s*=/) {
369         $kccontents.="opacdir=$opacdir\n";
370         $kc->{'opacdir'}=1;
371     } else {
372         $kccontents.="$_";
373     }
374 }
375
376 unless (defined($kc->{'kohaversion'})) {
377     $kccontents.="kohaversion=$newversion\n";
378 }
379 unless (defined($kc->{'includes'})) {
380     $kccontents.="includes=$intranetdir/htdocs/includes\n";
381 }
382 unless (defined($kc->{'httpduser'})) {
383     $kccontents.="httpduser=$httpduser\n";
384 }
385 unless (defined($kc->{'intranetdir'})) {
386     $kccontents.="intranetdir=$intranetdir\n";
387 }
388 unless (defined($kc->{'opacdir'})) {
389     $kccontents.="opacdir=$opacdir\n";
390 }
391 unless (defined($kc->{'kohalogdir'})) {
392     $kccontents.="kohalogdir=$kohalogdir\n";
393 }
394
395
396 system("mv /etc/koha.conf /etc/koha.conf.backup");
397
398 open (KC, ">/etc/koha.conf") || warn "Couldn't open /etc/koha.conf for writing.";
399 print KC $kccontents;
400 close KC;
401
402
403 print qq|
404
405 Upgrading Database
406 ==================
407 |;
408 system ("perl -I $intranetdir/modules scripts/updater/updatedatabase");
409
410
411 print qq|
412
413 ==================
414 = Authentication =
415 ==================
416
417 This release of Koha has a new authentication module.  If you are not already
418 using basic authentication on your intranet, you will be required to log in to
419 access some of the features of the intranet.  You can log in using the userid
420 and password from the /etc/koha.conf configuration file at any time.  Use the
421 "Members" module to add passwords for other accounts and set their permissions.
422
423 [NOTE PERMISSIONS ARE NOT COMPLETED AS OF 1.2.3RC1.  Do not give passwords to
424  any patrons unless you want them to have full access to your intranet.]
425 |;
426 print "Press the <ENTER> key to continue: ";
427 <STDIN>;
428
429
430
431 print qq|
432
433 ==========================
434 = Koha Upgrade Completed =
435 ==========================
436 The Koha Upgrade is finished.  If you are upgrading from a version of Koha
437 prior to 1.2.1, it is likely that you will have to modify your Apache
438 configuration to point it to the new files.
439
440 In your INTRANET VirtualHost section you should have:
441   DocumentRoot $intranetdir/htdocs
442   ScriptAlias /cgi-bin/koha/ $intranetdir/cgi-bin/
443   SetEnv PERL5LIB $intranetdir/modules
444
445 In the OPAC VirtualHost section you should have:
446   DocumentRoot $opacdir/htdocs
447   ScriptAlias /cgi-bin/koha/ $opacdir/cgi-bin/
448   SetEnv PERL5LIB $intranetdir/modules
449
450 You may also need to uncomment a "LoadModules env_module ... " line and restart
451 Apache.
452
453 Please report any problems you encounter through http://bugs.koha.org/
454 |;