~ubuntu-branches/ubuntu/oneiric/bugzilla/oneiric

« back to all changes in this revision

Viewing changes to Bugzilla/Update.pm

  • Committer: Bazaar Package Importer
  • Author(s): Raphael Bossek
  • Date: 2008-06-27 22:34:34 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20080627223434-0ib57vstn43bb4a3
Tags: 3.0.4.1-1
* Update of French, Russian and German translations. (closes: #488251)
* Added Bulgarian and Belarusian translations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- Mode: perl; indent-tabs-mode: nil -*-
2
 
#
3
 
# The contents of this file are subject to the Mozilla Public
4
 
# License Version 1.1 (the "License"); you may not use this file
5
 
# except in compliance with the License. You may obtain a copy of
6
 
# the License at http://www.mozilla.org/MPL/
7
 
#
8
 
# Software distributed under the License is distributed on an "AS
9
 
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
 
# implied. See the License for the specific language governing
11
 
# rights and limitations under the License.
12
 
#
13
 
# The Original Code is the Bugzilla Bug Tracking System.
14
 
#
15
 
# Contributor(s): Frédéric Buclin <LpSolit@gmail.com>
16
 
 
17
 
package Bugzilla::Update;
18
 
 
19
 
use strict;
20
 
 
21
 
use Bugzilla::Constants;
22
 
 
23
 
use constant REMOTE_FILE   => 'http://updates.bugzilla.org/bugzilla-update.xml';
24
 
use constant LOCAL_FILE    => "/bugzilla-update.xml"; # Relative to datadir.
25
 
use constant TIME_INTERVAL => 86400; # Default is one day, in seconds.
26
 
use constant TIMEOUT       => 5; # Number of seconds before timeout.
27
 
 
28
 
# Look for new releases and notify logged in administrators about them.
29
 
sub get_notifications {
30
 
    return if (Bugzilla->params->{'upgrade_notification'} eq 'disabled');
31
 
 
32
 
    # If the XML::Twig module is missing, we won't be able to parse
33
 
    # the XML file. So there is no need to go further.
34
 
    eval("require XML::Twig");
35
 
    return if $@;
36
 
 
37
 
    my $local_file = bz_locations()->{'datadir'} . LOCAL_FILE;
38
 
    # Update the local XML file if this one doesn't exist or if
39
 
    # the last modification time (stat[9]) is older than TIME_INTERVAL.
40
 
    if (!-e $local_file || (time() - (stat($local_file))[9] > TIME_INTERVAL)) {
41
 
        # Are we sure we didn't try to refresh this file already
42
 
        # but we failed because we cannot modify its timestamp?
43
 
        my $can_alter = (-e $local_file) ? utime(undef, undef, $local_file) : 1;
44
 
        if ($can_alter) {
45
 
            unlink $local_file; # Make sure the old copy is away.
46
 
            my $error = _synchronize_data();
47
 
            # If an error is returned, leave now.
48
 
            return $error if $error;
49
 
        }
50
 
        else {
51
 
            return {'error' => 'no_update', 'xml_file' => $local_file};
52
 
        }
53
 
    }
54
 
 
55
 
    # If we cannot access the local XML file, ignore it.
56
 
    return {'error' => 'no_access', 'xml_file' => $local_file} unless (-r $local_file);
57
 
 
58
 
    my $twig = XML::Twig->new();
59
 
    $twig->safe_parsefile($local_file);
60
 
    # If the XML file is invalid, return.
61
 
    return {'error' => 'corrupted', 'xml_file' => $local_file} if $@;
62
 
    my $root = $twig->root;
63
 
 
64
 
    my @releases;
65
 
    foreach my $branch ($root->children('branch')) {
66
 
        my $release = {
67
 
            'branch_ver' => $branch->{'att'}->{'id'},
68
 
            'latest_ver' => $branch->{'att'}->{'vid'},
69
 
            'status'     => $branch->{'att'}->{'status'},
70
 
            'url'        => $branch->{'att'}->{'url'},
71
 
            'date'       => $branch->{'att'}->{'date'}
72
 
        };
73
 
        push(@releases, $release);
74
 
    }
75
 
 
76
 
    # On which branch is the current installation running?
77
 
    my @current_version =
78
 
        (BUGZILLA_VERSION =~ m/^(\d+)\.(\d+)(?:(rc|\.)(\d+))?\+?$/);
79
 
 
80
 
    my @release;
81
 
    if (Bugzilla->params->{'upgrade_notification'} eq 'development_snapshot') {
82
 
        @release = grep {$_->{'status'} eq 'development'} @releases;
83
 
        # If there is no development snapshot available, then we are in the
84
 
        # process of releasing a release candidate. That's the release we want.
85
 
        unless (scalar(@release)) {
86
 
            @release = grep {$_->{'status'} eq 'release-candidate'} @releases;
87
 
        }
88
 
    }
89
 
    elsif (Bugzilla->params->{'upgrade_notification'} eq 'latest_stable_release') {
90
 
        @release = grep {$_->{'status'} eq 'stable'} @releases;
91
 
    }
92
 
    elsif (Bugzilla->params->{'upgrade_notification'} eq 'stable_branch_release') {
93
 
        # We want the latest stable version for the current branch.
94
 
        # If we are running a development snapshot, we won't match anything.
95
 
        my $branch_version = $current_version[0] . '.' . $current_version[1];
96
 
 
97
 
        # We do a string comparison instead of a numerical one, because
98
 
        # e.g. 2.2 == 2.20, but 2.2 ne 2.20 (and 2.2 is indeed much older).
99
 
        @release = grep {$_->{'branch_ver'} eq $branch_version} @releases;
100
 
 
101
 
        # If the branch is now closed, we should strongly suggest
102
 
        # to upgrade to the latest stable release available.
103
 
        if (scalar(@release) && $release[0]->{'status'} eq 'closed') {
104
 
            @release = grep {$_->{'status'} eq 'stable'} @releases;
105
 
            return {'data' => $release[0], 'deprecated' => $branch_version};
106
 
        }
107
 
    }
108
 
    else {
109
 
      # Unknown parameter.
110
 
      return {'error' => 'unknown_parameter'};
111
 
    }
112
 
 
113
 
    # Return if no new release is available.
114
 
    return unless scalar(@release);
115
 
 
116
 
    # Only notify the administrator if the latest version available
117
 
    # is newer than the current one.
118
 
    my @new_version =
119
 
        ($release[0]->{'latest_ver'} =~ m/^(\d+)\.(\d+)(?:(rc|\.)(\d+))?\+?$/);
120
 
 
121
 
    # We convert release candidates 'rc' to integers (rc ? 0 : 1) in order
122
 
    # to compare versions easily.
123
 
    $current_version[2] = ($current_version[2] && $current_version[2] eq 'rc') ? 0 : 1;
124
 
    $new_version[2] = ($new_version[2] && $new_version[2] eq 'rc') ? 0 : 1;
125
 
 
126
 
    my $is_newer = _compare_versions(\@current_version, \@new_version);
127
 
    return ($is_newer == 1) ? {'data' => $release[0]} : undef;
128
 
}
129
 
 
130
 
sub _synchronize_data {
131
 
    eval("require LWP::UserAgent");
132
 
    return {'error' => 'missing_package', 'package' => 'LWP::UserAgent'} if $@;
133
 
 
134
 
    my $local_file = bz_locations()->{'datadir'} . LOCAL_FILE;
135
 
 
136
 
    my $ua = LWP::UserAgent->new();
137
 
    $ua->timeout(TIMEOUT);
138
 
    $ua->protocols_allowed(['http', 'https']);
139
 
    # If the URL of the proxy is given, use it, else get this information
140
 
    # from the environment variable.
141
 
    my $proxy_url = Bugzilla->params->{'proxy_url'};
142
 
    if ($proxy_url) {
143
 
        $ua->proxy(['http', 'https'], $proxy_url);
144
 
    }
145
 
    else {
146
 
        $ua->env_proxy;
147
 
    }
148
 
    $ua->mirror(REMOTE_FILE, $local_file);
149
 
 
150
 
    # $ua->mirror() forces the modification time of the local XML file
151
 
    # to match the modification time of the remote one.
152
 
    # So we have to update it manually to reflect that a newer version
153
 
    # of the file has effectively been requested. This will avoid
154
 
    # any new download for the next TIME_INTERVAL.
155
 
    if (-e $local_file) {
156
 
        # Try to alter its last modification time.
157
 
        my $can_alter = utime(undef, undef, $local_file);
158
 
        # This error should never happen.
159
 
        $can_alter || return {'error' => 'no_update', 'xml_file' => $local_file};
160
 
    }
161
 
    else {
162
 
        # We have been unable to download the file.
163
 
        return {'error' => 'cannot_download', 'xml_file' => $local_file};
164
 
    }
165
 
 
166
 
    # Everything went well.
167
 
    return 0;
168
 
}
169
 
 
170
 
sub _compare_versions {
171
 
    my ($old_ver, $new_ver) = @_;
172
 
    while (scalar(@$old_ver) && scalar(@$new_ver)) {
173
 
        my $old = shift(@$old_ver) || 0;
174
 
        my $new = shift(@$new_ver) || 0;
175
 
        return $new <=> $old if ($new <=> $old);
176
 
    }
177
 
    return scalar(@$new_ver) <=> scalar(@$old_ver);
178
 
 
179
 
}
180
 
 
181
 
1;
182
 
 
183
 
__END__
184
 
 
185
 
=head1 NAME
186
 
 
187
 
Bugzilla::Update - Update routines for Bugzilla
188
 
 
189
 
=head1 SYNOPSIS
190
 
 
191
 
  use Bugzilla::Update;
192
 
 
193
 
  # Get information about new releases
194
 
  my $new_release = Bugzilla::Update::get_notifications();
195
 
 
196
 
=head1 DESCRIPTION
197
 
 
198
 
This module contains all required routines to notify you
199
 
about new releases. It downloads an XML file from bugzilla.org
200
 
and parses it, in order to display information based on your
201
 
preferences. Absolutely no information about the Bugzilla version
202
 
you are running is sent to bugzilla.org.
203
 
 
204
 
=head1 FUNCTIONS
205
 
 
206
 
=over
207
 
 
208
 
=item C<get_notifications()>
209
 
 
210
 
 Description: This function informs you about new releases, if any.
211
 
 
212
 
 Params:      None.
213
 
 
214
 
 Returns:     On success, a reference to a hash with data about
215
 
              new releases, if any.
216
 
              On failure, a reference to a hash with the reason
217
 
              of the failure and the name of the unusable XML file.
218
 
 
219
 
=back
220
 
 
221
 
=cut