Bug 18674: Added timezone information to about page
[koha-equinox.git] / about.pl
1 #!/usr/bin/perl
2
3 # Copyright Pat Eyler 2003
4 # Copyright Biblibre 2006
5 # Parts Copyright Liblime 2008
6 # Parts Copyright Chris Nighswonger 2010
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23 use Modern::Perl;
24
25 use CGI qw ( -utf8 );
26 use List::MoreUtils qw/ any /;
27 use LWP::Simple;
28 use XML::Simple;
29 use Config;
30 use Search::Elasticsearch;
31 use Try::Tiny;
32
33 use C4::Output;
34 use C4::Auth;
35 use C4::Context;
36 use C4::Installer;
37
38 use Koha;
39 use Koha::Acquisition::Currencies;
40 use Koha::Patron::Categories;
41 use Koha::Patrons;
42 use Koha::Caches;
43 use Koha::Config::SysPrefs;
44 use Koha::Illrequest::Config;
45 use Koha::SearchEngine::Elasticsearch;
46
47 use C4::Members::Statistics;
48
49
50 #use Smart::Comments '####';
51
52 my $query = new CGI;
53 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
54     {
55         template_name   => "about.tt",
56         query           => $query,
57         type            => "intranet",
58         authnotrequired => 0,
59         flagsrequired   => { catalogue => 1 },
60         debug           => 1,
61     }
62 );
63
64 my $time_zone = {
65     actual      => C4::Context->timezone(),
66     config      => C4::Context->config('timezone'),
67     environment => $ENV{TZ},
68 };
69 $template->param( 'time_zone' => $time_zone );
70
71 my $perl_path = $^X;
72 if ($^O ne 'VMS') {
73     $perl_path .= $Config{_exe} unless $perl_path =~ m/$Config{_exe}$/i;
74 }
75
76 my $zebraVersion = `zebraidx -V`;
77
78 # Check running PSGI env
79 if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
80     $template->param(
81         is_psgi => 1,
82         psgi_server => ($ENV{ PLACK_ENV }) ? "Plack ($ENV{PLACK_ENV})" :
83                        ($ENV{ MOD_PERL })  ? "mod_perl ($ENV{MOD_PERL})" :
84                                              'Unknown'
85     );
86 }
87
88 # Memcached configuration
89 my $memcached_servers   = $ENV{MEMCACHED_SERVERS} || C4::Context->config('memcached_servers');
90 my $memcached_namespace = $ENV{MEMCACHED_NAMESPACE} || C4::Context->config('memcached_namespace') // 'koha';
91
92 my $cache = Koha::Caches->get_instance;
93 my $effective_caching_method = ref($cache->cache);
94 # Memcached may have been running when plack has been initialized but could have been stopped since
95 # FIXME What are the consequences of that??
96 my $is_memcached_still_active = $cache->set_in_cache('test_for_about_page', "just a simple value");
97
98 my $where_is_memcached_config = 'nowhere';
99 if ( $ENV{MEMCACHED_SERVERS} and C4::Context->config('memcached_servers') ) {
100     $where_is_memcached_config = 'both';
101 } elsif ( $ENV{MEMCACHED_SERVERS} and not C4::Context->config('memcached_servers') ) {
102     $where_is_memcached_config = 'ENV_only';
103 } elsif ( C4::Context->config('memcached_servers') ) {
104     $where_is_memcached_config = 'config_only';
105 }
106
107 $template->param(
108     effective_caching_method => $effective_caching_method,
109     memcached_servers   => $memcached_servers,
110     memcached_namespace => $memcached_namespace,
111     is_memcached_still_active => $is_memcached_still_active,
112     where_is_memcached_config => $where_is_memcached_config,
113     memcached_running   => Koha::Caches->get_instance->memcached_cache,
114 );
115
116 # Additional system information for warnings
117
118 my $warnStatisticsFieldsError;
119 my $prefStatisticsFields = C4::Context->preference('StatisticsFields');
120 if ($prefStatisticsFields) {
121     $warnStatisticsFieldsError = $prefStatisticsFields
122         unless ( $prefStatisticsFields eq C4::Members::Statistics->get_fields() );
123 }
124
125 my $prefAutoCreateAuthorities = C4::Context->preference('AutoCreateAuthorities');
126 my $prefBiblioAddsAuthorities = C4::Context->preference('BiblioAddsAuthorities');
127 my $warnPrefBiblioAddsAuthorities = ( $prefAutoCreateAuthorities && ( !$prefBiblioAddsAuthorities) );
128
129 my $prefEasyAnalyticalRecords  = C4::Context->preference('EasyAnalyticalRecords');
130 my $prefUseControlNumber  = C4::Context->preference('UseControlNumber');
131 my $warnPrefEasyAnalyticalRecords  = ( $prefEasyAnalyticalRecords  && $prefUseControlNumber );
132 my $warnPrefAnonymousPatron = (
133     C4::Context->preference('OPACPrivacy')
134         and not C4::Context->preference('AnonymousPatron')
135 );
136
137 my $anonymous_patron = Koha::Patrons->find( C4::Context->preference('AnonymousPatron') );
138 my $warnPrefAnonymousPatron_PatronDoesNotExist = ( not $anonymous_patron and Koha::Patrons->search({ privacy => 2 })->count );
139
140 my $errZebraConnection = C4::Context->Zconn("biblioserver",0)->errcode();
141
142 my $warnIsRootUser   = (! $loggedinuser);
143
144 my $warnNoActiveCurrency = (! defined Koha::Acquisition::Currencies->get_active);
145
146 my @xml_config_warnings;
147
148 my $context = new C4::Context;
149
150 if ( ! defined C4::Context->config('zebra_bib_index_mode') ) {
151     push @xml_config_warnings, {
152         error => 'zebra_bib_index_mode_warn'
153     };
154     if ($context->{'server'}->{'biblioserver'}->{'config'} !~ /zebra-biblios-dom.cfg/) {
155         push @xml_config_warnings, {
156             error => 'zebra_bib_mode_seems_grs1'
157         };
158     }
159     else {
160         push @xml_config_warnings, {
161             error => 'zebra_bib_mode_seems_dom'
162         };
163     }
164 } else {
165     push @xml_config_warnings, { error => 'zebra_bib_grs_warn' }
166         if C4::Context->config('zebra_bib_index_mode') eq 'grs1';
167 }
168
169 if ( (C4::Context->config('zebra_bib_index_mode') eq 'dom') &&
170      ($context->{'server'}->{'biblioserver'}->{'config'} !~ /zebra-biblios-dom.cfg/) ) {
171
172     push @xml_config_warnings, {
173         error => 'zebra_bib_index_mode_mismatch_warn'
174     };
175 }
176
177 if ( (C4::Context->config('zebra_bib_index_mode') eq 'grs1') &&
178      ($context->{'server'}->{'biblioserver'}->{'config'} =~ /zebra-biblios-dom.cfg/) ) {
179
180     push @xml_config_warnings, {
181         error => 'zebra_bib_index_mode_mismatch_warn'
182     };
183 }
184
185 if ( ! defined C4::Context->config('zebra_auth_index_mode') ) {
186     push @xml_config_warnings, {
187         error => 'zebra_auth_index_mode_warn'
188     };
189     if ($context->{'server'}->{'authorityserver'}->{'config'} !~ /zebra-authorities-dom.cfg/) {
190         push @xml_config_warnings, {
191             error => 'zebra_auth_mode_seems_grs1'
192         };
193     }
194     else {
195         push @xml_config_warnings, {
196             error => 'zebra_auth_mode_seems_dom'
197         };
198     }
199 } else {
200     push @xml_config_warnings, { error => 'zebra_auth_grs_warn' }
201         if C4::Context->config('zebra_auth_index_mode') eq 'grs1';
202 }
203
204 if ( (C4::Context->config('zebra_auth_index_mode') eq 'dom') && ($context->{'server'}->{'authorityserver'}->{'config'} !~ /zebra-authorities-dom.cfg/) ) {
205     push @xml_config_warnings, {
206         error => 'zebra_auth_index_mode_mismatch_warn'
207     };
208 }
209
210 if ( (C4::Context->config('zebra_auth_index_mode') eq 'grs1') && ($context->{'server'}->{'authorityserver'}->{'config'} =~ /zebra-authorities-dom.cfg/) ) {
211     push @xml_config_warnings, {
212         error => 'zebra_auth_index_mode_mismatch_warn'
213     };
214 }
215
216 if ( ! defined C4::Context->config('log4perl_conf') ) {
217     push @xml_config_warnings, {
218         error => 'log4perl_entry_missing'
219     }
220 }
221
222 if ( ! defined C4::Context->config('upload_path') ) {
223     if ( Koha::Config::SysPrefs->find('OPACBaseURL')->value ) {
224         # OPACBaseURL seems to be set
225         push @xml_config_warnings, {
226             error => 'uploadpath_entry_missing'
227         }
228     } else {
229         push @xml_config_warnings, {
230             error => 'uploadpath_and_opacbaseurl_entry_missing'
231         }
232     }
233 }
234
235 # Test QueryParser configuration sanity
236 if ( C4::Context->preference( 'UseQueryParser' ) ) {
237     # Get the QueryParser configuration file name
238     my $queryparser_file          = C4::Context->config( 'queryparser_config' );
239     my $queryparser_fallback_file = '/etc/koha/searchengine/queryparser.yaml';
240     # Check QueryParser is functional
241     my $QParser = C4::Context->queryparser();
242     my $queryparser_error = {};
243     if ( ! defined $QParser || ref($QParser) ne 'Koha::QueryParser::Driver::PQF' ) {
244         # Error initializing the QueryParser object
245         # Get the used queryparser.yaml file path to report the user
246         $queryparser_error->{ fallback } = ( defined $queryparser_file ) ? 0 : 1;
247         $queryparser_error->{ file }     = ( defined $queryparser_file )
248                                                 ? $queryparser_file
249                                                 : $queryparser_fallback_file;
250         # Report error data to the template
251         $template->param( QueryParserError => $queryparser_error );
252     } else {
253         # Check for an absent queryparser_config entry in koha-conf.xml
254         if ( ! defined $queryparser_file ) {
255             # Not an error but a warning for the missing entry in koha-conf-xml
256             push @xml_config_warnings, {
257                     error => 'queryparser_entry_missing',
258                     file  => $queryparser_fallback_file
259             };
260         }
261     }
262 }
263
264 # Test Zebra facets configuration
265 if ( !defined C4::Context->config('use_zebra_facets') ) {
266     push @xml_config_warnings, { error => 'use_zebra_facets_entry_missing' };
267 } else {
268     if ( C4::Context->config('use_zebra_facets') &&
269          C4::Context->config('zebra_bib_index_mode') ) {
270         # use_zebra_facets works with DOM
271         push @xml_config_warnings, {
272             error => 'use_zebra_facets_needs_dom'
273         } if C4::Context->config('zebra_bib_index_mode') ne 'dom' ;
274     }
275 }
276
277 # ILL module checks
278 if ( C4::Context->preference('ILLModule') ) {
279     my $warnILLConfiguration = 0;
280     my $ill_config_from_file = C4::Context->config("interlibrary_loans");
281     my $ill_config = Koha::Illrequest::Config->new;
282
283     my $available_ill_backends =
284       ( scalar @{ $ill_config->available_backends } > 0 );
285
286     # Check backends
287     if ( !$available_ill_backends ) {
288         $template->param( no_ill_backends => 1 );
289         $warnILLConfiguration = 1;
290     }
291
292     # Check partner_code
293     if ( !Koha::Patron::Categories->find($ill_config->partner_code) ) {
294         $template->param( ill_partner_code_doesnt_exist => $ill_config->partner_code );
295         $warnILLConfiguration = 1;
296     }
297
298     if ( !$ill_config_from_file->{partner_code} ) {
299         # partner code not defined
300         $template->param( ill_partner_code_not_defined => 1 );
301         $warnILLConfiguration = 1;
302     }
303
304     $template->param( warnILLConfiguration => $warnILLConfiguration );
305 }
306
307 if ( C4::Context->preference('SearchEngine') eq 'Elasticsearch' ) {
308     # Check ES configuration health and runtime status
309
310     my $es_status;
311     my $es_config_error;
312     my $es_running = 1;
313
314     my $es_conf;
315     try {
316         $es_conf = Koha::SearchEngine::Elasticsearch::_read_configuration();
317     }
318     catch {
319         if ( ref($_) eq 'Koha::Exceptions::Config::MissingEntry' ) {
320             $template->param( elasticsearch_fatal_config_error => $_->message );
321             $es_config_error = 1;
322         }
323     };
324     if ( !$es_config_error ) {
325
326         my $biblios_index_name     = $es_conf->{index_name} . "_" . $Koha::SearchEngine::BIBLIOS_INDEX;
327         my $authorities_index_name = $es_conf->{index_name} . "_" . $Koha::SearchEngine::AUTHORITIES_INDEX;
328
329         my @indexes = ($biblios_index_name, $authorities_index_name);
330         # TODO: When new indexes get added, we could have other ways to
331         #       fetch the list of available indexes (e.g. plugins, etc)
332         $es_status->{nodes} = $es_conf->{nodes};
333         my $es = Search::Elasticsearch->new({ nodes => $es_conf->{nodes} });
334
335         foreach my $index ( @indexes ) {
336             my $count;
337             try {
338                 $count = $es->indices->stats( index => $index )
339                       ->{_all}{primaries}{docs}{count};
340             }
341             catch {
342                 if ( ref($_) eq 'Search::Elasticsearch::Error::Missing' ) {
343                     push @{ $es_status->{errors} }, "Index not found ($index)";
344                     $count = -1;
345                 }
346                 elsif ( ref($_) eq 'Search::Elasticsearch::Error::NoNodes' ) {
347                     $es_running = 0;
348                 }
349                 else {
350                     # TODO: when time comes, we will cover more use cases
351                     die $_;
352                 }
353             };
354
355             push @{ $es_status->{indexes} },
356               {
357                 index_name => $index,
358                 count      => $count
359               };
360         }
361         $es_status->{running} = $es_running;
362
363         $template->param( elasticsearch_status => $es_status );
364     }
365 }
366
367 # Sco Patron should not contain any other perms than circulate => self_checkout
368 if (  C4::Context->preference('WebBasedSelfCheck')
369       and C4::Context->preference('AutoSelfCheckAllowed')
370 ) {
371     my $userid = C4::Context->preference('AutoSelfCheckID');
372     my $all_permissions = C4::Auth::get_user_subpermissions( $userid );
373     my ( $has_self_checkout_perm, $has_other_permissions );
374     while ( my ( $module, $permissions ) = each %$all_permissions ) {
375         if ( $module eq 'self_check' ) {
376             while ( my ( $permission, $flag ) = each %$permissions ) {
377                 if ( $permission eq 'self_checkout_module' ) {
378                     $has_self_checkout_perm = 1;
379                 } else {
380                     $has_other_permissions = 1;
381                 }
382             }
383         } else {
384             $has_other_permissions = 1;
385         }
386     }
387     $template->param(
388         AutoSelfCheckPatronDoesNotHaveSelfCheckPerm => not ( $has_self_checkout_perm ),
389         AutoSelfCheckPatronHasTooManyPerm => $has_other_permissions,
390     );
391 }
392
393 {
394     my $dbh       = C4::Context->dbh;
395     my $patrons = $dbh->selectall_arrayref(
396         q|select b.borrowernumber from borrowers b join deletedborrowers db on b.borrowernumber=db.borrowernumber|,
397         { Slice => {} }
398     );
399     my $biblios = $dbh->selectall_arrayref(
400         q|select b.biblionumber from biblio b join deletedbiblio db on b.biblionumber=db.biblionumber|,
401         { Slice => {} }
402     );
403     my $items = $dbh->selectall_arrayref(
404         q|select i.itemnumber from items i join deleteditems di on i.itemnumber=di.itemnumber|,
405         { Slice => {} }
406     );
407     my $checkouts = $dbh->selectall_arrayref(
408         q|select i.issue_id from issues i join old_issues oi on i.issue_id=oi.issue_id|,
409         { Slice => {} }
410     );
411     my $holds = $dbh->selectall_arrayref(
412         q|select r.reserve_id from reserves r join old_reserves o on r.reserve_id=o.reserve_id|,
413         { Slice => {} }
414     );
415     if ( @$patrons or @$biblios or @$items or @$checkouts or @$holds ) {
416         $template->param(
417             has_ai_issues => 1,
418             ai_patrons    => $patrons,
419             ai_biblios    => $biblios,
420             ai_items      => $items,
421             ai_checkouts  => $checkouts,
422             ai_holds      => $holds,
423         );
424     }
425 }
426 my %versions = C4::Context::get_versions();
427
428 $template->param(
429     kohaVersion   => $versions{'kohaVersion'},
430     osVersion     => $versions{'osVersion'},
431     perlPath      => $perl_path,
432     perlVersion   => $versions{'perlVersion'},
433     perlIncPath   => [ map { perlinc => $_ }, @INC ],
434     mysqlVersion  => $versions{'mysqlVersion'},
435     apacheVersion => $versions{'apacheVersion'},
436     zebraVersion  => $zebraVersion,
437     prefBiblioAddsAuthorities => $prefBiblioAddsAuthorities,
438     prefAutoCreateAuthorities => $prefAutoCreateAuthorities,
439     warnPrefBiblioAddsAuthorities => $warnPrefBiblioAddsAuthorities,
440     warnPrefEasyAnalyticalRecords  => $warnPrefEasyAnalyticalRecords,
441     warnPrefAnonymousPatron => $warnPrefAnonymousPatron,
442     warnPrefAnonymousPatron_PatronDoesNotExist => $warnPrefAnonymousPatron_PatronDoesNotExist,
443     errZebraConnection => $errZebraConnection,
444     warnIsRootUser => $warnIsRootUser,
445     warnNoActiveCurrency => $warnNoActiveCurrency,
446     warnNoTemplateCaching => ( C4::Context->config('template_cache_dir') ? 0 : 1 ),
447     xml_config_warnings => \@xml_config_warnings,
448     warnStatisticsFieldsError => $warnStatisticsFieldsError,
449 );
450
451 my @components = ();
452
453 my $perl_modules = C4::Installer::PerlModules->new;
454 $perl_modules->versions_info;
455
456 my @pm_types = qw(missing_pm upgrade_pm current_pm);
457
458 foreach my $pm_type(@pm_types) {
459     my $modules = $perl_modules->get_attr($pm_type);
460     foreach (@$modules) {
461         my ($module, $stats) = each %$_;
462         push(
463             @components,
464             {
465                 name    => $module,
466                 version => $stats->{'cur_ver'},
467                 missing => ($pm_type eq 'missing_pm' ? 1 : 0),
468                 upgrade => ($pm_type eq 'upgrade_pm' ? 1 : 0),
469                 current => ($pm_type eq 'current_pm' ? 1 : 0),
470                 require => $stats->{'required'},
471                 reqversion => $stats->{'min_ver'},
472             }
473         );
474     }
475 }
476
477 @components = sort {$a->{'name'} cmp $b->{'name'}} @components;
478
479 my $counter=0;
480 my $row = [];
481 my $table = [];
482 foreach (@components) {
483     push (@$row, $_);
484     unless (++$counter % 4) {
485         push (@$table, {row => $row});
486         $row = [];
487     }
488 }
489 # Processing the last line (if there are any modules left)
490 if (scalar(@$row) > 0) {
491     # Extending $row to the table size
492     $$row[3] = '';
493     # Pushing the last line
494     push (@$table, {row => $row});
495 }
496 ## ## $table
497
498 $template->param( table => $table );
499
500
501 ## ------------------------------------------
502 ## Koha time line code
503
504 #get file location
505 my $docdir;
506 if ( defined C4::Context->config('docdir') ) {
507     $docdir = C4::Context->config('docdir');
508 } else {
509     # if no <docdir> is defined in koha-conf.xml, use the default location
510     # this is a work-around to stop breakage on upgraded Kohas, bug 8911
511     $docdir = C4::Context->config('intranetdir') . '/docs';
512 }
513
514 if ( open( my $file, "<:encoding(UTF-8)", "$docdir" . "/history.txt" ) ) {
515
516     my $i = 0;
517
518     my @rows2 = ();
519     my $row2  = [];
520
521     my @lines = <$file>;
522     close($file);
523
524     shift @lines; #remove header row
525
526     foreach (@lines) {
527         my ( $epoch, $date, $desc, $tag ) = split(/\t/);
528         if(!$desc && $date=~ /(?<=\d{4})\s+/) {
529             ($date, $desc)= ($`, $');
530         }
531         push(
532             @rows2,
533             {
534                 date => $date,
535                 desc => $desc,
536             }
537         );
538     }
539
540     my $table2 = [];
541     #foreach my $row2 (@rows2) {
542     foreach  (@rows2) {
543         push (@$row2, $_);
544         push( @$table2, { row2 => $row2 } );
545         $row2 = [];
546     }
547
548     $template->param( table2 => $table2 );
549 } else {
550     $template->param( timeline_read_error => 1 );
551 }
552
553 output_html_with_http_headers $query, $cookie, $template->output;