Update.pm 7.8 KB

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