1
# -*- Mode: perl; indent-tabs-mode: nil -*-
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/
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.
13
# The Original Code is the Bugzilla Bug Tracking System.
15
# Contributor(s): Frédéric Buclin <LpSolit@gmail.com>
17
package Bugzilla::Update;
21
use Bugzilla::Constants;
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.
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');
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");
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;
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;
51
return {'error' => 'no_update', 'xml_file' => $local_file};
55
# If we cannot access the local XML file, ignore it.
56
return {'error' => 'no_access', 'xml_file' => $local_file} unless (-r $local_file);
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;
65
foreach my $branch ($root->children('branch')) {
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'}
73
push(@releases, $release);
76
# On which branch is the current installation running?
78
(BUGZILLA_VERSION =~ m/^(\d+)\.(\d+)(?:(rc|\.)(\d+))?\+?$/);
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;
89
elsif (Bugzilla->params->{'upgrade_notification'} eq 'latest_stable_release') {
90
@release = grep {$_->{'status'} eq 'stable'} @releases;
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];
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;
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};
110
return {'error' => 'unknown_parameter'};
113
# Return if no new release is available.
114
return unless scalar(@release);
116
# Only notify the administrator if the latest version available
117
# is newer than the current one.
119
($release[0]->{'latest_ver'} =~ m/^(\d+)\.(\d+)(?:(rc|\.)(\d+))?\+?$/);
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;
126
my $is_newer = _compare_versions(\@current_version, \@new_version);
127
return ($is_newer == 1) ? {'data' => $release[0]} : undef;
130
sub _synchronize_data {
131
eval("require LWP::UserAgent");
132
return {'error' => 'missing_package', 'package' => 'LWP::UserAgent'} if $@;
134
my $local_file = bz_locations()->{'datadir'} . LOCAL_FILE;
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'};
143
$ua->proxy(['http', 'https'], $proxy_url);
148
$ua->mirror(REMOTE_FILE, $local_file);
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};
162
# We have been unable to download the file.
163
return {'error' => 'cannot_download', 'xml_file' => $local_file};
166
# Everything went well.
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);
177
return scalar(@$new_ver) <=> scalar(@$old_ver);
187
Bugzilla::Update - Update routines for Bugzilla
191
use Bugzilla::Update;
193
# Get information about new releases
194
my $new_release = Bugzilla::Update::get_notifications();
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.
208
=item C<get_notifications()>
210
Description: This function informs you about new releases, if any.
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.