You need to sign in or sign up before continuing.
Update.pm 6.94 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

package Bugzilla::Update;

use strict;

12
use Bugzilla::Constants;
13

14
use constant TIME_INTERVAL => 86400; # Default is one day, in seconds.
15 16 17 18
use constant TIMEOUT       => 5; # Number of seconds before timeout.

# Look for new releases and notify logged in administrators about them.
sub get_notifications {
19
    return if !Bugzilla->feature('updates');
20 21
    return if (Bugzilla->params->{'upgrade_notification'} eq 'disabled');

22
    my $local_file = bz_locations()->{'datadir'} . '/' . LOCAL_FILE;
23 24 25
    # Update the local XML file if this one doesn't exist or if
    # the last modification time (stat[9]) is older than TIME_INTERVAL.
    if (!-e $local_file || (time() - (stat($local_file))[9] > TIME_INTERVAL)) {
26
        unlink $local_file; # Make sure the old copy is away.
27 28
        return { 'error' => 'no_update' } if (-e $local_file);

29 30 31
        my $error = _synchronize_data();
        # If an error is returned, leave now.
        return $error if $error;
32 33 34
    }

    # If we cannot access the local XML file, ignore it.
35
    return { 'error' => 'no_access' } unless (-r $local_file);
36 37 38 39

    my $twig = XML::Twig->new();
    $twig->safe_parsefile($local_file);
    # If the XML file is invalid, return.
40
    return { 'error' => 'corrupted' } if $@;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    my $root = $twig->root;

    my @releases;
    foreach my $branch ($root->children('branch')) {
        my $release = {
            'branch_ver' => $branch->{'att'}->{'id'},
            'latest_ver' => $branch->{'att'}->{'vid'},
            'status'     => $branch->{'att'}->{'status'},
            'url'        => $branch->{'att'}->{'url'},
            'date'       => $branch->{'att'}->{'date'}
        };
        push(@releases, $release);
    }

    # On which branch is the current installation running?
    my @current_version =
57
        (BUGZILLA_VERSION =~ m/^(\d+)\.(\d+)(?:(rc|\.)(\d+))?\+?$/);
58 59 60 61

    my @release;
    if (Bugzilla->params->{'upgrade_notification'} eq 'development_snapshot') {
        @release = grep {$_->{'status'} eq 'development'} @releases;
62 63 64 65 66
        # If there is no development snapshot available, then we are in the
        # process of releasing a release candidate. That's the release we want.
        unless (scalar(@release)) {
            @release = grep {$_->{'status'} eq 'release-candidate'} @releases;
        }
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    }
    elsif (Bugzilla->params->{'upgrade_notification'} eq 'latest_stable_release') {
        @release = grep {$_->{'status'} eq 'stable'} @releases;
    }
    elsif (Bugzilla->params->{'upgrade_notification'} eq 'stable_branch_release') {
        # We want the latest stable version for the current branch.
        # If we are running a development snapshot, we won't match anything.
        my $branch_version = $current_version[0] . '.' . $current_version[1];

        # We do a string comparison instead of a numerical one, because
        # e.g. 2.2 == 2.20, but 2.2 ne 2.20 (and 2.2 is indeed much older).
        @release = grep {$_->{'branch_ver'} eq $branch_version} @releases;

        # If the branch is now closed, we should strongly suggest
        # to upgrade to the latest stable release available.
        if (scalar(@release) && $release[0]->{'status'} eq 'closed') {
            @release = grep {$_->{'status'} eq 'stable'} @releases;
            return {'data' => $release[0], 'deprecated' => $branch_version};
        }
    }
    else {
      # Unknown parameter.
      return {'error' => 'unknown_parameter'};
    }

    # Return if no new release is available.
    return unless scalar(@release);

    # Only notify the administrator if the latest version available
    # is newer than the current one.
    my @new_version =
        ($release[0]->{'latest_ver'} =~ m/^(\d+)\.(\d+)(?:(rc|\.)(\d+))?\+?$/);

    # We convert release candidates 'rc' to integers (rc ? 0 : 1) in order
    # to compare versions easily.
    $current_version[2] = ($current_version[2] && $current_version[2] eq 'rc') ? 0 : 1;
    $new_version[2] = ($new_version[2] && $new_version[2] eq 'rc') ? 0 : 1;

    my $is_newer = _compare_versions(\@current_version, \@new_version);
    return ($is_newer == 1) ? {'data' => $release[0]} : undef;
}

sub _synchronize_data {
110
    my $local_file = bz_locations()->{'datadir'} . '/' . LOCAL_FILE;
111 112 113 114

    my $ua = LWP::UserAgent->new();
    $ua->timeout(TIMEOUT);
    $ua->protocols_allowed(['http', 'https']);
115 116 117 118 119 120 121 122 123
    # If the URL of the proxy is given, use it, else get this information
    # from the environment variable.
    my $proxy_url = Bugzilla->params->{'proxy_url'};
    if ($proxy_url) {
        $ua->proxy(['http', 'https'], $proxy_url);
    }
    else {
        $ua->env_proxy;
    }
124
    my $response = eval { $ua->mirror(REMOTE_FILE, $local_file) };
125 126 127 128 129 130

    # $ua->mirror() forces the modification time of the local XML file
    # to match the modification time of the remote one.
    # So we have to update it manually to reflect that a newer version
    # of the file has effectively been requested. This will avoid
    # any new download for the next TIME_INTERVAL.
131 132 133 134
    if (-e $local_file) {
        # Try to alter its last modification time.
        my $can_alter = utime(undef, undef, $local_file);
        # This error should never happen.
135
        $can_alter || return { 'error' => 'no_update' };
136
    }
137
    elsif ($response && $response->is_error) {
138
        # We have been unable to download the file.
139 140 141 142
        return { 'error' => 'cannot_download', 'reason' => $response->status_line };
    }
    else {
        return { 'error' => 'no_write', 'reason' => $@ };
143 144 145 146
    }

    # Everything went well.
    return 0;
147 148 149 150 151
}

sub _compare_versions {
    my ($old_ver, $new_ver) = @_;
    while (scalar(@$old_ver) && scalar(@$new_ver)) {
152 153
        my $old = shift(@$old_ver) || 0;
        my $new = shift(@$new_ver) || 0;
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        return $new <=> $old if ($new <=> $old);
    }
    return scalar(@$new_ver) <=> scalar(@$old_ver);

}

1;

__END__

=head1 NAME

Bugzilla::Update - Update routines for Bugzilla

=head1 SYNOPSIS

  use Bugzilla::Update;

  # Get information about new releases
  my $new_release = Bugzilla::Update::get_notifications();

=head1 DESCRIPTION

This module contains all required routines to notify you
about new releases. It downloads an XML file from bugzilla.org
and parses it, in order to display information based on your
preferences. Absolutely no information about the Bugzilla version
you are running is sent to bugzilla.org.

=head1 FUNCTIONS

=over

=item C<get_notifications()>

 Description: This function informs you about new releases, if any.

 Params:      None.

 Returns:     On success, a reference to a hash with data about
              new releases, if any.
              On failure, a reference to a hash with the reason
              of the failure and the name of the unusable XML file.

=back

=cut