Requirements.pm 28.4 KB
Newer Older
1 2 3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
#
5 6
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
7 8 9 10 11 12 13 14 15

package Bugzilla::Install::Requirements;

# NOTE: This package MUST NOT "use" any Bugzilla modules other than
# Bugzilla::Constants, anywhere. We may "use" standard perl modules.
#
# Subroutines may "require" and "import" from modules, but they
# MUST NOT "use."

16
use 5.10.1;
17
use strict;
18
use warnings;
19

20
use Bugzilla::Constants;
21
use Bugzilla::Install::Util qw(install_string bin_loc
22
                               extension_requirement_packages);
23
use List::Util qw(max);
24
use Term::ANSIColor;
25

26
use parent qw(Exporter);
27 28
our @EXPORT = qw(
    REQUIRED_MODULES
29
    OPTIONAL_MODULES
30
    FEATURE_FILES
31

32
    check_requirements
33
    check_graphviz
34 35
    have_vers
    install_command
36
    map_files_to_features
37 38
);

39 40 41
# This is how many *'s are in the top of each "box" message printed
# by checksetup.pl.
use constant TABLE_WIDTH => 71;
42

43 44 45 46 47 48 49 50 51
# Optional Apache modules that have no Perl component to them.
# If these are installed, Bugzilla has additional functionality.
#
# The keys are the names of the modules, the values are what the module
# is called in the output of "apachectl -t -D DUMP_MODULES".
use constant APACHE_MODULES => { 
    mod_headers => 'headers_module',
    mod_env     => 'env_module',
    mod_expires => 'expires_module',
52 53
    mod_rewrite => 'rewrite_module',
    mod_version => 'version_module'
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
};

# These are all of the binaries that we could possibly use that can
# give us info about which Apache modules are installed.
# If we can't use "apachectl", the "httpd" binary itself takes the same
# parameters. Note that on Debian and Gentoo, there is an "apache2ctl",
# but it takes different parameters on each of those two distros, so we
# don't use apache2ctl.
use constant APACHE => qw(apachectl httpd apache2 apache);

# If we don't find any of the above binaries in the normal PATH,
# these are extra places we look.
use constant APACHE_PATH => [qw(
    /usr/sbin 
    /usr/local/sbin
    /usr/libexec
    /usr/local/libexec
)];

73 74 75
# The below two constants are subroutines so that they can implement
# a hook. Other than that they are actually constants.

76 77 78 79
# "package" is the perl package we're checking for. "module" is the name
# of the actual module we load with "require" to see if the package is
# installed or not. "version" is the version we need, or 0 if we'll accept
# any version.
80 81 82 83
#
# "blacklist" is an arrayref of regular expressions that describe versions that
# are 'blacklisted'--that is, even if the version is high enough, Bugzilla
# will refuse to say that it's OK to run with that version.
84 85
sub REQUIRED_MODULES {
    my @modules = (
86
    {
87
        package => 'CGI.pm',
88
        module  => 'CGI',
89
        # 3.51 fixes a security problem that affects Bugzilla.
90
        # (bug 591165)
91
        version => '3.51',
92
    },
93 94 95 96 97
    {
        package => 'Digest-SHA',
        module  => 'Digest::SHA',
        version => 0
    },
98
    # 0.23 fixes incorrect handling of 1/2 & 3/4 timezones.
99
    {
100 101
        package => 'TimeDate',
        module  => 'Date::Format',
102
        version => '2.23'
103
    },
104
    # 0.75 fixes a warning thrown with Perl 5.17 and newer.
105 106 107
    {
        package => 'DateTime',
        module  => 'DateTime',
108
        version => '0.75'
109
    },
110 111
    # 1.64 fixes a taint issue preventing the local timezone from
    # being determined on some systems.
112 113 114
    {
        package => 'DateTime-TimeZone',
        module  => 'DateTime::TimeZone',
115
        version => '1.64'
116
    },
117
    # 1.54 is required for Perl 5.10+. It also makes DBD::Oracle happy.
118 119 120
    {
        package => 'DBI',
        module  => 'DBI',
121
        version => ($^V >= v5.13.3) ? '1.614' : '1.54'
122
    },
123
    # 2.24 contains several useful text virtual methods.
124
    {
125 126
        package => 'Template-Toolkit',
        module  => 'Template',
127
        version => '2.24'
128
    },
129
    # 1.300011 has a debug mode for SMTP and automatically pass -i to sendmail.
130
    {
131 132 133
        package => 'Email-Sender',
        module  => 'Email::Sender',
        version => '1.300011',
134 135
    },
    {
136 137
        package => 'Email-MIME',
        module  => 'Email::MIME',
138 139
        # This fixes a memory leak in walk_parts that affected jobqueue.pl.
        version => '1.904'
140
    },
141 142 143
    {
        package => 'URI',
        module  => 'URI',
144 145
        # Follows RFC 3986 to escape characters in URI::Escape.
        version => '1.55',
146
    },
147
    # 0.32 fixes several memory leaks in the XS version of some functions.
148 149 150
    {
        package => 'List-MoreUtils',
        module  => 'List::MoreUtils',
151
        version => 0.32,
152
    },
153
    {
154 155 156
        package => 'Math-Random-ISAAC',
        module  => 'Math::Random::ISAAC',
        version => '1.0.1',
157
    },
158 159 160 161 162 163
    {
        package => 'JSON-XS',
        module  => 'JSON::XS',
        # 2.0 is the first version that will work with JSON::RPC.
        version => '2.01',
    },
164 165
    );

166
    if (ON_WINDOWS) {
167 168
        push(@modules,
        {
169 170 171 172 173 174 175 176 177 178
            package => 'Win32',
            module  => 'Win32',
            # 0.35 fixes a memory leak in GetOSVersion, which we use.
            version => 0.35,
        }, 
        {
            package => 'Win32-API',
            module  => 'Win32::API',
            # 0.55 fixes a bug with char* that might affect Bugzilla::RNG.
            version => '0.55',
179 180 181 182 183 184 185 186
        },
        {
            package => 'DateTime-TimeZone-Local-Win32',
            module  => 'DateTime::TimeZone::Local::Win32',
            # We require DateTime::TimeZone 1.64, so this version must match.
            version => '1.64',
        }
        );
187 188
    }

189 190 191
    my $extra_modules = _get_extension_requirements('REQUIRED_MODULES');
    push(@modules, @$extra_modules);
    return \@modules;
192
};
193

194 195
sub OPTIONAL_MODULES {
    my @modules = (
196
    {
197 198 199
        package => 'GD',
        module  => 'GD',
        version => '1.20',
200
        feature => [qw(graphical_reports new_charts old_charts)],
201
    },
202 203
    {
        package => 'Chart',
204
        module  => 'Chart::Lines',
205 206 207
        # Versions below 2.4.1 cannot be compared accurately, see
        # https://rt.cpan.org/Public/Bug/Display.html?id=28218.
        version => '2.4.1',
208
        feature => [qw(new_charts old_charts)],
209
    },
210
    {
211
        package => 'Template-GD',
212 213
        # This module tells us whether or not Template-GD is installed
        # on Template-Toolkits after 2.14, and still works with 2.14 and lower.
214 215
        module  => 'Template::Plugin::GD::Image',
        version => 0,
216
        feature => ['graphical_reports'],
217
    },
218
    {
219 220
        package => 'GDTextUtil',
        module  => 'GD::Text',
221
        version => 0,
222
        feature => ['graphical_reports'],
223
    },
224 225 226
    {
        package => 'GDGraph',
        module  => 'GD::Graph',
227
        version => 0,
228
        feature => ['graphical_reports'],
229
    },
230 231 232 233 234
    {
        package => 'MIME-tools',
        # MIME::Parser is packaged as MIME::Tools on ActiveState Perl
        module  => ON_WINDOWS ? 'MIME::Tools' : 'MIME::Parser',
        version => '5.406',
235
        feature => ['moving'],
236
    },
237
    {
238 239 240
        package => 'libwww-perl',
        module  => 'LWP::UserAgent',
        version => 0,
241
        feature => ['updates'],
242
    },
243 244 245 246 247 248
    {
        package => 'XML-Twig',
        module  => 'XML::Twig',
        version => 0,
        feature => ['moving', 'updates'],
    },
249
    {
250 251
        package => 'PatchReader',
        module  => 'PatchReader',
252 253
        # 0.9.6 fixes two notable bugs and significantly improves the UX.
        version => '0.9.6',
254
        feature => ['patch_viewer'],
255 256
    },
    {
257 258 259
        package => 'perl-ldap',
        module  => 'Net::LDAP',
        version => 0,
260
        feature => ['auth_ldap'],
261
    },
262
    {
263
        package => 'Authen-SASL',
264 265
        module  => 'Authen::SASL',
        version => 0,
266
        feature => ['smtp_auth'],
267
    },
268 269 270 271 272 273
    {
        package => 'Net-SMTP-SSL',
        module  => 'Net::SMTP::SSL',
        version => 1.01,
        feature => ['smtp_ssl'],
    },
274 275 276 277
    {
        package => 'RadiusPerl',
        module  => 'Authen::Radius',
        version => 0,
278
        feature => ['auth_radius'],
279
    },
280 281
    # XXX - Once we require XMLRPC::Lite 0.717 or higher, we can
    # remove SOAP::Lite from the list.
282
    {
283 284
        package => 'SOAP-Lite',
        module  => 'SOAP::Lite',
285 286 287
        # Fixes various bugs, including 542931 and 552353 + stops
        # throwing warnings with Perl 5.12.
        version => '0.712',
288 289
        # SOAP::Transport::HTTP 1.12 is bogus.
        blacklist => ['^1\.12$'],
290
        feature => ['xmlrpc'],
291
    },
292 293 294 295 296 297 298 299
    # Since SOAP::Lite 1.0, XMLRPC::Lite is no longer included
    # and so it must be checked separately.
    {
        package => 'XMLRPC-Lite',
        module  => 'XMLRPC::Lite',
        version => '0.712',
        feature => ['xmlrpc'],
    },
300 301 302 303
    {
        package => 'JSON-RPC',
        module  => 'JSON::RPC',
        version => 0,
304
        feature => ['jsonrpc', 'rest'],
305
    },
306 307 308
    {
        package => 'Test-Taint',
        module  => 'Test::Taint',
309 310
        # 1.06 no longer throws warnings with Perl 5.10+.
        version => 1.06,
311
        feature => ['jsonrpc', 'xmlrpc', 'rest'],
312
    },
313
    {
314 315 316
        # We need the 'utf8_mode' method of HTML::Parser, for HTML::Scrubber.
        package => 'HTML-Parser',
        module  => 'HTML::Parser',
317
        version => ($^V >= v5.13.3) ? '3.67' : '3.40',
318
        feature => ['html_desc'],
319 320
    },
    {
321 322 323
        package => 'HTML-Scrubber',
        module  => 'HTML::Scrubber',
        version => 0,
324
        feature => ['html_desc'],
325
    },
326 327 328 329 330 331 332 333 334 335 336 337 338
    {
        # we need version 2.21 of Encode for mime_name
        package => 'Encode',
        module  => 'Encode',
        version => 2.21,
        feature => ['detect_charset'],
    },
    {
        package => 'Encode-Detect',
        module  => 'Encode::Detect',
        version => 0,
        feature => ['detect_charset'],
    },
339 340 341 342 343 344

    # Inbound Email
    {
        package => 'Email-Reply',
        module  => 'Email::Reply',
        version => 0,
345
        feature => ['inbound_email'],
346
    },
347 348 349 350 351 352 353
    {
        package => 'HTML-FormatText-WithLinks',
        module  => 'HTML::FormatText::WithLinks',
        # We need 0.13 to set the "bold" marker to "*".
        version => '0.13',
        feature => ['inbound_email'],
    },
354

355 356 357 358
    # Mail Queueing
    {
        package => 'TheSchwartz',
        module  => 'TheSchwartz',
359 360
        # 1.07 supports the prioritization of jobs.
        version => 1.07,
361
        feature => ['jobqueue'],
362 363 364 365 366
    },
    {
        package => 'Daemon-Generic',
        module  => 'Daemon::Generic',
        version => 0,
367
        feature => ['jobqueue'],
368 369
    },

370
    # mod_perl
371
    {
372 373 374
        package => 'mod_perl',
        module  => 'mod_perl2',
        version => '1.999022',
375
        feature => ['mod_perl'],
376
    },
377 378 379
    {
        package => 'Apache-SizeLimit',
        module  => 'Apache2::SizeLimit',
380 381
        # 0.96 properly determines process size on Linux.
        version => '0.96',
382 383
        feature => ['mod_perl'],
    },
384 385 386 387 388 389 390 391 392 393 394 395 396 397

    # typesniffer
    {
        package => 'File-MimeInfo',
        module  => 'File::MimeInfo::Magic',
        version => '0',
        feature => ['typesniffer'],
    },
    {
        package => 'IO-stringy',
        module  => 'IO::Scalar',
        version => '0',
        feature => ['typesniffer'],
    },
398 399 400 401 402 403 404 405

    # memcached
    {
        package => 'Cache-Memcached',
        module  => 'Cache::Memcached',
        version => '0',
        feature => ['memcached'],
    },
406 407 408 409 410 411 412

    # Documentation
    {
        package => 'File-Copy-Recursive',
        module  => 'File::Copy::Recursive',
        version => 0,
        feature => ['documentation'],
413 414 415 416 417 418 419
    },
    {
        package => 'File-Which',
        module  => 'File::Which',
        version => 0,
        feature => ['documentation'],
    },
420 421
    );

422 423 424
    my $extra_modules = _get_extension_requirements('OPTIONAL_MODULES');
    push(@modules, @$extra_modules);
    return \@modules;
425 426
};

427 428 429 430 431 432
# This maps features to the files that require that feature in order
# to compile. It is used by t/001compile.t and mod_perl.pl.
use constant FEATURE_FILES => (
    jsonrpc       => ['Bugzilla/WebService/Server/JSONRPC.pm', 'jsonrpc.cgi'],
    xmlrpc        => ['Bugzilla/WebService/Server/XMLRPC.pm', 'xmlrpc.cgi',
                      'Bugzilla/WebService.pm', 'Bugzilla/WebService/*.pm'],
433 434
    rest          => ['Bugzilla/WebService/Server/REST.pm', 'rest.cgi',
                      'Bugzilla/WebService/Server/REST/Resources/*.pm'],
435 436 437
    moving        => ['importxml.pl'],
    auth_ldap     => ['Bugzilla/Auth/Verify/LDAP.pm'],
    auth_radius   => ['Bugzilla/Auth/Verify/RADIUS.pm'],
438
    documentation => ['docs/makedocs.pl'],
439 440 441 442 443
    inbound_email => ['email_in.pl'],
    jobqueue      => ['Bugzilla/Job/*', 'Bugzilla/JobQueue.pm',
                      'Bugzilla/JobQueue/*', 'jobqueue.pl'],
    patch_viewer  => ['Bugzilla/Attachment/PatchReader.pm'],
    updates       => ['Bugzilla/Update.pm'],
444
    memcached     => ['Bugzilla/Memcache.pm'],
445 446
);

447 448
# This implements the REQUIRED_MODULES and OPTIONAL_MODULES stuff
# described in in Bugzilla::Extension.
449
sub _get_extension_requirements {
450 451 452 453 454 455 456 457
    my ($function) = @_;

    my $packages = extension_requirement_packages();
    my @modules;
    foreach my $package (@$packages) {
        if ($package->can($function)) {
            my $extra_modules = $package->$function;
            push(@modules, @$extra_modules);
458 459
        }
    }
460
    return \@modules;
461
};
462

463 464 465
sub check_requirements {
    my ($output) = @_;

466
    print "\n", install_string('checking_modules'), "\n" if $output;
467
    my $root = ROOT_USER;
468
    my $missing = _check_missing(REQUIRED_MODULES, $output);
469

470
    print "\n", install_string('checking_dbd'), "\n" if $output;
471 472 473
    my $have_one_dbd = 0;
    my $db_modules = DB_MODULE;
    foreach my $db (keys %$db_modules) {
474 475
        my $dbd = $db_modules->{$db}->{dbd};
        $have_one_dbd = 1 if have_vers($dbd, $output);
476 477
    }

478
    print "\n", install_string('checking_optional'), "\n" if $output;
479
    my $missing_optional = _check_missing(OPTIONAL_MODULES, $output);
480

481 482
    my $missing_apache = _missing_apache_modules(APACHE_MODULES, $output);

483 484 485 486
    # If we're running on Windows, reset the input line terminator so that
    # console input works properly - loading CGI tends to mess it up
    $/ = "\015\012" if ON_WINDOWS;

487
    my $pass = !scalar(@$missing) && $have_one_dbd;
488 489 490
    return {
        pass     => $pass,
        one_dbd  => $have_one_dbd,
491 492
        missing  => $missing,
        optional => $missing_optional,
493
        apache   => $missing_apache,
494
        any_missing => !$pass || scalar(@$missing_optional),
495 496
    };
}
497

498 499 500
# A helper for check_requirements
sub _check_missing {
    my ($modules, $output) = @_;
501

502
    my @missing;
503 504
    foreach my $module (@$modules) {
        unless (have_vers($module, $output)) {
505
            push(@missing, $module);
506
        }
507
    }
508

509
    return \@missing;
510
}
511

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
sub _missing_apache_modules {
    my ($modules, $output) = @_;
    my $apachectl = _get_apachectl();
    return [] if !$apachectl;
    my $command = "$apachectl -t -D DUMP_MODULES";
    my $cmd_info = `$command 2>&1`;
    # If apachectl returned a value greater than 0, then there was an
    # error parsing Apache's configuration, and we can't check modules.
    my $retval = $?;
    if ($retval > 0) {
        print STDERR install_string('apachectl_failed', 
            { command => $command, root => ROOT_USER }), "\n";
        return [];
    }
    my @missing;
527
    foreach my $module (sort keys %$modules) {
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
        my $ok = _check_apache_module($module, $modules->{$module}, 
                                      $cmd_info, $output);
        push(@missing, $module) if !$ok;
    }
    return \@missing;
}

sub _get_apachectl {
    foreach my $bin_name (APACHE) {
        my $bin = bin_loc($bin_name);
        return $bin if $bin;
    }
    # Try again with a possibly different path.
    foreach my $bin_name (APACHE) {
        my $bin = bin_loc($bin_name, APACHE_PATH);
        return $bin if $bin;
    }
    return undef;
}

sub _check_apache_module {
    my ($module, $config_name, $mod_info, $output) = @_;
    my $ok;
    if ($mod_info =~ /^\s+\Q$config_name\E\b/m) {
        $ok = 1;
    }
    if ($output) {
        _checking_for({ package => $module, ok => $ok });
    }
    return $ok;
}

560 561
sub print_module_instructions {
    my ($check_results, $output) = @_;
562

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
    # First we print the long explanatory messages.

    if (scalar @{$check_results->{missing}}) {
        print install_string('modules_message_required');
    }

    if (!$check_results->{one_dbd}) {
        print install_string('modules_message_db');
    }

    if (my @missing = @{$check_results->{optional}} and $output) {
        print install_string('modules_message_optional');
        # Now we have to determine how large the table cols will be.
        my $longest_name = max(map(length($_->{package}), @missing));

        # The first column header is at least 11 characters long.
        $longest_name = 11 if $longest_name < 11;

        # The table is TABLE_WIDTH characters long. There are seven mandatory
        # characters (* and space) in the string. So, we have a total
        # of TABLE_WIDTH - 7 characters to work with.
        my $remaining_space = (TABLE_WIDTH - 7) - $longest_name;
        print '*' x TABLE_WIDTH . "\n";
        printf "* \%${longest_name}s * %-${remaining_space}s *\n",
               'MODULE NAME', 'ENABLES FEATURE(S)';
        print '*' x TABLE_WIDTH . "\n";
        foreach my $package (@missing) {
            printf "* \%${longest_name}s * %-${remaining_space}s *\n",
591 592
                   $package->{package}, 
                   _translate_feature($package->{feature});
593 594 595
        }
    }

596 597 598 599 600 601 602 603 604 605 606 607 608 609
    if (my @missing = @{ $check_results->{apache} }) {
        print install_string('modules_message_apache');
        my $missing_string = join(', ', @missing);
        my $size = TABLE_WIDTH - 7;
        printf "*    \%-${size}s *\n", $missing_string;
        my $spaces = TABLE_WIDTH - 2;
        print "*", (' ' x $spaces), "*\n";
    }

    my $need_module_instructions =  
        ( (!$output and @{$check_results->{missing}})
          or ($output and $check_results->{any_missing}) ) ? 1 : 0;

    if ($need_module_instructions or @{ $check_results->{apache} }) {
610 611
        # If any output was required, we want to close the "table"
        print "*" x TABLE_WIDTH . "\n";
612 613
    }

614 615 616 617 618 619 620
    # And now we print the actual installation commands.

    if (my @missing = @{$check_results->{optional}} and $output) {
        print install_string('commands_optional') . "\n\n";
        foreach my $module (@missing) {
            my $command = install_command($module);
            printf "%15s: $command\n", $module->{package};
621 622 623 624
        }
        print "\n";
    }

625
    if (!$check_results->{one_dbd}) {
626
        print install_string('commands_dbd') . "\n";
627 628
        my %db_modules = %{DB_MODULE()};
        foreach my $db (keys %db_modules) {
629
            my $command = install_command($db_modules{$db}->{dbd});
630
            printf "%10s: \%s\n", $db_modules{$db}->{name}, $command;
631 632 633 634
        }
        print "\n";
    }

635
    if (my @missing = @{$check_results->{missing}}) {
636
        print colored(install_string('commands_required'), COLOR_ERROR), "\n";
637
        foreach my $package (@missing) {
638 639
            my $command = install_command($package);
            print "    $command\n";
640 641
        }
    }
642

643
    if ($output && $check_results->{any_missing} && !ON_ACTIVESTATE
644 645
        && !$check_results->{hide_all}) 
    {
646 647
        print install_string('install_all', { perl => $^X });
    }
648
    if (!$check_results->{pass}) {
649 650
        print colored(install_string('installation_failed'), COLOR_ERROR),
              "\n\n";
651
    }
652 653
}

654 655 656 657 658 659 660 661 662
sub _translate_feature {
    my $features = shift;
    my @strings;
    foreach my $feature (@$features) {
        push(@strings, install_string("feature_$feature"));
    }
    return join(', ', @strings);
}

663 664 665
sub check_graphviz {
    my ($output) = @_;

666 667
    my $webdotbase = Bugzilla->params->{'webdotbase'};
    return 1 if $webdotbase =~ /^https?:/;
668

669 670 671 672 673 674
    my $return;
    $return = 1 if -x $webdotbase;

    if ($output) {
        _checking_for({ package => 'GraphViz', ok => $return });
    }
675

676
    if (!$return) {
677
        print install_string('bad_executable', { bin => $webdotbase }), "\n";
678 679 680 681 682 683 684 685
    }

    my $webdotdir = bz_locations()->{'webdotdir'};
    # Check .htaccess allows access to generated images
    if (-e "$webdotdir/.htaccess") {
        my $htaccess = new IO::File("$webdotdir/.htaccess", 'r') 
            || die "$webdotdir/.htaccess: " . $!;
        if (!grep(/png/, $htaccess->getlines)) {
686 687
            print STDERR install_string('webdot_bad_htaccess',
                                        { dir => $webdotdir }), "\n";
688 689 690 691 692 693 694
        }
        $htaccess->close;
    }

    return $return;
}

695 696
# This was originally clipped from the libnet Makefile.PL, adapted here for
# accurate version checking.
697
sub have_vers {
698 699 700 701 702 703 704 705 706 707
    my ($params, $output) = @_;
    my $module  = $params->{module};
    my $package = $params->{package};
    if (!$package) {
        $package = $module;
        $package =~ s/::/-/g;
    }
    my $wanted  = $params->{version};

    eval "require $module;";
708 709 710 711
    # Don't let loading a module change the output-encoding of STDOUT
    # or STDERR. (CGI.pm tries to set "binmode" on these file handles when
    # it's loaded, and other modules may do the same in the future.)
    Bugzilla::Install::Util::set_output_encoding();
712

713
    # VERSION is provided by UNIVERSAL::, and can be called even if
714 715 716 717 718 719 720
    # the module isn't loaded. We eval'uate ->VERSION because it can die
    # when the version is not valid (yes, this happens from time to time).
    # In that case, we use an uglier method to get the version.
    my $vnum = eval { $module->VERSION };
    if ($@) {
        no strict 'refs';
        $vnum = ${"${module}::VERSION"};
721

722 723 724 725 726
        # If we come here, then the version is not a valid one.
        # We try to sanitize it.
        if ($vnum =~ /^((\d+)(\.\d+)*)/) {
            $vnum = $1;
        }
727
    }
728
    $vnum ||= -1;
729

730 731
    # Must do a string comparison as $vnum may be of the form 5.10.1.
    my $vok = ($vnum ne '-1' && version->new($vnum) >= version->new($wanted)) ? 1 : 0;
732 733 734 735 736 737
    my $blacklisted;
    if ($vok && $params->{blacklist}) {
        $blacklisted = grep($vnum =~ /$_/, @{$params->{blacklist}});
        $vok = 0 if $blacklisted;
    }

738
    if ($output) {
739 740 741 742
        _checking_for({ 
            package => $package, ok => $vok, wanted => $wanted,
            found   => $vnum, blacklisted => $blacklisted
        });
743 744
    }
    
745 746 747
    return $vok ? 1 : 0;
}

748 749 750 751 752 753 754 755 756 757 758 759 760
sub _checking_for {
    my ($params) = @_;
    my ($package, $ok, $wanted, $blacklisted, $found) = 
        @$params{qw(package ok wanted blacklisted found)};

    my $ok_string = $ok ? install_string('module_ok') : '';

    # If we're actually checking versions (like for Perl modules), then
    # we have some rather complex logic to determine what we want to 
    # show. If we're not checking versions (like for GraphViz) we just
    # show "ok" or "not found".
    if (exists $params->{found}) {
        my $found_string;
761 762 763
        # We do a string compare in case it's non-numeric. We make sure
        # it's not a version object as negative versions are forbidden.
        if ($found && !ref($found) && $found eq '-1') {
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
            $found_string = install_string('module_not_found');
        }
        elsif ($found) {
            $found_string = install_string('module_found', { ver => $found });
        }
        else {
            $found_string = install_string('module_unknown_version');
        }
        $ok_string = $ok ? "$ok_string: $found_string" : $found_string;
    }
    elsif (!$ok) {
        $ok_string = install_string('module_not_found');
    }

    my $black_string = $blacklisted ? install_string('blacklisted') : '';
    my $want_string  = $wanted ? "v$wanted" : install_string('any');

    my $str = sprintf "%s %20s %-11s $ok_string $black_string\n",
                install_string('checking_for'), $package, "($want_string)";
    print $ok ? $str : colored($str, COLOR_ERROR);
}

786 787
sub install_command {
    my $module = shift;
788 789
    my ($command, $package);

790
    if (ON_ACTIVESTATE) {
791 792 793 794
        $command = 'ppm install %s';
        $package = $module->{package};
    }
    else {
795
        $command = "$^X install-module.pl \%s";
796 797 798
        # Non-Windows installations need to use module names, because
        # CPAN doesn't understand package names.
        $package = $module->{module};
799
    }
800
    return sprintf $command, $package;
801 802
}

803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
# This does a reverse mapping for FEATURE_FILES.
sub map_files_to_features {
    my %features = FEATURE_FILES;
    my %files;
    foreach my $feature (keys %features) {
        my @my_files = @{ $features{$feature} };
        foreach my $pattern (@my_files) {
            foreach my $file (glob $pattern) {
                $files{$file} = $feature;
            }
        }
    }
    return \%files;
}

818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
1;

__END__

=head1 NAME

Bugzilla::Install::Requirements - Functions and variables dealing
  with Bugzilla's perl-module requirements.

=head1 DESCRIPTION

This module is used primarily by C<checksetup.pl> to determine whether
or not all of Bugzilla's prerequisites are installed. (That is, all the
perl modules it requires.)

=head1 CONSTANTS

835
=over
836 837 838 839

=item C<REQUIRED_MODULES>

An arrayref of hashrefs that describes the perl modules required by 
840 841 842 843 844 845 846 847 848 849 850 851
Bugzilla. The hashes have three keys: 

=over

=item C<package> - The name of the Perl package that you'd find on
CPAN for this requirement. 

=item C<module> - The name of a module that can be passed to the
C<install> command in C<CPAN.pm> to install this module.

=item C<version> - The version of this module that we require, or C<0>
if any version is acceptable.
852 853 854

=back

855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
=item C<OPTIONAL_MODULES>

An arrayref of hashrefs that describes the perl modules that add
additional features to Bugzilla if installed. Its hashes have all
the fields of L</REQUIRED_MODULES>, plus a C<feature> item--an arrayref
of strings that describe what features require this module.

=item C<FEATURE_FILES>

A hashref that describes what files should only be compiled if a certain
feature is enabled. The feature is the key, and the values are arrayrefs
of file names (which are passed to C<glob>, so shell patterns work).

=back


871 872 873 874
=head1 SUBROUTINES

=over 4

875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
=item C<check_requirements>

=over

=item B<Description>

This checks what optional or required perl modules are installed, like
C<checksetup.pl> does.

=item B<Params>

=over

=item C<$output> - C<true> if you want the function to print out information
about what it's doing, and the versions of everything installed.

=back

=item B<Returns>

A hashref containing these values:

=over

=item C<pass> - Whether or not we have all the mandatory requirements.

=item C<missing> - An arrayref containing any required modules that
are not installed or that are not up-to-date. Each item in the array is
a hashref in the format of items from L</REQUIRED_MODULES>.

=item C<optional> - The same as C<missing>, but for optional modules.

907 908
=item C<apache> - The name of each optional Apache module that is missing.

909 910
=item C<have_one_dbd> - True if at least one C<DBD::> module is installed.

911 912
=item C<any_missing> - True if there are any missing Perl modules, even
optional modules.
913 914 915 916

=back

=back
917

918 919 920 921 922 923 924 925 926 927
=item C<check_graphviz($output)>

Description: Checks if the graphviz binary specified in the 
  C<webdotbase> parameter is a valid binary, or a valid URL.

Params:      C<$output> - C<$true> if you want the function to
                 print out information about what it's doing.

Returns:     C<1> if the check was successful, C<0> otherwise.

928
=item C<have_vers($module, $output)>
929 930 931 932 933 934

 Description: Tells you whether or not you have the appropriate
              version of the module requested. It also prints
              out a message to the user explaining the check
              and the result.

935 936
 Params:      C<$module> - A hashref, in the format of an item from 
                           L</REQUIRED_MODULES>.
937 938 939
              C<$output> - Set to true if you want this function to
                           print information to STDOUT about what it's
                           doing.
940 941 942 943 944 945 946 947 948 949

 Returns:   C<1> if you have the module installed and you have the
            appropriate version. C<0> otherwise.

=item C<install_command($module)>

 Description: Prints out the appropriate command to install the
              module specified, depending on whether you're
              on Windows or Linux.

950 951
 Params:      C<$module> - A hashref, in the format of an item from
                           L</REQUIRED_MODULES>.
952 953 954

 Returns:     nothing

955 956 957 958 959
=item C<map_files_to_features>

Returns a hashref where file names are the keys and the value is the feature
that must be enabled in order to compile that file.

960
=back
961 962 963 964 965 966 967 968

=head1 B<Methods in need of POD>

=over

=item print_module_instructions

=back