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