1 #!/usr/bin/perl -w # please develop with -w
9 use C4
::Languages
qw(getAllLanguages getTranslatedLanguages);
12 use strict
; # please develop with the strict pragma
17 my $step = $query->param('step');
19 my $language = $query->param('language');
20 my ( $template, $loggedinuser, $cookie );
22 my $all_languages = getAllLanguages
();
24 if ( defined($language) ) {
25 setlanguagecookie
( $query, $language, "install.pl?step=1" );
27 ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
29 template_name
=> "installer/step" . ( $step ?
$step : 1 ) . ".tmpl",
37 my $installer = C4
::Installer
->new();
39 $info{'dbname'} = C4
::Context
->config("database");
41 ( C4
::Context
->config("db_scheme")
42 ? C4
::Context
->config("db_scheme")
44 $info{'hostname'} = C4
::Context
->config("hostname");
45 $info{'port'} = C4
::Context
->config("port");
46 $info{'user'} = C4
::Context
->config("user");
47 $info{'password'} = C4
::Context
->config("pass");
48 my $dbh = DBI
->connect(
49 "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
50 . ( $info{port
} ?
";port=$info{port}" : "" ),
51 $info{'user'}, $info{'password'}
54 if ( $step && $step == 1 ) {
56 #Checking ALL perl Modules and services needed are installed.
57 #Whenever there is an error, adding a report to the page
58 $template->param( language
=> 1 );
61 unless ( $] >= 5.006001 ) { # Bug 179
62 $template->param( "problems" => 1, "perlversion" => 1 );
66 # We could here use a special find
68 unless ( eval { require ZOOM
} ) {
69 push @missing, { name
=> "ZOOM" };
71 unless ( eval { require YAML
::Syck
} ) {
72 push @missing, { name
=> "YAML::Syck" };
74 unless ( eval { require LWP
::Simple
} ) {
75 push @missing, { name
=> "LWP::Simple" };
77 unless ( eval { require XML
::Simple
} ) {
78 push @missing, { name
=> "XML::Simple" };
80 unless ( eval { require MARC
::File
::XML
} ) {
81 push @missing, { name
=> "MARC::File::XML" };
83 unless ( eval { require MARC
::File
::USMARC
} ) {
84 push @missing, { name
=> "MARC::File::USMARC" };
86 unless ( eval { require DBI
} ) {
87 push @missing, { name
=> "DBI" };
89 unless ( eval { require Date
::Manip
} ) {
90 push @missing, { name
=> "Date::Manip" };
92 unless ( eval { require DBD
::mysql
} ) {
93 push @missing, { name
=> "DBD::mysql" };
95 unless ( eval { require HTML
::Template
::Pro
} ) {
96 push @missing, { name
=> "HTML::Template::Pro" };
98 unless ( eval { require Date
::Calc
} ) {
99 push @missing, { name
=> "Date::Calc" };
101 unless ( eval { require Digest
::MD5
} ) {
102 push @missing, { name
=> "Digest::MD5" };
104 unless ( eval { require MARC
::Record
} ) {
105 push @missing, { name
=> "MARC::Record" };
107 unless ( eval { require Mail
::Sendmail
} ) {
108 push @missing, { name
=> "Mail::Sendmail", usagemail
=> 1 };
110 unless ( eval { require List
::MoreUtils
} ) {
111 push @missing, { name
=> "List::MoreUtils" };
113 unless ( eval { require XML
::RSS
} ) {
114 push @missing, { name
=> "XML::RSS" };
116 unless ( eval { require CGI
::Carp
} ) {
117 push @missing, { name
=> "CGI::Carp" };
121 # The following modules are not mandatory, depends on how the library want to use Koha
122 unless ( eval { require PDF
::API2
} ) {
123 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
124 push @missing, { name
=> "PDF::API2", usagebarcode
=> 1 };
127 unless ( eval { require GD
::Barcorde
} ) {
128 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
130 { name
=> "GD::Barcode", usagebarcode
=> 1, usagespine
=> 1 };
133 unless ( eval { require Data
::Random
} ) {
134 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
135 push @missing, { name
=> "Data::Random", usagebarcode
=> 1 };
138 unless ( eval { require PDF
::Reuse
::Barcode
} ) {
139 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
140 push @missing, { name
=> "PDF::Reuse::Barcode", usagebarcode
=> 1 };
143 unless ( eval { require PDF
::Report
} ) {
144 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
145 push @missing, { name
=> "PDF::Report", usagebarcode
=> 1 };
148 unless ( eval { require Algorithm
::CheckDigits
} ) {
149 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
150 push @missing, { name
=> "Algorithm::CheckDigits", usagebarcode
=> 1 };
153 unless ( eval { require GD
::Barcode
::UPCE
} ) {
154 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
155 push @missing, { name
=> "GD::Barcode::UPCE", usagepine
=> 1 };
158 unless ( eval { require Net
::LDAP
} ) {
159 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
160 push @missing, { name
=> "Net::LDAP", usageLDAP
=> 1 };
163 $template->param( missings
=> \
@missing ) if ( scalar(@missing) > 0 );
164 $template->param( 'checkmodule' => 1 )
165 unless ( scalar(@missing) && $problem );
168 elsif ( $step && $step == 2 ) {
170 #STEP 2 Check Database connection and access
172 $template->param(%info);
173 my $checkdb = $query->param("checkdb");
174 $template->param( 'dbconnection' => $checkdb );
178 # Can connect to the mysql
179 $template->param( "checkdatabaseaccess" => 1 );
180 if ( $info{dbms
} eq "mysql" ) {
182 #Check if database created
183 my $rv = $dbh->do("SHOW DATABASES LIKE \'$info{dbname}\'");
185 $template->param( 'checkdatabasecreated' => 1 );
188 #Check if user have all necessary grants on this database.
191 "SHOW GRANTS FOR \'$info{user}\'\@'$info{hostname}'");
194 while ( my ($line) = $rq->fetchrow ) {
195 my $dbname = $info{dbname
};
196 if ( $line =~ m/^GRANT (.*?) ON `$dbname`\.\*/ || index( $line, '*.*' ) > 0 ) {
199 index( $line, 'ALL PRIVILEGES' ) > 0
200 || ( ( index( $line, 'SELECT' ) > 0 )
201 && ( index( $line, 'INSERT' ) > 0 )
202 && ( index( $line, 'UPDATE' ) > 0 )
203 && ( index( $line, 'DELETE' ) > 0 )
204 && ( index( $line, 'CREATE' ) > 0 )
205 && ( index( $line, 'DROP' ) > 0 ) )
209 unless ($grantaccess) {
211 $dbh->prepare("SHOW GRANTS FOR \'$info{user}\'\@'\%'");
213 while ( my ($line) = $rq->fetchrow ) {
214 my $dbname = $info{dbname
};
215 if ( $line =~ m/$dbname/ || index( $line, '*.*' ) > 0 )
219 index( $line, 'ALL PRIVILEGES' ) > 0
220 || ( ( index( $line, 'SELECT' ) > 0 )
221 && ( index( $line, 'INSERT' ) > 0 )
222 && ( index( $line, 'UPDATE' ) > 0 )
223 && ( index( $line, 'DELETE' ) > 0 )
224 && ( index( $line, 'CREATE' ) > 0 )
225 && ( index( $line, 'DROP' ) > 0 ) )
230 $template->param( "checkgrantaccess" => $grantaccess );
231 } # End mysql connect check...
233 elsif ( $info{dbms
} eq "Pg" ) {
234 # Check if database has been created...
235 my $rv = $dbh->do( "SELECT * FROM pg_catalog.pg_database WHERE datname = \'$info{dbname}\';" );
237 $template->param( 'checkdatabasecreated' => 1 );
240 # Check if user has all necessary grants on this database...
241 my $rq = $dbh->do( "SELECT u.usesuper
242 FROM pg_catalog.pg_user as u
243 WHERE u.usename = \'$info{user}\';" );
245 $template->param( "checkgrantaccess" => 1 );
247 } # End Pg connect check...
250 $template->param( "error" => DBI
::err
, "message" => DBI
::errstr
);
254 elsif ( $step && $step == 3 ) {
257 # STEP 3 : database setup
260 my $op = $query->param('op');
261 if ( $op && $op eq 'finished' ) {
263 # we have finished, just redirect to mainpage.
265 print $query->redirect("/cgi-bin/koha/mainpage.pl");
268 elsif ( $op && $op eq 'finish' ) {
269 $installer->set_version_syspref();
271 # Installation is finished.
272 # We just deny anybody access to install
273 # And we redirect people to mainpage.
274 # The installer will have to relogin since we do not pass cookie to redirection.
275 $template->param( "$op" => 1 );
277 elsif ( $op && $op eq 'SetIndexingEngine' ) {
278 $installer->set_indexing_engine($query->param('NoZebra'));
279 $template->param( "$op" => 1 );
281 elsif ( $op && $op eq 'addframeworks' ) {
283 # 1ST install, 3rd sub-step : insert the SQL files the user has selected
286 my ($fwk_language, $list) = $installer->load_sql_in_order($all_languages, $query->param('framework'));
288 "fwklanguage" => $fwk_language,
291 $template->param( "$op" => 1 );
293 elsif ( $op && $op eq 'selectframeworks' ) {
296 # 1ST install, 2nd sub-step : show the user the sql datas he can insert in the database.
299 # (note that the term "selectframeworks is not correct. The user can select various files, not only frameworks)
302 #sql data for import are supposed to be located in installer/data/<language>/<level>
303 # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
304 # Where <level> is a category of requirement : required, recommended optional
305 # level should contain :
306 # SQL File for import With a readable name.
307 # txt File taht explains what this SQL File is meant for.
308 # Could be VERY useful to have A Big file for a kind of library.
309 # But could also be useful to have some Authorised values data set prepared here.
310 # Framework Selection is achieved through checking boxes.
311 my $langchoice = $query->param('fwklanguage');
312 $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
313 my $marcflavour = $query->param('marcflavour');
315 $installer->set_marcflavour_syspref($marcflavour);
317 $marcflavour = C4
::Context
->preference('marcflavour') unless ($marcflavour);
318 #Insert into database the selected marcflavour
320 my ($marc_defaulted_to_en, $fwklist) = $installer->marc_framework_sql_list($langchoice, $marcflavour);
321 $template->param('en_marc_frameworks' => $marc_defaulted_to_en);
322 $template->param( "frameworksloop" => $fwklist );
323 $template->param( "marcflavour" => ucfirst($marcflavour));
325 my ($sample_defaulted_to_en, $levellist) = $installer->sample_data_sql_list($langchoice, $marcflavour);
326 $template->param( "en_sample_data" => $sample_defaulted_to_en);
327 $template->param( "levelloop" => $levellist );
328 $template->param( "$op" => 1 );
330 elsif ( $op && $op eq 'choosemarc' ) {
333 # 1ST install, 2nd sub-step : show the user the marcflavour available.
338 #sql data are supposed to be located in installer/data/<dbms>/<language>/marcflavour/marcflavourname
339 # Where <dbms> is database type according to DBD syntax
340 # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
341 # Where <level> is a category of requirement : required, recommended optional
342 # level should contain :
343 # SQL File for import With a readable name.
344 # txt File taht explains what this SQL File is meant for.
345 # Could be VERY useful to have A Big file for a kind of library.
346 # But could also be useful to have some Authorised values data set prepared here.
347 # Marcflavour Selection is achieved through radiobuttons.
348 my $langchoice = $query->param('fwklanguage');
349 $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
351 C4
::Context
->config('intranetdir') . "/installer/data/$info{dbms}/$langchoice/marcflavour";
352 unless (opendir( MYDIR
, $dir )) {
353 if ($langchoice eq 'en') {
354 warn "cannot open MARC frameworks directory $dir";
356 # if no translated MARC framework is available,
358 $dir = C4
::Context
->config('intranetdir') . "/installer/data/$info{dbms}/en/marcflavour";
359 opendir(MYDIR
, $dir) or warn "cannot open English MARC frameworks directory $dir";
362 my @listdir = grep { !/^\./ && -d
"$dir/$_" } readdir(MYDIR
);
364 my $marcflavour=C4
::Context
->preference("marcflavour");
366 foreach my $marc (@listdir) {
368 "label"=> ucfirst($marc),
370 "checked"=> defined($marcflavour) ?
uc($marc) eq $marcflavour : 0);
371 # $cell{"description"}= do { local $/ = undef; open INPUT "<$dir/$marc.txt"||"";<INPUT> };
372 push @flavourlist, \
%cell;
374 $template->param( "flavourloop" => \
@flavourlist );
375 $template->param( "$op" => 1 );
377 elsif ( $op && $op eq 'importdatastructure' ) {
380 # 1st install, 1st "sub-step" : import kohastructure
383 my $error = $installer->load_db_schema();
389 elsif ( $op && $op eq 'updatestructure' ) {
391 # Not 1st install, the only sub-step : update database
393 #Do updatedatabase And report
395 C4
::Context
->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl";
397 my $string = qx($execstring 2>&1 1>/dev/null
); # added '1>/dev/null' to return only stderr in $string. Needs testing here. -fbcit
399 $string =~ s/\n|\r/<br \/>/g
;
401 s/(DBD::mysql.*? failed: .*? line [0-9]*.|=================.*?====================)/<font color=red>$1<\/font
>/g
;
402 $template->param( "updatereport" => $string );
404 $template->param( $op => 1 );
408 # check wether it's a 1st install or an update
410 #Check if there are enough tables.
411 # Paul has cleaned up tables so reduced the count
412 #I put it there because it implied a data import if condition was not satisfied.
413 my $dbh = DBI
->connect(
414 "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
415 . ( $info{port
} ?
";port=$info{port}" : "" ),
416 $info{'user'}, $info{'password'}
419 if ( $info{dbms
} eq 'mysql' ) { $rq = $dbh->prepare( "SHOW TABLES FROM " . $info{'dbname'} ); }
420 elsif ( $info{dbms
} eq 'Pg' ) { $rq = $dbh->prepare( "SELECT *
421 FROM information_schema.tables
422 WHERE table_schema='public' and table_type='BASE TABLE';" ); }
424 my $data = $rq->fetchall_arrayref( {} );
425 my $count = scalar(@
$data);
427 # we don't have tables, propose DB import
430 $template->param( "count" => $count, "proposeimport" => 1 );
434 # we have tables, propose to select files to upload or updatedatabase
436 $template->param( "count" => $count, "default" => 1 );
438 # 1st part of step 3 : check if there is a databaseversion systempreference
439 # if there is, then we just need to upgrade
440 # if there is none, then we need to install the database
442 if (C4
::Context
->preference('Version')) {
443 my $dbversion = C4
::Context
->preference('Version');
444 $dbversion =~ /(.*)\.(..)(..)(...)/;
445 $dbversion = "$1.$2.$3.$4";
446 $template->param("upgrading" => 1,
447 "dbversion" => $dbversion,
448 "kohaversion" => C4
::Context
->KOHAVERSION,
458 # LANGUAGE SELECTION page by default
459 # using opendir + language Hash
460 my $languages_loop = getTranslatedLanguages
('intranet');
461 $template->param( installer_languages_loop
=> $languages_loop );
465 "SELECT * from systempreferences WHERE variable='Version'");
466 if ( $rq->execute ) {
467 my ($version) = $rq->fetchrow;
469 $query->redirect("install.pl?step=3");
475 output_html_with_http_headers
$query, $cookie, $template->output;