1# This Source Code Form is subject to the terms of the Mozilla Public 2# License, v. 2.0. If a copy of the MPL was not distributed with this 3# file, You can obtain one at http://mozilla.org/MPL/2.0/. 4# 5# This Source Code Form is "Incompatible With Secondary Licenses", as 6# defined by the Mozilla Public License, v. 2.0. 7 8package Bugzilla::Update; 9 10use strict; 11 12use Bugzilla::Constants; 13 14use constant TIME_INTERVAL => 86400; # Default is one day, in seconds. 15use constant TIMEOUT => 5; # Number of seconds before timeout. 16 17# Look for new releases and notify logged in administrators about them. 18sub get_notifications { 19 return if !Bugzilla->feature('updates'); 20 return if (Bugzilla->params->{'upgrade_notification'} eq 'disabled'); 21 22 my $local_file = bz_locations()->{'datadir'} . '/' . LOCAL_FILE; 23 # Update the local XML file if this one doesn't exist or if 24 # the last modification time (stat[9]) is older than TIME_INTERVAL. 25 if (!-e $local_file || (time() - (stat($local_file))[9] > TIME_INTERVAL)) { 26 unlink $local_file; # Make sure the old copy is away. 27 return { 'error' => 'no_update' } if (-e $local_file); 28 29 my $error = _synchronize_data(); 30 # If an error is returned, leave now. 31 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 my $twig = XML::Twig->new(); 38 $twig->safe_parsefile($local_file); 39 # If the XML file is invalid, return. 40 return { 'error' => 'corrupted' } if $@; 41 my $root = $twig->root; 42 43 my @releases; 44 foreach my $branch ($root->children('branch')) { 45 my $release = { 46 'branch_ver' => $branch->{'att'}->{'id'}, 47 'latest_ver' => $branch->{'att'}->{'vid'}, 48 'status' => $branch->{'att'}->{'status'}, 49 'url' => $branch->{'att'}->{'url'}, 50 'date' => $branch->{'att'}->{'date'} 51 }; 52 push(@releases, $release); 53 } 54 55 # On which branch is the current installation running? 56 my @current_version = 57 (BUGZILLA_VERSION =~ m/^(\d+)\.(\d+)(?:(rc|\.)(\d+))?\+?$/); 58 59 my @release; 60 if (Bugzilla->params->{'upgrade_notification'} eq 'development_snapshot') { 61 @release = grep {$_->{'status'} eq 'development'} @releases; 62 # If there is no development snapshot available, then we are in the 63 # process of releasing a release candidate. That's the release we want. 64 unless (scalar(@release)) { 65 @release = grep {$_->{'status'} eq 'release-candidate'} @releases; 66 } 67 } 68 elsif (Bugzilla->params->{'upgrade_notification'} eq 'latest_stable_release') { 69 @release = grep {$_->{'status'} eq 'stable'} @releases; 70 } 71 elsif (Bugzilla->params->{'upgrade_notification'} eq 'stable_branch_release') { 72 # We want the latest stable version for the current branch. 73 # If we are running a development snapshot, we won't match anything. 74 my $branch_version = $current_version[0] . '.' . $current_version[1]; 75 76 # We do a string comparison instead of a numerical one, because 77 # e.g. 2.2 == 2.20, but 2.2 ne 2.20 (and 2.2 is indeed much older). 78 @release = grep {$_->{'branch_ver'} eq $branch_version} @releases; 79 80 # If the branch is now closed, we should strongly suggest 81 # to upgrade to the latest stable release available. 82 if (scalar(@release) && $release[0]->{'status'} eq 'closed') { 83 @release = grep {$_->{'status'} eq 'stable'} @releases; 84 return {'data' => $release[0], 'deprecated' => $branch_version}; 85 } 86 } 87 else { 88 # Unknown parameter. 89 return {'error' => 'unknown_parameter'}; 90 } 91 92 # Return if no new release is available. 93 return unless scalar(@release); 94 95 # Only notify the administrator if the latest version available 96 # is newer than the current one. 97 my @new_version = 98 ($release[0]->{'latest_ver'} =~ m/^(\d+)\.(\d+)(?:(rc|\.)(\d+))?\+?$/); 99 100 # We convert release candidates 'rc' to integers (rc ? 0 : 1) in order 101 # to compare versions easily. 102 $current_version[2] = ($current_version[2] && $current_version[2] eq 'rc') ? 0 : 1; 103 $new_version[2] = ($new_version[2] && $new_version[2] eq 'rc') ? 0 : 1; 104 105 my $is_newer = _compare_versions(\@current_version, \@new_version); 106 return ($is_newer == 1) ? {'data' => $release[0]} : undef; 107} 108 109sub _synchronize_data { 110 my $local_file = bz_locations()->{'datadir'} . '/' . LOCAL_FILE; 111 112 my $ua = LWP::UserAgent->new(); 113 $ua->timeout(TIMEOUT); 114 $ua->protocols_allowed(['http', 'https']); 115 # If the URL of the proxy is given, use it, else get this information 116 # from the environment variable. 117 my $proxy_url = Bugzilla->params->{'proxy_url'}; 118 if ($proxy_url) { 119 $ua->proxy(['http', 'https'], $proxy_url); 120 } 121 else { 122 $ua->env_proxy; 123 } 124 my $response = eval { $ua->mirror(REMOTE_FILE, $local_file) }; 125 126 # $ua->mirror() forces the modification time of the local XML file 127 # to match the modification time of the remote one. 128 # So we have to update it manually to reflect that a newer version 129 # of the file has effectively been requested. This will avoid 130 # any new download for the next TIME_INTERVAL. 131 if (-e $local_file) { 132 # Try to alter its last modification time. 133 my $can_alter = utime(undef, undef, $local_file); 134 # 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 return { 'error' => 'cannot_download', 'reason' => $response->status_line }; 140 } 141 else { 142 return { 'error' => 'no_write', 'reason' => $@ }; 143 } 144 145 # Everything went well. 146 return 0; 147} 148 149sub _compare_versions { 150 my ($old_ver, $new_ver) = @_; 151 while (scalar(@$old_ver) && scalar(@$new_ver)) { 152 my $old = shift(@$old_ver) || 0; 153 my $new = shift(@$new_ver) || 0; 154 return $new <=> $old if ($new <=> $old); 155 } 156 return scalar(@$new_ver) <=> scalar(@$old_ver); 157 158} 159 1601; 161 162__END__ 163 164=head1 NAME 165 166Bugzilla::Update - Update routines for Bugzilla 167 168=head1 SYNOPSIS 169 170 use Bugzilla::Update; 171 172 # Get information about new releases 173 my $new_release = Bugzilla::Update::get_notifications(); 174 175=head1 DESCRIPTION 176 177This module contains all required routines to notify you 178about new releases. It downloads an XML file from bugzilla.org 179and parses it, in order to display information based on your 180preferences. Absolutely no information about the Bugzilla version 181you are running is sent to bugzilla.org. 182 183=head1 FUNCTIONS 184 185=over 186 187=item C<get_notifications()> 188 189 Description: This function informs you about new releases, if any. 190 191 Params: None. 192 193 Returns: On success, a reference to a hash with data about 194 new releases, if any. 195 On failure, a reference to a hash with the reason 196 of the failure and the name of the unusable XML file. 197 198=back 199 200=cut 201